ClusteredLighting.js 1.8 KB

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