RectAreaLightHelper.js 2.0 KB

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