示例#1
0
            void OnRenderImage(RenderTexture r, RenderTexture r2)
            {
                if (HmdOn)
                {
                    EVRCompositorError vrCompositorError = EVRCompositorError.None;
                    if (left && !leftReady)
                    {
                        //       log("left");
                        lock (KerbalVRPlugin.hmdRightEyeRenderTexture) lock (KerbalVRPlugin.hmdLeftEyeRenderTexture) lock (r) lock (vrCompositor)
                                    {
                                        //             log("leftLOCK");
                                        //         log("left");
                                        hmdLeftEyeTexture.handle = r.GetNativeTexturePtr();

                                        vrCompositorError = vrCompositor.Submit(EVREye.Eye_Left, ref hmdLeftEyeTexture, ref hmdTextureBounds, EVRSubmitFlags.Submit_Default);
                                        leftReady         = true;
                                        if (vrCompositorError != EVRCompositorError.None && vrCompositorError != lastError)
                                        {
                                            lastError = vrCompositorError;

                                            warn("Submit (Eye_Left) failed: " + vrCompositorError.ToString());
                                        }
                                    }
                    }
                    else if (!rightReady)
                    {
                        //     log("right");
                        lock (KerbalVRPlugin.hmdRightEyeRenderTexture) lock (KerbalVRPlugin.hmdLeftEyeRenderTexture) lock (r) lock (vrCompositor)
                                    {
                                        //              log("rightLOCK");
                                        hmdRightEyeTexture.handle = r.GetNativeTexturePtr();

                                        vrCompositorError = vrCompositor.Submit(EVREye.Eye_Right, ref hmdRightEyeTexture, ref hmdTextureBounds, EVRSubmitFlags.Submit_Default);
                                        rightReady        = true;
                                        if (vrCompositorError != EVRCompositorError.None && vrCompositorError != lastError)
                                        {
                                            lastError = vrCompositorError;

                                            warn("Submit (Eye_Left) failed: " + vrCompositorError.ToString());
                                        }
                                        posTracker.gotPoses = false;
                                    }
                    }
                }
            }
示例#2
0
        public void OnLateUpdate()
        {
            dispatchOpenVREvents();

            EVRCompositorError compositorError = OpenVR.Compositor.WaitGetPoses(VRManager.RenderPoses, VRManager.GamePoses);

            if (compositorError != EVRCompositorError.None)
            {
                throw new Exception("OpenVr error: " + compositorError.ToString());
            }

            OpenVR.System.GetDeviceToAbsoluteTrackingPose(ETrackingUniverseOrigin.TrackingUniverseStanding, Utils.CalculatePredictedSecondsToPhotons(), VRManager.DevicePoses);
            SteamVR_Events.NewPoses.Send(VRManager.DevicePoses);

            for (int i = 0; i < VRManager.DevicePoses.Length; i++)
            {
                TrackedDevicePose_t devicePose = VRManager.DevicePoses[i];
                if (!devicePose.bDeviceIsConnected || !devicePose.bPoseIsValid)
                {
                    continue;
                }

                SteamVR_Utils.RigidTransform rigidTransform = new SteamVR_Utils.RigidTransform(devicePose.mDeviceToAbsoluteTracking);

                ETrackedDeviceClass deviceType = OpenVR.System.GetTrackedDeviceClass((uint)i);

                if (deviceType == ETrackedDeviceClass.HMD)
                {
                    Head.transform.localPosition = rigidTransform.pos;
                    Head.transform.localRotation = rigidTransform.rot;
                    Head.Velocity        = fromHmdVector3_t(devicePose.vVelocity);
                    Head.AngularVelocity = fromHmdVector3_t(devicePose.vAngularVelocity);
                    Head.DeviceIndex     = (uint)i;
                }
                else if (deviceType == ETrackedDeviceClass.Controller)
                {
                    ETrackedControllerRole role = OpenVR.System.GetControllerRoleForTrackedDeviceIndex((uint)i);
                    if (role == ETrackedControllerRole.LeftHand)
                    {
                        LeftController.transform.localPosition = rigidTransform.pos;
                        LeftController.transform.localRotation = rigidTransform.rot;
                        LeftController.Velocity        = fromHmdVector3_t(devicePose.vVelocity);
                        LeftController.AngularVelocity = fromHmdVector3_t(devicePose.vAngularVelocity);
                        LeftController.DeviceIndex     = (uint)i;
                    }
                    else if (role == ETrackedControllerRole.RightHand)
                    {
                        RightController.transform.localPosition = rigidTransform.pos;
                        RightController.transform.localRotation = rigidTransform.rot;
                        RightController.Velocity        = fromHmdVector3_t(devicePose.vVelocity);
                        RightController.AngularVelocity = fromHmdVector3_t(devicePose.vAngularVelocity);
                        RightController.DeviceIndex     = (uint)i;
                    }
                }
            }
        }
