RectAreaLightHelper.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * @author abelnation / http://github.com/abelnation
  3. * @author Mugen87 / http://github.com/Mugen87
  4. * @author WestLangley / http://github.com/WestLangley
  5. */
  6. import { Object3D } from '../core/Object3D';
  7. import { Line } from '../objects/Line';
  8. import { LineBasicMaterial } from '../materials/LineBasicMaterial';
  9. import { BufferGeometry } from '../core/BufferGeometry';
  10. import { BufferAttribute } from '../core/BufferAttribute';
  11. function RectAreaLightHelper( light, overrideColor ) {
  12. Object3D.call( this );
  13. this.light = light;
  14. this.light.updateMatrixWorld();
  15. this.matrix = light.matrixWorld;
  16. this.matrixAutoUpdate = false;
  17. this.overrideColor = overrideColor;
  18. var material = new LineBasicMaterial( { fog: false, color: this.overrideColor } );
  19. var geometry = new BufferGeometry();
  20. geometry.addAttribute( 'position', new BufferAttribute( new Float32Array( 5 * 3 ), 3 ) );
  21. this.line = new Line( geometry, material );
  22. this.add( this.line );
  23. this.update();
  24. }
  25. RectAreaLightHelper.prototype = Object.create( Object3D.prototype );
  26. RectAreaLightHelper.prototype.constructor = RectAreaLightHelper;
  27. RectAreaLightHelper.prototype.dispose = function () {
  28. this.children[ 0 ].geometry.dispose();
  29. this.children[ 0 ].material.dispose();
  30. };
  31. RectAreaLightHelper.prototype.update = function () {
  32. // calculate new dimensions of the helper
  33. var hx = this.light.width * 0.5;
  34. var hy = this.light.height * 0.5;
  35. var position = this.line.geometry.attributes.position;
  36. var array = position.array;
  37. // update vertices
  38. array[ 0 ] = hx; array[ 1 ] = - hy; array[ 2 ] = 0;
  39. array[ 3 ] = hx; array[ 4 ] = hy; array[ 5 ] = 0;
  40. array[ 6 ] = - hx; array[ 7 ] = hy; array[ 8 ] = 0;
  41. array[ 9 ] = - hx; array[ 10 ] = - hy; array[ 11 ] = 0;
  42. array[ 12 ] = hx; array[ 13 ] = - hy; array[ 14 ] = 0;
  43. position.needsUpdate = true;
  44. if ( ! this.overrideColor ) this.line.material.color.copy( this.light.color );
  45. else this.line.material.color.set( this.overrideColor );
  46. };
  47. export { RectAreaLightHelper };