Home | History | Annotate | Download | only in post
      1 /*
      2  * Copyright (c) 2009-2010 jMonkeyEngine
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are
      7  * met:
      8  *
      9  * * Redistributions of source code must retain the above copyright
     10  *   notice, this list of conditions and the following disclaimer.
     11  *
     12  * * Redistributions in binary form must reproduce the above copyright
     13  *   notice, this list of conditions and the following disclaimer in the
     14  *   documentation and/or other materials provided with the distribution.
     15  *
     16  * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
     17  *   may be used to endorse or promote products derived from this software
     18  *   without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 package jme3test.post;
     34 
     35 import com.jme3.app.SimpleApplication;
     36 import com.jme3.light.DirectionalLight;
     37 import com.jme3.material.Material;
     38 import com.jme3.math.ColorRGBA;
     39 import com.jme3.math.FastMath;
     40 import com.jme3.math.Quaternion;
     41 import com.jme3.math.Vector3f;
     42 import com.jme3.post.FilterPostProcessor;
     43 import com.jme3.post.filters.CartoonEdgeFilter;
     44 import com.jme3.renderer.Caps;
     45 import com.jme3.scene.Geometry;
     46 import com.jme3.scene.Node;
     47 import com.jme3.scene.Spatial;
     48 import com.jme3.scene.Spatial.CullHint;
     49 import com.jme3.texture.Texture;
     50 
     51 public class TestCartoonEdge extends SimpleApplication {
     52 
     53     private FilterPostProcessor fpp;
     54 
     55     public static void main(String[] args){
     56         TestCartoonEdge app = new TestCartoonEdge();
     57         app.start();
     58     }
     59 
     60     public void setupFilters(){
     61         if (renderer.getCaps().contains(Caps.GLSL100)){
     62             fpp=new FilterPostProcessor(assetManager);
     63             //fpp.setNumSamples(4);
     64             CartoonEdgeFilter toon=new CartoonEdgeFilter();
     65             toon.setEdgeColor(ColorRGBA.Yellow);
     66             fpp.addFilter(toon);
     67             viewPort.addProcessor(fpp);
     68         }
     69     }
     70 
     71     public void makeToonish(Spatial spatial){
     72         if (spatial instanceof Node){
     73             Node n = (Node) spatial;
     74             for (Spatial child : n.getChildren())
     75                 makeToonish(child);
     76         }else if (spatial instanceof Geometry){
     77             Geometry g = (Geometry) spatial;
     78             Material m = g.getMaterial();
     79             if (m.getMaterialDef().getName().equals("Phong Lighting")){
     80                 Texture t = assetManager.loadTexture("Textures/ColorRamp/toon.png");
     81 //                t.setMinFilter(Texture.MinFilter.NearestNoMipMaps);
     82 //                t.setMagFilter(Texture.MagFilter.Nearest);
     83                 m.setTexture("ColorRamp", t);
     84                 m.setBoolean("UseMaterialColors", true);
     85                 m.setColor("Specular", ColorRGBA.Black);
     86                 m.setColor("Diffuse", ColorRGBA.White);
     87                 m.setBoolean("VertexLighting", true);
     88             }
     89         }
     90     }
     91 
     92     public void setupLighting(){
     93 
     94         DirectionalLight dl = new DirectionalLight();
     95         dl.setDirection(new Vector3f(-1, -1, 1).normalizeLocal());
     96         dl.setColor(new ColorRGBA(2,2,2,1));
     97 
     98         rootNode.addLight(dl);
     99     }
    100 
    101     public void setupModel(){
    102         Spatial model = assetManager.loadModel("Models/MonkeyHead/MonkeyHead.mesh.xml");
    103         makeToonish(model);
    104         model.rotate(0, FastMath.PI, 0);
    105 //        signpost.setLocalTranslation(12, 3.5f, 30);
    106 //        model.scale(0.10f);
    107 //        signpost.setShadowMode(ShadowMode.CastAndReceive);
    108         rootNode.attachChild(model);
    109     }
    110 
    111     @Override
    112     public void simpleInitApp() {
    113         viewPort.setBackgroundColor(ColorRGBA.Gray);
    114 
    115         cam.setLocation(new Vector3f(-5.6310086f, 5.0892987f, -13.000479f));
    116         cam.setRotation(new Quaternion(0.1779095f, 0.20036356f, -0.03702727f, 0.96272093f));
    117         cam.update();
    118 
    119         cam.setFrustumFar(300);
    120         flyCam.setMoveSpeed(30);
    121 
    122         rootNode.setCullHint(CullHint.Never);
    123 
    124         setupLighting();
    125         setupModel();
    126         setupFilters();
    127     }
    128 
    129 }
    130