/// <summary> /// Fixes the offset and make the correction if necessary /// </summary> /// <returns>The offset.</returns> IEnumerator FixOffset() { while (true) { if (VRTracker.Manager.VRT_Manager.Instance != null) { if (tag == null) { tag = VRTracker.Manager.VRT_Manager.Instance.GetHeadsetTag(); } if (tag != null) { UpdateOrientationData(); if (ShoudlBlink()) { t = timeToReachTarget; if (Blink != null) { Blink(); } } else { t = 0; } } yield return(new WaitForSeconds(waitTimeBeforeVerification)); } else { yield return(new WaitForSeconds(0.5f)); } } }
/// <summary> /// Adds the tag to the tags list needed for a player /// </summary> /// <param name="tag">Tag.</param> public void AddTag(VRTracker.Manager.VRT_Tag tag) { tags.Add(tag); if (OnAddTag != null) { OnAddTag(); } }
/// <summary> /// Show the message to tell which tag is going to be paired /// </summary> /// <returns>The menu.</returns> /// <param name="fader">Fader.</param> /// <param name="tag">Tag.</param> /// <param name="delayToPressButton">Delay to press button.</param> private IEnumerator ShowMenu(UIFader fader, VRTracker.Manager.VRT_Tag tag, float delayToPressButton) { yield return(StartCoroutine(fader.InteruptAndFadeIn())); yield return(StartCoroutine(tag.WaitForAssignation(delayToPressButton))); if (tag.IDisAssigned) { transform.GetComponent <AudioSource>().Play(); } yield return(StartCoroutine(fader.InteruptAndFadeOut())); }
IEnumerator FixOffset2() { while (true) { if (VRTracker.Manager.VRT_Manager.Instance != null) { if (tag == null) { tag = VRTracker.Manager.VRT_Manager.Instance.GetHeadsetTag(); } if (tag != null) { if (tag != null && tag.trackedEndpoints.ContainsKey((0))) { Vector3 tagRotation = UnmultiplyQuaternion(tag.trackedEndpoints[0].getOrientation()); Vector3 cameraRotation = UnmultiplyQuaternion(camera.transform.localRotation); if (GetShortestAngle(newRotation.y, tagRotation.y - cameraRotation.y) > errorOffset) { if (offsetBuffer.Size > 0) { foreach (float f in offsetBuffer) { offsetBuffer.PopFront(); } } t = timeToReachTarget; if (Blink != null) { Blink(); } } else { t = 0; } offsetBuffer.PushFront(tagRotation.y - cameraRotation.y); newRotation.y = AngleAvergage(offsetBuffer); previousOffset = destinationOffset; destinationOffset = Quaternion.Euler(newRotation); } } yield return(new WaitForSeconds(waitTimeBeforeVerification)); } else { yield return(new WaitForSeconds(0.5f)); } } }
/// <summary> /// Resets the orientation and fade a blink to the user /// </summary> public virtual void ResetOrientation() { VRTracker.Manager.VRT_Tag headTag = VRTracker.Manager.VRT_Manager.Instance.GetTag(VRTracker.Manager.VRT_Tag.TagType.Head); VRTracker.Manager.VRT_Tag gunTag = VRTracker.Manager.VRT_Manager.Instance.GetTag(VRTracker.Manager.VRT_Tag.TagType.Gun); VRTracker.VRT_SixDofOffset tagOffsetHead = null; VRTracker.VRT_SixDofOffset tagOffsetGun = null; if (headTag != null) { tagOffsetHead = headTag.GetComponent <VRTracker.VRT_SixDofOffset>(); } if (Blink != null) { Blink(); } }
/*[Tooltip("The VRTK Headset Fade script to use when fading the headset. If this is left blank then the script will need to be applied to the same GameObject.")] * public VRTK.VRTK_HeadsetFade headsetFade; */ void Start() { offsetBuffer = new CircularBuffer <float>(20); // Don't use when XR device is not connected if (!XRDevice.isPresent) { enabled = false; return; } if (networkIdentity == null) { networkIdentity = GetComponentInParent <NetworkIdentity>(); } newRotation = Vector3.zero; if (tag == null && VRTracker.Manager.VRT_Manager.Instance != null) { tag = VRTracker.Manager.VRT_Manager.Instance.GetHeadsetTag(); } if (networkIdentity != null && !networkIdentity.isLocalPlayer) { gameObject.SetActive(false); this.enabled = false; return; } VRStandardAssets.Utils.VRCameraFade fader = gameObject.GetComponentInChildren <VRStandardAssets.Utils.VRCameraFade>(); if (fader != null) { Blink += fader.FadeBlink; if (tag.trackedEndpoints[0].filter != null && tag.trackedEndpoints[0].blinkOnJump) { tag.trackedEndpoints[0].filter.Blink += fader.FadeBlink; } } previousOffset = Quaternion.identity; destinationOffset = Quaternion.identity; // ResetOrientation(); StartCoroutine(FixOffset2()); }
IEnumerator FixOrientationOffset() { while (true) { if (VRTracker.Manager.VRT_Manager.Instance != null) { if (tagToFollow == null) { tagToFollow = VRTracker.Manager.VRT_Manager.Instance.GetTag(VRTracker.Manager.VRT_Tag.TagType.Head); } if (tagToFollow != null) { //TODO: Re enable /* * Vector3 tagRotation = UnmultiplyQuaternion(Quaternion.Euler(tagToFollow.getOrientation())); * Vector3 cameraRotation = UnmultiplyQuaternion(playerCamera.transform.localRotation); * float yRotation = tagRotation.y - cameraRotation.y; * * * float offsetY = Mathf.Abs(destinationOrientationOffset.eulerAngles.y - yRotation) % 360; * offsetY = offsetY > 180.0f ? offsetY - 360 : offsetY; * * previousOrientationOffset = destinationOrientationOffset; * destinationOrientationOffset = Quaternion.Euler(new Vector3(0, yRotation, 0)); * if (Mathf.Abs(offsetY) > minRotationOffsetToBLink) * { * tRotation = timeToReachTargetRotation; * StartCoroutine(Blink()); * } * else * tRotation = 0; */ } yield return(new WaitForSeconds(20)); } else { yield return(new WaitForSeconds(0.5f)); } } }
// Use this for initialization void Start() { tagPositionWithEyeOffset = Vector3.zero; if (VRTracker.Manager.VRT_Manager.Instance != null) { tagToFollow = VRTracker.Manager.VRT_Manager.Instance.GetTag(VRTracker.Manager.VRT_Tag.TagType.Head); if (tagToFollow && tagToFollow is VRT_TagSimulator) { simulatorTag = true; } } if (playerCamera == null) { playerCamera = GetComponentInChildren <Camera>(); } previousOrientationOffset = Quaternion.Euler(Vector3.zero); destinationOrientationOffset = Quaternion.Euler(Vector3.zero); StartCoroutine(FixOrientationOffset()); }
// Update is called once per frame void Update() { if (NetIdent != null && !NetIdent.isLocalPlayer) { return; } if (!tagFound && VRTracker.Manager.VRT_Manager.Instance != null) { tagToFollow = VRTracker.Manager.VRT_Manager.Instance.GetTag(tagTypeToFollow); if (tagToFollow != null && tagToFollow.trackedEndpoints.ContainsKey((int)endpoint)) { tagFound = true; tagToFollow.trackedEndpoints[(int)endpoint].positionUpdateHandler += UpdatePosition; tagToFollow.trackedEndpoints[(int)endpoint].orientationUpdateHandler += UpdateOrientation; OnTagJoinEvent(EventArgs.Empty); } } else if (simulatorTag) { Debug.LogWarning("Simulator Tag is not implement in this version"); } }
// Use this for initialization void Start() { if (GetComponentsInParent <NetworkIdentity>().Length > 0) { NetIdent = GetComponentsInParent <NetworkIdentity>()[0]; if (NetIdent != null && !NetIdent.isLocalPlayer) { enabled = false; return; } } originalPosition = transform.position; originalRotation = transform.rotation.eulerAngles; if (VRTracker.Manager.VRT_Manager.Instance != null) { tagToFollow = VRTracker.Manager.VRT_Manager.Instance.GetTag(tagTypeToFollow); if (tagToFollow && tagToFollow is VRT_TagSimulator) { simulatorTag = true; } if (tagToFollow != null && tagToFollow.trackedEndpoints.ContainsKey((int)endpoint)) { tagFound = true; tagToFollow.trackedEndpoints[(int)endpoint].positionUpdateHandler += UpdatePosition; tagToFollow.trackedEndpoints[(int)endpoint].orientationUpdateHandler += UpdateOrientation; OnTagJoinEvent(EventArgs.Empty); } } else { Debug.LogError("No VR Tracker script found in current Scene. Import VRTracker prefab"); } }
/// <summary> /// Removes the tag from the tag list /// Should occur on a tag deconnection /// </summary> /// <param name="tag">Tag.</param> public void RemoveTag(VRTracker.Manager.VRT_Tag tag) { tags.Remove(tag); }
/// <summary> /// Raises the tag data event. /// Handle the binary data received from the tag /// </summary> /// <param name="data">Data.</param> public void OnTagData(byte[] data) { //Debug.Log("UDP size " + data.Length + " Timestamp " + System.DateTime.Now.Millisecond); bool shouldCheckMac = true; // In case of position data, MAC should be checked at the end of each sensor data as multiple Tag are in the same message int i = 0; byte[] uid = { data[i], data[i + 1] }; VRTracker.Manager.VRT_Tag currentTag = FindTag(uid); bool tagFound = false; while (i < data.Length) { if (shouldCheckMac) { uid[0] = data[i]; uid[1] = data[i + 1]; currentTag = FindTag(uid); tagFound = currentTag == null ? false : true; i = i + 2; // Set i to sensor type } switch (data[i] >> 4 & 0x0F) { // Position case 0: { shouldCheckMac = true; int sensorCount = data[i] & 0x0F; byte length = data[i + 1]; double timestamp = ((double)((data[i + 2] << 8) + data[i + 3]) / 10000.0d); bool isNeg = (data[i + 4] & 0x80) == 0x80 ? true : false; float x = isNeg ? (((data[i + 4] & 0x7F) << 8) + data[i + 5]) - 32768 : (((data[i + 4] & 0x7F) << 8) + data[i + 5]); isNeg = (data[i + 6] & 0x80) == 0x80 ? true : false; float y = isNeg ? (((data[i + 6] & 0x7F) << 8) + data[i + 7]) - 32768 : (((data[i + 6] & 0x7F) << 8) + data[i + 7]); isNeg = (data[i + 8] & 0x80) == 0x80 ? true : false; float z = isNeg ? (((data[i + 8] & 0x7F) << 8) + data[i + 9]) - 32768 : (((data[i + 8] & 0x7F) << 8) + data[i + 9]); Vector3 position = new Vector3(x / 1000.0f, z / 1000.0f, y / 1000.0f); i += 10; shouldCheckMac = true; // Create Endpoint if inexsiting if (!tagFound || !currentTag.trackedEndpoints.ContainsKey(sensorCount)) { Debug.LogWarning("Tag does not exist for message. Tag found ? " + tagFound.ToString()); } else { currentTag.trackedEndpoints[sensorCount].UpdatePosition(position, timestamp); } break; } // IMU case 1: { int sensorCount = data[i] & 0x0F; float ox = ((data[i + 4] << 8) + data[i + 5]) / 100; float oy = ((data[i + 6] << 8) + data[i + 7]) / 100; float oz = ((data[i + 8] << 8) + data[i + 9]) / 100; bool isNeg = (data[i + 10] & 0x80) == 0x80 ? true : false; int ax = isNeg ? (((data[i + 10] & 0x7F) << 8) + data[i + 11]) - 32768 : (((data[i + 10] & 0x7F) << 8) + data[i + 11]); isNeg = (data[i + 12] & 0x80) == 0x80 ? true : false; int ay = isNeg ? (((data[i + 12] & 0x7F) << 8) + data[i + 13]) - 32768 : (((data[i + 12] & 0x7F) << 8) + data[i + 13]); isNeg = (data[i + 14] & 0x80) == 0x80 ? true : false; int az = isNeg ? (((data[i + 14] & 0x7F) << 8) + data[i + 15]) - 32768 : (((data[i + 14] & 0x7F) << 8) + data[i + 15]); Vector3 rec_orientation = new Vector3(ox, oy, oz); Vector3 rec_acceleration = new Vector3((float)ax * (9.80665f / 1000f), (float)ay * (9.80665f / 1000f), (float)az * (9.80665f / 1000.0f)); i += 16; shouldCheckMac = false; if (!tagFound || !currentTag.trackedEndpoints.ContainsKey(sensorCount)) { Debug.LogWarning("Tag does not exist for message. Tag found ? " + tagFound.ToString()); } else { currentTag.trackedEndpoints[sensorCount].UpdateOrientationAndAcceleration(rec_orientation, rec_acceleration); } break; } // IMU Quaternion case 2: { int length = data[i + 1]; int sensorCount = data[i] & 0x0F; // float accuracy = (data[i + 1] << 8) / 10; float ow = ((float)((data[i + 2] << 8) + data[i + 3]) / 10000) - 1; float ox = -(((float)((data[i + 4] << 8) + data[i + 5]) / 10000) - 1); float oz = -(((float)((data[i + 6] << 8) + data[i + 7]) / 10000) - 1); float oy = -(((float)((data[i + 8] << 8) + data[i + 9]) / 10000) - 1); bool isNeg = (data[i + 10] & 0x80) == 0x80 ? true : false; int ax = isNeg ? (((data[i + 10] & 0x7F) << 8) + data[i + 11]) - 32768 : (((data[i + 10] & 0x7F) << 8) + data[i + 11]); isNeg = (data[i + 12] & 0x80) == 0x80 ? true : false; int ay = isNeg ? (((data[i + 12] & 0x7F) << 8) + data[i + 13]) - 32768 : (((data[i + 12] & 0x7F) << 8) + data[i + 13]); isNeg = (data[i + 14] & 0x80) == 0x80 ? true : false; int az = isNeg ? (((data[i + 14] & 0x7F) << 8) + data[i + 15]) - 32768 : (((data[i + 14] & 0x7F) << 8) + data[i + 15]); Quaternion rec_orientation = new Quaternion(ox, oy, oz, ow); Vector3 rec_acceleration = new Vector3((float)ax * (9.80665f / 1000f), (float)ay * (9.80665f / 1000f), (float)az * (9.80665f / 1000.0f)); // If Timestamped bool timestamped = length == 18 ? true : false; double timestamp = 0; if (timestamped) { timestamp = ((double)((data[i + 16] << 8) + data[i + 17]) / 10000.0d); } i += length; shouldCheckMac = false; if (!tagFound || !currentTag.trackedEndpoints.ContainsKey(sensorCount)) { Debug.LogWarning("Tag does not exist for message. Tag found ? " + tagFound.ToString()); } else { if (timestamped) { currentTag.trackedEndpoints[sensorCount].UpdateOrientationAndAcceleration(timestamp, rec_orientation, rec_acceleration); } else { currentTag.trackedEndpoints[sensorCount].UpdateOrientationAndAcceleration(rec_orientation, rec_acceleration); } } break; } // Trackpad case 3: { int sensorCount = data[i] & 0x0F; byte x = data[i + 1]; byte y = data[i + 2]; //byte pressure = (byte)(data[i + 3] >> 4); byte btn = (byte)(data[i + 3] & 0x0F); float yFloat = (float)-(x - 127.5) / 255; float xFloat = (float)-(y - 127.5) / 255; i += 4; shouldCheckMac = false; if (!tagFound || !currentTag.trackedEndpoints.ContainsKey(sensorCount)) { Debug.LogWarning("Tag does not exist for message. Tag found ? " + tagFound.ToString()); } else { currentTag.UpdateTrackpadData(xFloat, yFloat, btn); } break; } // Gun case 6: { int sensorCount = data[i] & 0x0F; // byte buttons : 0 [trigger, grab, joy_d, a, b, x, y] 7 byte buttons = data[i + 1]; bool new_trigger = (buttons & (1 << 0)) != 0; bool new_grab = (buttons & (1 << 1)) != 0; bool new_joystick = (buttons & (1 << 2)) != 0; bool new_a = (buttons & (1 << 3)) != 0; bool new_b = (buttons & (1 << 4)) != 0; bool new_x = (buttons & (1 << 5)) != 0; bool new_y = (buttons & (1 << 6)) != 0; i += 4; shouldCheckMac = false; if (!tagFound || !currentTag.trackedEndpoints.ContainsKey(sensorCount)) { Debug.LogWarning("Tag does not exist for message. Tag found ? " + tagFound.ToString()); } else { currentTag.UpdateGunData(new_trigger, new_grab, new_joystick, new_a, new_b, new_x, new_y); } break; } default: { Debug.Log("Data could not be parsed : " + data[i].ToString() + " index: " + i); i = data.Length; break; } } } }
/// <summary> /// Shows the information to tell the user to pair the Tag /// </summary> /// <param name="tagName">Tag name.</param> public abstract IEnumerator ShowPairTag(VRTracker.Manager.VRT_Tag tag);
/// <summary> /// Shows the information to tell the user to pair the Tag /// </summary> /// <param name="tag">Tag.</param> public override IEnumerator ShowPairTag(VRTracker.Manager.VRT_Tag mTag) { m_PairingTagFader.transform.Find("PairingTagInstructions/Title").GetComponentInChildren <Text>().text = "Assign " + mTag.tagType.ToString(); yield return(StartCoroutine(ShowMenu(m_PairingTagFader, mTag, 10.0f))); }
/// <summary> /// Shows information when the pairing timed out, with a button to restart it. /// </summary> /// <param name="tag">Tag.</param> public override IEnumerator ShowFailToPairTag(VRTracker.Manager.VRT_Tag mTag) { yield return(StartCoroutine(ShowMenu(m_PairingFailedFader, m_PairingFailedSlider))); }