示例#3
0
        void OnPostRender()
        {
            if (EyeType == EVREye.Eye_Left)
            {
                VRManager.Instance.LeftEyeTexture.handle = VRManager.Instance.LeftEyeRenderTexture.GetNativeTexturePtr();

                EVRCompositorError error = OpenVR.Compositor.Submit(EVREye.Eye_Left, ref VRManager.Instance.LeftEyeTexture, ref _bounds, EVRSubmitFlags.Submit_Default);
                if (error != EVRCompositorError.None)
                {
                    throw new Exception("OpenVR Sumbit error on left eye: " + error.ToString());
                }
            }
            else
            {
                VRManager.Instance.RightEyeTexture.handle = VRManager.Instance.RightEyeRenderTexture.GetNativeTexturePtr();

                EVRCompositorError error = OpenVR.Compositor.Submit(EVREye.Eye_Right, ref VRManager.Instance.RightEyeTexture, ref _bounds, EVRSubmitFlags.Submit_Default);
                if (error != EVRCompositorError.None)
                {
                    throw new Exception("OpenVR Sumbit error on right eye: " + error.ToString());
                }
            }
            _owner.OnEyeFinishedRendering();
        }
示例#4
0
        /// <summary>
        /// Renders a set of cameras onto a RenderTexture, and submit the frame to the HMD.
        /// </summary>
        private void RenderHmdCameras(
            EVREye eye,
            SteamVR_Utils.RigidTransform hmdTransform,
            SteamVR_Utils.RigidTransform hmdEyeTransform,
            RenderTexture hmdEyeRenderTexture,
            Texture_t hmdEyeTexture)
        {
            /**
             * hmdEyeTransform is in a coordinate system that follows the headset, where
             * the origin is the headset device position. Therefore the eyes are at a constant
             * offset from the device. hmdEyeTransform does not change (per eye).
             *      hmdEyeTransform.x+  towards the right of the headset
             *      hmdEyeTransform.y+  towards the top the headset
             *      hmdEyeTransform.z+  towards the front of the headset
             *
             * hmdTransform is in a coordinate system set in physical space, where the
             * origin is the initial seated position. Or for room-scale, the physical origin of the room.
             *      hmdTransform.x+     towards the right
             *      hmdTransform.y+     upwards
             *      hmdTransform.z+     towards the front
             *
             *  Scene.InitialPosition and Scene.InitialRotation are the Unity world coordinates where
             *  we initialize the VR scene, i.e. the origin of a coordinate system that maps
             *  1-to-1 with physical space.
             *
             *  1. Calculate the position of the eye in the physical coordinate system.
             *  2. Transform the calculated position into Unity world coordinates, offset from
             *     InitialPosition and InitialRotation.
             */

            // position of the eye in the VR reference frame
            Vector3 positionToEye = hmdTransform.pos + hmdTransform.rot * hmdEyeTransform.pos;

            // update position of the cameras
            Scene.Instance.UpdateScene(eye, hmdTransform, hmdEyeTransform);

            // render the set of cameras
            for (int i = 0; i < Scene.Instance.NumVRCameras; i++)
            {
                Types.CameraData camData = Scene.Instance.VRCameras[i];

                // set projection matrix
                camData.camera.projectionMatrix = (eye == EVREye.Eye_Left) ?
                                                  camData.hmdProjectionMatrixL : camData.hmdProjectionMatrixR;

                // set texture to render to, then render
                camData.camera.targetTexture = hmdEyeRenderTexture;
                camData.camera.Render();
            }

            hmdEyeTexture.handle = hmdEyeRenderTexture.GetNativeTexturePtr();

            // Submit frames to HMD
            EVRCompositorError vrCompositorError = OpenVR.Compositor.Submit(eye, ref hmdEyeTexture, ref hmdTextureBounds, EVRSubmitFlags.Submit_Default);

            if (vrCompositorError != EVRCompositorError.None)
            {
                throw new Exception("Submit (" + eye + ") failed: (" + (int)vrCompositorError + ") " + vrCompositorError.ToString());
            }
        }
