DirectionalLightHelper.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { Vector3 } from '../math/Vector3.js';
  2. import { Object3D } from '../core/Object3D.js';
  3. import { Line } from '../objects/Line.js';
  4. import { Float32BufferAttribute } from '../core/BufferAttribute.js';
  5. import { BufferGeometry } from '../core/BufferGeometry.js';
  6. import { LineBasicMaterial } from '../materials/LineBasicMaterial.js';
  7. const _v1 = new Vector3();
  8. const _v2 = new Vector3();
  9. const _v3 = new Vector3();
  10. class DirectionalLightHelper extends Object3D {
  11. constructor( light, size, color ) {
  12. super();
  13. this.light = light;
  14. this.light.updateMatrixWorld();
  15. this.matrix = light.matrixWorld;
  16. this.matrixAutoUpdate = false;
  17. this.color = color;
  18. if ( size === undefined ) size = 1;
  19. let geometry = new BufferGeometry();
  20. geometry.setAttribute( 'position', new Float32BufferAttribute( [
  21. - size, size, 0,
  22. size, size, 0,
  23. size, - size, 0,
  24. - size, - size, 0,
  25. - size, size, 0
  26. ], 3 ) );
  27. const material = new LineBasicMaterial( { fog: false, toneMapped: false } );
  28. this.lightPlane = new Line( geometry, material );
  29. this.add( this.lightPlane );
  30. geometry = new BufferGeometry();
  31. geometry.setAttribute( 'position', new Float32BufferAttribute( [ 0, 0, 0, 0, 0, 1 ], 3 ) );
  32. this.targetLine = new Line( geometry, material );
  33. this.add( this.targetLine );
  34. this.update();
  35. }
  36. dispose() {
  37. this.lightPlane.geometry.dispose();
  38. this.lightPlane.material.dispose();
  39. this.targetLine.geometry.dispose();
  40. this.targetLine.material.dispose();
  41. }
  42. update() {
  43. _v1.setFromMatrixPosition( this.light.matrixWorld );
  44. _v2.setFromMatrixPosition( this.light.target.matrixWorld );
  45. _v3.subVectors( _v2, _v1 );
  46. this.lightPlane.lookAt( _v2 );
  47. if ( this.color !== undefined ) {
  48. this.lightPlane.material.color.set( this.color );
  49. this.targetLine.material.color.set( this.color );
  50. } else {
  51. this.lightPlane.material.color.copy( this.light.color );
  52. this.targetLine.material.color.copy( this.light.color );
  53. }
  54. this.targetLine.lookAt( _v2 );
  55. this.targetLine.scale.z = _v3.length();
  56. }
  57. }
  58. export { DirectionalLightHelper };