@@ -262,6 +262,8 @@ class BatchedMesh extends Mesh {
262
262
// flags
263
263
this . _visibilityChanged = true ;
264
264
this . _geometryInitialized = false ;
265
+ this . _availableInstanceIdsSorted = true ;
266
+ this . _availableGeometryIdsSorted = true ;
265
267
266
268
// cached user options
267
269
this . _maxInstanceCount = maxInstanceCount ;
@@ -580,7 +582,12 @@ class BatchedMesh extends Mesh {
580
582
// Prioritize using previously freed instance ids
581
583
if ( this . _availableInstanceIds . length > 0 ) {
582
584
583
- this . _availableInstanceIds . sort ( ascIdSort ) ;
585
+ if ( ! this . _availableInstanceIdsSorted ) {
586
+
587
+ this . _availableInstanceIds . sort ( ascIdSort ) ;
588
+ this . _availableInstanceIdsSorted = true ;
589
+
590
+ }
584
591
585
592
drawId = this . _availableInstanceIds . shift ( ) ;
586
593
this . _instanceInfo [ drawId ] = instanceInfo ;
@@ -677,7 +684,12 @@ class BatchedMesh extends Mesh {
677
684
let geometryId ;
678
685
if ( this . _availableGeometryIds . length > 0 ) {
679
686
680
- this . _availableGeometryIds . sort ( ascIdSort ) ;
687
+ if ( ! this . _availableGeometryIdsSorted ) {
688
+
689
+ this . _availableGeometryIds . sort ( ascIdSort ) ;
690
+ this . _availableGeometryIdsSorted = true ;
691
+
692
+ }
681
693
682
694
geometryId = this . _availableGeometryIds . shift ( ) ;
683
695
geometryInfoList [ geometryId ] = geometryInfo ;
@@ -846,6 +858,7 @@ class BatchedMesh extends Mesh {
846
858
847
859
geometryInfoList [ geometryId ] . active = false ;
848
860
this . _availableGeometryIds . push ( geometryId ) ;
861
+ this . _availableGeometryIdsSorted = false ;
849
862
this . _visibilityChanged = true ;
850
863
851
864
return this ;
@@ -864,6 +877,7 @@ class BatchedMesh extends Mesh {
864
877
865
878
this . _instanceInfo [ instanceId ] . active = false ;
866
879
this . _availableInstanceIds . push ( instanceId ) ;
880
+ this . _availableGeometryIdsSorted = false ;
867
881
this . _visibilityChanged = true ;
868
882
869
883
return this ;
@@ -1250,7 +1264,13 @@ class BatchedMesh extends Mesh {
1250
1264
// shrink the available instances as much as possible
1251
1265
const availableInstanceIds = this . _availableInstanceIds ;
1252
1266
const instanceInfo = this . _instanceInfo ;
1253
- availableInstanceIds . sort ( ascIdSort ) ;
1267
+ if ( ! this . _availableInstanceIdsSorted ) {
1268
+
1269
+ availableInstanceIds . sort ( ascIdSort ) ;
1270
+ this . _availableInstanceIdsSorted = true ;
1271
+
1272
+ }
1273
+
1254
1274
while ( availableInstanceIds [ availableInstanceIds . length - 1 ] === instanceInfo . length - 1 ) {
1255
1275
1256
1276
instanceInfo . pop ( ) ;
@@ -1445,6 +1465,8 @@ class BatchedMesh extends Mesh {
1445
1465
1446
1466
this . _availableInstanceIds = source . _availableInstanceIds . slice ( ) ;
1447
1467
this . _availableGeometryIds = source . _availableGeometryIds . slice ( ) ;
1468
+ this . _availableInstanceIdsSorted = source . _availableInstanceIdsSorted ;
1469
+ this . _availableGeometryIdsSorted = source . _availableGeometryIdsSorted ;
1448
1470
1449
1471
this . _nextIndexStart = source . _nextIndexStart ;
1450
1472
this . _nextVertexStart = source . _nextVertexStart ;
0 commit comments