示例#5
0
        /// <summary>
        /// On LateUpdate, dispatch OpenVR events, run the main HMD loop code.
        /// </summary>
        private void LateUpdate()
        {
            // dispatch any OpenVR events
            if (hmdState == HmdState.Initialized)
            {
                DispatchOpenVREvents();
            }

            // check if the current scene allows VR
            HmdIsAllowed = Scene.Instance.SceneAllowsVR();

            // process the state of OpenVR
            ProcessHmdState();

            // check if we are running the HMD
            HmdIsRunning = HmdIsAllowed && (hmdState == HmdState.Initialized) && HmdIsEnabled;

            // perform regular updates if HMD is initialized
            if (HmdIsRunning)
            {
                EVRCompositorError vrCompositorError = EVRCompositorError.None;

                // we've just started VR
                if (!hmdIsRunningPrev)
                {
                    Utils.Log("HMD is now on");
                    Scene.Instance.SetupScene();
                    ResetInitialHmdPosition();
                }

                try {
                    // get latest device poses, emit an event to indicate devices have been updated
                    float secondsToPhotons = Utils.CalculatePredictedSecondsToPhotons();
                    OpenVR.System.GetDeviceToAbsoluteTrackingPose(Scene.Instance.TrackingSpace, secondsToPhotons, devicePoses);
                    SteamVR_Events.NewPoses.Send(devicePoses);

                    HmdMatrix34_t vrLeftEyeTransform  = OpenVR.System.GetEyeToHeadTransform(EVREye.Eye_Left);
                    HmdMatrix34_t vrRightEyeTransform = OpenVR.System.GetEyeToHeadTransform(EVREye.Eye_Right);
                    vrCompositorError = OpenVR.Compositor.WaitGetPoses(renderPoses, gamePoses);

                    if (vrCompositorError != EVRCompositorError.None)
                    {
                        throw new Exception("WaitGetPoses failed: (" + (int)vrCompositorError + ") " + vrCompositorError.ToString());
                    }

                    // convert SteamVR poses to Unity coordinates
                    var hmdTransform = new SteamVR_Utils.RigidTransform(devicePoses[OpenVR.k_unTrackedDeviceIndex_Hmd].mDeviceToAbsoluteTracking);
                    SteamVR_Utils.RigidTransform[] hmdEyeTransform = new SteamVR_Utils.RigidTransform[2];
                    hmdEyeTransform[0] = new SteamVR_Utils.RigidTransform(vrLeftEyeTransform);
                    hmdEyeTransform[1] = new SteamVR_Utils.RigidTransform(vrRightEyeTransform);

                    // don't highlight parts with the mouse
                    Mouse.HoveredPart = null;

                    // render each eye
                    for (int i = 0; i < 2; i++)
                    {
                        RenderHmdCameras(
                            (EVREye)i,
                            hmdTransform,
                            hmdEyeTransform[i],
                            hmdEyeRenderTexture[i],
                            hmdEyeTexture[i]);
                    }

                    // [insert dark magic here]
                    OpenVR.Compositor.PostPresentHandoff();

                    // render to the game screen
                    if (RenderHmdToScreen)
                    {
                        Graphics.Blit(hmdEyeRenderTexture[0], null as RenderTexture);
                    }
                } catch (Exception e) {
                    // shut off VR when an error occurs
                    Utils.LogError(e);
                    HmdIsEnabled = false;
                    HmdIsRunning = false;
                }
            }

            // reset cameras when HMD is turned off
            if (!HmdIsRunning && hmdIsRunningPrev)
            {
                Utils.Log("HMD is now off, resetting cameras...");
                Scene.Instance.CloseScene();

                // TODO: figure out why we can no longer manipulate the IVA camera in the regular game
            }


#if DEBUG
            // debug hooks
            if (Input.GetKeyDown(KeyCode.Y))
            {
                // Utils.PrintAllCameras();
                // Utils.PrintAllLayers();
                Utils.PrintDebug();
                // Utils.PrintFonts();
                // Utils.PrintCollisionMatrix();
            }
#endif

            // keep track of whether we were running the HMD
            hmdIsRunningPrev = HmdIsRunning;
        }
