RectAreaLightHelper.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import {
  2. BackSide,
  3. BufferGeometry,
  4. Float32BufferAttribute,
  5. Mesh,
  6. MeshBasicMaterial
  7. } from 'three';
  8. /**
  9. * Creates a visual aid for rect area lights.
  10. *
  11. * `RectAreaLightHelper` must be added as a child of the light.
  12. *
  13. * ```js
  14. * const light = new THREE.RectAreaLight( 0xffffbb, 1.0, 5, 5 );
  15. * const helper = new RectAreaLightHelper( light );
  16. * light.add( helper );
  17. * ```
  18. *
  19. * @augments Mesh
  20. * @three_import import { RectAreaLightHelper } from 'three/addons/helpers/RectAreaLightHelper.js';
  21. */
  22. class RectAreaLightHelper extends Mesh {
  23. /**
  24. * Constructs a new rect area light helper.
  25. *
  26. * @param {RectAreaLight} light - The light to visualize.
  27. * @param {number|Color|string} [color] - The helper's color.
  28. * If this is not the set, the helper will take the color of the light.
  29. */
  30. constructor( light, color ) {
  31. const positions = [ 1, 1, 0, - 1, 1, 0, - 1, - 1, 0, 1, 1, 0, - 1, - 1, 0, 1, - 1, 0 ];
  32. const uvs = [ 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0 ];
  33. const geometry = new BufferGeometry();
  34. geometry.setAttribute( 'position', new Float32BufferAttribute( positions, 3 ) );
  35. geometry.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
  36. geometry.computeBoundingSphere();
  37. const material = new MeshBasicMaterial( { side: BackSide, 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. updateMatrixWorld() {
  54. this.scale.set( 0.5 * this.light.width, 0.5 * this.light.height, 1 );
  55. if ( this.color !== undefined ) {
  56. this.material.color.set( this.color );
  57. } else {
  58. this.material.color.copy( this.light.color ).multiplyScalar( this.light.intensity );
  59. // prevent hue shift
  60. const c = this.material.color;
  61. const max = Math.max( c.r, c.g, c.b );
  62. if ( max > 1 ) c.multiplyScalar( 1 / max );
  63. }
  64. // Apply light's map texture to the helper mesh
  65. if ( this.material.map !== this.light.map ) {
  66. this.material.map = this.light.map;
  67. this.material.needsUpdate = true;
  68. }
  69. if ( this.light.map !== null && this.color === undefined ) {
  70. const intensityScale = Math.sqrt( this.light.intensity / 5 );
  71. this.material.color.setScalar( Math.min( intensityScale, 2.0 ) );
  72. }
  73. // ignore world scale on light
  74. this.matrixWorld.extractRotation( this.light.matrixWorld ).scale( this.scale ).copyPosition( this.light.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. }
  84. }
  85. export { RectAreaLightHelper };
粤ICP备19079148号