Esempio n. 1
0
        public static Vector2[] GetStencilMask()
        {
            HiddenAreaMesh_t leftMesh        = m_vrSystem.GetHiddenAreaMesh(EVREye.Eye_Left);
            HiddenAreaMesh_t rightMesh       = m_vrSystem.GetHiddenAreaMesh(EVREye.Eye_Right);
            uint             leftVertsCount  = leftMesh.unTriangleCount * 3;
            uint             rightVertsCount = rightMesh.unTriangleCount * 3;

            Vector2[] verts = new Vector2[leftVertsCount + rightVertsCount];

            unsafe
            {
                uint index = 0;
                for (int i = 0, j = 0; i < leftMesh.unTriangleCount * 3; i++)
                {
                    Vector2 vertex;
                    float   x = ((float *)leftMesh.pVertexData.ToPointer())[j++];
                    float   y = ((float *)leftMesh.pVertexData.ToPointer())[j++];
                    vertex.X       = -1 + 2 * x;
                    vertex.Y       = -1 + 2 * y;
                    vertex.X       = vertex.X / 2 - 0.5f;
                    verts[index++] = vertex;
                }
                for (int i = 0, j = 0; i < rightMesh.unTriangleCount * 3; i++)
                {
                    Vector2 vertex;
                    float   x = ((float *)rightMesh.pVertexData.ToPointer())[j++];
                    float   y = ((float *)rightMesh.pVertexData.ToPointer())[j++];
                    vertex.X       = -1 + 2 * x;
                    vertex.Y       = -1 + 2 * y;
                    vertex.X       = vertex.X / 2 + 0.5f;
                    verts[index++] = vertex;
                }
            }
            return(verts);
        }
Esempio n. 2
0
        public override void Evaluate(int SpreadMax, CVRSystem system)
        {
            FTexSizeOut[0] = OpenVRManager.RecommendedRenderTargetSize;

            if (OpenVRManager.RenderPoses == null)
            {
                return;
            }

            //camera properties
            var projL = system.GetProjectionMatrix(EVREye.Eye_Left, FNearPlane[0], FFarPlane[0]);
            var projR = system.GetProjectionMatrix(EVREye.Eye_Right, FNearPlane[0], FFarPlane[0]);

            FProjectionOut.SliceCount = 2;
            FProjectionOut[0]         = projL.ToProjectionMatrix();
            FProjectionOut[1]         = projR.ToProjectionMatrix();

            var eyeL = system.GetEyeToHeadTransform(EVREye.Eye_Left).ToEyeMatrix();
            var eyeR = system.GetEyeToHeadTransform(EVREye.Eye_Right).ToEyeMatrix();

            FEyeToHeadOut.SliceCount = 2;
            FEyeToHeadOut[0]         = eyeL;
            FEyeToHeadOut[1]         = eyeR;

            //view
            FViewOut.SliceCount = 2;
            FViewOut[0]         = Matrix.Invert(eyeL * OpenVRManager.RenderPoses[0].mDeviceToAbsoluteTracking.ToMatrix());
            FViewOut[1]         = Matrix.Invert(eyeR * OpenVRManager.RenderPoses[0].mDeviceToAbsoluteTracking.ToMatrix());

            //hidden pixels mesh
            var meshLeft  = system.GetHiddenAreaMesh(EVREye.Eye_Left, EHiddenAreaMeshType.k_eHiddenAreaMesh_Standard);
            var meshRight = system.GetHiddenAreaMesh(EVREye.Eye_Right, EHiddenAreaMeshType.k_eHiddenAreaMesh_Standard);

            if (meshLeft.unTriangleCount > 0 && meshRight.unTriangleCount > 0)
            {
                GetMeshData(meshLeft, FVerticesLeftOut);
                GetMeshData(meshRight, FVerticesRightOut);
            }
            else
            {
                FVerticesLeftOut.SliceCount  = 0;
                FVerticesRightOut.SliceCount = 0;
            }

            FHMDPoseOut[0] = OpenVRManager.GamePoses[0].mDeviceToAbsoluteTracking.ToMatrix();
        }