示例#6
0
            void OnPreRender()
            {
                if (!gotPoses && HmdOn)
                {
                    Part hoveredPart = Mouse.HoveredPart;
                    if (hoveredPart != null)
                    {
                        hoveredPart.HighlightActive = false;
                    }

                    lock (KerbalVRPlugin.hmdRightEyeRenderTexture) lock (KerbalVRPlugin.hmdLeftEyeRenderTexture)
                        {
                            //check if active kerbal changed
                            if (CameraManager.Instance.currentCameraMode.Equals(CameraManager.CameraMode.IVA) && CameraManager.Instance.IVACameraActiveKerbalIndex != lastKerbalID)
                            {
                                //reenable last kerbal
                                activeKerbal.SetVisibleInPortrait(true);
                                activeKerbal.gameObject.active = true;

                                activeKerbal = CameraManager.Instance.IVACameraActiveKerbal;
                                lastKerbalID = CameraManager.Instance.IVACameraActiveKerbalIndex;

                                //deactivate curent kerbal
                                activeKerbal.SetVisibleInPortrait(false);
                                activeKerbal.gameObject.active = false;
                            }

                            gotPoses = true;

                            EVRCompositorError vrCompositorError = EVRCompositorError.None;

                            //get poses from VR api
                            vrSystem.GetDeviceToAbsoluteTrackingPose(ETrackingUniverseOrigin.TrackingUniverseSeated, predict, vrDevicePoses);
                            HmdMatrix34_t vrLeftEyeTransform  = vrSystem.GetEyeToHeadTransform(EVREye.Eye_Left);
                            HmdMatrix34_t vrRightEyeTransform = vrSystem.GetEyeToHeadTransform(EVREye.Eye_Right);
                            vrCompositorError      = vrCompositor.WaitGetPoses(vrRenderPoses, vrGamePoses);
                            RenderSlave.leftReady  = false;
                            RenderSlave.rightReady = false;
                            if (vrCompositorError != EVRCompositorError.None)
                            {
                                KerbalVRPlugin.warn("WaitGetPoses failed: " + vrCompositorError.ToString());
                            }

                            // convert SteamVR poses to Unity coordinates
                            hmdTransform         = new Utils.RigidTransform(vrDevicePoses[OpenVR.k_unTrackedDeviceIndex_Hmd].mDeviceToAbsoluteTracking);
                            hmdLeftEyeTransform  = new Utils.RigidTransform(vrLeftEyeTransform);
                            hmdRightEyeTransform = new Utils.RigidTransform(vrRightEyeTransform);
                            ctrlPoseLeft         = new Utils.RigidTransform(vrDevicePoses[ctrlIndexLeft].mDeviceToAbsoluteTracking);
                            ctrlPoseRight        = new Utils.RigidTransform(vrDevicePoses[ctrlIndexRight].mDeviceToAbsoluteTracking);


                            //calculate corect position acording to vessel orientation
                            hmdTransform.rot = (O_Interior.transform.rotation) * hmdTransform.rot;
                            hmdTransform.pos = (O_Interior.transform.rotation) * hmdTransform.pos + O_Interior.transform.position;

                            //shema:
                            //rotate Camera acording to Hmd rotation
                            //reset local position
                            //set new local position acording to eye position
                            //add position of hmd

                            //internal camera has no special transformations
                            camLeft_Interior.transform.localRotation = hmdTransform.rot;
                            camLeft_Interior.transform.localPosition = new Vector3(0f, 0f, 0f);
                            camLeft_Interior.transform.Translate(hmdLeftEyeTransform.pos);
                            camLeft_Interior.transform.localPosition += hmdTransform.pos;

                            camRight_Interior.transform.localRotation = hmdTransform.rot;
                            camRight_Interior.transform.localPosition = new Vector3(0f, 0f, 0f);
                            camRight_Interior.transform.Translate(hmdRightEyeTransform.pos);
                            camRight_Interior.transform.localPosition += hmdTransform.pos;

                            //rotations and positions for all following cameras are converted from internal to wolrd space:
                            camLeft_Near.transform.localRotation = InternalSpace.InternalToWorld(hmdTransform.rot);
                            camLeft_Near.transform.localPosition = new Vector3(0f, 0f, 0f);
                            camLeft_Near.transform.Translate(hmdLeftEyeTransform.pos);
                            camLeft_Near.transform.localPosition += InternalSpace.InternalToWorld(hmdTransform.pos);

                            camRight_Near.transform.localRotation = InternalSpace.InternalToWorld(hmdTransform.rot);
                            camRight_Near.transform.localPosition = new Vector3(0f, 0f, 0f);
                            camRight_Near.transform.Translate(hmdRightEyeTransform.pos);
                            camRight_Near.transform.localPosition += InternalSpace.InternalToWorld(hmdTransform.pos);

                            camLeft_Far.transform.localRotation = InternalSpace.InternalToWorld(hmdTransform.rot);
                            camLeft_Far.transform.localPosition = new Vector3(0f, 0f, 0f);
                            camLeft_Far.transform.Translate(hmdLeftEyeTransform.pos);
                            camLeft_Far.transform.localPosition += InternalSpace.InternalToWorld(hmdTransform.pos);

                            camRight_Far.transform.localRotation = InternalSpace.InternalToWorld(hmdTransform.rot);
                            camRight_Far.transform.localPosition = new Vector3(0f, 0f, 0f);
                            camRight_Far.transform.Translate(hmdRightEyeTransform.pos);
                            camRight_Far.transform.localPosition += InternalSpace.InternalToWorld(hmdTransform.pos);

                            //the sky and star Cameras are in ScaledSpace so the vectors have to be scaled down
                            leftSky.transform.localRotation = InternalSpace.InternalToWorld(hmdTransform.rot);
                            leftSky.transform.localPosition = new Vector3(0f, 0f, 0f);
                            leftSky.transform.Translate(hmdLeftEyeTransform.pos * ScaledSpace.InverseScaleFactor);
                            leftSky.transform.localPosition += (hmdTransform.pos * ScaledSpace.InverseScaleFactor);

                            rightSky.transform.localRotation = InternalSpace.InternalToWorld(hmdTransform.rot);
                            rightSky.transform.localPosition = new Vector3(0f, 0f, 0f);
                            rightSky.transform.Translate(hmdRightEyeTransform.pos * ScaledSpace.InverseScaleFactor);
                            rightSky.transform.localPosition += (hmdTransform.pos * ScaledSpace.InverseScaleFactor);

                            leftStars.transform.localRotation = InternalSpace.InternalToWorld(hmdTransform.rot);
                            leftStars.transform.localPosition = new Vector3(0f, 0f, 0f);
                            leftStars.transform.Translate(hmdLeftEyeTransform.pos * ScaledSpace.InverseScaleFactor);
                            leftStars.transform.localPosition += InternalSpace.InternalToWorld(hmdTransform.pos * ScaledSpace.InverseScaleFactor);

                            rightStars.transform.localRotation = InternalSpace.InternalToWorld(hmdTransform.rot);
                            rightStars.transform.localPosition = new Vector3(0f, 0f, 0f);
                            rightStars.transform.Translate(hmdRightEyeTransform.pos * ScaledSpace.InverseScaleFactor);
                            rightStars.transform.localPosition += InternalSpace.InternalToWorld(hmdTransform.pos * ScaledSpace.InverseScaleFactor);
                        }
                }
            }
