2
0

RectAreaLightHelper.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. }
  30. RectAreaLightHelper.prototype = Object.create( Line.prototype );
  31. RectAreaLightHelper.prototype.constructor = RectAreaLightHelper;
  32. RectAreaLightHelper.prototype.updateMatrixWorld = function () {
  33. this.scale.set( 0.5 * this.light.width, 0.5 * this.light.height, 1 );
  34. if ( this.color !== undefined ) {
  35. this.material.color.set( this.color );
  36. this.children[ 0 ].material.color.set( this.color );
  37. } else {
  38. this.material.color.copy( this.light.color ).multiplyScalar( this.light.intensity );
  39. // prevent hue shift
  40. var c = this.material.color;
  41. var max = Math.max( c.r, c.g, c.b );
  42. if ( max > 1 ) c.multiplyScalar( 1 / max );
  43. this.children[ 0 ].material.color.copy( this.material.color );
  44. }
  45. this.matrixWorld.copy( this.light.matrixWorld ).scale( this.scale );
  46. this.children[ 0 ].matrixWorld.copy( this.matrixWorld );
  47. };
  48. RectAreaLightHelper.prototype.dispose = function () {
  49. this.geometry.dispose();
  50. this.material.dispose();
  51. this.children[ 0 ].geometry.dispose();
  52. this.children[ 0 ].material.dispose();
  53. };
  54. export { RectAreaLightHelper };