@@ -73,6 +73,8 @@ function getLights( objects ) {
7373
7474}
7575
76+ let materialUuids = null ;
77+
7678export class PathTracingSceneGenerator {
7779
7880 get initialized ( ) {
@@ -176,9 +178,29 @@ export class PathTracingSceneGenerator {
176178 // generate the geometry
177179 const result = staticGeometryGenerator . generate ( geometry ) ;
178180 const materials = result . materials ;
181+ let needsMaterialIndexUpdate = result . changeType !== NO_CHANGE || materialUuids === null || materialUuids . length !== length ;
182+ for ( let index = 0 , length = materials . length ; ( index < length ) && ! needsMaterialIndexUpdate ; index ++ ) {
183+
184+ if ( materialUuids === null || materialUuids . length !== length ) {
185+
186+ needsMaterialIndexUpdate = true ;
187+ break ;
188+
189+ }
190+
191+ const material = materials [ index ] ;
192+ if ( material . uuid !== materialUuids [ index ] ) needsMaterialIndexUpdate = true ;
193+
194+ }
195+
196+ materialUuids = materials . map ( material => material . uuid ) ;
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+
203+ }
182204
183205 // only generate a new bvh if the objects used have changed
184206 if ( this . generateBVH ) {
@@ -220,6 +242,7 @@ export class PathTracingSceneGenerator {
220242 return {
221243 bvhChanged : result . changeType !== NO_CHANGE ,
222244 bvh : this . bvh ,
245+ needsMaterialIndexUpdate,
223246 lights,
224247 iesTextures,
225248 geometry,
0 commit comments