forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTiledLightsNode.js
421 lines (264 loc) · 9.86 KB
/
TiledLightsNode.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import { DataTexture, FloatType, RGBAFormat, Vector2, Vector3, LightsNode, NodeUpdateType } from 'three/webgpu';
import {
attributeArray, nodeProxy, int, float, vec2, ivec2, ivec4, uniform, Break, Loop, positionView,
Fn, If, Return, textureLoad, instanceIndex, screenCoordinate, directPointLight
} from 'three/tsl';
export const circleIntersectsAABB = /*@__PURE__*/ Fn( ( [ circleCenter, radius, minBounds, maxBounds ] ) => {
// Find the closest point on the AABB to the circle's center using method chaining
const closestX = minBounds.x.max( circleCenter.x.min( maxBounds.x ) );
const closestY = minBounds.y.max( circleCenter.y.min( maxBounds.y ) );
// Compute the distance between the circle's center and the closest point
const distX = circleCenter.x.sub( closestX );
const distY = circleCenter.y.sub( closestY );
// Calculate the squared distance
const distSquared = distX.mul( distX ).add( distY.mul( distY ) );
return distSquared.lessThanEqual( radius.mul( radius ) );
} ).setLayout( {
name: 'circleIntersectsAABB',
type: 'bool',
inputs: [
{ name: 'circleCenter', type: 'vec2' },
{ name: 'radius', type: 'float' },
{ name: 'minBounds', type: 'vec2' },
{ name: 'maxBounds', type: 'vec2' }
]
} );
const _vector3 = /*@__PURE__*/ new Vector3();
const _size = /*@__PURE__*/ new Vector2();
/**
* A custom version of `LightsNode` implementing tiled lighting. This node is used in
* {@link TiledLighting} to overwrite the renderer's default lighting with
* a custom implementation.
*
* @augments LightsNode
*/
class TiledLightsNode extends LightsNode {
static get type() {
return 'TiledLightsNode';
}
/**
* Constructs a new tiled lights node.
*
* @param {number} [maxLights=1024] - The maximum number of lights.
* @param {number} [tileSize=32] - The tile size.
*/
constructor( maxLights = 1024, tileSize = 32 ) {
super();
this.materialLights = [];
this.tiledLights = [];
/**
* The maximum number of lights.
*
* @type {number}
* @default 1024
*/
this.maxLights = maxLights;
/**
* The tile size.
*
* @type {number}
* @default 32
*/
this.tileSize = tileSize;
this._bufferSize = null;
this._lightIndexes = null;
this._screenTileIndex = null;
this._compute = null;
this._lightsTexture = null;
this._lightsCount = uniform( 0, 'int' );
this._tileLightCount = 8;
this._screenSize = uniform( new Vector2() );
this._cameraProjectionMatrix = uniform( 'mat4' );
this._cameraViewMatrix = uniform( 'mat4' );
this.updateBeforeType = NodeUpdateType.RENDER;
}
customCacheKey() {
return this._compute.getCacheKey() + super.customCacheKey();
}
updateLightsTexture() {
const { _lightsTexture: lightsTexture, tiledLights } = this;
const data = lightsTexture.image.data;
const lineSize = lightsTexture.image.width * 4;
this._lightsCount.value = tiledLights.length;
for ( let i = 0; i < tiledLights.length; i ++ ) {
const light = tiledLights[ i ];
// world position
_vector3.setFromMatrixPosition( light.matrixWorld );
// store data
const offset = i * 4;
data[ offset + 0 ] = _vector3.x;
data[ offset + 1 ] = _vector3.y;
data[ offset + 2 ] = _vector3.z;
data[ offset + 3 ] = light.distance;
data[ lineSize + offset + 0 ] = light.color.r * light.intensity;
data[ lineSize + offset + 1 ] = light.color.g * light.intensity;
data[ lineSize + offset + 2 ] = light.color.b * light.intensity;
data[ lineSize + offset + 3 ] = light.decay;
}
lightsTexture.needsUpdate = true;
}
updateBefore( frame ) {
const { renderer, camera } = frame;
this.updateProgram( renderer );
this.updateLightsTexture( camera );
this._cameraProjectionMatrix.value = camera.projectionMatrix;
this._cameraViewMatrix.value = camera.matrixWorldInverse;
renderer.getDrawingBufferSize( _size );
this._screenSize.value.copy( _size );
renderer.compute( this._compute );
}
setLights( lights ) {
const { tiledLights, materialLights } = this;
let materialindex = 0;
let tiledIndex = 0;
for ( const light of lights ) {
if ( light.isPointLight === true ) {
tiledLights[ tiledIndex ++ ] = light;
} else {
materialLights[ materialindex ++ ] = light;
}
}
materialLights.length = materialindex;
tiledLights.length = tiledIndex;
return super.setLights( materialLights );
}
getBlock( block = 0 ) {
return this._lightIndexes.element( this._screenTileIndex.mul( int( 2 ).add( int( block ) ) ) );
}
getTile( element ) {
element = int( element );
const stride = int( 4 );
const tileOffset = element.div( stride );
const tileIndex = this._screenTileIndex.mul( int( 2 ) ).add( tileOffset );
return this._lightIndexes.element( tileIndex ).element( element.mod( stride ) );
}
getLightData( index ) {
index = int( index );
const dataA = textureLoad( this._lightsTexture, ivec2( index, 0 ) );
const dataB = textureLoad( this._lightsTexture, ivec2( index, 1 ) );
const position = dataA.xyz;
const viewPosition = this._cameraViewMatrix.mul( position );
const distance = dataA.w;
const color = dataB.rgb;
const decay = dataB.w;
return {
position,
viewPosition,
distance,
color,
decay
};
}
setupLights( builder, lightNodes ) {
this.updateProgram( builder.renderer );
//
const lightingModel = builder.context.reflectedLight;
// force declaration order, before of the loop
lightingModel.directDiffuse.append();
lightingModel.directSpecular.append();
super.setupLights( builder, lightNodes );
Fn( () => {
Loop( this._tileLightCount, ( { i } ) => {
const lightIndex = this.getTile( i );
If( lightIndex.equal( int( 0 ) ), () => {
Break();
} );
const { color, decay, viewPosition, distance } = this.getLightData( lightIndex.sub( 1 ) );
builder.lightsNode.setupDirectLight( builder, this, directPointLight( {
color,
lightVector: viewPosition.sub( positionView ),
cutoffDistance: distance,
decayExponent: decay
} ) );
} );
} )().append();
}
getBufferFitSize( value ) {
const multiple = this.tileSize;
return Math.ceil( value / multiple ) * multiple;
}
setSize( width, height ) {
width = this.getBufferFitSize( width );
height = this.getBufferFitSize( height );
if ( ! this._bufferSize || this._bufferSize.width !== width || this._bufferSize.height !== height ) {
this.create( width, height );
}
return this;
}
updateProgram( renderer ) {
renderer.getDrawingBufferSize( _size );
const width = this.getBufferFitSize( _size.width );
const height = this.getBufferFitSize( _size.height );
if ( this._bufferSize === null ) {
this.create( width, height );
} else if ( this._bufferSize.width !== width || this._bufferSize.height !== height ) {
this.create( width, height );
}
}
create( width, height ) {
const { tileSize, maxLights } = this;
const bufferSize = new Vector2( width, height );
const lineSize = Math.floor( bufferSize.width / tileSize );
const count = Math.floor( ( bufferSize.width * bufferSize.height ) / tileSize );
// buffers
const lightsData = new Float32Array( maxLights * 4 * 2 ); // 2048 lights, 4 elements(rgba), 2 components, 1 component per line (position, distance, color, decay)
const lightsTexture = new DataTexture( lightsData, lightsData.length / 8, 2, RGBAFormat, FloatType );
const lightIndexesArray = new Int32Array( count * 4 * 2 );
const lightIndexes = attributeArray( lightIndexesArray, 'ivec4' ).label( 'lightIndexes' );
// compute
const getBlock = ( index ) => {
const tileIndex = instanceIndex.mul( int( 2 ) ).add( int( index ) );
return lightIndexes.element( tileIndex );
};
const getTile = ( elementIndex ) => {
elementIndex = int( elementIndex );
const stride = int( 4 );
const tileOffset = elementIndex.div( stride );
const tileIndex = instanceIndex.mul( int( 2 ) ).add( tileOffset );
return lightIndexes.element( tileIndex ).element( elementIndex.mod( stride ) );
};
const compute = Fn( () => {
const { _cameraProjectionMatrix: cameraProjectionMatrix, _bufferSize: bufferSize, _screenSize: screenSize } = this;
const tiledBufferSize = bufferSize.clone().divideScalar( tileSize ).floor();
const tileScreen = vec2(
instanceIndex.mod( tiledBufferSize.width ),
instanceIndex.div( tiledBufferSize.width )
).mul( tileSize ).div( screenSize );
const blockSize = float( tileSize ).div( screenSize );
const minBounds = tileScreen;
const maxBounds = minBounds.add( blockSize );
const index = int( 0 ).toVar();
getBlock( 0 ).assign( ivec4( 0 ) );
getBlock( 1 ).assign( ivec4( 0 ) );
Loop( this.maxLights, ( { i } ) => {
If( index.greaterThanEqual( this._tileLightCount ).or( int( i ).greaterThanEqual( int( this._lightsCount ) ) ), () => {
Return();
} );
const { viewPosition, distance } = this.getLightData( i );
const projectedPosition = cameraProjectionMatrix.mul( viewPosition );
const ndc = projectedPosition.div( projectedPosition.w );
const screenPosition = ndc.xy.mul( 0.5 ).add( 0.5 ).flipY();
const distanceFromCamera = viewPosition.z;
const pointRadius = distance.div( distanceFromCamera );
If( circleIntersectsAABB( screenPosition, pointRadius, minBounds, maxBounds ), () => {
getTile( index ).assign( i.add( int( 1 ) ) );
index.addAssign( int( 1 ) );
} );
} );
} )().compute( count );
// screen coordinate lighting indexes
const screenTile = screenCoordinate.div( tileSize ).floor().toVar();
const screenTileIndex = screenTile.x.add( screenTile.y.mul( lineSize ) );
// assigns
this._bufferSize = bufferSize;
this._lightIndexes = lightIndexes;
this._screenTileIndex = screenTileIndex;
this._compute = compute;
this._lightsTexture = lightsTexture;
}
get hasLights() {
return super.hasLights || this.tiledLights.length > 0;
}
}
export default TiledLightsNode;
export const tiledLights = /*@__PURE__*/ nodeProxy( TiledLightsNode );