Esempio n. 1
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);
                        }
                }
            }
Esempio n. 2
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;
                            }
                        }
                }
            }