31
31
*/
32
32
package com .jme3 .scene ;
33
33
34
- import java .nio .Buffer ;
35
- import java .nio .FloatBuffer ;
36
- import java .util .ArrayList ;
37
- import java .util .HashMap ;
38
- import java .util .List ;
39
- import java .util .Map ;
40
- import java .util .logging .Level ;
41
- import java .util .logging .Logger ;
42
-
43
34
import com .jme3 .collision .Collidable ;
44
35
import com .jme3 .collision .CollisionResults ;
45
36
import com .jme3 .material .Material ;
50
41
import com .jme3 .util .TempVars ;
51
42
import com .jme3 .util .clone .Cloner ;
52
43
import com .jme3 .util .clone .JmeCloneable ;
44
+ import java .nio .Buffer ;
45
+ import java .nio .FloatBuffer ;
46
+ import java .util .ArrayList ;
47
+ import java .util .HashMap ;
48
+ import java .util .List ;
49
+ import java .util .Map ;
50
+ import java .util .logging .Level ;
51
+ import java .util .logging .Logger ;
53
52
54
53
/**
55
54
* BatchNode holds geometries that are a batched version of all the geometries that are in its sub scenegraph.
@@ -188,6 +187,11 @@ protected void doBatch() {
188
187
Map <Material , List <Geometry >> matMap = new HashMap <>();
189
188
int nbGeoms = 0 ;
190
189
190
+ // Recalculate the maxVertCount during gatherGeometries() so it's always
191
+ // accurate. Keep track of what it used to be so we know if temp arrays need
192
+ // to be reallocated.
193
+ int oldMaxVertCount = maxVertCount ;
194
+ maxVertCount = 0 ;
191
195
gatherGeometries (matMap , this , needsFullRebatch );
192
196
if (needsFullRebatch ) {
193
197
for (Batch batch : batches .getArray ()) {
@@ -196,10 +200,6 @@ protected void doBatch() {
196
200
batches .clear ();
197
201
batchesByGeom .clear ();
198
202
}
199
- //only reset maxVertCount if there is something new to batch
200
- if (matMap .size () > 0 ) {
201
- maxVertCount = 0 ;
202
- }
203
203
204
204
for (Map .Entry <Material , List <Geometry >> entry : matMap .entrySet ()) {
205
205
Mesh m = new Mesh ();
@@ -244,8 +244,8 @@ protected void doBatch() {
244
244
logger .log (Level .FINE , "Batched {0} geometries in {1} batches." , new Object []{nbGeoms , batches .size ()});
245
245
}
246
246
247
- //init the temp arrays if something has been batched only.
248
- if (matMap . size () > 0 ) {
247
+ //init the temp arrays if the size has changed
248
+ if (oldMaxVertCount != maxVertCount ) {
249
249
initTempFloatArrays ();
250
250
}
251
251
}
@@ -285,6 +285,13 @@ private void gatherGeometries(Map<Material, List<Geometry>> map, Spatial n, bool
285
285
286
286
if (!isBatch (n ) && n .getBatchHint () != BatchHint .Never ) {
287
287
Geometry g = (Geometry ) n ;
288
+
289
+ // Need to recalculate the max vert count whether we are rebatching this
290
+ // particular geometry or not.
291
+ if (maxVertCount < g .getVertexCount ()) {
292
+ maxVertCount = g .getVertexCount ();
293
+ }
294
+
288
295
if (!g .isGrouped () || rebatch ) {
289
296
if (g .getMaterial () == null ) {
290
297
throw new IllegalStateException ("No material is set for Geometry: " + g .getName () + " please set a material before batching" );
@@ -385,11 +392,8 @@ private void mergeGeometries(Mesh outMesh, List<Geometry> geometries) {
385
392
totalVerts += geom .getVertexCount ();
386
393
totalTris += geom .getTriangleCount ();
387
394
totalLodLevels = Math .min (totalLodLevels , geom .getMesh ().getNumLodLevels ());
388
- if (maxVertCount < geom .getVertexCount ()) {
389
- maxVertCount = geom .getVertexCount ();
390
- }
395
+
391
396
Mesh .Mode listMode ;
392
- //float listLineWidth = 1f;
393
397
int components ;
394
398
switch (geom .getMesh ().getMode ()) {
395
399
case Points :
@@ -530,7 +534,6 @@ private void doTransforms(FloatBuffer bindBufPos, FloatBuffer bindBufNorm, Float
530
534
Vector3f norm = vars .vect2 ;
531
535
Vector3f tan = vars .vect3 ;
532
536
533
- validateTempFloatArrays (end - start );
534
537
int length = (end - start ) * 3 ;
535
538
int tanLength = (end - start ) * 4 ;
536
539
@@ -611,13 +614,6 @@ private void doTransforms(FloatBuffer bindBufPos, FloatBuffer bindBufNorm, Float
611
614
}
612
615
}
613
616
614
- private void validateTempFloatArrays (int vertCount ) {
615
- if (maxVertCount < vertCount ) {
616
- maxVertCount = vertCount ;
617
- initTempFloatArrays ();
618
- }
619
- }
620
-
621
617
private void initTempFloatArrays () {
622
618
//TODO these arrays should be allocated by chunk instead to avoid recreating them each time the batch is changed.
623
619
tmpFloat = new float [maxVertCount * 3 ];
0 commit comments