File size: 1,775 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
import { Line } from '../objects/Line.js';
import { Mesh } from '../objects/Mesh.js';
import { LineBasicMaterial } from '../materials/LineBasicMaterial.js';
import { MeshBasicMaterial } from '../materials/MeshBasicMaterial.js';
import { Float32BufferAttribute } from '../core/BufferAttribute.js';
import { BufferGeometry } from '../core/BufferGeometry.js';
import { FrontSide, BackSide } from '../constants.js';

class PlaneHelper extends Line {
	constructor(plane, size = 1, hex = 0xffff00) {
		const color = hex;

		const positions = [1, -1, 1, -1, 1, 1, -1, -1, 1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0];

		const geometry = new BufferGeometry();
		geometry.setAttribute('position', new Float32BufferAttribute(positions, 3));
		geometry.computeBoundingSphere();

		super(geometry, new LineBasicMaterial({ color: color, toneMapped: false }));

		this.type = 'PlaneHelper';

		this.plane = plane;

		this.size = size;

		const positions2 = [1, 1, 1, -1, 1, 1, -1, -1, 1, 1, 1, 1, -1, -1, 1, 1, -1, 1];

		const geometry2 = new BufferGeometry();
		geometry2.setAttribute('position', new Float32BufferAttribute(positions2, 3));
		geometry2.computeBoundingSphere();

		this.add(new Mesh(geometry2, new MeshBasicMaterial({ color: color, opacity: 0.2, transparent: true, depthWrite: false, toneMapped: false })));
	}

	updateMatrixWorld(force) {
		let scale = -this.plane.constant;

		if (Math.abs(scale) < 1e-8) scale = 1e-8; // sign does not matter

		this.scale.set(0.5 * this.size, 0.5 * this.size, scale);

		this.children[0].material.side = scale < 0 ? BackSide : FrontSide; // renderer flips side when determinant < 0; flipping not wanted here

		this.lookAt(this.plane.normal);

		super.updateMatrixWorld(force);
	}
}

export { PlaneHelper };