@@ -21,8 +21,6 @@ export class Depth {
2121 // The main camera.
2222 private camera ! : THREE . Camera ;
2323 private renderer ! : THREE . WebGLRenderer ;
24- private scene ! : THREE . Scene ;
25- private projectionMatrixInverse = new THREE . Matrix4 ( ) ;
2624
2725 enabled = false ;
2826 view : XRView [ ] = [ ] ;
@@ -50,8 +48,11 @@ export class Depth {
5048 private depthClients = new Set < object > ( ) ;
5149
5250 depthProjectionMatrices : THREE . Matrix4 [ ] = [ ] ;
51+ depthProjectionInverseMatrices : THREE . Matrix4 [ ] = [ ] ;
5352 depthViewMatrices : THREE . Matrix4 [ ] = [ ] ;
5453 depthViewProjectionMatrices : THREE . Matrix4 [ ] = [ ] ;
54+ depthCameraPositions : THREE . Vector3 [ ] = [ ] ;
55+ depthCameraRotations : THREE . Quaternion [ ] = [ ] ;
5556
5657 /**
5758 * Depth is a lightweight manager based on three.js to simply prototyping
@@ -77,7 +78,6 @@ export class Depth {
7778 this . camera = camera ;
7879 this . options = options ;
7980 this . renderer = renderer ;
80- this . scene = scene ;
8181 this . enabled = options . enabled ;
8282
8383 if ( this . options . depthTexture . enabled ) {
@@ -97,8 +97,7 @@ export class Depth {
9797 this . renderer . shadowMap . enabled = true ;
9898 this . renderer . shadowMap . type = THREE . PCFShadowMap ;
9999 }
100- camera . add ( this . depthMesh ) ;
101- scene . add ( camera ) ;
100+ scene . add ( this . depthMesh ) ;
102101 }
103102
104103 if ( this . options . occlusion . enabled ) {
@@ -123,24 +122,24 @@ export class Depth {
123122 }
124123
125124 /**
126- * Projects the given world position to clip space and then to view
127- * space using the depth.
125+ * Projects the given world position to depth camera's clip space and then
126+ * to the depth camera's view space using the depth.
128127 * @param position - The world position to project.
128+ * @returns The depth camera view space position.
129129 */
130130 getProjectedDepthViewPositionFromWorldPosition (
131131 position : THREE . Vector3 ,
132132 target = new THREE . Vector3 ( )
133133 ) {
134- const camera = this . renderer . xr ?. getCamera ?.( ) ?. cameras ?. [ 0 ] || this . camera ;
135134 clipSpacePosition
136135 . copy ( position )
137- . applyMatrix4 ( camera . matrixWorldInverse )
138- . applyMatrix4 ( camera . projectionMatrix ) ;
136+ . applyMatrix4 ( this . depthViewMatrices [ 0 ] )
137+ . applyMatrix4 ( this . depthProjectionMatrices [ 0 ] ) ;
139138 const u = 0.5 * ( clipSpacePosition . x + 1.0 ) ;
140139 const v = 0.5 * ( clipSpacePosition . y + 1.0 ) ;
141140 const depth = this . getDepth ( u , v ) ;
142141 target . set ( 2.0 * ( u - 0.5 ) , 2.0 * ( v - 0.5 ) , - 1 ) ;
143- target . applyMatrix4 ( camera . projectionMatrixInverse ) ;
142+ target . applyMatrix4 ( this . depthProjectionInverseMatrices [ 0 ] ) ;
144143 target . multiplyScalar ( ( target . z - depth ) / target . z ) ;
145144 return target ;
146145 }
@@ -165,7 +164,7 @@ export class Depth {
165164 2.0 * ( v - 0.5 ) ,
166165 - 1
167166 ) ;
168- vertexPosition . applyMatrix4 ( this . projectionMatrixInverse ) ;
167+ vertexPosition . applyMatrix4 ( this . depthProjectionInverseMatrices [ 0 ] ) ;
169168 vertexPosition . multiplyScalar ( - depth / vertexPosition . z ) ;
170169 return vertexPosition ;
171170 }
@@ -176,6 +175,9 @@ export class Depth {
176175 this . depthViewMatrices . push ( new THREE . Matrix4 ( ) ) ;
177176 this . depthViewProjectionMatrices . push ( new THREE . Matrix4 ( ) ) ;
178177 this . depthProjectionMatrices . push ( new THREE . Matrix4 ( ) ) ;
178+ this . depthProjectionInverseMatrices . push ( new THREE . Matrix4 ( ) ) ;
179+ this . depthCameraPositions . push ( new THREE . Vector3 ( ) ) ;
180+ this . depthCameraRotations . push ( new THREE . Quaternion ( ) ) ;
179181 }
180182 if ( depthData . projectionMatrix && depthData . transform ) {
181183 this . depthProjectionMatrices [ viewId ] . fromArray (
@@ -184,12 +186,28 @@ export class Depth {
184186 this . depthViewMatrices [ viewId ] . fromArray (
185187 depthData . transform . inverse . matrix
186188 ) ;
189+ this . depthCameraPositions [ viewId ] . set (
190+ depthData . transform . position . x ,
191+ depthData . transform . position . y ,
192+ depthData . transform . position . z
193+ ) ;
194+ this . depthCameraRotations [ viewId ] . set (
195+ depthData . transform . orientation . x ,
196+ depthData . transform . orientation . y ,
197+ depthData . transform . orientation . z ,
198+ depthData . transform . orientation . w
199+ ) ;
187200 } else {
188201 const camera =
189202 this . renderer . xr ?. getCamera ( ) ?. cameras ?. [ viewId ] ?? this . camera ;
190203 this . depthProjectionMatrices [ viewId ] . copy ( camera . projectionMatrix ) ;
191204 this . depthViewMatrices [ viewId ] . copy ( camera . matrixWorldInverse ) ;
205+ this . depthCameraPositions [ viewId ] . copy ( camera . position ) ;
206+ this . depthCameraRotations [ viewId ] . copy ( camera . quaternion ) ;
192207 }
208+ this . depthProjectionInverseMatrices [ viewId ]
209+ . copy ( this . depthProjectionMatrices [ viewId ] )
210+ . invert ( ) ;
193211 this . depthViewProjectionMatrices [ viewId ] . multiplyMatrices (
194212 this . depthProjectionMatrices [ viewId ] ,
195213 this . depthViewMatrices [ viewId ]
@@ -198,6 +216,7 @@ export class Depth {
198216
199217 updateCPUDepthData ( depthData : XRCPUDepthInformation , viewId = 0 ) {
200218 this . cpuDepthData [ viewId ] = depthData ;
219+ this . updateDepthMatrices ( depthData , viewId ) ;
201220
202221 // Updates Depth Array.
203222 if ( this . depthArray [ viewId ] == null ) {
@@ -221,14 +240,18 @@ export class Depth {
221240 }
222241
223242 if ( this . options . depthMesh . enabled && this . depthMesh && viewId == 0 ) {
224- this . depthMesh . updateDepth ( depthData ) ;
243+ this . depthMesh . updateDepth (
244+ depthData ,
245+ this . depthProjectionInverseMatrices [ 0 ]
246+ ) ;
247+ this . depthMesh . position . copy ( this . depthCameraPositions [ 0 ] ) ;
248+ this . depthMesh . quaternion . copy ( this . depthCameraRotations [ 0 ] ) ;
225249 }
226-
227- this . updateDepthMatrices ( depthData , viewId ) ;
228250 }
229251
230252 updateGPUDepthData ( depthData : XRWebGLDepthInformation , viewId = 0 ) {
231253 this . gpuDepthData [ viewId ] = depthData ;
254+ this . updateDepthMatrices ( depthData , viewId ) ;
232255
233256 // For now, assume that we need cpu depth only if depth mesh is enabled.
234257 // In the future, add a separate option.
@@ -261,13 +284,19 @@ export class Depth {
261284
262285 if ( this . options . depthMesh . enabled && this . depthMesh && viewId == 0 ) {
263286 if ( cpuDepth ) {
264- this . depthMesh . updateDepth ( cpuDepth ) ;
287+ this . depthMesh . updateDepth (
288+ cpuDepth ,
289+ this . depthProjectionInverseMatrices [ 0 ]
290+ ) ;
265291 } else {
266- this . depthMesh . updateGPUDepth ( depthData ) ;
292+ this . depthMesh . updateGPUDepth (
293+ depthData ,
294+ this . depthProjectionInverseMatrices [ 0 ]
295+ ) ;
267296 }
297+ this . depthMesh . position . copy ( this . depthCameraPositions [ 0 ] ) ;
298+ this . depthMesh . quaternion . copy ( this . depthCameraRotations [ 0 ] ) ;
268299 }
269-
270- this . updateDepthMatrices ( depthData , viewId ) ;
271300 }
272301
273302 getTexture ( viewId : number ) {
@@ -286,12 +315,6 @@ export class Depth {
286315 }
287316
288317 updateLocalDepth ( frame : XRFrame ) {
289- const leftCamera = this . renderer . xr ?. getCamera ?.( ) ?. cameras ?. [ 0 ] ;
290- if ( leftCamera && this . depthMesh && this . depthMesh . parent != leftCamera ) {
291- leftCamera . add ( this . depthMesh ) ;
292- this . scene . add ( leftCamera ) ;
293- }
294-
295318 const session = frame . session ;
296319 const binding = this . renderer . xr . getBinding ( ) ;
297320
0 commit comments