SpotLightHelper.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { Vector3 } from '../math/Vector3.js';
  2. import { Object3D } from '../core/Object3D.js';
  3. import { LineSegments } from '../objects/LineSegments.js';
  4. import { LineBasicMaterial } from '../materials/LineBasicMaterial.js';
  5. import { Float32BufferAttribute } from '../core/BufferAttribute.js';
  6. import { BufferGeometry } from '../core/BufferGeometry.js';
  7. const _vector = /*@__PURE__*/ new Vector3();
  8. class SpotLightHelper extends Object3D {
  9. constructor( light, color ) {
  10. super();
  11. this.light = light;
  12. this.matrix = light.matrixWorld;
  13. this.matrixAutoUpdate = false;
  14. this.color = color;
  15. this.type = 'SpotLightHelper';
  16. const geometry = new BufferGeometry();
  17. const positions = [
  18. 0, 0, 0, 0, 0, 1,
  19. 0, 0, 0, 1, 0, 1,
  20. 0, 0, 0, - 1, 0, 1,
  21. 0, 0, 0, 0, 1, 1,
  22. 0, 0, 0, 0, - 1, 1
  23. ];
  24. for ( let i = 0, j = 1, l = 32; i < l; i ++, j ++ ) {
  25. const p1 = ( i / l ) * Math.PI * 2;
  26. const p2 = ( j / l ) * Math.PI * 2;
  27. positions.push(
  28. Math.cos( p1 ), Math.sin( p1 ), 1,
  29. Math.cos( p2 ), Math.sin( p2 ), 1
  30. );
  31. }
  32. geometry.setAttribute( 'position', new Float32BufferAttribute( positions, 3 ) );
  33. const material = new LineBasicMaterial( { fog: false, toneMapped: false } );
  34. this.cone = new LineSegments( geometry, material );
  35. this.add( this.cone );
  36. this.update();
  37. }
  38. dispose() {
  39. this.cone.geometry.dispose();
  40. this.cone.material.dispose();
  41. }
  42. update() {
  43. this.light.updateWorldMatrix( true, false );
  44. this.light.target.updateWorldMatrix( true, false );
  45. const coneLength = this.light.distance ? this.light.distance : 1000;
  46. const coneWidth = coneLength * Math.tan( this.light.angle );
  47. this.cone.scale.set( coneWidth, coneWidth, coneLength );
  48. _vector.setFromMatrixPosition( this.light.target.matrixWorld );
  49. this.cone.lookAt( _vector );
  50. if ( this.color !== undefined ) {
  51. this.cone.material.color.set( this.color );
  52. } else {
  53. this.cone.material.color.copy( this.light.color );
  54. }
  55. }
  56. }
  57. export { SpotLightHelper };