DirectionalLightHelper.js 2.2 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. function DirectionalLightHelper( light, size, color ) {
  11. Object3D.call( this );
  12. this.light = light;
  13. this.light.updateMatrixWorld();
  14. this.matrix = light.matrixWorld;
  15. this.matrixAutoUpdate = false;
  16. this.color = color;
  17. if ( size === undefined ) size = 1;
  18. let geometry = new BufferGeometry();
  19. geometry.setAttribute( 'position', new Float32BufferAttribute( [
  20. - size, size, 0,
  21. size, size, 0,
  22. size, - size, 0,
  23. - size, - size, 0,
  24. - size, size, 0
  25. ], 3 ) );
  26. const material = new LineBasicMaterial( { fog: false, toneMapped: false } );
  27. this.lightPlane = new Line( geometry, material );
  28. this.add( this.lightPlane );
  29. geometry = new BufferGeometry();
  30. geometry.setAttribute( 'position', new Float32BufferAttribute( [ 0, 0, 0, 0, 0, 1 ], 3 ) );
  31. this.targetLine = new Line( geometry, material );
  32. this.add( this.targetLine );
  33. this.update();
  34. }
  35. DirectionalLightHelper.prototype = Object.create( Object3D.prototype );
  36. DirectionalLightHelper.prototype.constructor = DirectionalLightHelper;
  37. DirectionalLightHelper.prototype.dispose = function () {
  38. this.lightPlane.geometry.dispose();
  39. this.lightPlane.material.dispose();
  40. this.targetLine.geometry.dispose();
  41. this.targetLine.material.dispose();
  42. };
  43. DirectionalLightHelper.prototype.update = function () {
  44. _v1.setFromMatrixPosition( this.light.matrixWorld );
  45. _v2.setFromMatrixPosition( this.light.target.matrixWorld );
  46. _v3.subVectors( _v2, _v1 );
  47. this.lightPlane.lookAt( _v2 );
  48. if ( this.color !== undefined ) {
  49. this.lightPlane.material.color.set( this.color );
  50. this.targetLine.material.color.set( this.color );
  51. } else {
  52. this.lightPlane.material.color.copy( this.light.color );
  53. this.targetLine.material.color.copy( this.light.color );
  54. }
  55. this.targetLine.lookAt( _v2 );
  56. this.targetLine.scale.z = _v3.length();
  57. };
  58. export { DirectionalLightHelper };