|
| 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