UNPKG

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