Skip to content

Commit 0dce66b

Browse files
mrdoobsunagclaude
authored
WebGPURenderer: Added ClusteredLighting (Forward+ clustered) shading. (#33406)
Co-authored-by: sunag <sunagbrasil@gmail.com> Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 95743af commit 0dce66b

5 files changed

Lines changed: 968 additions & 0 deletions

File tree

examples/files.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@
354354
"webgpu_lensflares",
355355
"webgpu_lightprobe",
356356
"webgpu_lightprobe_cubecamera",
357+
"webgpu_lights_clustered",
357358
"webgpu_lights_custom",
358359
"webgpu_lights_dynamic",
359360
"webgpu_lights_ies_spotlight",
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { Lighting } from 'three/webgpu';
2+
import ClusteredLightsNode from '../tsl/lighting/ClusteredLightsNode.js';
3+
4+
/**
5+
* A custom lighting implementation based on Forward+ Clustered Shading that
6+
* overwrites the default lighting system in {@link WebGPURenderer}. Suitable
7+
* for 3D scenes with many point lights and real depth complexity — the view
8+
* frustum is partitioned into a 3D cluster grid so only the lights actually
9+
* reaching each fragment are evaluated.
10+
*
11+
* ```js
12+
* const lighting = new ClusteredLighting();
13+
* renderer.lighting = lighting; // set lighting system
14+
* ```
15+
*
16+
* @augments Lighting
17+
* @three_import import { ClusteredLighting } from 'three/addons/lighting/ClusteredLighting.js';
18+
*/
19+
export class ClusteredLighting extends Lighting {
20+
21+
/**
22+
* Constructs a new clustered lighting system.
23+
*
24+
* @param {number} [maxLights=1024] - Maximum number of point lights.
25+
* @param {number} [tileSize=32] - Screen tile size in pixels (cluster XY size).
26+
* @param {number} [zSlices=24] - Number of exponential depth slices.
27+
* @param {number} [maxLightsPerCluster=64] - Per-cluster light-list capacity.
28+
*/
29+
constructor( maxLights = 1024, tileSize = 32, zSlices = 24, maxLightsPerCluster = 64 ) {
30+
31+
super();
32+
33+
this.maxLights = maxLights;
34+
this.tileSize = tileSize;
35+
this.zSlices = zSlices;
36+
this.maxLightsPerCluster = maxLightsPerCluster;
37+
38+
}
39+
40+
/**
41+
* Creates a new clustered lights node for the given array of lights.
42+
*
43+
* This method is called internally by the renderer and must be overwritten by
44+
* all custom lighting implementations.
45+
*
46+
* @param {Array<Light>} lights - The lights.
47+
* @return {ClusteredLightsNode} The clustered lights node.
48+
*/
49+
createNode( lights = [] ) {
50+
51+
return new ClusteredLightsNode( this.maxLights, this.tileSize, this.zSlices, this.maxLightsPerCluster ).setLights( lights );
52+
53+
}
54+
55+
}

0 commit comments

Comments
 (0)