RectAreaLightHelper.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import {
  2. BackSide,
  3. BufferGeometry,
  4. Float32BufferAttribute,
  5. Line,
  6. LineBasicMaterial,
  7. Mesh,
  8. MeshBasicMaterial
  9. } from 'three';
  10. /**
  11. * Creates a visual aid for rect area lights.
  12. *
  13. * `RectAreaLightHelper` must be added as a child of the light.
  14. *
  15. * ```js
  16. * const light = new THREE.RectAreaLight( 0xffffbb, 1.0, 5, 5 );
  17. * const helper = new RectAreaLightHelper( light );
  18. * light.add( helper );
  19. * ```
  20. *
  21. * @augments Line
  22. * @three_import import { RectAreaLightHelper } from 'three/addons/helpers/RectAreaLightHelper.js';
  23. */
  24. class RectAreaLightHelper extends Line {
  25. /**
  26. * Constructs a new rect area light helper.
  27. *
  28. * @param {RectAreaLight} light - The light to visualize.
  29. * @param {number|Color|string} [color] - The helper's color.
  30. * If this is not the set, the helper will take the color of the light.
  31. */
  32. constructor( light, color ) {
  33. const positions = [ 1, 1, 0, - 1, 1, 0, - 1, - 1, 0, 1, - 1, 0, 1, 1, 0 ];
  34. const geometry = new BufferGeometry();
  35. geometry.setAttribute( 'position', new Float32BufferAttribute( positions, 3 ) );
  36. geometry.computeBoundingSphere();
  37. const material = new LineBasicMaterial( { fog: false } );
  38. super( geometry, material );
  39. /**
  40. * The light to visualize.
  41. *
  42. * @type {RectAreaLight}
  43. */
  44. this.light = light;
  45. /**
  46. * The helper's color. If `undefined`, the helper will take the color of the light.
  47. *
  48. * @type {number|Color|string|undefined}
  49. */
  50. this.color = color;
  51. this.type = 'RectAreaLightHelper';
  52. //
  53. const positions2 = [ 1, 1, 0, - 1, 1, 0, - 1, - 1, 0, 1, 1, 0, - 1, - 1, 0, 1, - 1, 0 ];
  54. const geometry2 = new BufferGeometry();
  55. geometry2.setAttribute( 'position', new Float32BufferAttribute( positions2, 3 ) );
  56. geometry2.computeBoundingSphere();
  57. this.add( new Mesh( geometry2, new MeshBasicMaterial( { side: BackSide, fog: false } ) ) );
  58. }
  59. updateMatrixWorld() {
  60. this.scale.set( 0.5 * this.light.width, 0.5 * this.light.height, 1 );
  61. if ( this.color !== undefined ) {
  62. this.material.color.set( this.color );
  63. this.children[ 0 ].material.color.set( this.color );
  64. } else {
  65. this.material.color.copy( this.light.color ).multiplyScalar( this.light.intensity );
  66. // prevent hue shift
  67. const c = this.material.color;
  68. const max = Math.max( c.r, c.g, c.b );
  69. if ( max > 1 ) c.multiplyScalar( 1 / max );
  70. this.children[ 0 ].material.color.copy( this.material.color );
  71. }
  72. // ignore world scale on light
  73. this.matrixWorld.extractRotation( this.light.matrixWorld ).scale( this.scale ).copyPosition( this.light.matrixWorld );
  74. this.children[ 0 ].matrixWorld.copy( this.matrixWorld );
  75. }
  76. /**
  77. * Frees the GPU-related resources allocated by this instance. Call this
  78. * method whenever this instance is no longer used in your app.
  79. */
  80. dispose() {
  81. this.geometry.dispose();
  82. this.material.dispose();
  83. this.children[ 0 ].geometry.dispose();
  84. this.children[ 0 ].material.dispose();
  85. }
  86. }
  87. export { RectAreaLightHelper };