RectAreaLightHelper.js 2.4 KB

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