1package jme3test.post;
2
3import com.jme3.app.SimpleApplication;
4import com.jme3.light.AmbientLight;
5import com.jme3.light.DirectionalLight;
6import com.jme3.material.Material;
7import com.jme3.math.*;
8import com.jme3.post.FilterPostProcessor;
9import com.jme3.post.filters.CartoonEdgeFilter;
10import com.jme3.renderer.queue.RenderQueue.Bucket;
11import com.jme3.renderer.queue.RenderQueue.ShadowMode;
12import com.jme3.scene.Geometry;
13import com.jme3.scene.Node;
14import com.jme3.scene.Spatial;
15import com.jme3.scene.shape.Quad;
16import com.jme3.texture.Texture;
17
18public class TestTransparentCartoonEdge extends SimpleApplication {
19
20    public static void main(String[] args){
21        TestTransparentCartoonEdge app = new TestTransparentCartoonEdge();
22        app.start();
23    }
24
25    public void simpleInitApp() {
26        renderManager.setAlphaToCoverage(true);
27        cam.setLocation(new Vector3f(0.14914267f, 0.58147097f, 4.7686534f));
28        cam.setRotation(new Quaternion(-0.0044764364f, 0.9767943f, 0.21314798f, 0.020512417f));
29
30//        cam.setLocation(new Vector3f(2.0606942f, 3.20342f, 6.7860126f));
31//        cam.setRotation(new Quaternion(-0.017481906f, 0.98241085f, -0.12393151f, -0.13857932f));
32
33        viewPort.setBackgroundColor(ColorRGBA.DarkGray);
34
35        Quad q = new Quad(20, 20);
36        q.scaleTextureCoordinates(Vector2f.UNIT_XY.mult(5));
37        Geometry geom = new Geometry("floor", q);
38        Material mat = assetManager.loadMaterial("Textures/Terrain/Pond/Pond.j3m");
39        geom.setMaterial(mat);
40
41        geom.rotate(-FastMath.HALF_PI, 0, 0);
42        geom.center();
43        geom.setShadowMode(ShadowMode.Receive);
44        rootNode.attachChild(geom);
45
46        // create the geometry and attach it
47        Spatial teaGeom = assetManager.loadModel("Models/Tree/Tree.mesh.j3o");
48        teaGeom.setQueueBucket(Bucket.Transparent);
49        teaGeom.setShadowMode(ShadowMode.Cast);
50        makeToonish(teaGeom);
51
52        AmbientLight al = new AmbientLight();
53        al.setColor(ColorRGBA.White.mult(2));
54        rootNode.addLight(al);
55
56        DirectionalLight dl1 = new DirectionalLight();
57        dl1.setDirection(new Vector3f(1, -1, 1).normalizeLocal());
58        dl1.setColor(new ColorRGBA(0.965f, 0.949f, 0.772f, 1f).mult(0.7f));
59        rootNode.addLight(dl1);
60
61        DirectionalLight dl = new DirectionalLight();
62        dl.setDirection(new Vector3f(-1, -1, -1).normalizeLocal());
63        dl.setColor(new ColorRGBA(0.965f, 0.949f, 0.772f, 1f).mult(0.7f));
64        rootNode.addLight(dl);
65
66        rootNode.attachChild(teaGeom);
67
68        FilterPostProcessor fpp=new FilterPostProcessor(assetManager);
69        CartoonEdgeFilter toon=new CartoonEdgeFilter();
70        toon.setEdgeWidth(0.5f);
71        toon.setEdgeIntensity(1.0f);
72        toon.setNormalThreshold(0.8f);
73        fpp.addFilter(toon);
74        viewPort.addProcessor(fpp);
75    }
76
77        public void makeToonish(Spatial spatial){
78        if (spatial instanceof Node){
79            Node n = (Node) spatial;
80            for (Spatial child : n.getChildren())
81                makeToonish(child);
82        }else if (spatial instanceof Geometry){
83            Geometry g = (Geometry) spatial;
84            Material m = g.getMaterial();
85            if (m.getMaterialDef().getName().equals("Phong Lighting")){
86                Texture t = assetManager.loadTexture("Textures/ColorRamp/toon.png");
87//                t.setMinFilter(Texture.MinFilter.NearestNoMipMaps);
88//                t.setMagFilter(Texture.MagFilter.Nearest);
89                m.setTexture("ColorRamp", t);
90                m.setBoolean("UseMaterialColors", true);
91                m.setColor("Specular", ColorRGBA.Black);
92                m.setColor("Diffuse", ColorRGBA.White);
93                m.setBoolean("VertexLighting", true);
94            }
95        }
96    }
97}
98