-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
/
XRPlanes.js
100 lines (61 loc) · 1.89 KB
/
XRPlanes.js
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import {
BoxGeometry,
Matrix4,
Mesh,
MeshBasicMaterial,
Object3D
} from 'three';
class XRPlanes extends Object3D {
constructor( renderer ) {
super();
const matrix = new Matrix4();
const currentPlanes = new Map();
const xr = renderer.xr;
xr.addEventListener( 'planesdetected', event => {
const frame = event.data;
const planes = frame.detectedPlanes;
const referenceSpace = xr.getReferenceSpace();
let planeschanged = false;
for ( const [ plane, mesh ] of currentPlanes ) {
if ( planes.has( plane ) === false ) {
mesh.geometry.dispose();
mesh.material.dispose();
this.remove( mesh );
currentPlanes.delete( plane );
planeschanged = true;
}
}
for ( const plane of planes ) {
if ( currentPlanes.has( plane ) === false ) {
const pose = frame.getPose( plane.planeSpace, referenceSpace );
matrix.fromArray( pose.transform.matrix );
const polygon = plane.polygon;
let minX = Number.MAX_SAFE_INTEGER;
let maxX = Number.MIN_SAFE_INTEGER;
let minZ = Number.MAX_SAFE_INTEGER;
let maxZ = Number.MIN_SAFE_INTEGER;
for ( const point of polygon ) {
minX = Math.min( minX, point.x );
maxX = Math.max( maxX, point.x );
minZ = Math.min( minZ, point.z );
maxZ = Math.max( maxZ, point.z );
}
const width = maxX - minX;
const height = maxZ - minZ;
const geometry = new BoxGeometry( width, 0.01, height );
const material = new MeshBasicMaterial( { color: 0xffffff * Math.random() } );
const mesh = new Mesh( geometry, material );
mesh.position.setFromMatrixPosition( matrix );
mesh.quaternion.setFromRotationMatrix( matrix );
this.add( mesh );
currentPlanes.set( plane, mesh );
planeschanged = true;
}
}
if ( planeschanged ) {
this.dispatchEvent( { type: 'planeschanged' } );
}
} );
}
}
export { XRPlanes };