Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Calculating transform parameters instead of overriding worldToCameraM…
Browse files Browse the repository at this point in the history
…atrix
  • Loading branch information
delapuente committed Feb 26, 2018
1 parent ab86a31 commit c965205
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions Assets/WebVR/Scripts/WebVRCamera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,11 @@ void Update()
}

if (active) {
cameraL.worldToCameraMatrix = clv * sitStand.inverse * transform.worldToLocalMatrix;
SetTransformFromViewMatrix (cameraL.transform, clv * sitStand.inverse * transform.worldToLocalMatrix);
cameraL.projectionMatrix = clp;
cameraR.worldToCameraMatrix = crv * sitStand.inverse * transform.worldToLocalMatrix;
SetTransformFromViewMatrix (cameraR.transform, crv * sitStand.inverse * transform.worldToLocalMatrix);
cameraR.projectionMatrix = crp;
SetHeadTransform ();
} else {
// apply left
cameraMain.worldToCameraMatrix = clv * sitStand.inverse * transform.worldToLocalMatrix;
Expand All @@ -244,6 +245,35 @@ void Update()
#endif
}

// According to https://answers.unity.com/questions/402280/how-to-decompose-a-trs-matrix.html
private void SetTransformFromViewMatrix(Transform transform, Matrix4x4 webVRViewMatrix) {
Matrix4x4 trs = TransformViewMatrixToTRS(webVRViewMatrix);
transform.localPosition = trs.GetColumn(3);
transform.localRotation = Quaternion.LookRotation(trs.GetColumn(2), trs.GetColumn(1));
transform.localScale = new Vector3(
trs.GetColumn(0).magnitude,
trs.GetColumn(1).magnitude,
trs.GetColumn(2).magnitude
);
}

// According to https://forum.unity.com/threads/reproducing-cameras-worldtocameramatrix.365645/#post-2367177
private Matrix4x4 TransformViewMatrixToTRS(Matrix4x4 openGLViewMatrix) {
openGLViewMatrix.m20 *= -1;
openGLViewMatrix.m21 *= -1;
openGLViewMatrix.m22 *= -1;
openGLViewMatrix.m23 *= -1;
return openGLViewMatrix.inverse;
}

private void SetHeadTransform() {
Transform leftTransform = cameraL.transform;
Transform rightTransform = cameraR.transform;
cameraMain.transform.localPosition =
(rightTransform.localPosition - leftTransform.localPosition) / 2f + leftTransform.localPosition;
cameraMain.transform.localRotation = leftTransform.localRotation;
cameraMain.transform.localScale = leftTransform.localScale;
}
void OnGUI()
{
if (!showPerf)
Expand Down

0 comments on commit c965205

Please sign in to comment.