Esempio n. 3
0
        public override void Evaluate(int SpreadMax, CVRSystem system)
        {
            if (IsInit || FInit[0])
            {
                FTexSizeOut[0] = OpenVRManager.RecommendedRenderTargetSize;

                var projL = system.GetProjectionMatrix(EVREye.Eye_Left, 0.05f, 100, EGraphicsAPIConvention.API_DirectX);
                var projR = system.GetProjectionMatrix(EVREye.Eye_Right, 0.05f, 100, EGraphicsAPIConvention.API_DirectX);
                FProjectionOut.SliceCount = 2;
                FProjectionOut[0]         = projL.ToProjectionMatrix();
                FProjectionOut[1]         = projR.ToProjectionMatrix();

                EyeL = system.GetEyeToHeadTransform(EVREye.Eye_Left).ToEyeMatrix();
                EyeR = system.GetEyeToHeadTransform(EVREye.Eye_Right).ToEyeMatrix();
                FEyeToHeadOut.SliceCount = 2;
                FEyeToHeadOut[0]         = EyeL;
                FEyeToHeadOut[1]         = EyeR;
                IsInit = false;

                var meshLeft  = system.GetHiddenAreaMesh(EVREye.Eye_Left);
                var meshRight = system.GetHiddenAreaMesh(EVREye.Eye_Right);

                //hidden pixels mesh
                if (meshLeft.unTriangleCount > 0 && meshRight.unTriangleCount > 0)
                {
                    GetMeshData(meshLeft, FVerticesLeftOut);
                    GetMeshData(meshRight, FVerticesRightOut);
                }
                else
                {
                    FVerticesLeftOut.SliceCount  = 0;
                    FVerticesRightOut.SliceCount = 0;
                }
            }

            //view
            FViewOut.SliceCount = 2;
            FViewOut[0]         = Matrix.Invert(EyeL * OpenVRManager.RenderPoses[0].mDeviceToAbsoluteTracking.ToMatrix());
            FViewOut[1]         = Matrix.Invert(EyeR * OpenVRManager.RenderPoses[0].mDeviceToAbsoluteTracking.ToMatrix());
            FHMDPoseOut[0]      = OpenVRManager.GamePoses[0].mDeviceToAbsoluteTracking.ToMatrix();
        }
