RectAreaLightHelper.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. */
  23. class RectAreaLightHelper extends Line {
  24. /**
  25. * Constructs a new rect area light helper.
  26. *
  27. * @param {RectAreaLight} light - The light to visualize.
  28. * @param {number|Color|string} [color] - The helper's color.
  29. * If this is not the set, the helper will take the color of the light.
  30. */
  31. constructor( light, color ) {
  32. const positions = [ 1, 1, 0, - 1, 1, 0, - 1, - 1, 0, 1, - 1, 0, 1, 1, 0 ];
  33. const geometry = new BufferGeometry();
  34. geometry.setAttribute( 'position', new Float32BufferAttribute( positions, 3 ) );
  35. geometry.computeBoundingSphere();
  36. const material = new LineBasicMaterial( { fog: false } );
  37. super( geometry, material );
  38. /**
  39. * The light to visualize.
  40. *
  41. * @type {RectAreaLight}
  42. */
  43. this.light = light;
  44. /**
  45. * The helper's color. If `undefined`, the helper will take the color of the light.
  46. *
  47. * @type {number|Color|string|undefined}
  48. */
  49. this.color = color;
  50. this.type = 'RectAreaLightHelper';
  51. //
  52. const positions2 = [ 1, 1, 0, - 1, 1, 0, - 1, - 1, 0, 1, 1, 0, - 1, - 1, 0, 1, - 1, 0 ];
  53. const geometry2 = new BufferGeometry();
  54. geometry2.setAttribute( 'position', new Float32BufferAttribute( positions2, 3 ) );
  55. geometry2.computeBoundingSphere();
  56. this.add( new Mesh( geometry2, new MeshBasicMaterial( { side: BackSide, fog: false } ) ) );
  57. }
  58. updateMatrixWorld() {
  59. this.scale.set( 0.5 * this.light.width, 0.5 * this.light.height, 1 );
  60. if ( this.color !== undefined ) {
  61. this.material.color.set( this.color );
  62. this.children[ 0 ].material.color.set( this.color );
  63. } else {
  64. this.material.color.copy( this.light.color ).multiplyScalar( this.light.intensity );
  65. // prevent hue shift
  66. const c = this.material.color;
  67. const max = Math.max( c.r, c.g, c.b );
  68. if ( max > 1 ) c.multiplyScalar( 1 / max );
  69. this.children[ 0 ].material.color.copy( this.material.color );
  70. }
  71. // ignore world scale on light
  72. this.matrixWorld.extractRotation( this.light.matrixWorld ).scale( this.scale ).copyPosition( this.light.matrixWorld );
  73. this.children[ 0 ].matrixWorld.copy( this.matrixWorld );
  74. }
  75. /**
  76. * Frees the GPU-related resources allocated by this instance. Call this
  77. * method whenever this instance is no longer used in your app.
  78. */
  79. dispose() {
  80. this.geometry.dispose();
  81. this.material.dispose();
  82. this.children[ 0 ].geometry.dispose();
  83. this.children[ 0 ].material.dispose();
  84. }
  85. }
  86. export { RectAreaLightHelper };
粤ICP备19079148号