UNPKG

1.84 kBJavaScriptView Raw
1import { Line } from '../objects/Line.js';
2import { Mesh } from '../objects/Mesh.js';
3import { LineBasicMaterial } from '../materials/LineBasicMaterial.js';
4import { MeshBasicMaterial } from '../materials/MeshBasicMaterial.js';
5import { Float32BufferAttribute } from '../core/BufferAttribute.js';
6import { BufferGeometry } from '../core/BufferGeometry.js';
7import { FrontSide, BackSide } from '../constants.js';
8
9class PlaneHelper extends Line {
10
11 constructor( plane, size = 1, hex = 0xffff00 ) {
12
13 const color = hex;
14
15 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 ];
16
17 const geometry = new BufferGeometry();
18 geometry.setAttribute( 'position', new Float32BufferAttribute( positions, 3 ) );
19 geometry.computeBoundingSphere();
20
21 super( geometry, new LineBasicMaterial( { color: color, toneMapped: false } ) );
22
23 this.type = 'PlaneHelper';
24
25 this.plane = plane;
26
27 this.size = size;
28
29 const positions2 = [ 1, 1, 1, - 1, 1, 1, - 1, - 1, 1, 1, 1, 1, - 1, - 1, 1, 1, - 1, 1 ];
30
31 const geometry2 = new BufferGeometry();
32 geometry2.setAttribute( 'position', new Float32BufferAttribute( positions2, 3 ) );
33 geometry2.computeBoundingSphere();
34
35 this.add( new Mesh( geometry2, new MeshBasicMaterial( { color: color, opacity: 0.2, transparent: true, depthWrite: false, toneMapped: false } ) ) );
36
37 }
38
39 updateMatrixWorld( force ) {
40
41 let scale = - this.plane.constant;
42
43 if ( Math.abs( scale ) < 1e-8 ) scale = 1e-8; // sign does not matter
44
45 this.scale.set( 0.5 * this.size, 0.5 * this.size, scale );
46
47 this.children[ 0 ].material.side = ( scale < 0 ) ? BackSide : FrontSide; // renderer flips side when determinant < 0; flipping not wanted here
48
49 this.lookAt( this.plane.normal );
50
51 super.updateMatrixWorld( force );
52
53 }
54
55}
56
57export { PlaneHelper };