Skip to content

Commit d7b6a32

Browse files
authored
TiledLightsNode: Fix cache-key performance (mrdoob#30130)
* fix lights property reference * fix normal map tiles * improve cache-key performance * cleanup * Update TiledLightsNode.js * cleanup * Update TiledLightsNode.js
1 parent 23d9a43 commit d7b6a32

File tree

3 files changed

+46
-37
lines changed

3 files changed

+46
-37
lines changed

examples/jsm/tsl/lighting/TiledLightsNode.js

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -52,30 +52,36 @@ class TiledLightsNode extends LightsNode {
5252
this.maxLights = maxLights;
5353
this.tileSize = tileSize;
5454

55-
this.bufferSize = null;
56-
this.lightIndexes = null;
57-
this.screenTileIndex = null;
58-
this.compute = null;
59-
this.lightsTexture = null;
60-
61-
this.lightsCount = uniform( 0, 'int' );
62-
this.tileLightCount = 8;
63-
this.screenSize = uniform( new Vector2() );
64-
this.cameraProjectionMatrix = uniform( 'mat4' );
65-
this.cameraViewMatrix = uniform( 'mat4' );
55+
this._bufferSize = null;
56+
this._lightIndexes = null;
57+
this._screenTileIndex = null;
58+
this._compute = null;
59+
this._lightsTexture = null;
60+
61+
this._lightsCount = uniform( 0, 'int' );
62+
this._tileLightCount = 8;
63+
this._screenSize = uniform( new Vector2() );
64+
this._cameraProjectionMatrix = uniform( 'mat4' );
65+
this._cameraViewMatrix = uniform( 'mat4' );
6666

6767
this.updateBeforeType = NodeUpdateType.RENDER;
6868

6969
}
7070

71+
customCacheKey() {
72+
73+
return this._compute.getCacheKey() + super.customCacheKey();
74+
75+
}
76+
7177
updateLightsTexture() {
7278

73-
const { lightsTexture, tiledLights } = this;
79+
const { _lightsTexture: lightsTexture, tiledLights } = this;
7480

7581
const data = lightsTexture.image.data;
7682
const lineSize = lightsTexture.image.width * 4;
7783

78-
this.lightsCount.value = tiledLights.length;
84+
this._lightsCount.value = tiledLights.length;
7985

8086
for ( let i = 0; i < tiledLights.length; i ++ ) {
8187

@@ -113,13 +119,13 @@ class TiledLightsNode extends LightsNode {
113119

114120
this.updateLightsTexture( camera );
115121

116-
this.cameraProjectionMatrix.value = camera.projectionMatrix;
117-
this.cameraViewMatrix.value = camera.matrixWorldInverse;
122+
this._cameraProjectionMatrix.value = camera.projectionMatrix;
123+
this._cameraViewMatrix.value = camera.matrixWorldInverse;
118124

119125
renderer.getDrawingBufferSize( _size );
120-
this.screenSize.value.copy( _size );
126+
this._screenSize.value.copy( _size );
121127

122-
renderer.compute( this.compute );
128+
renderer.compute( this._compute );
123129

124130
}
125131

@@ -153,7 +159,7 @@ class TiledLightsNode extends LightsNode {
153159

154160
getBlock( block = 0 ) {
155161

156-
return this.lightIndexes.element( this.screenTileIndex.mul( int( 2 ).add( int( block ) ) ) );
162+
return this._lightIndexes.element( this._screenTileIndex.mul( int( 2 ).add( int( block ) ) ) );
157163

158164
}
159165

@@ -163,21 +169,21 @@ class TiledLightsNode extends LightsNode {
163169

164170
const stride = int( 4 );
165171
const tileOffset = element.div( stride );
166-
const tileIndex = this.screenTileIndex.mul( int( 2 ) ).add( tileOffset );
172+
const tileIndex = this._screenTileIndex.mul( int( 2 ) ).add( tileOffset );
167173

168-
return this.lightIndexes.element( tileIndex ).element( element.modInt( stride ) );
174+
return this._lightIndexes.element( tileIndex ).element( element.modInt( stride ) );
169175

170176
}
171177

172178
getLightData( index ) {
173179

174180
index = int( index );
175181

176-
const dataA = textureLoad( this.lightsTexture, ivec2( index, 0 ) );
177-
const dataB = textureLoad( this.lightsTexture, ivec2( index, 1 ) );
182+
const dataA = textureLoad( this._lightsTexture, ivec2( index, 0 ) );
183+
const dataB = textureLoad( this._lightsTexture, ivec2( index, 1 ) );
178184

179185
const position = dataA.xyz;
180-
const viewPosition = this.cameraViewMatrix.mul( position );
186+
const viewPosition = this._cameraViewMatrix.mul( position );
181187
const distance = dataA.w;
182188
const color = dataB.rgb;
183189
const decay = dataB.w;
@@ -208,7 +214,7 @@ class TiledLightsNode extends LightsNode {
208214

209215
Fn( () => {
210216

211-
Loop( this.tileLightCount, ( { i } ) => {
217+
Loop( this._tileLightCount, ( { i } ) => {
212218

213219
const lightIndex = this.getTile( i );
214220

@@ -246,7 +252,7 @@ class TiledLightsNode extends LightsNode {
246252
width = this.getBufferFitSize( width );
247253
height = this.getBufferFitSize( height );
248254

249-
if ( ! this.bufferSize || this.bufferSize.width !== width || this.bufferSize.height !== height ) {
255+
if ( ! this._bufferSize || this._bufferSize.width !== width || this._bufferSize.height !== height ) {
250256

251257
this.create( width, height );
252258

@@ -263,11 +269,11 @@ class TiledLightsNode extends LightsNode {
263269
const width = this.getBufferFitSize( _size.width );
264270
const height = this.getBufferFitSize( _size.height );
265271

266-
if ( this.bufferSize === null ) {
272+
if ( this._bufferSize === null ) {
267273

268274
this.create( width, height );
269275

270-
} else if ( this.bufferSize.width !== width || this.bufferSize.height !== height ) {
276+
} else if ( this._bufferSize.width !== width || this._bufferSize.height !== height ) {
271277

272278
this.create( width, height );
273279

@@ -315,7 +321,7 @@ class TiledLightsNode extends LightsNode {
315321

316322
const compute = Fn( () => {
317323

318-
const { cameraProjectionMatrix, bufferSize, screenSize } = this;
324+
const { _cameraProjectionMatrix: cameraProjectionMatrix, _bufferSize: bufferSize, _screenSize: screenSize } = this;
319325

320326
const tiledBufferSize = bufferSize.clone().divideScalar( tileSize ).floor();
321327

@@ -335,7 +341,7 @@ class TiledLightsNode extends LightsNode {
335341

336342
Loop( this.maxLights, ( { i } ) => {
337343

338-
If( index.greaterThanEqual( this.tileLightCount ).or( int( i ).greaterThanEqual( int( this.lightsCount ) ) ), () => {
344+
If( index.greaterThanEqual( this._tileLightCount ).or( int( i ).greaterThanEqual( int( this._lightsCount ) ) ), () => {
339345

340346
Return();
341347

@@ -368,11 +374,11 @@ class TiledLightsNode extends LightsNode {
368374

369375
// assigns
370376

371-
this.bufferSize = bufferSize;
372-
this.lightIndexes = lightIndexes;
373-
this.screenTileIndex = screenTileIndex;
374-
this.compute = compute;
375-
this.lightsTexture = lightsTexture;
377+
this._bufferSize = bufferSize;
378+
this._lightIndexes = lightIndexes;
379+
this._screenTileIndex = screenTileIndex;
380+
this._compute = compute;
381+
this._lightsTexture = lightsTexture;
376382

377383
}
378384

examples/webgpu_lights_tiled.html

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<script type="module">
2727

2828
import * as THREE from 'three';
29-
import { texture, uv, pass, uniform } from 'three/tsl';
29+
import { texture, uv, pass, normalMap, uniform } from 'three/tsl';
3030
import { bloom } from 'three/addons/tsl/display/BloomNode.js';
3131

3232
import { TiledLighting } from 'three/addons/lighting/TiledLighting.js';
@@ -122,10 +122,12 @@
122122
floorNormal.wrapS = THREE.RepeatWrapping;
123123
floorNormal.wrapT = THREE.RepeatWrapping;
124124

125+
const uvTile = uv().mul( 50 );
126+
125127
const planeGeometry = new THREE.PlaneGeometry( 1000, 1000 );
126128
const planeMaterial = new THREE.MeshPhongNodeMaterial( {
127-
colorNode: texture( floorColor, uv().mul( 50 ) ),
128-
normalMap: floorNormal
129+
colorNode: texture( floorColor, uvTile ),
130+
normalNode: normalMap( texture( floorNormal, uvTile ) ),
129131
} );
130132

131133
const ground = new THREE.Mesh( planeGeometry, planeMaterial );

src/nodes/lighting/LightsNode.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ class LightsNode extends Node {
117117
customCacheKey() {
118118

119119
const lightIDs = [];
120+
const lights = this._lights;
120121

121122
for ( let i = 0; i < lights.length; i ++ ) {
122123

0 commit comments

Comments
 (0)