RectAreaLightHelper.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. function RectAreaLightHelper( light, color ) {
  14. this.light = light;
  15. this.color = color; // optional hardwired color for the helper
  16. var positions = [ 1, 1, 0, - 1, 1, 0, - 1, - 1, 0, 1, - 1, 0, 1, 1, 0 ];
  17. var geometry = new BufferGeometry();
  18. geometry.setAttribute( 'position', new Float32BufferAttribute( positions, 3 ) );
  19. geometry.computeBoundingSphere();
  20. var material = new LineBasicMaterial( { fog: false } );
  21. Line.call( this, geometry, material );
  22. this.type = 'RectAreaLightHelper';
  23. //
  24. var positions2 = [ 1, 1, 0, - 1, 1, 0, - 1, - 1, 0, 1, 1, 0, - 1, - 1, 0, 1, - 1, 0 ];
  25. var geometry2 = new BufferGeometry();
  26. geometry2.setAttribute( 'position', new Float32BufferAttribute( positions2, 3 ) );
  27. geometry2.computeBoundingSphere();
  28. this.add( new Mesh( geometry2, new MeshBasicMaterial( { side: BackSide, fog: false } ) ) );
  29. this.update();
  30. }
  31. RectAreaLightHelper.prototype = Object.create( Line.prototype );
  32. RectAreaLightHelper.prototype.constructor = RectAreaLightHelper;
  33. RectAreaLightHelper.prototype.update = function () {
  34. this.scale.set( 0.5 * this.light.width, 0.5 * this.light.height, 1 );
  35. if ( this.color !== undefined ) {
  36. this.material.color.set( this.color );
  37. this.children[ 0 ].material.color.set( this.color );
  38. } else {
  39. this.material.color.copy( this.light.color ).multiplyScalar( this.light.intensity );
  40. // prevent hue shift
  41. var c = this.material.color;
  42. var max = Math.max( c.r, c.g, c.b );
  43. if ( max > 1 ) c.multiplyScalar( 1 / max );
  44. this.children[ 0 ].material.color.copy( this.material.color );
  45. }
  46. };
  47. RectAreaLightHelper.prototype.dispose = function () {
  48. this.geometry.dispose();
  49. this.material.dispose();
  50. this.children[ 0 ].geometry.dispose();
  51. this.children[ 0 ].material.dispose();
  52. };
  53. export { RectAreaLightHelper };