Rotate object from headmovement using webvr?,Rotate object from head tracking movement? usingWEBVR asset store?
I'm trying to have a camera rotate around an object via headtracking movement. I'm using WEBVR as I want it to go on with all devices and on html. Two options:
Trying to move the camera around the object.. while object stays stationary if somehow I can make camera node go around using lookat on object but then camera node should be linked to headvr movement. I was able to do a normal lookat fixed point using mouse coordinates but not in VR.
Other option , keeping camera still and rotating the object via headmovement. This will also involve disabling movement of camera.
I'm fairly new to unity and coding, and then VR coding. I tried lookat functions and somehow try to put it in webvrcamera.cs script which comes in webvrassets. But nothing worked. Please help me get around a sol.
webvrcamera.cs_:
private Camera cameraMain, cameraL, cameraR;
private bool vrActive = false;
[DllImport("__Internal")]
private static extern void PostRender();
private IEnumerator endOfFrame()
{
// Wait until end of frame to report back to WebVR browser to submit frame.
yield return new WaitForEndOfFrame();
PostRender ();
}
void OnEnable()
{
WebVRManager.Instance.OnVRChange += onVRChange;
WebVRManager.Instance.OnHeadsetUpdate += onHeadsetUpdate;
cameraMain = GameObject.Find("CameraMain").GetComponent<Camera>();
cameraL = GameObject.Find("CameraL").GetComponent<Camera>();
cameraR = GameObject.Find("CameraR").GetComponent<Camera>();
cameraMain.transform.Translate(new Vector3(0, WebVRManager.Instance.DefaultHeight, 0));
}
void Update()
{
if (vrActive)
{
cameraMain.enabled = false;
cameraL.enabled = true;
cameraR.enabled = true;
}
else
{
cameraMain.enabled = true;
cameraL.enabled = false;
cameraR.enabled = false;
}
#if !UNITY_EDITOR && UNITY_WEBGL
// Calls Javascript to Submit Frame to the browser WebVR API.
StartCoroutine(endOfFrame());
#endif
}
private void onVRChange(WebVRState state)
{
vrActive = state == WebVRState.ENABLED;
}
private void onHeadsetUpdate (
Matrix4x4 leftProjectionMatrix,
Matrix4x4 leftViewMatrix,
Matrix4x4 rightProjectionMatrix,
Matrix4x4 rightViewMatrix,
Matrix4x4 sitStandMatrix)
{
if (vrActive)
{
SetTransformFromViewMatrix (cameraL.transform, leftViewMatrix * sitStandMatrix.inverse);
cameraL.projectionMatrix = leftProjectionMatrix;
SetTransformFromViewMatrix (cameraR.transform, rightViewMatrix * sitStandMatrix.inverse);
cameraR.projectionMatrix = rightProjectionMatrix;
}
}
// 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;
}
,