DirectionalLightHelper.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. * @author mrdoob / http://mrdoob.com/
  4. * @author WestLangley / http://github.com/WestLangley
  5. */
  6. import { Vector3 } from '../math/Vector3.js';
  7. import { Object3D } from '../core/Object3D.js';
  8. import { Line } from '../objects/Line.js';
  9. import { Float32BufferAttribute } from '../core/BufferAttribute.js';
  10. import { BufferGeometry } from '../core/BufferGeometry.js';
  11. import { LineBasicMaterial } from '../materials/LineBasicMaterial.js';
  12. function DirectionalLightHelper( light, size, color ) {
  13. Object3D.call( this );
  14. this.light = light;
  15. this.light.updateMatrixWorld();
  16. this.matrix = light.matrixWorld;
  17. this.matrixAutoUpdate = false;
  18. this.color = color;
  19. if ( size === undefined ) size = 1;
  20. var geometry = new BufferGeometry();
  21. geometry.addAttribute( 'position', new Float32BufferAttribute( [
  22. - size, size, 0,
  23. size, size, 0,
  24. size, - size, 0,
  25. - size, - size, 0,
  26. - size, size, 0
  27. ], 3 ) );
  28. var material = new LineBasicMaterial( { fog: false } );
  29. this.lightPlane = new Line( geometry, material );
  30. this.add( this.lightPlane );
  31. geometry = new BufferGeometry();
  32. geometry.addAttribute( 'position', new Float32BufferAttribute( [ 0, 0, 0, 0, 0, 1 ], 3 ) );
  33. this.targetLine = new Line( geometry, material );
  34. this.add( this.targetLine );
  35. this.update();
  36. }
  37. DirectionalLightHelper.prototype = Object.create( Object3D.prototype );
  38. DirectionalLightHelper.prototype.constructor = DirectionalLightHelper;
  39. DirectionalLightHelper.prototype.dispose = function () {
  40. this.lightPlane.geometry.dispose();
  41. this.lightPlane.material.dispose();
  42. this.targetLine.geometry.dispose();
  43. this.targetLine.material.dispose();
  44. };
  45. DirectionalLightHelper.prototype.update = function () {
  46. var v1 = new Vector3();
  47. var v2 = new Vector3();
  48. var v3 = new Vector3();
  49. return function update() {
  50. v1.setFromMatrixPosition( this.light.matrixWorld );
  51. v2.setFromMatrixPosition( this.light.target.matrixWorld );
  52. v3.subVectors( v2, v1 );
  53. this.lightPlane.lookAt( v2 );
  54. if ( this.color !== undefined ) {
  55. this.lightPlane.material.color.set( this.color );
  56. this.targetLine.material.color.set( this.color );
  57. } else {
  58. this.lightPlane.material.color.copy( this.light.color );
  59. this.targetLine.material.color.copy( this.light.color );
  60. }
  61. this.targetLine.lookAt( v2 );
  62. this.targetLine.scale.z = v3.length();
  63. };
  64. }();
  65. export { DirectionalLightHelper };