Beispiel #1
0
    private void UpdateCamera(WebXRViewEyes eye)
    {
        var id = (int)eye - 1;

        // If the camera for this id should not exist
        if ((ViewEye & eye) != eye)
        {
            if (_cameras[id])
            {
                Destroy(_cameras[id].gameObject);
                _cameras[id] = null;
            }

            return;
        }

        // Create camera
        if (!_cameras[id])
        {
            if (id > 0)
            {
                var camGameObject = new GameObject("WebXRCamera_" + id);
                camGameObject.transform.parent = gameObject.transform;
                _cameras[id] = camGameObject.AddComponent <Camera>();
            }
            else
            {
                _cameras[0] = Camera.main;
            }
            _cameras[id].clearFlags      = CameraClearFlags.SolidColor;
            _cameras[id].backgroundColor = new Color(0, 0, 0, 0);
            _cameras[id].nearClipPlane   = 0.1f;
            _cameras[id].farClipPlane    = 1000;
        }

        var floatStartId = id * 27;

        var rect = new Rect(_dataArray[floatStartId + 23], _dataArray[floatStartId + 24], _dataArray[floatStartId + 25], _dataArray[floatStartId + 26]);

        if (id > 0)
        {
            if (_cameras[0] && _cameras[0].rect == rect)
            {
                _cameras[id].gameObject.SetActive(false);
                return;
            }
            else
            {
                _cameras[id].gameObject.SetActive(true);
            }
        }

        _cameras[id].rect = rect;

        // Get and transpose projection matrix
        var pm = new Matrix4x4();

        pm.m00 = _dataArray[floatStartId + 0];
        pm.m01 = _dataArray[floatStartId + 4];
        pm.m02 = _dataArray[floatStartId + 8];
        pm.m03 = _dataArray[floatStartId + 12];
        pm.m10 = _dataArray[floatStartId + 1];
        pm.m11 = _dataArray[floatStartId + 5];
        pm.m12 = _dataArray[floatStartId + 9];
        pm.m13 = _dataArray[floatStartId + 13];
        pm.m20 = _dataArray[floatStartId + 2];
        pm.m21 = _dataArray[floatStartId + 6];
        pm.m22 = _dataArray[floatStartId + 10];
        pm.m23 = _dataArray[floatStartId + 14];
        pm.m30 = _dataArray[floatStartId + 3];
        pm.m31 = _dataArray[floatStartId + 7];
        pm.m32 = _dataArray[floatStartId + 11];
        pm.m33 = _dataArray[floatStartId + 15];

        _cameras[id].projectionMatrix = pm;

        // Get position and rotation Z, RX and RY are inverted
        _cameras[id].transform.position = ToUnityPosition(_dataArray[floatStartId + 16], _dataArray[floatStartId + 17], _dataArray[floatStartId + 18]);
        _cameras[id].transform.rotation = ToUnityRotation(_dataArray[floatStartId + 19], _dataArray[floatStartId + 20], _dataArray[floatStartId + 21], _dataArray[floatStartId + 22]);
    }
Beispiel #2
0
        // Create and update camera pose
        private static void UpdateCamera(WebXRViewEyes eye)
        {
            var id = (eye == WebXRViewEyes.Left) ? 0 : 1;

            // If the camera for this id should not exist
            if ((ViewEye & eye) != eye)
            {
                if (_cameras[id])
                {
                    if (eye == WebXRViewEyes.Left) // don't destroy main camera
                    {
                        if (_shouldRestoreMainCameraProperties)
                        {
                            Camera.main.backgroundColor  = _mainCameraBackgroundColor;
                            Camera.main.clearFlags       = _mainCameraClearFlags;
                            Camera.main.projectionMatrix = _mainCameraProjectionMatrix;
                            Camera.main.rect             = _mainCameraRect;
                        }
                    }
                    else
                    {
                        Destroy(_cameras[id].gameObject);
                    }

                    _cameras[id] = null;
                }

                return;
            }

            // Create camera
            if (!_cameras[id])
            {
                if (id > 0)
                {
                    // clone main camera
                    _cameras[id]       = Instantiate(Camera.main, Camera.main.gameObject.transform.parent);
                    _cameras[id].name  = "WebXRCamera_" + id;
                    _cameras[id].depth = Camera.main.depth - 1;
                }
                else
                {
                    _shouldRestoreMainCameraProperties = false;

                    if (Camera.main)
                    {
                        _cameras[0] = Camera.main;
                        _shouldRestoreMainCameraProperties = true;
                        _mainCameraBackgroundColor         = Camera.main.backgroundColor;
                        _mainCameraClearFlags       = Camera.main.clearFlags;
                        _mainCameraProjectionMatrix = Camera.main.projectionMatrix;
                        _mainCameraRect             = Camera.main.rect;
                    }
                    else
                    {
                        var go = new GameObject("WebXRCamera_0");
                        _cameras[0] = go.AddComponent <Camera>();
                    }
                }

                if (IsArSupported())
                {
                    _cameras[id].clearFlags      = CameraClearFlags.SolidColor;
                    _cameras[id].backgroundColor = new Color(0, 0, 0, 0);
                }
            }

            var floatStartId = id * 27;

            var rect = new Rect(_dataArray[floatStartId + 23], _dataArray[floatStartId + 24], _dataArray[floatStartId + 25], _dataArray[floatStartId + 26]);

            if (id > 0)
            {
                if (_cameras[0] && _cameras[0].rect == rect)
                {
                    _cameras[id].gameObject.SetActive(false);
                    return;
                }
                else
                {
                    _cameras[id].gameObject.SetActive(true);
                }
            }

            _cameras[id].rect = rect;

            // Get and transpose projection matrix
            var pm = new Matrix4x4();

            pm.m00 = _dataArray[floatStartId + 0];
            pm.m01 = _dataArray[floatStartId + 4];
            pm.m02 = _dataArray[floatStartId + 8];
            pm.m03 = _dataArray[floatStartId + 12];
            pm.m10 = _dataArray[floatStartId + 1];
            pm.m11 = _dataArray[floatStartId + 5];
            pm.m12 = _dataArray[floatStartId + 9];
            pm.m13 = _dataArray[floatStartId + 13];
            pm.m20 = _dataArray[floatStartId + 2];
            pm.m21 = _dataArray[floatStartId + 6];
            pm.m22 = _dataArray[floatStartId + 10];
            pm.m23 = _dataArray[floatStartId + 14];
            pm.m30 = _dataArray[floatStartId + 3];
            pm.m31 = _dataArray[floatStartId + 7];
            pm.m32 = _dataArray[floatStartId + 11];
            pm.m33 = _dataArray[floatStartId + 15];

            _cameras[id].projectionMatrix = pm;

            // Get position and rotation Z, RX and RY are inverted
            _cameras[id].transform.localPosition = ToUnityPosition(_dataArray[floatStartId + 16], _dataArray[floatStartId + 17], _dataArray[floatStartId + 18]);
            _cameras[id].transform.localRotation = ToUnityRotation(_dataArray[floatStartId + 19], _dataArray[floatStartId + 20], _dataArray[floatStartId + 21], _dataArray[floatStartId + 22]);
        }