Skip to content

Commit

Permalink
Merge pull request #666 from dongho-shin/dev/fix-materialAttributeIndex
Browse files Browse the repository at this point in the history
updateMaterialIndexAttribute when it needs
  • Loading branch information
gkjohnson authored Jul 31, 2024
2 parents 44599eb + e8e5cc1 commit 46b395e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
26 changes: 25 additions & 1 deletion src/core/PathTracingSceneGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export class PathTracingSceneGenerator {
this._bvhWorker = null;
this._pendingGenerate = null;
this._buildAsync = false;
this._materialUuids = null;

}

Expand Down Expand Up @@ -176,9 +177,31 @@ export class PathTracingSceneGenerator {
// generate the geometry
const result = staticGeometryGenerator.generate( geometry );
const materials = result.materials;
let needsMaterialIndexUpdate = result.changeType !== NO_CHANGE || this._materialUuids === null || this._materialUuids.length !== length;
if ( ! needsMaterialIndexUpdate ) {

for ( let i = 0, length = materials.length; i < length; i ++ ) {

const material = materials[ i ];
if ( material.uuid !== this._materialUuids[ i ] ) {

needsMaterialIndexUpdate = true;
break;

}

}

}

const textures = getTextures( materials );
const { lights, iesTextures } = getLights( objects );
updateMaterialIndexAttribute( geometry, materials, materials );
if ( needsMaterialIndexUpdate ) {

updateMaterialIndexAttribute( geometry, materials, materials );
this._materialUuids = materials.map( material => material.uuid );

}

// only generate a new bvh if the objects used have changed
if ( this.generateBVH ) {
Expand Down Expand Up @@ -220,6 +243,7 @@ export class PathTracingSceneGenerator {
return {
bvhChanged: result.changeType !== NO_CHANGE,
bvh: this.bvh,
needsMaterialIndexUpdate,
lights,
iesTextures,
geometry,
Expand Down
7 changes: 6 additions & 1 deletion src/core/WebGLPathTracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ export class WebGLPathTracer {
geometry,
bvh,
bvhChanged,
needsMaterialIndexUpdate,
} = results;

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

}

material.materialIndexAttribute.updateFrom( geometry.attributes.materialIndex );
if ( needsMaterialIndexUpdate ) {

material.materialIndexAttribute.updateFrom( geometry.attributes.materialIndex );

}

// save previously used items
this._previousScene = scene;
Expand Down
1 change: 1 addition & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export interface PathTracingSceneGeneratorResult {
lights: Array<Light>;
iesTextures: Array<DataTexture>;
geometry: BufferGeometry;
needsMaterialIndexUpdate: boolean;
materials: Array<Material>;
textures: Array<Texture>;
objects: Array<Object3D>;
Expand Down

0 comments on commit 46b395e

Please sign in to comment.