示例#7
0
            void OnPreRender()
            {
                //   log("pre");
                if (!gotPoses && HmdOn)
                {
                    //   log("get");
                    lock (KerbalVRPlugin.hmdRightEyeRenderTexture) lock (KerbalVRPlugin.hmdLeftEyeRenderTexture)
                        {
                            //   log("getLOCK");
                            gotPoses = true;

                            EVRCompositorError vrCompositorError = EVRCompositorError.None;

                            vrSystem.GetDeviceToAbsoluteTrackingPose(ETrackingUniverseOrigin.TrackingUniverseSeated, predict, vrDevicePoses);
                            HmdMatrix34_t vrLeftEyeTransform  = vrSystem.GetEyeToHeadTransform(EVREye.Eye_Left);
                            HmdMatrix34_t vrRightEyeTransform = vrSystem.GetEyeToHeadTransform(EVREye.Eye_Right);
                            vrCompositorError      = vrCompositor.WaitGetPoses(vrRenderPoses, vrGamePoses);
                            RenderSlave.leftReady  = false;
                            RenderSlave.rightReady = false;
                            if (vrCompositorError != EVRCompositorError.None)
                            {
                                KerbalVRPlugin.warn("WaitGetPoses failed: " + vrCompositorError.ToString());
                            }

                            // convert SteamVR poses to Unity coordinates

                            hmdTransform         = new Utils.RigidTransform(vrDevicePoses[OpenVR.k_unTrackedDeviceIndex_Hmd].mDeviceToAbsoluteTracking);
                            hmdLeftEyeTransform  = new Utils.RigidTransform(vrLeftEyeTransform);
                            hmdRightEyeTransform = new Utils.RigidTransform(vrRightEyeTransform);
                            ctrlPoseLeft         = new Utils.RigidTransform(vrDevicePoses[ctrlIndexLeft].mDeviceToAbsoluteTracking);
                            ctrlPoseRight        = new Utils.RigidTransform(vrDevicePoses[ctrlIndexRight].mDeviceToAbsoluteTracking);
                            //     log(hmdTransform.pos.ToString());

                            // foreach (Camera cam in leftCameras)
                            {
                                //camLeft_Interior.transform.localRotation = hmdTransform.rot;
                                //camLeft_Interior.transform.localPosition = new Vector3(0f, 0f, 0f);
                                //camLeft_Interior.transform.Translate(hmdLeftEyeTransform.pos);
                                //camLeft_Interior.transform.localPosition += hmdTransform.pos;

                                //camRight_Interior.transform.localRotation = hmdTransform.rot;
                                //camRight_Interior.transform.localPosition = new Vector3(0f, 0f, 0f);
                                //camRight_Interior.transform.Translate(hmdRightEyeTransform.pos);
                                //camRight_Interior.transform.localPosition += hmdTransform.pos;


                                //right cam
                                camLeft_Near.transform.localRotation = hmdTransform.rot;
                                //camRight.transform.RotateAround(new Vector3(0, 0, 0), new Vector3(1, 0, 0), -90);

                                // translate the camera to match the position of the left eye, from origin
                                camLeft_Near.transform.localPosition = new Vector3(0f, 0f, 0f);
                                camLeft_Near.transform.Translate(hmdLeftEyeTransform.pos);

                                // translate the camera to match the position of the HMD
                                camLeft_Near.transform.localPosition += hmdTransform.pos;

                                //right cam
                                camRight_Near.transform.localRotation = hmdTransform.rot;
                                //camRight.transform.RotateAround(new Vector3(0, 0, 0), new Vector3(1, 0, 0), -90);

                                // translate the camera to match the position of the left eye, from origin
                                camRight_Near.transform.localPosition = new Vector3(0f, 0f, 0f);
                                camRight_Near.transform.Translate(hmdRightEyeTransform.pos);

                                // translate the camera to match the position of the HMD
                                camRight_Near.transform.localPosition += hmdTransform.pos;

                                camLeft_Far.transform.localRotation = hmdTransform.rot;
                                //camRight.transform.RotateAround(new Vector3(0, 0, 0), new Vector3(1, 0, 0), -90);

                                // translate the camera to match the position of the left eye, from origin
                                camLeft_Far.transform.localPosition = new Vector3(0f, 0f, 0f);
                                camLeft_Far.transform.Translate(hmdLeftEyeTransform.pos);

                                // translate the camera to match the position of the HMD
                                camLeft_Far.transform.localPosition += hmdTransform.pos;

                                camRight_Far.transform.localRotation = hmdTransform.rot;
                                //camRight.transform.RotateAround(new Vector3(0, 0, 0), new Vector3(1, 0, 0), -90);

                                // translate the camera to match the position of the left eye, from origin
                                camRight_Far.transform.localPosition = new Vector3(0f, 0f, 0f);
                                camRight_Far.transform.Translate(hmdRightEyeTransform.pos);

                                // translate the camera to match the position of the HMD
                                camRight_Far.transform.localPosition += hmdTransform.pos;


                                leftSky.transform.localRotation = hmdTransform.rot;
                                leftSky.transform.localPosition = new Vector3(0f, 0f, 0f);
                                leftSky.transform.Translate(hmdLeftEyeTransform.pos);
                                leftSky.transform.localPosition += hmdTransform.pos;

                                rightSky.transform.localRotation = hmdTransform.rot;
                                rightSky.transform.localPosition = new Vector3(0f, 0f, 0f);
                                rightSky.transform.Translate(hmdRightEyeTransform.pos);
                                rightSky.transform.localPosition += hmdTransform.pos;

                                leftStars.transform.localRotation = hmdTransform.rot;
                                leftStars.transform.localPosition = new Vector3(0f, 0f, 0f);
                                leftStars.transform.Translate(hmdLeftEyeTransform.pos);
                                leftStars.transform.localPosition += hmdTransform.pos;

                                rightStars.transform.localRotation = hmdTransform.rot;
                                rightStars.transform.localPosition = new Vector3(0f, 0f, 0f);
                                rightStars.transform.Translate(hmdRightEyeTransform.pos);
                                rightStars.transform.localPosition += hmdTransform.pos;

                                //GalaxyCubeControl.Instance.transform.rotation = hmdTransform

                                //  sky.transform.position = ScaledSpace.Instance.transform.position;
                                //  sky.transform.rotation = hmdTransform.rot;
                                //  sky.farClipPlane = 3.0e7f;
                                //sky.cullingMask = (1 << 10) | (1 << 23);

                                /*  foreach(var go in interiorModelList)
                                 * {
                                 *    // = activeVessel.vesselTransform;// .rootPart.transform.rotation;
                                 * }*/


                                lastTransform = hmdTransform;
                            }
                        }
                }
            }
