DynamicLighting.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { Lighting, LightsNode } from 'three/webgpu';
  2. import DynamicLightsNode from '../tsl/lighting/DynamicLightsNode.js';
  3. const _defaultLights = /*@__PURE__*/ new LightsNode();
  4. /**
  5. * A custom lighting implementation that batches supported analytic lights into
  6. * uniform arrays so light count changes do not recompile materials.
  7. *
  8. * ```js
  9. * const lighting = new DynamicLighting( { maxPointLights: 64 } );
  10. * renderer.lighting = lighting;
  11. * ```
  12. *
  13. * @augments Lighting
  14. * @three_import import { DynamicLighting } from 'three/addons/lighting/DynamicLighting.js';
  15. */
  16. export class DynamicLighting extends Lighting {
  17. /**
  18. * Constructs a new dynamic lighting system.
  19. *
  20. * @param {Object} [options={}] - Dynamic lighting configuration.
  21. * @param {number} [options.maxDirectionalLights=8] - Maximum number of batched directional lights.
  22. * @param {number} [options.maxPointLights=16] - Maximum number of batched point lights.
  23. * @param {number} [options.maxSpotLights=16] - Maximum number of batched spot lights.
  24. * @param {number} [options.maxHemisphereLights=4] - Maximum number of batched hemisphere lights.
  25. */
  26. constructor( options = {} ) {
  27. super();
  28. this.options = {
  29. maxDirectionalLights: 8,
  30. maxPointLights: 16,
  31. maxSpotLights: 16,
  32. maxHemisphereLights: 4,
  33. ...options
  34. };
  35. this._nodes = new WeakMap();
  36. }
  37. /**
  38. * Creates a new dynamic lights node for the given array of lights.
  39. *
  40. * @param {Array<Light>} lights - The lights to bind to the node.
  41. * @return {DynamicLightsNode} The dynamic lights node.
  42. */
  43. createNode( lights = [] ) {
  44. return new DynamicLightsNode( this.options ).setLights( lights );
  45. }
  46. /**
  47. * Returns a lights node for the given scene.
  48. *
  49. * @param {Scene} scene - The scene.
  50. * @return {LightsNode} The lights node.
  51. */
  52. getNode( scene ) {
  53. if ( scene.isQuadMesh ) return _defaultLights;
  54. let node = this._nodes.get( scene );
  55. if ( node === undefined ) {
  56. node = this.createNode();
  57. this._nodes.set( scene, node );
  58. }
  59. return node;
  60. }
  61. }
  62. export default DynamicLighting;
粤ICP备19079148号