bool isRightHand; //Is this sword in the right hand? void Awake() { if (!UnityEngine.XR.XRSettings.enabled) { Destroy(this); return; } //Get a reference to the animator component on the sword child object anim = GetComponentInChildren <Animator> (); //Determine if this is the right hand or not by examining the parent VRObjectTracking script UnityEngine.XR.XRNode node = transform.parent.GetComponent <VRObjectTracking> ().node; if (node == UnityEngine.XR.XRNode.RightHand) { isRightHand = true; } }
/// <summary> /// Use this function to set texture and texNativePtr when app is running /// GetNativeTexturePtr is a slow behavior, the value should be pre-cached /// </summary> public void OverrideOverlayTextureInfo(Texture srcTexture, IntPtr nativePtr, UnityEngine.XR.XRNode node) { int index = (node == UnityEngine.XR.XRNode.RightEye) ? 1 : 0; if (textures.Length <= index) { return; } stageCount = 3; CreateLayerTextures(true, true, new OVRPlugin.Sizei() { w = srcTexture.width, h = srcTexture.height }, false); textures[index] = srcTexture; layerTextures[index].appTexture = srcTexture; layerTextures[index].appTexturePtr = nativePtr; }
void Start() { //Cache and setup our rigidbody rb = GetComponent<Rigidbody>(); rb.isKinematic = true; //Cache our node switch (target) { case VRTarget.Head: node = UnityEngine.XR.XRNode.Head; break; case VRTarget.LeftHand: node = UnityEngine.XR.XRNode.LeftHand; break; case VRTarget.RightHand: node = UnityEngine.XR.XRNode.RightHand; break; } }
private static bool GetUnityXRNodeStateQuaternion(Node nodeType, NodeStatePropertyType propertyType, out Quaternion retQuat) { retQuat = Quaternion.identity; NodeState requestedNodeState = default(NodeState); if (!ValidateProperty(nodeType, ref requestedNodeState)) { return(false); } if (propertyType == NodeStatePropertyType.Orientation) { if (requestedNodeState.TryGetRotation(out retQuat)) { return(true); } } return(false); }
Quaternion GetStereoRotation(Camera camera, UnityEngine.XR.XRNode node) { if (camera.stereoEnabled) { Quaternion localRotation = UnityEngine.XR.InputTracking.GetLocalRotation(node); Transform parent = camera.transform.parent; if (parent) { return(parent.rotation * localRotation); } else { return(localRotation); } } else { // Otherwise, we can just return the camera's position return(camera.transform.rotation); } }
public static bool GetNodeStatePropertyQuaternion(Node nodeType, NodeStatePropertyType propertyType, OVRPlugin.Node ovrpNodeType, OVRPlugin.Step stepType, out Quaternion retQuat) { retQuat = Quaternion.identity; switch (propertyType) { case NodeStatePropertyType.Orientation: if (OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus) { retQuat = OVRPlugin.GetNodePose(ovrpNodeType, stepType).ToOVRPose().orientation; return(true); } #if UNITY_2017_1_OR_NEWER if (GetUnityXRNodeStateQuaternion(nodeType, NodeStatePropertyType.Orientation, out retQuat)) { return(true); } #endif break; } return(false); }
/// <summary> /// Gets the node of the AR tracking system. /// </summary> /// <returns>The AR camera node.</returns> /// <param name="node">Node.</param> public static Transform GetNode(UnityEngine.XR.XRNode node) { switch (node) { case UnityEngine.XR.XRNode.LeftEye: if (leftEye) { return(leftEye); } else { leftEye = ARCamera.Singleton.transform.Find("LeftEye"); return(leftEye); } case UnityEngine.XR.XRNode.RightEye: if (rightEye) { return(rightEye); } else { rightEye = ARCamera.Singleton.transform.Find("RightEye"); return(rightEye); } case UnityEngine.XR.XRNode.CenterEye: case UnityEngine.XR.XRNode.Head: return(ARCamera.Singleton.transform); case UnityEngine.XR.XRNode.TrackingReference: return(TagTracker.TrackingAnchor); default: Debug.LogErrorFormat("Unknown node: {0}", node.ToString()); return(null); } }
// Application: private void Update() { if (Camera.main == null) { return; } var leftEyePos = GetEyePosition(UnityEngine.XR.XRNode.LeftEye); var rightEyePos = GetEyePosition(UnityEngine.XR.XRNode.RightEye); var leftEyeProx = GetEyeProximity(leftEyePos); var rightEyeProx = GetEyeProximity(rightEyePos); activeEye = leftEyeProx > rightEyeProx ? UnityEngine.XR.XRNode.LeftEye : UnityEngine.XR.XRNode.RightEye; float eyeProxmity = activeEye == UnityEngine.XR.XRNode.LeftEye ? leftEyeProx : rightEyeProx; //Debug.Log("X IN=" + eyeProxmity); // Debug and print the input value X for the linear equation. if (eyeProxmity <= 5 + eyeMaxDistance) { m_camera.enabled = true; eyeProxmity = GetLinearResult(eyeProxmity); //Debug.Log("Y OUT=" + eyeProxmity); // Debug and print the output value Y from the linear equation. ComputeDeltas(leftEyePos, activeEye == UnityEngine.XR.XRNode.LeftEye); DoLensEffect(vignetteLeft, leftEyeProx); ComputeDeltas(rightEyePos, activeEye == UnityEngine.XR.XRNode.RightEye); DoLensEffect(vignetteRight, rightEyeProx); } else { m_camera.transform.localPosition = cameraInitialPosition; // Move the camera to align with the camera eye and mimic a lens refraction effect. m_camera.enabled = false; } }
private bool enableReticule; // Will be set true if there is both a texture and material present. //(De)Initialization: private void Start() { post = GetComponent <PostProcessingBehaviour> (); post.profile.vignette.enabled = true; vignette = post.profile.vignette.settings; enableReticule = reticleMaterial != null && reticleTexture != null; // Determine which VRNode to use based on selected eye for the attached camera. var isDefaultEye = EyeSelect == EyeSelector.left; eye = isDefaultEye ? UnityEngine.XR.XRNode.LeftEye : UnityEngine.XR.XRNode.RightEye; // Precompute the constants for the linear equation: // m = (PT2_Y - PT1_Y ) / ( PT2_X - PT1_X ) slopeProx = (1.0f - eyeMinIntensity) / (eyeMaxDistance - eyeMinDistance); // Precomputed slope: m = (y2-y1) / (x2-x1) // b = PT1_Y - ( m * PT1_X ) yInterceptProx = eyeMinIntensity - (slopeProx * eyeMinDistance); // Given the slope, substitue a value for PT1 and solve for Y. //Debug and print the constants for the linear equation: //Debug.Log("y2=" + 1 + " y1=" + eyeMinIntensity + " x2=" + eyeMaxDistance + " x1=" + eyeMinDistance); //Debug.Log("m=" + slopeProx + " b=" + yInterceptProx); }
public static void Update() { #if UNITY_EDITOR || !UNITY_ANDROID if (simulateController != OVRInput.Controller.LTrackedRemote && simulateController != OVRInput.Controller.RTrackedRemote && simulateController != OVRInput.Controller.LTouch && simulateController != OVRInput.Controller.RTouch) { // Default to a right handed controller if not set! simulateController = OVRInput.Controller.RTrackedRemote; } UnityEngine.XR.XRNode handNode = (simulateController == OVRInput.Controller.LTouch || simulateController == OVRInput.Controller.LTrackedRemote) ? UnityEngine.XR.XRNode.LeftHand : UnityEngine.XR.XRNode.RightHand; OVRPose remotePoseWithoutPosition = new OVRPose(); remotePoseWithoutPosition.orientation = ConvertHandedness(UnityEngine.XR.InputTracking.GetLocalRotation(handNode)); remotePoseWithoutPosition.position = new Vector3(0.0f, 0.0f, 0.0f); OVRPose headPoseWithoutPosition = new OVRPose(); headPoseWithoutPosition.orientation = ConvertHandedness(UnityEngine.XR.InputTracking.GetLocalRotation(UnityEngine.XR.XRNode.Head)); #if UNITY_ANDROID && !UNITY_EDITOR // Running on Gear VR, the head position is not needed headPoseWithoutPosition.position = new Vector3(0.0f, 0.0f, 0.0f); #else // Running on Rift, the head position is needed headPoseWithoutPosition.position = ConvertHandedness(UnityEngine.XR.InputTracking.GetLocalPosition(UnityEngine.XR.XRNode.Head)); #endif OVRPose remotePose = new OVRPose(); ovrArmModel.Update(headPoseWithoutPosition, remotePoseWithoutPosition, handNode, OVRPlugin.shouldRecenter, out remotePose); localRotation = ConvertHandedness(remotePose.orientation); localPosition = ConvertHandedness(remotePose.position); #endif if (BackClicked && OnBackClicked != null) { OnBackClicked(); } if (lastTriggerState && !TriggerDown && OnTriggerUp != null) { OnTriggerUp(); } else if (!lastTriggerState && TriggerDown && OnTriggerDown != null) { OnTriggerDown(); } if (lastTouchpadState && !TouchpadDown && OnTouchpadUp != null) { OnTouchpadUp(); } else if (!lastTriggerState && TouchpadDown && OnTouchpadDown != null) { OnTouchpadDown(); } if (TouchpadTouched && OnTouch != null) { OnTouch(TouchpadPosition); } lastTriggerState = TriggerDown; lastTouchpadState = TouchpadDown; }
private void ConfigureEyeDesc(UnityEngine.XR.XRNode eye)
/// <summary> /// Gets the resolution and field of view for the given eye. /// </summary> #if UNITY_2017_2_OR_NEWER public EyeRenderDesc GetEyeRenderDesc(UnityEngine.XR.XRNode eye)
// rotates entire player body so that this becomes true (but doesn't change position) public void SetBodyPartRotation(UnityEngine.XR.XRNode bodyPart, Vector3 eulerAngles) { SetBodyPartRotation(bodyPart, Quaternion.Euler(eulerAngles)); }
// if you want to change rotation you should probably only use this to keep the headset tilted the same way (y axis is up, so 0 is forward along x axis, 180 is backwards along x axis, 90 and 270 are along z axis, etc.) public void SetBodyPartYRotation(UnityEngine.XR.XRNode bodyPart, float yRot) { Vector3 curRot = GetBodyPartRotation(bodyPart).eulerAngles; SetBodyPartRotation(bodyPart, new Vector3(curRot.x, yRot, curRot.z)); }
// moves entire player body so that this becomes true public void SetBodyPartPosition(UnityEngine.XR.XRNode bodyPart, Vector3 pos) { Vector3 bodyPartPos = vrCameraHolder.localToWorldMatrix.MultiplyPoint(UnityEngine.XR.InputTracking.GetLocalPosition(bodyPart)); vrCameraHolder.position += pos - bodyPartPos; }
public void Update(OVRPose headPose, OVRPose remotePose, UnityEngine.XR.XRNode handNode, bool recenteredController, out OVRPose outPose) { float eyeYaw; float eyePitch; float eyeRoll; GetYawPitchRoll(headPose.orientation, out eyeYaw, out eyePitch, out eyeRoll); TorsoYaw = ConstrainTorsoYaw(headPose.orientation, TorsoYaw); if (recenteredController) { TorsoYaw = eyeYaw; } OVRPose FootPose = MakePose(Quaternion.AngleAxis(TorsoYaw * Mathf.Rad2Deg, new Vector3(0.0f, 1.0f, 0.0f)), new Vector3(headPose.position.x, headPose.position.y, headPose.position.z)); float handSign = (handNode == UnityEngine.XR.XRNode.LeftHand) ? -1.0f : 1.0f; OVRPose pose = Skeleton[ClavicleJointIdx].Key; pose.position.x = Mathf.Abs(Skeleton[ClavicleJointIdx].Key.position.x) * handSign; Skeleton[ClavicleJointIdx] = new OvrJoint(pose, Skeleton[ClavicleJointIdx].Value); List <OvrJointMod> jointMods = new List <OvrJointMod>(); Quaternion remoteRot = new Quaternion(remotePose.orientation.x, remotePose.orientation.y, remotePose.orientation.z, remotePose.orientation.w); float MAX_ROLL = Mathf.PI * 0.5f; float remoteYaw = 0.0f; float remotePitch = 0.0f; float remoteRoll = 0.0f; GetYawPitchRoll(remoteRot, out remoteYaw, out remotePitch, out remoteRoll); if (remoteRoll >= -MAX_ROLL && remoteRoll <= MAX_ROLL) { LastUnclampedRoll = remoteRoll; } else { remoteRoll = LastUnclampedRoll; } remoteRot = Quaternion.AngleAxis(remoteYaw * Mathf.Rad2Deg, new Vector3(0, 1, 0)) * Quaternion.AngleAxis(remotePitch * Mathf.Rad2Deg, new Vector3(1, 0, 0)) * Quaternion.AngleAxis(remoteRoll * Mathf.Rad2Deg, new Vector3(0, 0, 1)); Quaternion localRemoteRot = Quaternion.Inverse(FootPose.orientation) * (remoteRot); Quaternion shoulderRot = Quaternion.Slerp(Quaternion.identity, localRemoteRot, 0.0f); Quaternion elbowRot = Quaternion.Slerp(Quaternion.identity, localRemoteRot, 0.6f); Quaternion wristRot = Quaternion.Slerp(Quaternion.identity, localRemoteRot, 0.4f); jointMods.Add(new OvrJointMod(ShoulderJointIdx, MakePose(shoulderRot, new Vector3(0.0f, 0.0f, 0.0f)))); jointMods.Add(new OvrJointMod(ElbowJointIdx, MakePose(elbowRot, new Vector3(0.0f, 0.0f, 0.0f)))); jointMods.Add(new OvrJointMod(WristJointIdx, MakePose(wristRot, new Vector3(0.0f, 0.0f, 0.0f)))); Transform(FootPose, Skeleton, jointMods, ref TransformedJoints); outPose = MakePose(new Quaternion(remotePose.orientation.x, remotePose.orientation.y, remotePose.orientation.z, remotePose.orientation.w), TransformedJoints[TransformedJoints.Count - 1].Key.position); }
// Gets the body part position in world space public Vector3 GetBodyPartPosition(UnityEngine.XR.XRNode bodyPart) { return(vrCameraHolder.localToWorldMatrix.MultiplyPoint(UnityEngine.XR.InputTracking.GetLocalPosition(bodyPart))); }
/// <summary> /// Gets the resolution and field of view for the given eye. /// </summary> public EyeRenderDesc GetEyeRenderDesc(UnityEngine.XR.XRNode eye) { return(eyeDescs[(int)eye]); }
private Vector3 GetEyePosition(UnityEngine.XR.XRNode eye) { return(Camera.main.transform.parent.TransformPoint(UnityEngine.XR.InputTracking.GetLocalPosition(eye))); }
OpenvrJoystickFrame UpdateNode(UnityEngine.XR.XRNode Node, string Name, KeyCode TriggerButton, KeyCode TouchpadButton, KeyCode TouchpadClickButton, KeyCode AppButton, string AxisX, string AxisY)
public Vector3 GetNodeStateProperty(Node nodeType, NodeStatePropertyType propertyType) { List <NodeState> nodeStateList = new List <NodeState>(); InputTracking.GetNodeStates(nodeStateList); if (nodeStateList.Count == 0) { return(Vector3.zero); } bool nodeStateFound = false; NodeState requestedNodeState = nodeStateList[0]; for (int i = 0; i < nodeStateList.Count; i++) { if (nodeStateList[i].nodeType == nodeType) { requestedNodeState = nodeStateList[i]; nodeStateFound = true; break; } } if (!nodeStateFound) { return(Vector3.zero); } Vector3 retVec; if (propertyType == NodeStatePropertyType.Acceleration) { if (requestedNodeState.TryGetAcceleration(out retVec)) { return(retVec); } } else if (propertyType == NodeStatePropertyType.AngularAcceleration) { #if UNITY_2017_2_OR_NEWER if (requestedNodeState.TryGetAngularAcceleration(out retVec)) { retVec = retVec * Mathf.Rad2Deg; return(retVec); } #endif } else if (propertyType == NodeStatePropertyType.Velocity) { if (requestedNodeState.TryGetVelocity(out retVec)) { return(retVec); } } else if (propertyType == NodeStatePropertyType.AngularVelocity) { #if UNITY_2017_2_OR_NEWER if (requestedNodeState.TryGetAngularVelocity(out retVec)) { retVec = retVec * Mathf.Rad2Deg; return(retVec); } #endif } return(Vector3.zero); }
public static bool GetNodeStatePropertyVector3(Node nodeType, NodeStatePropertyType propertyType, OVRPlugin.Node ovrpNodeType, OVRPlugin.Step stepType, out Vector3 retVec) { retVec = Vector3.zero; switch (propertyType) { case NodeStatePropertyType.Acceleration: if (OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus) { retVec = OVRPlugin.GetNodeAcceleration(ovrpNodeType, stepType).FromFlippedZVector3f(); return(true); } #if UNITY_2017_1_OR_NEWER if (GetUnityXRNodeStateVector3(nodeType, NodeStatePropertyType.Acceleration, out retVec)) { return(true); } #endif break; case NodeStatePropertyType.AngularAcceleration: if (OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus) { retVec = OVRPlugin.GetNodeAngularAcceleration(ovrpNodeType, stepType).FromFlippedZVector3f(); return(true); } #if UNITY_2017_2_OR_NEWER if (GetUnityXRNodeStateVector3(nodeType, NodeStatePropertyType.AngularAcceleration, out retVec)) { return(true); } #endif break; case NodeStatePropertyType.Velocity: if (OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus) { retVec = OVRPlugin.GetNodeVelocity(ovrpNodeType, stepType).FromFlippedZVector3f(); return(true); } #if UNITY_2017_1_OR_NEWER if (GetUnityXRNodeStateVector3(nodeType, NodeStatePropertyType.Velocity, out retVec)) { return(true); } #endif break; case NodeStatePropertyType.AngularVelocity: if (OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus) { retVec = OVRPlugin.GetNodeAngularVelocity(ovrpNodeType, stepType).FromFlippedZVector3f(); return(true); } #if UNITY_2017_2_OR_NEWER if (GetUnityXRNodeStateVector3(nodeType, NodeStatePropertyType.AngularVelocity, out retVec)) { return(true); } #endif break; case NodeStatePropertyType.Position: if (OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus) { retVec = OVRPlugin.GetNodePose(ovrpNodeType, stepType).ToOVRPose().position; return(true); } #if UNITY_2017_1_OR_NEWER if (GetUnityXRNodeStateVector3(nodeType, NodeStatePropertyType.Position, out retVec)) { return(true); } #endif break; } return(false); }
// Gets the body part rotation in world space public Quaternion GetBodyPartRotation(UnityEngine.XR.XRNode bodyPart) { return(LocalToWorldRotation(vrCameraHolder, UnityEngine.XR.InputTracking.GetLocalRotation(bodyPart))); }
public virtual bool GetNode(out UnityEngine.XR.XRNode node) { node = UnityEngine.XR.XRNode.LeftEye; return(false); }
void UpdatePosition(Transform hand, UnityEngine.XR.XRNode node) { hand.position = UnityEngine.XR.InputTracking.GetLocalPosition(node); hand.rotation = UnityEngine.XR.InputTracking.GetLocalRotation(node); }
/// <summary> /// Use this function to set texture and texNativePtr when app is running /// GetNativeTexturePtr is a slow behavior, the value should be pre-cached /// </summary> #if UNITY_2017_2_OR_NEWER public void OverrideOverlayTextureInfo(Texture srcTexture, IntPtr nativePtr, UnityEngine.XR.XRNode node)
private Transform ConfigureEyeAnchor(Transform root, UnityEngine.XR.XRNode eye)