Exemplo n.º 1
0
        /// <summary>
        /// Creates the command buffer for single eye (left or right)
        /// </summary>
        private void createCommandBufferForSingleEye()
        {
            Camera eye = this.GetComponent <Camera>();

            //Setup orthographic view command buffer:
            float pScreenW, pScreenH;//physical screen width, height

            if (!PEUtils.GetPhysicalScreenSize(out pScreenW, out pScreenH))
            {
                throw new UnityException("Fail to get screen physical size");
            }
            //in case screen ori not correct
            if (pScreenH > pScreenW)
            {
                var t = pScreenH;
                pScreenH = pScreenW;
                pScreenW = t;
            }
//            Debug.LogFormat("Physical w/h: {0}/{1}", pScreenW, pScreenH);
            pScreenW /= 2;

            eye.orthographic = true;
            var phoneProfile = PhoneProfileContainer.GetCurrent();

            eye.orthographicSize = phoneProfile.OrthoRectSize;
            Matrix4x4 matrix_ortho_prj = eye.projectionMatrix;

            Matrix4x4 matrix_World2Cam = Matrix4x4.TRS(new Vector3(0, 0, 0), Quaternion.identity, new Vector3(1, 1, -1));

            if (m_CmdBuffer_SingleEye != null)
            {
                eye.RemoveCommandBuffer(CameraEvent.AfterImageEffects, m_CmdBuffer_SingleEye);
            }
            m_CmdBuffer_SingleEye = new CommandBuffer();
            m_CmdBuffer_SingleEye.SetViewProjectionMatrices(matrix_World2Cam, matrix_ortho_prj);
            m_CmdBuffer_SingleEye.ClearRenderTarget(clearDepth: true, clearColor: false, backgroundColor: Color.black);
            var undistortionRender   = undistortionRoot.transform.GetChild(0).GetComponent <MeshRenderer>();
            var undistortionBGRender = undistortionRoot.transform.GetChild(1).GetComponent <MeshRenderer>();

            m_CmdBuffer_SingleEye.DrawRenderer(undistortionBGRender, undistortionBGRender.sharedMaterial);//order does matter
            m_CmdBuffer_SingleEye.DrawRenderer(undistortionRender, undistortionRender.sharedMaterial);
            eye.AddCommandBuffer(CameraEvent.AfterImageEffects, m_CmdBuffer_SingleEye);


            //Setup rendering camera properties:
            ReflectionLenProfile lenProfile = ReflectionLenProfileContainer.GetLenProfile();
            var hFov = lenProfile.hFov;
            var vFov = lenProfile.vFov;

            eye.SetCameraFov(hFov, vFov);

            #region Alignment

            var projectionMatrix = eye.projectionMatrix;
            projectionMatrix[1, 1] = 1.23646257F;
            projectionMatrix[1, 2] = -0.0777916671F;
            eye.projectionMatrix   = projectionMatrix;

            #endregion
        }
Exemplo n.º 2
0
        private void createUndistortionMeshInstance()
        {
            Camera eye = this.GetComponent <Camera>();

            //Generate undistortion instance:
            if (undistortionRoot != null)
            {
                DestroyImmediate(undistortionRoot);
            }

            //Prepare undistortion view shader:
            Shader undistortionShader = Resources.Load <Shader>("XimmerseAR");

            this.m_UndistortionMaterial  = new Material(undistortionShader);
            m_UndistortionMaterial.color = Color.white;

            undistortionRoot = new GameObject("UndistortionView-" + eye.stereoTargetEye.ToString());

            ReflectionLenProfile lenProfile = ReflectionLenProfileContainer.GetLenProfile();
            Vector3 offset = lenProfile.UndistortionMeshOffset;

            offset += eye.stereoTargetEye == StereoTargetEyeMask.Left ? lenProfile.LeftUndistortionMeshOffset : lenProfile.RightUndistortionMeshOffset;
            if (eye.stereoTargetEye == StereoTargetEyeMask.Left)
            {
                offset.x *= -1;
            }

            undistortionRoot.transform.position = offset + Vector3.forward * 1;

            m_UndistortionMeshInstance = new GameObject("UndistortionMesh", new System.Type[] { typeof(MeshFilter), typeof(MeshRenderer) });
            Mesh undistortionMesh = lenProfile.UndistortionMesh;

            if (eye.stereoTargetEye == StereoTargetEyeMask.Left)
            {
                m_UndistortionMaterial.SetFloat("_ViewCutFromRight", 1);
            }
            else if (eye.stereoTargetEye == StereoTargetEyeMask.Right)
            {
                m_UndistortionMaterial.SetFloat("_ViewCutFromRight", 2);
            }
            m_UndistortionMeshInstance.GetComponent <MeshFilter>().sharedMesh       = undistortionMesh;
            m_UndistortionMeshInstance.GetComponent <MeshRenderer>().sharedMaterial = m_UndistortionMaterial;
            m_UndistortionMeshInstance.transform.localScale = lenProfile.UndistortionMeshScale;
            m_UndistortionMeshInstance.transform.SetParent(undistortionRoot.transform, false);
            m_UndistortionMeshInstance.transform.localEulerAngles = lenProfile.UndistortionMeshViewDirection;


            GameObject undistrotionMeshBg = GameObject.CreatePrimitive(PrimitiveType.Quad);

            undistrotionMeshBg.transform.SetParent(undistortionRoot.transform, false);
            undistrotionMeshBg.transform.localPosition = new Vector3(0, 0, 3);
            undistrotionMeshBg.transform.localScale    = Vector3.one * 10;//big enough to cover full view
            undistrotionMeshBg.GetComponent <MeshRenderer>().sharedMaterial = Material_black;
            undistrotionMeshBg.name = "Black-Blackground";

            this.undistortionRoot.SetActive(false);
            m_TargetTexture = new RenderTexture(eye.pixelWidth, eye.pixelHeight, 24, RenderTextureFormat.ARGB32);
            m_UndistortionMaterial.mainTexture = m_TargetTexture;

            //undistortionViewMat.mainTexture = Resources.Load<Texture2D>("uvcheck");
        }
