@@ -8,10 +8,11 @@ pc.extend(pc, function () {
88 this . isDynamic = isDynamic ;
99 } ;
1010
11- var BatchGroup = function ( isDynamic , maxAabbSize ) {
11+ var BatchGroup = function ( id , name , isDynamic , maxAabbSize ) {
1212 this . isDynamic = isDynamic ;
1313 this . maxAabbSize = maxAabbSize ;
14- this . name = "Group" ;
14+ this . id = id ;
15+ this . name = name ;
1516 } ;
1617
1718 // Modified SkinInstance for batching
@@ -101,25 +102,55 @@ pc.extend(pc, function () {
101102 this . scene = scene ;
102103 this . _init = false ;
103104
104- this . batchGroups = { } ;
105+ this . _batchGroups = { } ;
106+ this . _batchGroupNameToId = { } ;
107+ this . _batchGroupCounter = 0 ;
105108
106109 this . _stats = {
107110 time : 0
108111 } ;
109112 } ;
110113
114+ Batching . prototype . addGroup = function ( name , isDynamic , maxAabbSize ) {
115+ if ( this . _batchGroupNameToId [ name ] ) {
116+ // #ifdef DEBUG
117+ console . error ( "batch group " + name + " already exists" ) ;
118+ // #endif
119+ return ;
120+ }
121+ var id = this . _batchGroupCounter ;
122+ var group ;
123+ this . _batchGroups [ id ] = group = new pc . BatchGroup ( id , name , isDynamic , maxAabbSize ) ;
124+ this . _batchGroupNameToId [ name ] = id ;
125+ this . _batchGroupCounter ++ ;
126+ return group ;
127+ } ;
128+
129+ Batching . prototype . removeGroup = function ( name ) {
130+ if ( ! this . _batchGroupNameToId [ name ] ) {
131+ // #ifdef DEBUG
132+ console . error ( "batch group " + name + " doesn't exist" ) ;
133+ // #endif
134+ return ;
135+ }
136+ var id = this . _batchGroupNameToId [ name ] ;
137+ delete this . _batchGroups [ id ] ;
138+ delete this . _batchGroupNameToId [ name ] ;
139+ } ;
140+
141+ Batching . prototype . removeGroup = function ( name , isDynamic , maxAabbSize ) {
142+ this . _batchGroups [ name ] = new pc . BatchGroup ( name , isDynamic , maxAabbSize ) ;
143+ } ;
144+
111145 Batching . prototype . _collectAndRemoveModels = function ( node , groupMeshInstances ) {
112146 if ( ! node . enabled ) return ;
113- if ( ! node . model ) return ;
114- if ( node . model . batchGroup < 0 ) return ;
115- if ( ! node . model . model ) return ;
116- if ( ! node . model . enabled ) return ;
117147
118- var arr = groupMeshInstances [ node . model . batchGroup ] ;
119- if ( ! arr ) arr = groupMeshInstances [ node . model . batchGroup ] = [ ] ;
120- arr = arr . concat ( node . model . meshInstances ) ;
121-
122- this . scene . removeModel ( node . model ) ;
148+ if ( node . model && node . model . batchGroupId >= 0 && node . model . model && node . model . enabled ) {
149+ var arr = groupMeshInstances [ node . model . batchGroupId ] ;
150+ if ( ! arr ) arr = groupMeshInstances [ node . model . batchGroupId ] = [ ] ;
151+ groupMeshInstances [ node . model . batchGroupId ] = arr . concat ( node . model . meshInstances ) ;
152+ this . scene . removeModel ( node . model . model ) ;
153+ }
123154
124155 for ( var i = 0 ; i < node . _children . length ; i ++ ) {
125156 this . _collectAndRemoveModels ( node . _children [ i ] , groupMeshInstances ) ;
@@ -165,12 +196,12 @@ pc.extend(pc, function () {
165196 }
166197 }
167198
168- var group , lists , groupData , j , batch ;
199+ var group , lists , groupData , batch ;
169200 for ( var groupId in groupMeshInstances ) {
170201 if ( ! groupMeshInstances . hasOwnProperty ( groupId ) ) continue ;
171202 group = groupMeshInstances [ groupId ] ;
172203
173- groupData = this . batchGroups [ groupId ] ;
204+ groupData = this . _batchGroups [ groupId ] ;
174205 if ( ! groupData ) {
175206 // #ifdef DEBUG
176207 console . error ( "batch group " + groupId + " not found" ) ;
@@ -179,7 +210,7 @@ pc.extend(pc, function () {
179210 }
180211
181212 lists = this . prepare ( group , groupData . isDynamic , groupData . maxAabbSize ) ;
182- for ( j = 0 ; j < lists . length ; j ++ ) {
213+ for ( i = 0 ; i < lists . length ; i ++ ) {
183214 batch = this . create ( lists [ i ] , groupData . isDynamic ) ;
184215 this . scene . addModel ( batch . model ) ;
185216 this . _registerEntities ( batch , group ) ;
@@ -678,7 +709,7 @@ pc.extend(pc, function () {
678709
679710 return {
680711 Batch : Batch ,
681- SkinBatchInstance : SkinBatchInstance ,
712+ BatchGroup : BatchGroup ,
682713 Batching : Batching
683714 } ;
684715} ( ) ) ;
0 commit comments