Esempio n. 4
0
        /// <summary>
        /// Initialize HMD using OpenVR API calls.
        /// </summary>
        /// <returns>True on success, false otherwise. Errors logged.</returns>
        bool InitHMD()
        {
            bool retVal = false;

            // return if HMD has already been initialized
            if (hmdIsInitialized)
            {
                return(true);
            }

            // check if HMD is connected on the system
            retVal = OpenVR.IsHmdPresent();
            if (!retVal)
            {
                Debug.Log("[KerbalVR] HMD not found on this system.");
                return(retVal);
            }

            // check if SteamVR runtime is installed.
            // For this plugin, MAKE SURE IT IS ALREADY RUNNING.
            retVal = OpenVR.IsRuntimeInstalled();
            if (!retVal)
            {
                Debug.Log("[KerbalVR] SteamVR runtime not found on this system.");
                return(retVal);
            }

            // initialize HMD
            EVRInitError hmdInitErrorCode = EVRInitError.None;

            vrSystem = OpenVR.Init(ref hmdInitErrorCode, EVRApplicationType.VRApplication_Scene);

            // return if failure
            retVal = (hmdInitErrorCode == EVRInitError.None);
            if (!retVal)
            {
                Debug.Log("[KerbalVR] Failed to initialize HMD. Init returned: " + OpenVR.GetStringForHmdError(hmdInitErrorCode));
                return(retVal);
            }
            else
            {
                Debug.Log("[KerbalVR] OpenVR.Init passed.");
            }

            // reset "seated position" and capture initial position. this means you should hold the HMD in
            // the position you would like to consider "seated", before running this code.
            hmdIsInitialized = true;
            ResetInitialHmdPosition();

            // initialize Compositor
            vrCompositor = OpenVR.Compositor;

            // initialize render textures (for displaying on HMD)
            uint renderTextureWidth  = 0;
            uint renderTextureHeight = 0;

            vrSystem.GetRecommendedRenderTargetSize(ref renderTextureWidth, ref renderTextureHeight);
            //renderTextureWidth /= 2;
            //renderTextureHeight /= 2;

            Debug.Log("[KerbalVR] Render Texture size: " + renderTextureWidth + " x " + renderTextureHeight);

            hmdLeftEyeRenderTexture = new RenderTexture((int)renderTextureWidth, (int)renderTextureHeight, 24, RenderTextureFormat.ARGB32);
            hmdLeftEyeRenderTexture.antiAliasing = 1;
            hmdLeftEyeRenderTexture.Create();

            hmdRightEyeRenderTexture = new RenderTexture((int)renderTextureWidth, (int)renderTextureHeight, 24, RenderTextureFormat.ARGB32);
            hmdRightEyeRenderTexture.Create();

            hmdLeftEyeTexture.handle = hmdLeftEyeRenderTexture.GetNativeTexturePtr();
            hmdLeftEyeTexture.eType  = EGraphicsAPIConvention.API_OpenGL;
            //hmdLeftEyeTexture.eType = EGraphicsAPIConvention.API_DirectX; // this doesn't seem to work
            hmdLeftEyeTexture.eColorSpace = EColorSpace.Auto;

            hmdRightEyeTexture.handle = hmdRightEyeRenderTexture.GetNativeTexturePtr();
            hmdRightEyeTexture.eType  = EGraphicsAPIConvention.API_OpenGL;
            //hmdRightEyeTexture.eType = EGraphicsAPIConvention.API_DirectX; // this doesn't seem to work
            hmdRightEyeTexture.eColorSpace = EColorSpace.Auto;

            // Set rendering bounds on texture to render?
            // I assume min=0.0 and max=1.0 renders to the full extent of the texture
            hmdTextureBounds.uMin = 0.0f;
            hmdTextureBounds.uMax = 1.0f;
            hmdTextureBounds.vMin = 0.0f;
            hmdTextureBounds.vMax = 1.0f;

            // create the hidden area mask meshes
            HiddenAreaMesh_t vrHiddenAreaMesh = vrSystem.GetHiddenAreaMesh(EVREye.Eye_Left);

            hmdHiddenAreaMeshLeft  = SteamVR_Utils.CreateHiddenAreaMesh(vrHiddenAreaMesh, hmdTextureBounds);
            vrHiddenAreaMesh       = vrSystem.GetHiddenAreaMesh(EVREye.Eye_Right);
            hmdHiddenAreaMeshRight = SteamVR_Utils.CreateHiddenAreaMesh(vrHiddenAreaMesh, hmdTextureBounds);

            // TODO: Need to understand better how to create render targets and incorporate hidden area mask mesh

            // search for camera objects to render
            foreach (string cameraName in cameraNamesToRender)
            {
                foreach (Camera camera in Camera.allCameras)
                {
                    if (cameraName.Equals(camera.name))
                    {
                        float nearClipPlane = (camera.name.Equals(cameraNames[3])) ? 0.05f : camera.nearClipPlane;

                        HmdMatrix44_t projLeft  = vrSystem.GetProjectionMatrix(EVREye.Eye_Left, nearClipPlane, camera.farClipPlane, EGraphicsAPIConvention.API_OpenGL);
                        HmdMatrix44_t projRight = vrSystem.GetProjectionMatrix(EVREye.Eye_Right, nearClipPlane, camera.farClipPlane, EGraphicsAPIConvention.API_OpenGL);
                        //HmdMatrix44_t projLeft = vrSystem.GetProjectionMatrix(EVREye.Eye_Left, nearClipPlane, camera.farClipPlane, EGraphicsAPIConvention.API_DirectX); // this doesn't seem to work
                        //HmdMatrix44_t projRight = vrSystem.GetProjectionMatrix(EVREye.Eye_Right, nearClipPlane, camera.farClipPlane, EGraphicsAPIConvention.API_DirectX); // this doesn't seem to work
                        camerasToRender.Add(new CameraProperties(camera, camera.projectionMatrix, MathUtils.Matrix4x4_OpenVr2UnityFormat(ref projLeft), MathUtils.Matrix4x4_OpenVr2UnityFormat(ref projRight)));
                        break;
                    }
                }
            }

            foreach (Camera camera in Camera.allCameras)
            {
                if (cameraNames[5].Equals(camera.name))
                {
                    uiCamera = camera;
                }
            }

            // detect controllers
            for (uint idx = 0; idx < OpenVR.k_unMaxTrackedDeviceCount; idx++)
            {
                if ((ctrlIndexLeft == 0) && (vrSystem.GetTrackedDeviceClass(idx) == ETrackedDeviceClass.Controller))
                {
                    ctrlIndexLeft = idx;
                }
                else if ((ctrlIndexRight == 0) && (vrSystem.GetTrackedDeviceClass(idx) == ETrackedDeviceClass.Controller))
                {
                    ctrlIndexRight = idx;
                }
            }
            bool ctrlFocusCaptured = vrSystem.CaptureInputFocus();

            if (!ctrlFocusCaptured)
            {
                Debug.LogWarning("[KerbalVR] Controller input focus was not captured");
            }

            return(retVal);
        }