Exemplo n.º 3
0
        /// <summary>
        /// Initializes the camera instances.
        /// By default assuming the rendering mode is Stereo.
        /// </summary>
        private void InitializeCameraInstances()
        {
            mainCamera = this.GetComponent <Camera>();
            var mainCameraTransform = mainCamera.transform;

            //clear left-right eye if existence.
            if (leftEye != null)
            {
                DestroyImmediate(leftEye.gameObject);
            }
            if (rightEye != null)
            {
                DestroyImmediate(rightEye.gameObject);
            }

            leftEye  = new GameObject("LeftEye", new System.Type[] { typeof(Camera) }).GetComponent <Camera>();
            rightEye = new GameObject("RightEye", new System.Type[] { typeof(Camera) }).GetComponent <Camera>();
            leftEye.CopyFrom(mainCamera);//clone camera setting and override some of it later.
            rightEye.CopyFrom(mainCamera);

            PhoneProfile         phoneProfile = PhoneProfileContainer.GetCurrent();
            ReflectionLenProfile lenProfile   = ReflectionLenProfileContainer.GetLenProfile();

            leftEye.transform.SetParent(mainCameraTransform);
            leftEye.transform.localPosition = new Vector3(-lenProfile.EyeSeparation, 0, 0);
            rightEye.transform.SetParent(mainCameraTransform);
            rightEye.transform.localPosition = new Vector3(lenProfile.EyeSeparation, 0, 0);
            m_EyeSeparation            = new Vector3(lenProfile.EyeSeparation, 0, 0);
            m_UndistortionMeshPan      = lenProfile.UndistortionMeshOffset;
            m_LeftUndistortionMeshPan  = lenProfile.LeftUndistortionMeshOffset;
            m_RightUndistortionMeshPan = lenProfile.RightUndistortionMeshOffset;
            m_UndistortionMeshEuler    = lenProfile.UndistortionMeshViewDirection;
            m_UndistortionMeshScale    = lenProfile.UndistortionMeshScale;

            float hFov = lenProfile.hFov, vFov = lenProfile.vFov;

            leftEye.cullingMask = rightEye.cullingMask = mainCamera.cullingMask;

            this.m_hFov = hFov;
            this.m_vFov = vFov;
            mainCamera.SetCameraFov(hFov, vFov);

            //left - right bg color = black
            leftEye.clearFlags      = rightEye.clearFlags = CameraClearFlags.SolidColor;
            leftEye.backgroundColor = rightEye.backgroundColor = Color.black;

            //Apply phone profile view rect:
            leftEye.rect  = phoneProfile.LeftViewRect;
            rightEye.rect = phoneProfile.RightViewRect;

            //Assumes the rendering mode is stereo.
            mainCamera.enabled         = false;
            leftEye.stereoTargetEye    = StereoTargetEyeMask.Left;
            rightEye.stereoTargetEye   = StereoTargetEyeMask.Right;
            mainCamera.stereoTargetEye = StereoTargetEyeMask.Both;
            leftEye.enabled            = rightEye.enabled = true;
            mainCamera.enabled         = false; //by default assuming rendering mode = stereo.
            leftEye.tag = "MainCamera";         //main camera is disable, we need Camera.main return a valid camera.

            if (OnCameraIsCreated != null)
            {
                OnCameraIsCreated(leftEye, rightEye);
            }

            AddCameraComponents();
            //For mono rendering:

            //Update eye covengence at birth:
            this.UpdateEyeCovengence();
        }