1 | import { Vector3 } from '../math/Vector3.js';
|
2 | import { Color } from '../math/Color.js';
|
3 | import { Object3D } from '../core/Object3D.js';
|
4 | import { Mesh } from '../objects/Mesh.js';
|
5 | import { MeshBasicMaterial } from '../materials/MeshBasicMaterial.js';
|
6 | import { OctahedronGeometry } from '../geometries/OctahedronGeometry.js';
|
7 | import { BufferAttribute } from '../core/BufferAttribute.js';
|
8 |
|
9 | const _vector = new Vector3();
|
10 | const _color1 = new Color();
|
11 | const _color2 = new Color();
|
12 |
|
13 | class HemisphereLightHelper extends Object3D {
|
14 |
|
15 | constructor( light, size, color ) {
|
16 |
|
17 | super();
|
18 | this.light = light;
|
19 | this.light.updateMatrixWorld();
|
20 |
|
21 | this.matrix = light.matrixWorld;
|
22 | this.matrixAutoUpdate = false;
|
23 |
|
24 | this.color = color;
|
25 |
|
26 | const geometry = new OctahedronGeometry( size );
|
27 | geometry.rotateY( Math.PI * 0.5 );
|
28 |
|
29 | this.material = new MeshBasicMaterial( { wireframe: true, fog: false, toneMapped: false } );
|
30 | if ( this.color === undefined ) this.material.vertexColors = true;
|
31 |
|
32 | const position = geometry.getAttribute( 'position' );
|
33 | const colors = new Float32Array( position.count * 3 );
|
34 |
|
35 | geometry.setAttribute( 'color', new BufferAttribute( colors, 3 ) );
|
36 |
|
37 | this.add( new Mesh( geometry, this.material ) );
|
38 |
|
39 | this.update();
|
40 |
|
41 | }
|
42 |
|
43 | dispose() {
|
44 |
|
45 | this.children[ 0 ].geometry.dispose();
|
46 | this.children[ 0 ].material.dispose();
|
47 |
|
48 | }
|
49 |
|
50 | update() {
|
51 |
|
52 | const mesh = this.children[ 0 ];
|
53 |
|
54 | if ( this.color !== undefined ) {
|
55 |
|
56 | this.material.color.set( this.color );
|
57 |
|
58 | } else {
|
59 |
|
60 | const colors = mesh.geometry.getAttribute( 'color' );
|
61 |
|
62 | _color1.copy( this.light.color );
|
63 | _color2.copy( this.light.groundColor );
|
64 |
|
65 | for ( let i = 0, l = colors.count; i < l; i ++ ) {
|
66 |
|
67 | const color = ( i < ( l / 2 ) ) ? _color1 : _color2;
|
68 |
|
69 | colors.setXYZ( i, color.r, color.g, color.b );
|
70 |
|
71 | }
|
72 |
|
73 | colors.needsUpdate = true;
|
74 |
|
75 | }
|
76 |
|
77 | mesh.lookAt( _vector.setFromMatrixPosition( this.light.matrixWorld ).negate() );
|
78 |
|
79 | }
|
80 |
|
81 | }
|
82 |
|
83 |
|
84 | export { HemisphereLightHelper };
|