@@ -221,10 +221,11 @@ void Update()
221221 deltaTime += ( Time . deltaTime - deltaTime ) * 0.1f ;
222222
223223 if ( active ) {
224- cameraL . worldToCameraMatrix = clv * sitStand . inverse * transform . worldToLocalMatrix ;
224+ SetTransformFromViewMatrix ( cameraL . transform , clv * sitStand . inverse * transform . worldToLocalMatrix ) ;
225225 cameraL . projectionMatrix = clp ;
226- cameraR . worldToCameraMatrix = crv * sitStand . inverse * transform . worldToLocalMatrix ;
226+ SetTransformFromViewMatrix ( cameraR . transform , crv * sitStand . inverse * transform . worldToLocalMatrix ) ;
227227 cameraR . projectionMatrix = crp ;
228+ SetHeadTransform ( ) ;
228229 } else {
229230 // apply left
230231 cameraMain . worldToCameraMatrix = clv * sitStand . inverse * transform . worldToLocalMatrix ;
@@ -255,6 +256,35 @@ void Update()
255256 #endif
256257 }
257258
259+ // According to https://answers.unity.com/questions/402280/how-to-decompose-a-trs-matrix.html
260+ private void SetTransformFromViewMatrix ( Transform transform , Matrix4x4 webVRViewMatrix ) {
261+ Matrix4x4 trs = TransformViewMatrixToTRS ( webVRViewMatrix ) ;
262+ transform . localPosition = trs . GetColumn ( 3 ) ;
263+ transform . localRotation = Quaternion . LookRotation ( trs . GetColumn ( 2 ) , trs . GetColumn ( 1 ) ) ;
264+ transform . localScale = new Vector3 (
265+ trs . GetColumn ( 0 ) . magnitude ,
266+ trs . GetColumn ( 1 ) . magnitude ,
267+ trs . GetColumn ( 2 ) . magnitude
268+ ) ;
269+ }
270+
271+ // According to https://forum.unity.com/threads/reproducing-cameras-worldtocameramatrix.365645/#post-2367177
272+ private Matrix4x4 TransformViewMatrixToTRS ( Matrix4x4 openGLViewMatrix ) {
273+ openGLViewMatrix . m20 *= - 1 ;
274+ openGLViewMatrix . m21 *= - 1 ;
275+ openGLViewMatrix . m22 *= - 1 ;
276+ openGLViewMatrix . m23 *= - 1 ;
277+ return openGLViewMatrix . inverse ;
278+ }
279+
280+ private void SetHeadTransform ( ) {
281+ Transform leftTransform = cameraL . transform ;
282+ Transform rightTransform = cameraR . transform ;
283+ cameraMain . transform . localPosition =
284+ ( rightTransform . localPosition - leftTransform . localPosition ) / 2f + leftTransform . localPosition ;
285+ cameraMain . transform . localRotation = leftTransform . localRotation ;
286+ cameraMain . transform . localScale = leftTransform . localScale ;
287+ }
258288 void OnGUI ( )
259289 {
260290 if ( ! showPerf )
0 commit comments