Skip to content

Commit 46b395e

Browse files
authored
Merge pull request #666 from dongho-shin/dev/fix-materialAttributeIndex
updateMaterialIndexAttribute when it needs
2 parents 44599eb + e8e5cc1 commit 46b395e

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

src/core/PathTracingSceneGenerator.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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,

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)