@@ -95,6 +95,7 @@ export class PathTracingSceneGenerator {
9595 this . _bvhWorker = null ;
9696 this . _pendingGenerate = null ;
9797 this . _buildAsync = false ;
98+ this . _materialUuids = null ;
9899
99100 }
100101
@@ -176,9 +177,31 @@ export class PathTracingSceneGenerator {
176177 // generate the geometry
177178 const result = staticGeometryGenerator . generate ( geometry ) ;
178179 const materials = result . materials ;
180+ let needsMaterialIndexUpdate = result . changeType !== NO_CHANGE || this . _materialUuids === null || this . _materialUuids . length !== length ;
181+ if ( ! needsMaterialIndexUpdate ) {
182+
183+ for ( let i = 0 , length = materials . length ; i < length ; i ++ ) {
184+
185+ const material = materials [ i ] ;
186+ if ( material . uuid !== this . _materialUuids [ i ] ) {
187+
188+ needsMaterialIndexUpdate = true ;
189+ break ;
190+
191+ }
192+
193+ }
194+
195+ }
196+
179197 const textures = getTextures ( materials ) ;
180198 const { lights, iesTextures } = getLights ( objects ) ;
181- updateMaterialIndexAttribute ( geometry , materials , materials ) ;
199+ if ( needsMaterialIndexUpdate ) {
200+
201+ updateMaterialIndexAttribute ( geometry , materials , materials ) ;
202+ this . _materialUuids = materials . map ( material => material . uuid ) ;
203+
204+ }
182205
183206 // only generate a new bvh if the objects used have changed
184207 if ( this . generateBVH ) {
@@ -220,6 +243,7 @@ export class PathTracingSceneGenerator {
220243 return {
221244 bvhChanged : result . changeType !== NO_CHANGE ,
222245 bvh : this . bvh ,
246+ needsMaterialIndexUpdate,
223247 lights,
224248 iesTextures,
225249 geometry,
0 commit comments