|
| 1 | +// Copyright (c) Reality Collective. All rights reserved. |
| 2 | +// Licensed under the MIT License. See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +using RealityCollective.Utilities.Extensions; |
| 5 | +using RealityToolkit.Input.Controllers; |
| 6 | +using RealityToolkit.Input.Events; |
| 7 | +using RealityToolkit.Input.Interactors; |
| 8 | +using System.Collections.Generic; |
| 9 | +using UnityEngine; |
| 10 | + |
| 11 | +namespace RealityToolkit.Input.InteractionBehaviours |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Syncs the <see cref="IControllerInteractor"/>'s <see cref="IControllerVisualizer"/> |
| 15 | + /// to the <see cref="Interactables.IInteractable"/> using <see cref="IControllerVisualizer.OverrideSourcePose"/>. |
| 16 | + /// </summary> |
| 17 | + /// <remarks> |
| 18 | + /// Only supports <see cref="IControllerInteractor"/>s. |
| 19 | + /// Does not support <see cref="IPokeInteractor"/>s and will ignore them. |
| 20 | + /// </remarks> |
| 21 | + [HelpURL(RealityToolkitRuntimePreferences.Toolkit_Docs_BaseUrl + "docs/interactions/interaction-behaviours/default-behaviours/hold-onto-behaviour")] |
| 22 | + [AddComponentMenu(RealityToolkitRuntimePreferences.Toolkit_InteractionsAddComponentMenu + "/" + nameof(HoldOntoBehaviour))] |
| 23 | + public class HoldOntoBehaviour : BaseInteractionBehaviour |
| 24 | + { |
| 25 | + /// <summary> |
| 26 | + /// Internal structure keeping track of locked visualizers and their state. |
| 27 | + /// </summary> |
| 28 | + private class VisualizerLockData |
| 29 | + { |
| 30 | + public bool IsLocked { get; set; } |
| 31 | + |
| 32 | + public bool IsPendingUnlock { get; set; } |
| 33 | + |
| 34 | + public float SmoothingStartTime { get; set; } |
| 35 | + |
| 36 | + public float SmoothingProgress { get; set; } |
| 37 | + |
| 38 | + public float SmoothingDuration { get; set; } |
| 39 | + } |
| 40 | + |
| 41 | + [SerializeField, Tooltip("Optional: The pose to hold / grab onto when locking onto this interactable. Must be a child " + |
| 42 | + "transform of the component transform. If not set, the component transform is used.")] |
| 43 | + private Transform hint = null; |
| 44 | + |
| 45 | + [SerializeField, Tooltip("If set, the controller visualizer will smoothly lock to the interactable instead of instantly.")] |
| 46 | + private bool smooth = true; |
| 47 | + |
| 48 | + [SerializeField, Min(.01f), Tooltip("Duration in seconds to sync the visualizer pose with the interactable. The value is in seconds per unit and will " + |
| 49 | + "interpolate based on distance. So if the visualizer is 5 units away, the total sync duration is five times the configured duration.")] |
| 50 | + private float smoothingDuration = 1f; |
| 51 | + |
| 52 | + private readonly Dictionary<IControllerVisualizer, VisualizerLockData> visualizers = new(); |
| 53 | + private readonly List<IControllerVisualizer> visualizersForClearing = new(); |
| 54 | + |
| 55 | + /// <inheritdoc/> |
| 56 | + protected override void Awake() |
| 57 | + { |
| 58 | + base.Awake(); |
| 59 | + |
| 60 | + if (hint.IsNull()) |
| 61 | + { |
| 62 | + hint = transform; |
| 63 | + } |
| 64 | + else if (!hint.IsChildOf(transform)) |
| 65 | + { |
| 66 | + Debug.LogError($"{hint.name} must be a child transform of {transform.name}.", this); |
| 67 | + } |
| 68 | + |
| 69 | + if (smooth && smoothingDuration < 0f) |
| 70 | + { |
| 71 | + Debug.LogError("Sync duration must be non-negative and above 0.", this); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /// <inheritdoc/> |
| 76 | + protected override void Update() |
| 77 | + { |
| 78 | + if (visualizers.Count == 0) |
| 79 | + { |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + foreach (var item in visualizers) |
| 84 | + { |
| 85 | + var visualizer = item.Key; |
| 86 | + var data = item.Value; |
| 87 | + |
| 88 | + var t = CalculateSmoothingTransition(visualizer, data); |
| 89 | + if (data.IsPendingUnlock && t >= 1f) |
| 90 | + { |
| 91 | + visualizersForClearing.Add(visualizer); |
| 92 | + continue; |
| 93 | + } |
| 94 | + |
| 95 | + var lockPose = GetInteractableLockPose(visualizer); |
| 96 | + var targetPose = lockPose; |
| 97 | + |
| 98 | + if (data.IsPendingUnlock) |
| 99 | + { |
| 100 | + // Smoothly return to actual controller pose. |
| 101 | + var startPose = GetInteractableLockPose(visualizer); |
| 102 | + var unlockPose = GetVisualizerUnlockPose(visualizer); |
| 103 | + |
| 104 | + targetPose.position = Vector3.Slerp(startPose.position, unlockPose.position, data.SmoothingProgress); |
| 105 | + targetPose.rotation = Quaternion.Slerp(startPose.rotation, unlockPose.rotation, data.SmoothingProgress); |
| 106 | + } |
| 107 | + else if (!data.IsPendingUnlock && t < 1f) |
| 108 | + { |
| 109 | + // Smoothly lock onto the interactable. |
| 110 | + var startPose = GetVisualizerUnlockPose(visualizer); |
| 111 | + targetPose.position = Vector3.Slerp(startPose.position, lockPose.position, data.SmoothingProgress); |
| 112 | + targetPose.rotation = Quaternion.Slerp(startPose.rotation, lockPose.rotation, data.SmoothingProgress); |
| 113 | + } |
| 114 | + |
| 115 | + // The position we store is in local space while the rotation is a world rotation. |
| 116 | + visualizer.PoseDriver.localPosition = targetPose.position; |
| 117 | + visualizer.PoseDriver.rotation = targetPose.rotation; |
| 118 | + } |
| 119 | + |
| 120 | + for (var i = 0; i < visualizersForClearing.Count; i++) |
| 121 | + { |
| 122 | + CleanUpVisualizer(visualizersForClearing[i]); |
| 123 | + } |
| 124 | + |
| 125 | + visualizersForClearing.Clear(); |
| 126 | + } |
| 127 | + |
| 128 | + /// <inheritdoc/> |
| 129 | + protected override void OnSelectEntered(InteractionEventArgs eventArgs) |
| 130 | + { |
| 131 | + if (eventArgs.Interactor is not IControllerInteractor controllerInteractor || |
| 132 | + eventArgs.Interactor is IPokeInteractor) |
| 133 | + { |
| 134 | + return; |
| 135 | + } |
| 136 | + |
| 137 | + LockVisualizer(controllerInteractor.Controller.Visualizer); |
| 138 | + } |
| 139 | + |
| 140 | + /// <inheritdoc/> |
| 141 | + protected override void OnSelectExited(InteractionExitEventArgs eventArgs) |
| 142 | + { |
| 143 | + if (eventArgs.Interactor is not IControllerInteractor controllerInteractor || |
| 144 | + eventArgs.Interactor is IPokeInteractor) |
| 145 | + { |
| 146 | + return; |
| 147 | + } |
| 148 | + |
| 149 | + UnlockVisualizer(controllerInteractor.Controller.Visualizer); |
| 150 | + } |
| 151 | + |
| 152 | + /// <inheritdoc/> |
| 153 | + protected override void OnGrabEntered(InteractionEventArgs eventArgs) |
| 154 | + { |
| 155 | + if (eventArgs.Interactor is not IControllerInteractor controllerInteractor || |
| 156 | + eventArgs.Interactor is IPokeInteractor) |
| 157 | + { |
| 158 | + return; |
| 159 | + } |
| 160 | + |
| 161 | + LockVisualizer(controllerInteractor.Controller.Visualizer); |
| 162 | + } |
| 163 | + |
| 164 | + /// <inheritdoc/> |
| 165 | + protected override void OnGrabExited(InteractionExitEventArgs eventArgs) |
| 166 | + { |
| 167 | + if (eventArgs.Interactor is not IControllerInteractor controllerInteractor || |
| 168 | + eventArgs.Interactor is IPokeInteractor) |
| 169 | + { |
| 170 | + return; |
| 171 | + } |
| 172 | + |
| 173 | + UnlockVisualizer(controllerInteractor.Controller.Visualizer); |
| 174 | + } |
| 175 | + |
| 176 | + private void LockVisualizer(IControllerVisualizer visualizer) |
| 177 | + { |
| 178 | + visualizers.EnsureDictionaryItem(visualizer, new VisualizerLockData |
| 179 | + { |
| 180 | + IsPendingUnlock = false, |
| 181 | + IsLocked = !smooth, |
| 182 | + SmoothingStartTime = Time.time, |
| 183 | + SmoothingProgress = 0f, |
| 184 | + SmoothingDuration = smooth ? CalculateSmoothingDuration(visualizer) : 0f |
| 185 | + }); |
| 186 | + |
| 187 | + visualizer.OverrideSourcePose = true; |
| 188 | + } |
| 189 | + |
| 190 | + private void UnlockVisualizer(IControllerVisualizer visualizer) |
| 191 | + { |
| 192 | + if (!smooth) |
| 193 | + { |
| 194 | + CleanUpVisualizer(visualizer); |
| 195 | + return; |
| 196 | + } |
| 197 | + |
| 198 | + visualizers.EnsureDictionaryItem(visualizer, new VisualizerLockData |
| 199 | + { |
| 200 | + IsPendingUnlock = true, |
| 201 | + IsLocked = false, |
| 202 | + SmoothingStartTime = Time.time, |
| 203 | + SmoothingProgress = 0f, |
| 204 | + SmoothingDuration = CalculateSmoothingDuration(visualizer) |
| 205 | + }, true); |
| 206 | + } |
| 207 | + |
| 208 | + private void CleanUpVisualizer(IControllerVisualizer visualizer) |
| 209 | + { |
| 210 | + visualizers.SafeRemoveDictionaryItem(visualizer); |
| 211 | + visualizer.OverrideSourcePose = false; |
| 212 | + } |
| 213 | + |
| 214 | + private Pose GetInteractableLockPose(IControllerVisualizer visualizer) |
| 215 | + { |
| 216 | + // If the visualizer is not parented, that means we'll be positioning it in world space. |
| 217 | + // So we can use the hint transform world pose directly. |
| 218 | + if (visualizer.PoseDriver.parent.IsNull()) |
| 219 | + { |
| 220 | + return new Pose(hint.position, hint.rotation); |
| 221 | + } |
| 222 | + |
| 223 | + // If the visualzer is parented, we'll be positioning it within the local space of its parent, |
| 224 | + // so we must first figure out where the hint transform is located within that space. |
| 225 | + return new Pose(visualizer.PoseDriver.parent.InverseTransformPoint(hint.position), hint.rotation); |
| 226 | + } |
| 227 | + |
| 228 | + private Pose GetVisualizerUnlockPose(IControllerVisualizer visualizer) |
| 229 | + { |
| 230 | + visualizer.Controller.TryGetPose(Space.World, out var controllerPose); |
| 231 | + |
| 232 | + // If the visualizer is not parented, we can use the controller's actual world space |
| 233 | + // pose to determine where it currently is, since we'll be positioning in world space. |
| 234 | + if (visualizer.PoseDriver.parent.IsNull()) |
| 235 | + { |
| 236 | + return controllerPose; |
| 237 | + } |
| 238 | + |
| 239 | + // If the visualizer is parented, we'll be positioning it within the local space of its parent, |
| 240 | + // so we need to know where the actual controller currently is within that space. |
| 241 | + return new Pose(visualizer.PoseDriver.parent.InverseTransformPoint(controllerPose.position), controllerPose.rotation); |
| 242 | + } |
| 243 | + |
| 244 | + private float CalculateSmoothingDuration(IControllerVisualizer visualizer) |
| 245 | + { |
| 246 | + var start = hint.position; |
| 247 | + var end = visualizer.Controller.TryGetPose(Space.World, out var controllerPose) ? controllerPose.position : visualizer.PoseDriver.position; |
| 248 | + var distance = Vector3.Distance(start, end); |
| 249 | + |
| 250 | + return distance * smoothingDuration; |
| 251 | + } |
| 252 | + |
| 253 | + private float CalculateSmoothingTransition(IControllerVisualizer visualizer, VisualizerLockData data) |
| 254 | + { |
| 255 | + if (!smooth || data.IsLocked || data.SmoothingProgress >= 1f) |
| 256 | + { |
| 257 | + return 1f; |
| 258 | + } |
| 259 | + |
| 260 | + var t = (Time.time - data.SmoothingStartTime) / data.SmoothingDuration; |
| 261 | + data.SmoothingProgress = t; |
| 262 | + |
| 263 | + return t; |
| 264 | + } |
| 265 | + } |
| 266 | +} |
0 commit comments