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

Calculating transform parameters instead of overriding worldToCameraMatrix #159

Merged
merged 1 commit into from
Mar 9, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation


// 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