Pārlūkot izejas kodu

Fixed InstanceGeomerty not working properly when using the lighting material and a non DirectionalLight
see https://hub.jmonkeyengine.org/t/instancednode-doesnt-work-with-light/38316/5

Nehon 8 gadi atpakaļ
vecāks
revīzija
29875e6085

+ 27 - 0
jme3-core/src/main/java/com/jme3/scene/instancing/InstancedGeometry.java

@@ -31,6 +31,7 @@
  */
 package com.jme3.scene.instancing;
 
+import com.jme3.bounding.BoundingVolume;
 import com.jme3.export.InputCapsule;
 import com.jme3.export.JmeExporter;
 import com.jme3.export.JmeImporter;
@@ -309,6 +310,7 @@ public class InstancedGeometry extends Geometry {
         } else {
             // Deleting element in the middle
         }
+        setBoundRefresh();
     }
 
     public void addInstance(Geometry geometry) {
@@ -327,6 +329,31 @@ public class InstancedGeometry extends Geometry {
 
         geometries[freeIndex] = geometry;
         InstancedNode.setGeometryStartIndex2(geometry, freeIndex);
+        setBoundRefresh();
+    }
+
+    @Override
+    protected void updateWorldBound() {
+        refreshFlagAnd(~RF_BOUND);
+        BoundingVolume resultBound = null;
+
+        for (int i = 0; i < firstUnusedIndex; i++) {
+            Geometry geom = geometries[i];
+
+            if (geom != null) {
+                if (resultBound != null) {
+                    // merge current world bound with child world bound
+                    resultBound.mergeLocal(geom.getWorldBound());
+                } else {
+                    // set world bound to first non-null child world bound
+                    if (geom.getWorldBound() != null) {
+                        resultBound = geom.getWorldBound().clone(this.worldBound);
+                    }
+                }
+            }
+        }
+
+        this.worldBound = resultBound;
     }
 
     public Geometry[] getGeometries() {

+ 62 - 0
jme3-examples/src/main/java/jme3test/scene/instancing/TestInstanceNodeWithLight.java

@@ -0,0 +1,62 @@
+package jme3test.scene.instancing;
+
+import com.jme3.app.SimpleApplication;
+import com.jme3.light.PointLight;
+import com.jme3.material.Material;
+import com.jme3.math.ColorRGBA;
+import com.jme3.math.Vector3f;
+import com.jme3.scene.Geometry;
+import com.jme3.scene.instancing.InstancedNode;
+import com.jme3.scene.shape.Box;
+
+public class TestInstanceNodeWithLight extends SimpleApplication {
+    // Try to test with different offset
+    private static float offset = 12;
+
+    public static void main(String[] args) {
+        TestInstanceNodeWithLight app = new TestInstanceNodeWithLight();
+        app.start();
+    }
+
+    Geometry box;
+    PointLight pointLight;
+
+    @Override
+    public void simpleInitApp() {
+        InstancedNode instancedNode = new InstancedNode("testInstancedNode");
+        rootNode.attachChild(instancedNode);
+
+        box = new Geometry("Box", new Box(0.5f, 0.5f, 0.5f));
+        Material material = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
+        material.setBoolean("UseInstancing", true);
+        material.setColor("Diffuse", ColorRGBA.Red);
+        material.setBoolean("UseMaterialColors", true);
+        box.setMaterial(material);
+
+        instancedNode.attachChild(box);
+        instancedNode.instance();
+
+        pointLight = new PointLight();
+        pointLight.setColor(ColorRGBA.White);
+        pointLight.setRadius(10f);
+        rootNode.addLight(pointLight);
+
+        box.setLocalTranslation(new Vector3f(offset, 0, 0));
+        pointLight.setPosition(new Vector3f(offset - 3f, 0, 0));
+
+        cam.setLocation(new Vector3f(offset - 5f, 0, 0));
+        cam.lookAtDirection(Vector3f.UNIT_X, Vector3f.UNIT_Y);
+    }
+
+    @Override
+    public void simpleUpdate(float tpf) {
+        offset += tpf;
+
+        System.err.println(offset);
+        box.setLocalTranslation(new Vector3f(offset, 0, 0));
+        pointLight.setPosition(new Vector3f(offset - 3f, 0, 0));
+
+        cam.setLocation(new Vector3f(offset - 5f, 0, 0));
+        cam.lookAtDirection(Vector3f.UNIT_X, Vector3f.UNIT_Y);
+    }
+}