Skip to content

Commit a41b679

Browse files
committed
updateMaterialIndexAttribute when it needs
1 parent 911852a commit a41b679

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

src/core/PathTracingSceneGenerator.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ function getLights( objects ) {
7373

7474
}
7575

76+
let materialUuids = null;
77+
7678
export 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,

src/core/WebGLPathTracer.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ export class WebGLPathTracer {
343343
geometry,
344344
bvh,
345345
bvhChanged,
346+
needsMaterialIndexUpdate,
346347
} = results;
347348

348349
this._materials = materials;
@@ -362,7 +363,11 @@ export class WebGLPathTracer {
362363

363364
}
364365

365-
material.materialIndexAttribute.updateFrom( geometry.attributes.materialIndex );
366+
if ( needsMaterialIndexUpdate ) {
367+
368+
material.materialIndexAttribute.updateFrom( geometry.attributes.materialIndex );
369+
370+
}
366371

367372
// save previously used items
368373
this._previousScene = scene;

src/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export interface PathTracingSceneGeneratorResult {
5757
lights: Array<Light>;
5858
iesTextures: Array<DataTexture>;
5959
geometry: BufferGeometry;
60+
needsMaterialIndexUpdate: boolean;
6061
materials: Array<Material>;
6162
textures: Array<Texture>;
6263
objects: Array<Object3D>;

0 commit comments

Comments
 (0)