Skip to content

Commit 8e66fe5

Browse files
authored
WebGPURenderer: Fix lights hash (#27150)
1 parent c767592 commit 8e66fe5

File tree

7 files changed

+64
-48
lines changed

7 files changed

+64
-48
lines changed

examples/jsm/nodes/Nodes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ export { default as DirectionalLightNode } from './lighting/DirectionalLightNode
136136
export { default as SpotLightNode } from './lighting/SpotLightNode.js';
137137
export { default as IESSpotLightNode } from './lighting/IESSpotLightNode.js';
138138
export { default as AmbientLightNode } from './lighting/AmbientLightNode.js';
139-
export { default as LightsNode, lights, lightsWithoutWrap, addLightNode } from './lighting/LightsNode.js';
139+
export { default as LightsNode, lights, lightNodes, addLightNode } from './lighting/LightsNode.js';
140140
export { default as LightingNode /* @TODO: lighting (abstract), light */ } from './lighting/LightingNode.js';
141141
export { default as LightingContextNode, lightingContext } from './lighting/LightingContextNode.js';
142142
export { default as HemisphereLightNode } from './lighting/HemisphereLightNode.js';

examples/jsm/nodes/core/Node.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ class Node extends EventDispatcher {
5454

5555
* getChildren() {
5656

57-
const self = this;
58-
5957
for ( const { childNode } of getNodeChildren( this ) ) {
6058

6159
yield childNode;

examples/jsm/nodes/core/NodeUtils.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,13 @@ export function getCacheKey( object ) {
66

77
if ( object.isNode === true ) {
88

9-
cacheKey += `uuid:"${ object.uuid }"`;
9+
cacheKey += object.id;
1010

1111
}
1212

13-
for ( const { property, index, childNode } of getNodeChildren( object ) ) {
13+
for ( const { property, childNode } of getNodeChildren( object ) ) {
1414

15-
// @TODO: Think about implement NodeArray and NodeObject.
16-
17-
let childCacheKey = getCacheKey( childNode );
18-
if ( ! childCacheKey.includes( ',' ) ) childCacheKey = childCacheKey.slice( childCacheKey.indexOf( '"' ), childCacheKey.indexOf( '}' ) );
19-
cacheKey += `,${ property }${ index !== undefined ? '/' + index : '' }:${ childCacheKey }`;
15+
cacheKey += ',' + property.slice( 0, - 4 ) + ':' + childNode.getCacheKey();
2016

2117
}
2218

examples/jsm/nodes/lighting/AnalyticLightNode.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,21 @@ class AnalyticLightNode extends LightingNode {
2828
this.shadowNode = null;
2929

3030
this.color = new Color();
31-
this.colorNode = uniform( this.color );
31+
this._defaultColorNode = uniform( this.color );
32+
33+
this.colorNode = this._defaultColorNode;
34+
35+
this.isAnalyticLightNode = true;
36+
37+
}
38+
39+
getCacheKey() {
40+
41+
return super.getCacheKey() + '-' + ( this.light.id + '-' + ( this.light.castShadow ? '1' : '0' ) );
3242

3343
}
3444

35-
getHash( /*builder*/ ) {
45+
getHash() {
3646

3747
return this.light.uuid;
3848

@@ -148,6 +158,7 @@ class AnalyticLightNode extends LightingNode {
148158
setup( builder ) {
149159

150160
if ( this.light.castShadow ) this.setupShadow( builder );
161+
else if ( this.shadowNode !== null ) this.disposeShadow();
151162

152163
}
153164

@@ -156,6 +167,8 @@ class AnalyticLightNode extends LightingNode {
156167
const { rtt, light } = this;
157168
const { renderer, scene } = frame;
158169

170+
const currentOverrideMaterial = scene.overrideMaterial;
171+
159172
scene.overrideMaterial = depthMaterial;
160173

161174
rtt.setSize( light.shadow.mapSize.width, light.shadow.mapSize.height );
@@ -182,7 +195,18 @@ class AnalyticLightNode extends LightingNode {
182195
renderer.setRenderTarget( currentRenderTarget );
183196
renderer.setRenderObjectFunction( currentRenderObjectFunction );
184197

185-
scene.overrideMaterial = null;
198+
scene.overrideMaterial = currentOverrideMaterial;
199+
200+
}
201+
202+
disposeShadow() {
203+
204+
this.rtt.dispose();
205+
206+
this.shadowNode = null;
207+
this.rtt = null;
208+
209+
this.colorNode = this._defaultColorNode;
186210

187211
}
188212

examples/jsm/nodes/lighting/LightsNode.js

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,26 @@ class LightsNode extends Node {
3333

3434
}
3535

36+
getHash() {
37+
38+
if ( this._hash === null ) {
39+
40+
const hash = [];
41+
42+
for ( const lightNode of this.lightNodes ) {
43+
44+
hash.push( lightNode.getHash() );
45+
46+
}
47+
48+
this._hash = 'lights-' + hash.join( ',' );
49+
50+
}
51+
52+
return this._hash;
53+
54+
}
55+
3656
setup( builder ) {
3757

3858
const context = builder.context;
@@ -57,7 +77,7 @@ class LightsNode extends Node {
5777
for ( const lightNode of lightNodes ) {
5878

5979
lightNode.build( builder );
60-
80+
6181
}
6282

6383
//
@@ -76,7 +96,7 @@ class LightsNode extends Node {
7696
if ( backdrop !== null ) {
7797

7898
totalDiffuse = vec3( backdropAlpha !== null ? backdropAlpha.mix( totalDiffuse, backdrop ) : backdrop );
79-
99+
80100
}
81101

82102
totalDiffuseNode.assign( totalDiffuse );
@@ -98,35 +118,11 @@ class LightsNode extends Node {
98118

99119
}
100120

101-
getHash( builder ) {
102-
103-
if ( this._hash === null ) {
104-
105-
let hash = '';
106-
107-
const lightNodes = this.lightNodes;
108-
109-
for ( const lightNode of lightNodes ) {
110-
111-
hash += lightNode.getHash( builder ) + ' ';
112-
113-
}
114-
115-
this._hash = hash;
116-
117-
}
118-
119-
return this._hash;
120-
121-
}
122-
123-
getLightNodeByHash( hash ) {
124-
125-
const lightNodes = this.lightNodes;
121+
_getLightNodeById( id ) {
126122

127-
for ( const lightNode of lightNodes ) {
123+
for ( const lightNode of this.lightNodes ) {
128124

129-
if ( lightNode.light.uuid === hash ) {
125+
if ( lightNode.isAnalyticLightNode && lightNode.light.id === id ) {
130126

131127
return lightNode;
132128

@@ -146,7 +142,7 @@ class LightsNode extends Node {
146142

147143
for ( const light of lights ) {
148144

149-
let lightNode = this.getLightNodeByHash( light.uuid );
145+
let lightNode = this._getLightNodeById( light.id );
150146

151147
if ( lightNode === null ) {
152148

@@ -173,7 +169,7 @@ class LightsNode extends Node {
173169
export default LightsNode;
174170

175171
export const lights = ( lights ) => nodeObject( new LightsNode().fromLights( lights ) );
176-
export const lightsWithoutWrap = nodeProxy( LightsNode );
172+
export const lightNodes = nodeProxy( LightsNode );
177173

178174
export function addLightNode( lightClass, lightNodeClass ) {
179175

examples/jsm/nodes/materials/NodeMaterial.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { skinning } from '../accessors/SkinningNode.js';
1111
import { morph } from '../accessors/MorphNode.js';
1212
import { texture } from '../accessors/TextureNode.js';
1313
import { cubeTexture } from '../accessors/CubeTextureNode.js';
14-
import { lightsWithoutWrap } from '../lighting/LightsNode.js';
14+
import { lightNodes } from '../lighting/LightsNode.js';
1515
import { mix } from '../math/MathNode.js';
1616
import { float, vec3, vec4 } from '../shadernode/ShaderNode.js';
1717
import AONode from '../lighting/AONode.js';
@@ -257,7 +257,7 @@ class NodeMaterial extends ShaderMaterial {
257257

258258
if ( materialLightsNode.length > 0 ) {
259259

260-
lightsNode = lightsWithoutWrap( [ ...lightsNode.lightNodes, ...materialLightsNode ] );
260+
lightsNode = lightNodes( [ ...lightsNode.lightNodes, ...materialLightsNode ] );
261261

262262
}
263263

examples/jsm/renderers/common/RenderObjects.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,16 @@ class RenderObjects {
3333

3434
if ( renderObject.version !== material.version || renderObject.needsUpdate ) {
3535

36-
renderObject.version = material.version;
37-
3836
if ( renderObject.initialCacheKey !== renderObject.getCacheKey() ) {
3937

4038
renderObject.dispose();
4139

4240
renderObject = this.get( object, material, scene, camera, lightsNode, renderContext, passId );
4341

42+
} else {
43+
44+
renderObject.version = material.version;
45+
4446
}
4547

4648
}

0 commit comments

Comments
 (0)