@@ -230,10 +230,11 @@ void Update()
230230 }
231231
232232 if ( active ) {
233- cameraL . worldToCameraMatrix = clv * sitStand . inverse * transform . worldToLocalMatrix ;
233+ SetTransformFromViewMatrix ( cameraL . transform , clv * sitStand . inverse * transform . worldToLocalMatrix ) ;
234234 cameraL . projectionMatrix = clp ;
235- cameraR . worldToCameraMatrix = crv * sitStand . inverse * transform . worldToLocalMatrix ;
235+ SetTransformFromViewMatrix ( cameraR . transform , crv * sitStand . inverse * transform . worldToLocalMatrix ) ;
236236 cameraR . projectionMatrix = crp ;
237+ SetHeadTransform ( ) ;
237238 } else {
238239 // apply left
239240 cameraMain . worldToCameraMatrix = clv * sitStand . inverse * transform . worldToLocalMatrix ;
@@ -244,6 +245,35 @@ void Update()
244245 #endif
245246 }
246247
248+ // According to https://answers.unity.com/questions/402280/how-to-decompose-a-trs-matrix.html
249+ private void SetTransformFromViewMatrix ( Transform transform , Matrix4x4 webVRViewMatrix ) {
250+ Matrix4x4 trs = TransformViewMatrixToTRS ( webVRViewMatrix ) ;
251+ transform . localPosition = trs . GetColumn ( 3 ) ;
252+ transform . localRotation = Quaternion . LookRotation ( trs . GetColumn ( 2 ) , trs . GetColumn ( 1 ) ) ;
253+ transform . localScale = new Vector3 (
254+ trs . GetColumn ( 0 ) . magnitude ,
255+ trs . GetColumn ( 1 ) . magnitude ,
256+ trs . GetColumn ( 2 ) . magnitude
257+ ) ;
258+ }
259+
260+ // According to https://forum.unity.com/threads/reproducing-cameras-worldtocameramatrix.365645/#post-2367177
261+ private Matrix4x4 TransformViewMatrixToTRS ( Matrix4x4 openGLViewMatrix ) {
262+ openGLViewMatrix . m20 *= - 1 ;
263+ openGLViewMatrix . m21 *= - 1 ;
264+ openGLViewMatrix . m22 *= - 1 ;
265+ openGLViewMatrix . m23 *= - 1 ;
266+ return openGLViewMatrix . inverse ;
267+ }
268+
269+ private void SetHeadTransform ( ) {
270+ Transform leftTransform = cameraL . transform ;
271+ Transform rightTransform = cameraR . transform ;
272+ cameraMain . transform . localPosition =
273+ ( rightTransform . localPosition - leftTransform . localPosition ) / 2f + leftTransform . localPosition ;
274+ cameraMain . transform . localRotation = leftTransform . localRotation ;
275+ cameraMain . transform . localScale = leftTransform . localScale ;
276+ }
247277 void OnGUI ( )
248278 {
249279 if ( ! showPerf )
0 commit comments