/// <summary> /// Attach the Overlay to [device] at [scale] size with offset [offset], and base position [point]. /// [point] isn't used for HMD or World, and can be ignored. /// </summary> /// <param name="device"></param> /// <param name="scale"></param> /// <param name="offset"></param> /// <param name="point"></param> public void AttachTo(AttachmentDevice device, float scale, Vector3 offset, AttachmentPoint point = AttachmentPoint.Center) { // Update Overlay Anchor position GetOverlayPosition(); if (_anchorDevice != device && OnOverlayAnchorChanges != null) { OnOverlayAnchorChanges(this, device); } // Update cached values _anchorDevice = device; AnchorDevice = device; _anchorPoint = point; AnchorPoint = point; _anchorOffset = offset; AnchorOffset = offset; Scale = scale; if (OnOverlayAnchorRotationChanges != null) { OnOverlayAnchorRotationChanges(this, Quaternion.identity); } // Attach Overlay switch (device) { case AttachmentDevice.Screen: _anchor = OpenVR.k_unTrackedDeviceIndexInvalid; gameObject.transform.localPosition = offset; OverlayReference.transform.localRotation = Quaternion.identity; break; case AttachmentDevice.World: _anchor = OpenVR.k_unTrackedDeviceIndexInvalid; gameObject.transform.localPosition = offset; OverlayReference.transform.localRotation = Quaternion.identity; break; case AttachmentDevice.LeftController: _anchor = HOTK_TrackedDeviceManager.Instance.LeftIndex; AttachToController(point, offset); break; case AttachmentDevice.RightController: _anchor = HOTK_TrackedDeviceManager.Instance.RightIndex; AttachToController(point, offset); break; default: throw new ArgumentOutOfRangeException("device", device, null); } if (OnOverlayAttachmentChanges != null) { OnOverlayAttachmentChanges(this); } }
// If the controller we are tracking changes index, update private void OnControllerIndexChanged(ETrackedControllerRole role, uint index) { if (_anchorDevice == AttachmentDevice.LeftController && role == ETrackedControllerRole.LeftHand) { _anchorDevice = AttachmentDevice.World; // This will trick the system into reattaching the overlay } else if (_anchorDevice == AttachmentDevice.RightController && role == ETrackedControllerRole.RightHand) { _anchorDevice = AttachmentDevice.World; // This will trick the system into reattaching the overlay } }
// If the controller we are tracking changes index, update private void OnControllerIndexChanged(EType role, uint index) { // If the Overlay is attached to a Controller, this will trick it into reanchoring when the Controller Index changes. switch (_anchorDevice) { case AttachmentDevice.World: case AttachmentDevice.Screen: break; case AttachmentDevice.LeftController: if (role == EType.LeftController) { _anchorDevice = AttachmentDevice.World; } break; case AttachmentDevice.RightController: if (role == EType.RightController) { _anchorDevice = AttachmentDevice.World; } break; case AttachmentDevice.ThirdController: if (role == EType.ThirdController) { _anchorDevice = AttachmentDevice.World; } break; case AttachmentDevice.FourthController: if (role == EType.FourthController) { _anchorDevice = AttachmentDevice.World; } break; default: throw new ArgumentOutOfRangeException(); } }
/// <summary> /// Attach the Overlay to [device] at [scale], and base position [point]. /// [point] isn't used for HMD or World, and can be ignored. /// </summary> /// <param name="device"></param> /// <param name="scale"></param> /// <param name="point"></param> public void AttachTo(AttachmentDevice device, float scale, AttachmentPoint point = AttachmentPoint.Center) { AttachTo(device, scale, Vector3.zero, point); }
/// <summary> /// Attach the Overlay to [device] at [scale] size with offset [offset], and base position [point]. /// [point] isn't used for HMD or World, and can be ignored. /// </summary> /// <param name="device"></param> /// <param name="scale"></param> /// <param name="offset"></param> /// <param name="point"></param> public void AttachTo(AttachmentDevice device, float scale, Vector3 offset, AttachmentPoint point = AttachmentPoint.Center) { // Update Overlay Anchor position GetOverlayPosition(); // Update cached values _anchorDevice = device; AnchorDevice = device; _anchorPoint = point; AnchorPoint = point; _anchorOffset = offset; AnchorOffset = offset; Scale = scale; // Attach Overlay switch (device) { case AttachmentDevice.Screen: _anchor = OpenVR.k_unTrackedDeviceIndexInvalid; OverlayReference.transform.localPosition = -offset; OverlayReference.transform.localRotation = Quaternion.identity; break; case AttachmentDevice.World: _anchor = OpenVR.k_unTrackedDeviceIndexInvalid; OverlayReference.transform.localPosition = -offset; OverlayReference.transform.localRotation = Quaternion.identity; break; case AttachmentDevice.LeftController: _anchor = HOTK_TrackedDeviceManager.Instance.LeftIndex; AttachToController(point, offset); break; case AttachmentDevice.RightController: _anchor = HOTK_TrackedDeviceManager.Instance.RightIndex; AttachToController(point, offset); break; case AttachmentDevice.ThirdController: _anchor = HOTK_TrackedDeviceManager.Instance.ThirdIndex; if (_anchor == OpenVR.k_unTrackedDeviceIndexInvalid) { gameObject.SetActive(false); // Third Controller not connected } AttachToController(point, offset); break; case AttachmentDevice.FourthController: _anchor = HOTK_TrackedDeviceManager.Instance.FourthIndex; if (_anchor == OpenVR.k_unTrackedDeviceIndexInvalid) { gameObject.SetActive(false); // Fourth Controller not connected } AttachToController(point, offset); break; default: throw new ArgumentOutOfRangeException("device", device, null); } }