@@ -4,60 +4,83 @@ const geometry = /* @__PURE__ */ new BoxGeometry()
4
4
const matrix = /* @__PURE__ */ new Matrix4 ( )
5
5
6
6
class XRPlanes extends Object3D {
7
+ public renderer : WebGLRenderer
8
+ readonly currentPlanes = new Map < XRPlane , Mesh < BoxGeometry , MeshBasicMaterial > > ( )
9
+
7
10
constructor ( renderer : WebGLRenderer ) {
8
11
super ( )
9
12
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
+ }
11
17
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
15
21
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
+ }
17
27
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
22
30
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 )
25
36
}
37
+ }
26
38
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 ! ) !
31
43
32
- const polygon = plane . polygon
44
+ matrix . fromArray ( pose . transform . matrix )
33
45
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
38
50
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
+ }
45
57
46
- const width = maxX - minX
47
- const height = maxZ - minZ
58
+ const width = maxX - minX
59
+ const height = maxZ - minZ
48
60
49
- const material = new MeshBasicMaterial ( { color : 0xffffff * Math . random ( ) } )
61
+ const material = new MeshBasicMaterial ( { color : 0xffffff * Math . random ( ) } )
50
62
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 )
56
67
57
- currentPlanes . set ( plane , mesh )
58
- }
68
+ this . add ( mesh )
69
+ this . currentPlanes . set ( plane , mesh )
59
70
}
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 )
61
84
}
62
85
}
63
86
0 commit comments