Sfoglia il codice sorgente

BatchNode: Fix IndexOutOfBoundsException (#2297)

- There are temporary arrays that are reused to work with vertex buffers.

- Some conditions cause the index to be used for storing more vertices that they can hold.

- This throws the exception.

- This fix validates the arrays size before accessing them, recreating the arrays if they are too small.
Juan Cruz Fandino 1 anno fa
parent
commit
1e10225f74
1 ha cambiato i file con 18 aggiunte e 8 eliminazioni
  1. 18 8
      jme3-core/src/main/java/com/jme3/scene/BatchNode.java

+ 18 - 8
jme3-core/src/main/java/com/jme3/scene/BatchNode.java

@@ -246,13 +246,7 @@ public class BatchNode extends GeometryGroupNode {
 
         //init the temp arrays if something has been batched only.
         if (matMap.size() > 0) {
-            //TODO these arrays should be allocated by chunk instead to avoid recreating them each time the batch is changed.
-            //init temp float arrays
-            tmpFloat = new float[maxVertCount * 3];
-            tmpFloatN = new float[maxVertCount * 3];
-            if (useTangents) {
-                tmpFloatT = new float[maxVertCount * 4];
-            }
+            initTempFloatArrays();
         }
     }
 
@@ -387,7 +381,6 @@ public class BatchNode extends GeometryGroupNode {
         int maxWeights = -1;
 
         Mesh.Mode mode = null;
-        float lineWidth = 1f;
         for (Geometry geom : geometries) {
             totalVerts += geom.getVertexCount();
             totalTris += geom.getTriangleCount();
@@ -537,6 +530,7 @@ public class BatchNode extends GeometryGroupNode {
         Vector3f norm = vars.vect2;
         Vector3f tan = vars.vect3;
 
+        validateTempFloatArrays(end - start);
         int length = (end - start) * 3;
         int tanLength = (end - start) * 4;
 
@@ -617,6 +611,22 @@ public class BatchNode extends GeometryGroupNode {
         }
     }
 
+    private void validateTempFloatArrays(int vertCount) {
+        if (maxVertCount < vertCount) {
+            maxVertCount = vertCount;
+            initTempFloatArrays();
+        }
+    }
+
+    private void initTempFloatArrays() {
+        //TODO these arrays should be allocated by chunk instead to avoid recreating them each time the batch is changed.
+        tmpFloat = new float[maxVertCount * 3];
+        tmpFloatN = new float[maxVertCount * 3];
+        if (useTangents) {
+            tmpFloatT = new float[maxVertCount * 4];
+        }
+    }
+
     private void doCopyBuffer(FloatBuffer inBuf, int offset, FloatBuffer outBuf, int componentSize) {
         TempVars vars = TempVars.get();
         Vector3f pos = vars.vect1;