示例#8
0
        /// <summary>
        /// Overrides the LateUpdate method, called every frame after all objects' Update.
        /// </summary>
        void LateUpdate()
        {
            // dispatch any OpenVR events
            if (hmdIsInitialized)
            {
                DispatchOpenVREvents();
            }

            // check if the current scene allows VR
            HmdIsAllowed = Scene.SceneAllowsVR();

            // check if we are running the HMD
            HmdIsRunning = HmdIsAllowed && hmdIsInitialized && HmdIsEnabled;

            // perform regular updates if HMD is initialized
            if (HmdIsRunning)
            {
                EVRCompositorError vrCompositorError = EVRCompositorError.None;

                try {
                    // TODO: investigate if we should really be capturing poses in LateUpdate

                    // get latest device poses
                    float secondsToPhotons = Utils.CalculatePredictedSecondsToPhotons();
                    OpenVR.System.GetDeviceToAbsoluteTrackingPose(Scene.TrackingSpace, secondsToPhotons, devicePoses);
                    SteamVR_Events.NewPoses.Send(devicePoses);

                    HmdMatrix34_t vrLeftEyeTransform  = OpenVR.System.GetEyeToHeadTransform(EVREye.Eye_Left);
                    HmdMatrix34_t vrRightEyeTransform = OpenVR.System.GetEyeToHeadTransform(EVREye.Eye_Right);
                    vrCompositorError = OpenVR.Compositor.WaitGetPoses(renderPoses, gamePoses);

                    if (vrCompositorError != EVRCompositorError.None)
                    {
                        throw new Exception("WaitGetPoses failed: (" + (int)vrCompositorError + ") " + vrCompositorError.ToString());
                    }

                    // convert SteamVR poses to Unity coordinates
                    var hmdTransform = new SteamVR_Utils.RigidTransform(devicePoses[OpenVR.k_unTrackedDeviceIndex_Hmd].mDeviceToAbsoluteTracking);
                    SteamVR_Utils.RigidTransform[] hmdEyeTransform = new SteamVR_Utils.RigidTransform[2];
                    hmdEyeTransform[0] = new SteamVR_Utils.RigidTransform(vrLeftEyeTransform);
                    hmdEyeTransform[1] = new SteamVR_Utils.RigidTransform(vrRightEyeTransform);

                    // render each eye
                    for (int i = 0; i < 2; i++)
                    {
                        RenderHmdCameras(
                            (EVREye)i,
                            hmdTransform,
                            hmdEyeTransform[i],
                            i == 0 ? leftEyeRenderTextures[qualityLevel]:rightEyeRenderTextures[qualityLevel],
                            renderTextures[qualityLevel]);
                    }

                    OpenVR.Compositor.PostPresentHandoff();
                } catch (Exception e) {
                    Utils.LogError(e);
                    HmdIsEnabled = false;
                    HmdIsRunning = false;
                }

                // disable highlighting of parts due to mouse
                // TODO: there needs to be a better way to do this. this affects the Part permanently
                Part hoveredPart = Mouse.HoveredPart;
                if (hoveredPart != null)
                {
                    hoveredPart.HighlightActive  = false;
                    hoveredPart.highlightColor.a = 0f;
                }
            }

            // reset cameras when HMD is turned off
            if (!HmdIsRunning && hmdIsRunningPrev)
            {
                Utils.Log("HMD is now off, resetting cameras...");
                Scene.CloseScene();
            }


#if DEBUG
            if (Input.GetKeyDown(KeyCode.Y))
            {
                Utils.PrintAllCameras();
                Utils.PrintAllLayers();
                Utils.PrintDebug();
            }
#endif

            hmdIsRunningPrev = HmdIsRunning;
        }