RectAreaLightHelper.js 2.1 KB

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