File size: 1,922 Bytes
2b7aae2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { Vector3 } from '../math/Vector3.js';
import { Color } from '../math/Color.js';
import { Object3D } from '../core/Object3D.js';
import { Mesh } from '../objects/Mesh.js';
import { MeshBasicMaterial } from '../materials/MeshBasicMaterial.js';
import { OctahedronGeometry } from '../geometries/OctahedronGeometry.js';
import { BufferAttribute } from '../core/BufferAttribute.js';

const _vector = /*@__PURE__*/ new Vector3();
const _color1 = /*@__PURE__*/ new Color();
const _color2 = /*@__PURE__*/ new Color();

class HemisphereLightHelper extends Object3D {
	constructor(light, size, color) {
		super();
		this.light = light;
		this.light.updateMatrixWorld();

		this.matrix = light.matrixWorld;
		this.matrixAutoUpdate = false;

		this.color = color;

		const geometry = new OctahedronGeometry(size);
		geometry.rotateY(Math.PI * 0.5);

		this.material = new MeshBasicMaterial({ wireframe: true, fog: false, toneMapped: false });
		if (this.color === undefined) this.material.vertexColors = true;

		const position = geometry.getAttribute('position');
		const colors = new Float32Array(position.count * 3);

		geometry.setAttribute('color', new BufferAttribute(colors, 3));

		this.add(new Mesh(geometry, this.material));

		this.update();
	}

	dispose() {
		this.children[0].geometry.dispose();
		this.children[0].material.dispose();
	}

	update() {
		const mesh = this.children[0];

		if (this.color !== undefined) {
			this.material.color.set(this.color);
		} else {
			const colors = mesh.geometry.getAttribute('color');

			_color1.copy(this.light.color);
			_color2.copy(this.light.groundColor);

			for (let i = 0, l = colors.count; i < l; i++) {
				const color = i < l / 2 ? _color1 : _color2;

				colors.setXYZ(i, color.r, color.g, color.b);
			}

			colors.needsUpdate = true;
		}

		mesh.lookAt(_vector.setFromMatrixPosition(this.light.matrixWorld).negate());
	}
}

export { HemisphereLightHelper };