Skip to content

Commit 8388ce7

Browse files
fix: get planes working again
1 parent d051272 commit 8388ce7

File tree

2 files changed

+61
-37
lines changed

2 files changed

+61
-37
lines changed

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ export * from './webxr/XREstimatedLight'
112112
export * from './webxr/XRHandMeshModel'
113113
export * from './webxr/XRHandModelFactory'
114114
export * from './webxr/XRHandPrimitiveModel'
115+
export * from './webxr/XRPlanes'
115116
export * from './geometries/ParametricGeometries'
116117
export * from './geometries/ParametricGeometry'
117118
export * from './geometries/ConvexGeometry'

src/webxr/XRPlanes.ts

Lines changed: 60 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,60 +4,83 @@ const geometry = /* @__PURE__ */ new BoxGeometry()
44
const matrix = /* @__PURE__ */ new Matrix4()
55

66
class XRPlanes extends Object3D {
7+
public renderer: WebGLRenderer
8+
readonly currentPlanes = new Map<XRPlane, Mesh<BoxGeometry, MeshBasicMaterial>>()
9+
710
constructor(renderer: WebGLRenderer) {
811
super()
912

10-
const currentPlanes = new Map()
13+
this.renderer = renderer
14+
this._onPlanesUpdate = this._onPlanesUpdate.bind(this)
15+
this.renderer.xr.addEventListener('planesdetected', this._onPlanesUpdate)
16+
}
1117

12-
renderer.xr.addEventListener('planesdetected', (event) => {
13-
const frame = event.data
14-
const planes = frame.detectedPlanes
18+
private _onPlanesUpdate() {
19+
// @ts-ignore https://github.com/mrdoob/three.js/pull/22260
20+
const frame = this.renderer.xr.getFrame() as XRFrame
1521

16-
const referenceSpace = renderer.xr.getReferenceSpace()
22+
// Event signature changed from XRPlaneSet => XRFRame
23+
// https://github.com/mrdoob/three.js/pull/24855
24+
// https://github.com/mrdoob/three.js/pull/26098
25+
this.update(frame)
26+
}
1727

18-
for (const [plane, mesh] of currentPlanes) {
19-
if (planes.has(plane) === false) {
20-
mesh.material.dispose()
21-
this.remove(mesh)
28+
update(frame: XRFrame): void {
29+
const planes = (frame as any).detectedPlanes as XRPlaneSet
2230

23-
currentPlanes.delete(plane)
24-
}
31+
for (const [plane, mesh] of this.currentPlanes) {
32+
if (!planes.has(plane)) {
33+
mesh.material.dispose()
34+
this.remove(mesh)
35+
this.currentPlanes.delete(plane)
2536
}
37+
}
2638

27-
for (const plane of planes) {
28-
if (currentPlanes.has(plane) === false) {
29-
const pose = frame.getPose(plane.planeSpace, referenceSpace)
30-
matrix.fromArray(pose.transform.matrix)
39+
const referenceSpace = this.renderer.xr.getReferenceSpace()
40+
for (const plane of planes) {
41+
if (!this.currentPlanes.has(plane)) {
42+
const pose = frame.getPose(plane.planeSpace, referenceSpace!)!
3143

32-
const polygon = plane.polygon
44+
matrix.fromArray(pose.transform.matrix)
3345

34-
let minX = Number.MAX_SAFE_INTEGER
35-
let maxX = Number.MIN_SAFE_INTEGER
36-
let minZ = Number.MAX_SAFE_INTEGER
37-
let maxZ = Number.MIN_SAFE_INTEGER
46+
let minX = Number.MAX_SAFE_INTEGER
47+
let maxX = Number.MIN_SAFE_INTEGER
48+
let minZ = Number.MAX_SAFE_INTEGER
49+
let maxZ = Number.MIN_SAFE_INTEGER
3850

39-
for (const point of polygon) {
40-
minX = Math.min(minX, point.x)
41-
maxX = Math.max(maxX, point.x)
42-
minZ = Math.min(minZ, point.z)
43-
maxZ = Math.max(maxZ, point.z)
44-
}
51+
for (const point of plane.polygon) {
52+
minX = Math.min(minX, point.x)
53+
maxX = Math.max(maxX, point.x)
54+
minZ = Math.min(minZ, point.z)
55+
maxZ = Math.max(maxZ, point.z)
56+
}
4557

46-
const width = maxX - minX
47-
const height = maxZ - minZ
58+
const width = maxX - minX
59+
const height = maxZ - minZ
4860

49-
const material = new MeshBasicMaterial({ color: 0xffffff * Math.random() })
61+
const material = new MeshBasicMaterial({ color: 0xffffff * Math.random() })
5062

51-
const mesh = new Mesh(geometry, material)
52-
mesh.position.setFromMatrixPosition(matrix)
53-
mesh.quaternion.setFromRotationMatrix(matrix)
54-
mesh.scale.set(width, 0.01, height)
55-
this.add(mesh)
63+
const mesh = new Mesh(geometry, material)
64+
mesh.position.setFromMatrixPosition(matrix)
65+
mesh.quaternion.setFromRotationMatrix(matrix)
66+
mesh.scale.set(width, 0.01, height)
5667

57-
currentPlanes.set(plane, mesh)
58-
}
68+
this.add(mesh)
69+
this.currentPlanes.set(plane, mesh)
5970
}
60-
})
71+
}
72+
}
73+
74+
dispose(): void {
75+
geometry.dispose()
76+
77+
for (const [plane, mesh] of this.currentPlanes) {
78+
mesh.material.dispose()
79+
this.remove(mesh)
80+
this.currentPlanes.delete(plane)
81+
}
82+
83+
this.renderer.xr.removeEventListener('planesdetected', this._onPlanesUpdate)
6184
}
6285
}
6386

0 commit comments

Comments
 (0)