DirectionalLightHelper.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 = /*@__PURE__*/ new Vector3();
  8. const _v2 = /*@__PURE__*/ new Vector3();
  9. const _v3 = /*@__PURE__*/ new Vector3();
  10. class DirectionalLightHelper extends Object3D {
  11. constructor( light, size, color ) {
  12. super();
  13. this.light = light;
  14. this.matrix = light.matrixWorld;
  15. this.matrixAutoUpdate = false;
  16. this.color = color;
  17. this.type = 'DirectionalLightHelper';
  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. this.light.updateWorldMatrix( true, false );
  44. this.light.target.updateWorldMatrix( true, false );
  45. _v1.setFromMatrixPosition( this.light.matrixWorld );
  46. _v2.setFromMatrixPosition( this.light.target.matrixWorld );
  47. _v3.subVectors( _v2, _v1 );
  48. this.lightPlane.lookAt( _v2 );
  49. if ( this.color !== undefined ) {
  50. this.lightPlane.material.color.set( this.color );
  51. this.targetLine.material.color.set( this.color );
  52. } else {
  53. this.lightPlane.material.color.copy( this.light.color );
  54. this.targetLine.material.color.copy( this.light.color );
  55. }
  56. this.targetLine.lookAt( _v2 );
  57. this.targetLine.scale.z = _v3.length();
  58. }
  59. }
  60. export { DirectionalLightHelper };
粤ICP备19079148号