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.light;
34 
35 import com.jme3.app.SimpleApplication;
36 import com.jme3.effect.ParticleEmitter;
37 import com.jme3.effect.ParticleMesh;
38 import com.jme3.light.AmbientLight;
39 import com.jme3.light.DirectionalLight;
40 import com.jme3.material.Material;
41 import com.jme3.math.*;
42 import com.jme3.renderer.queue.RenderQueue.Bucket;
43 import com.jme3.renderer.queue.RenderQueue.ShadowMode;
44 import com.jme3.scene.Geometry;
45 import com.jme3.scene.SceneGraphVisitorAdapter;
46 import com.jme3.scene.Spatial;
47 import com.jme3.scene.control.LodControl;
48 import com.jme3.scene.shape.Quad;
49 import com.jme3.shadow.PssmShadowRenderer;
50 import com.jme3.shadow.PssmShadowRenderer.CompareMode;
51 import com.jme3.shadow.PssmShadowRenderer.FilterMode;
52 
53 public class TestTransparentShadow extends SimpleApplication {
54 
main(String[] args)55     public static void main(String[] args){
56         TestTransparentShadow app = new TestTransparentShadow();
57         app.start();
58     }
59 
simpleInitApp()60     public void simpleInitApp() {
61 
62         cam.setLocation(new Vector3f(2.0606942f, 3.20342f, 6.7860126f));
63         cam.setRotation(new Quaternion(-0.017481906f, 0.98241085f, -0.12393151f, -0.13857932f));
64 
65         viewPort.setBackgroundColor(ColorRGBA.DarkGray);
66 
67         Quad q = new Quad(20, 20);
68         q.scaleTextureCoordinates(Vector2f.UNIT_XY.mult(5));
69         Geometry geom = new Geometry("floor", q);
70         Material mat = assetManager.loadMaterial("Textures/Terrain/Pond/Pond.j3m");
71         geom.setMaterial(mat);
72 
73         geom.rotate(-FastMath.HALF_PI, 0, 0);
74         geom.center();
75         geom.setShadowMode(ShadowMode.Receive);
76         rootNode.attachChild(geom);
77 
78         // create the geometry and attach it
79         Spatial teaGeom = assetManager.loadModel("Models/Tree/Tree.mesh.j3o");
80         teaGeom.setQueueBucket(Bucket.Transparent);
81         teaGeom.setShadowMode(ShadowMode.Cast);
82 
83         teaGeom.depthFirstTraversal(new SceneGraphVisitorAdapter(){
84             @Override
85              public void visit(Geometry geom) {
86                  LodControl lodCtrl = new LodControl();
87                  lodCtrl.setTrisPerPixel(0.25f);
88                  geom.addControl(lodCtrl);
89              }
90         });
91 
92         AmbientLight al = new AmbientLight();
93         al.setColor(ColorRGBA.White.mult(2));
94         rootNode.addLight(al);
95 
96         DirectionalLight dl1 = new DirectionalLight();
97         dl1.setDirection(new Vector3f(1, -1, 1).normalizeLocal());
98         dl1.setColor(new ColorRGBA(0.965f, 0.949f, 0.772f, 1f).mult(0.7f));
99         rootNode.addLight(dl1);
100 
101         DirectionalLight dl = new DirectionalLight();
102         dl.setDirection(new Vector3f(-1, -1, -1).normalizeLocal());
103         dl.setColor(new ColorRGBA(0.965f, 0.949f, 0.772f, 1f).mult(0.7f));
104         rootNode.addLight(dl);
105 
106         rootNode.attachChild(teaGeom);
107 
108         /** Uses Texture from jme3-test-data library! */
109         ParticleEmitter fire = new ParticleEmitter("Emitter", ParticleMesh.Type.Triangle, 30);
110         Material mat_red = new Material(assetManager, "Common/MatDefs/Misc/Particle.j3md");
111         mat_red.setTexture("Texture", assetManager.loadTexture("Effects/Explosion/flame.png"));
112         //mat_red.getAdditionalRenderState().setDepthTest(true);
113         //mat_red.getAdditionalRenderState().setDepthWrite(true);
114         fire.setMaterial(mat_red);
115         fire.setImagesX(2); fire.setImagesY(2); // 2x2 texture animation
116         fire.setEndColor(  new ColorRGBA(1f, 0f, 0f, 1f));   // red
117         fire.setStartColor(new ColorRGBA(1f, 1f, 0f, 0.5f)); // yellow
118         fire.setInitialVelocity(new Vector3f(0, 2, 0));
119         fire.setStartSize(0.6f);
120         fire.setEndSize(0.1f);
121         fire.setGravity(0, 0, 0);
122         fire.setLowLife(0.5f);
123         fire.setHighLife(1.5f);
124         fire.setVelocityVariation(0.3f);
125         fire.setLocalTranslation(1.0f, 0, 1.0f);
126         fire.setLocalScale(0.3f);
127         fire.setQueueBucket(Bucket.Translucent);
128         rootNode.attachChild(fire);
129 
130 
131         PssmShadowRenderer pssmRenderer = new PssmShadowRenderer(assetManager, 1024, 1);
132         pssmRenderer.setDirection(new Vector3f(0.01f, -1f, 0.01f).normalizeLocal());
133         pssmRenderer.setLambda(0.55f);
134         pssmRenderer.setShadowIntensity(0.6f);
135         pssmRenderer.setCompareMode(CompareMode.Software);
136         pssmRenderer.setFilterMode(FilterMode.PCF4);
137         pssmRenderer.displayDebug();
138         viewPort.addProcessor(pssmRenderer);
139     }
140 }
141