RectAreaLightHelper.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import {
  2. BackSide,
  3. BufferGeometry,
  4. Float32BufferAttribute,
  5. Line,
  6. LineBasicMaterial,
  7. Mesh,
  8. MeshBasicMaterial
  9. } from '../../../build/three.module.js';
  10. /**
  11. * This helper must be added as a child of the light
  12. */
  13. class RectAreaLightHelper extends Line {
  14. constructor( light, color ) {
  15. const positions = [ 1, 1, 0, - 1, 1, 0, - 1, - 1, 0, 1, - 1, 0, 1, 1, 0 ];
  16. const geometry = new BufferGeometry();
  17. geometry.setAttribute( 'position', new Float32BufferAttribute( positions, 3 ) );
  18. geometry.computeBoundingSphere();
  19. const material = new LineBasicMaterial( { fog: false } );
  20. super( geometry, material );
  21. this.light = light;
  22. this.color = color; // optional hardwired color for the helper
  23. this.type = 'RectAreaLightHelper';
  24. //
  25. const positions2 = [ 1, 1, 0, - 1, 1, 0, - 1, - 1, 0, 1, 1, 0, - 1, - 1, 0, 1, - 1, 0 ];
  26. const geometry2 = new BufferGeometry();
  27. geometry2.setAttribute( 'position', new Float32BufferAttribute( positions2, 3 ) );
  28. geometry2.computeBoundingSphere();
  29. this.add( new Mesh( geometry2, new MeshBasicMaterial( { side: BackSide, fog: false } ) ) );
  30. }
  31. updateMatrixWorld() {
  32. this.scale.set( 0.5 * this.light.width, 0.5 * this.light.height, 1 );
  33. if ( this.color !== undefined ) {
  34. this.material.color.set( this.color );
  35. this.children[ 0 ].material.color.set( this.color );
  36. } else {
  37. this.material.color.copy( this.light.color ).multiplyScalar( this.light.intensity );
  38. // prevent hue shift
  39. const c = this.material.color;
  40. const max = Math.max( c.r, c.g, c.b );
  41. if ( max > 1 ) c.multiplyScalar( 1 / max );
  42. this.children[ 0 ].material.color.copy( this.material.color );
  43. }
  44. // ignore world scale on light
  45. this.matrixWorld.extractRotation( this.light.matrixWorld ).scale( this.scale ).copyPosition( this.light.matrixWorld );
  46. this.children[ 0 ].matrixWorld.copy( this.matrixWorld );
  47. }
  48. dispose() {
  49. this.geometry.dispose();
  50. this.material.dispose();
  51. this.children[ 0 ].geometry.dispose();
  52. this.children[ 0 ].material.dispose();
  53. }
  54. }
  55. export { RectAreaLightHelper };