Browse Source

Fix #1997 (shadows on hardware-accelerated morph animations) (#2111)

* Fix shadows for morph animations

* Set shadows to the morph test
Toni Helenius 1 năm trước cách đây
mục cha
commit
231f75cd9e

+ 1 - 0
jme3-core/src/main/resources/Common/MatDefs/Shadow/PostShadow.vert

@@ -1,6 +1,7 @@
 #import "Common/ShaderLib/GLSLCompat.glsllib"
 #import "Common/ShaderLib/Instancing.glsllib"
 #import "Common/ShaderLib/Skinning.glsllib"
+#import "Common/ShaderLib/MorphAnim.glsllib"
 
 uniform mat4 m_LightViewProjectionMatrix0;
 uniform mat4 m_LightViewProjectionMatrix1;

+ 1 - 1
jme3-core/src/main/resources/Common/MatDefs/Shadow/PreShadow.vert

@@ -12,7 +12,7 @@ void main(){
     vec4 modelSpacePos = vec4(inPosition, 1.0);
 
    #ifdef NUM_MORPH_TARGETS
-           Morph_Compute(modelSpacePos, modelSpaceNorm);
+           Morph_Compute(modelSpacePos);
    #endif
 
    #ifdef NUM_BONES

+ 50 - 1
jme3-examples/src/main/java/jme3test/model/anim/TestGltfMorph.java

@@ -33,10 +33,17 @@ package jme3test.model.anim;
 
 import com.jme3.anim.AnimComposer;
 import com.jme3.app.*;
+import com.jme3.light.AmbientLight;
+import com.jme3.light.DirectionalLight;
+import com.jme3.material.Material;
 import com.jme3.math.*;
 import com.jme3.renderer.Limits;
+import com.jme3.renderer.queue.RenderQueue;
 import com.jme3.scene.*;
 import com.jme3.scene.plugins.gltf.GltfModelKey;
+import com.jme3.scene.shape.Quad;
+import com.jme3.shadow.DirectionalLightShadowRenderer;
+import com.jme3.shadow.EdgeFilteringMode;
 
 public class TestGltfMorph extends SimpleApplication {
     private Node probeNode;
@@ -50,6 +57,8 @@ public class TestGltfMorph extends SimpleApplication {
 
     @Override
     public void simpleInitApp() {
+        rootNode.setShadowMode(RenderQueue.ShadowMode.CastAndReceive);
+
         probeNode = (Node) assetManager.loadModel("Scenes/defaultProbe.j3o");
         rootNode.attachChild(probeNode);
 
@@ -59,11 +68,51 @@ public class TestGltfMorph extends SimpleApplication {
 
         flyCam.setMoveSpeed(5);
         flyCam.setDragToRotate(true);
-        flyCam.setEnabled(false);
+        flyCam.setEnabled(true);
         viewPort.setBackgroundColor(new ColorRGBA().setAsSrgb(0.2f, 0.2f, 0.2f, 1.0f));
 
+        setupFloor();
+
+        Vector3f lightDir = new Vector3f(-1, -1, .5f).normalizeLocal();
+
+        // To make shadows, sun
+        DirectionalLight dl = new DirectionalLight();
+        dl.setDirection(lightDir);
+        dl.setColor(ColorRGBA.White);
+        rootNode.addLight(dl);
+
+        // Add ambient light
+        AmbientLight al = new AmbientLight();
+        al.setColor(ColorRGBA.White.multLocal(0.4f));
+        rootNode.addLight(al);
+
+        final int SHADOWMAP_SIZE = 1024;
+        DirectionalLightShadowRenderer dlsr = new DirectionalLightShadowRenderer(getAssetManager(), SHADOWMAP_SIZE, 3);
+        dlsr.setLight(dl);
+        dlsr.setLambda(0.55f);
+        dlsr.setShadowIntensity(0.6f);
+        dlsr.setEdgeFilteringMode(EdgeFilteringMode.PCF8);
+        getViewPort().addProcessor(dlsr);
+
         loadModel("jme3test/morph/MorphStressTest.glb", new Vector3f(0, -1, 0), 1);
+    }
+
+    private void setupFloor() {
+        Material floorMaterial = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
+        floorMaterial.setColor("Diffuse", new ColorRGBA(.9f, .9f, .9f, .9f));
+
+        Node floorGeom = new Node("floorGeom");
+        Quad q = new Quad(20, 20);
+        Geometry g = new Geometry("geom", q);
+        g.setLocalRotation(new Quaternion().fromAngleAxis(-FastMath.HALF_PI, Vector3f.UNIT_X));
+        g.setShadowMode(RenderQueue.ShadowMode.Receive);
+        floorGeom.attachChild(g);
+
+        floorGeom.setMaterial(floorMaterial);
+
+        floorGeom.move(-10f, -2f, 10f);
 
+        rootNode.attachChild(floorGeom);
     }
 
     private void loadModel(String path, Vector3f offset, float scale) {