UNPKG

1.94 kBJavaScriptView Raw
1import { Vector3 } from '../math/Vector3.js';
2import { Object3D } from '../core/Object3D.js';
3import { LineSegments } from '../objects/LineSegments.js';
4import { LineBasicMaterial } from '../materials/LineBasicMaterial.js';
5import { Float32BufferAttribute } from '../core/BufferAttribute.js';
6import { BufferGeometry } from '../core/BufferGeometry.js';
7
8const _vector = /*@__PURE__*/ new Vector3();
9
10class SpotLightHelper extends Object3D {
11
12 constructor( light, color ) {
13
14 super();
15 this.light = light;
16 this.light.updateMatrixWorld();
17
18 this.matrix = light.matrixWorld;
19 this.matrixAutoUpdate = false;
20
21 this.color = color;
22
23 const geometry = new BufferGeometry();
24
25 const positions = [
26 0, 0, 0, 0, 0, 1,
27 0, 0, 0, 1, 0, 1,
28 0, 0, 0, - 1, 0, 1,
29 0, 0, 0, 0, 1, 1,
30 0, 0, 0, 0, - 1, 1
31 ];
32
33 for ( let i = 0, j = 1, l = 32; i < l; i ++, j ++ ) {
34
35 const p1 = ( i / l ) * Math.PI * 2;
36 const p2 = ( j / l ) * Math.PI * 2;
37
38 positions.push(
39 Math.cos( p1 ), Math.sin( p1 ), 1,
40 Math.cos( p2 ), Math.sin( p2 ), 1
41 );
42
43 }
44
45 geometry.setAttribute( 'position', new Float32BufferAttribute( positions, 3 ) );
46
47 const material = new LineBasicMaterial( { fog: false, toneMapped: false } );
48
49 this.cone = new LineSegments( geometry, material );
50 this.add( this.cone );
51
52 this.update();
53
54 }
55
56 dispose() {
57
58 this.cone.geometry.dispose();
59 this.cone.material.dispose();
60
61 }
62
63 update() {
64
65 this.light.updateMatrixWorld();
66
67 const coneLength = this.light.distance ? this.light.distance : 1000;
68 const coneWidth = coneLength * Math.tan( this.light.angle );
69
70 this.cone.scale.set( coneWidth, coneWidth, coneLength );
71
72 _vector.setFromMatrixPosition( this.light.target.matrixWorld );
73
74 this.cone.lookAt( _vector );
75
76 if ( this.color !== undefined ) {
77
78 this.cone.material.color.set( this.color );
79
80 } else {
81
82 this.cone.material.color.copy( this.light.color );
83
84 }
85
86 }
87
88}
89
90
91export { SpotLightHelper };