| 1 | /************************************************************************************ |
| 2 | Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved. |
| 3 | |
| 4 | Licensed under the Oculus Utilities SDK License Version 1.31 (the "License"); you may not use |
| 5 | the Utilities SDK except in compliance with the License, which is provided at the time of installation |
| 6 | or download, or which otherwise accompanies this software in either electronic or hard copy form. |
| 7 | |
| 8 | You may obtain a copy of the License at |
| 9 | https://developer.oculus.com/licenses/utilities-1.31 |
| 10 | |
| 11 | Unless required by applicable law or agreed to in writing, the Utilities SDK distributed |
| 12 | under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 13 | ANY KIND, either express or implied. See the License for the specific language governing |
| 14 | permissions and limitations under the License. |
| 15 | ************************************************************************************/ |
| 16 | |
| 17 | using System.Collections.Generic; |
| 18 | using UnityEngine; |
| 19 | |
| 20 | /// <summary> |
| 21 | /// Allows grabbing and throwing of objects with the OVRGrabbable component on them. |
| 22 | /// </summary> |
| 23 | [RequireComponent(typeof(Rigidbody))] |
| 24 | public class OVRGrabber : MonoBehaviour |
| 25 | { |
| 26 | // Grip trigger thresholds for picking up objects, with some hysteresis. |
| 27 | public float grabBegin = 0.55f; |
| 28 | public float grabEnd = 0.35f; |
| 29 | |
| 30 | // Demonstrates parenting the held object to the hand's transform when grabbed. |
| 31 | // When false, the grabbed object is moved every FixedUpdate using MovePosition. |
| 32 | // Note that MovePosition is required for proper physics simulation. If you set this to true, you can |
| 33 | // easily observe broken physics simulation by, for example, moving the bottom cube of a stacked |
| 34 | // tower and noting a complete loss of friction. |
| 35 | [SerializeField] |
| 36 | protected bool m_parentHeldObject = false; |
| 37 | |
| 38 | // Child/attached transforms of the grabber, indicating where to snap held objects to (if you snap them). |
| 39 | // Also used for ranking grab targets in case of multiple candidates. |
| 40 | [SerializeField] |
| 41 | protected Transform m_gripTransform = null; |
| 42 | // Child/attached Colliders to detect candidate grabbable objects. |
| 43 | [SerializeField] |
| 44 | protected Collider[] m_grabVolumes = null; |
| 45 | |
| 46 | // Should be OVRInput.Controller.LTouch or OVRInput.Controller.RTouch. |
| 47 | [SerializeField] |
| 48 | protected OVRInput.Controller m_controller; |
| 49 | |
| 50 | [SerializeField] |
| 51 | protected Transform m_parentTransform; |
| 52 | |
| 53 | protected bool m_grabVolumeEnabled = true; |
| 54 | protected Vector3 m_lastPos; |
| 55 | protected Quaternion m_lastRot; |
| 56 | protected Quaternion m_anchorOffsetRotation; |
| 57 | protected Vector3 m_anchorOffsetPosition; |
| 58 | protected float m_prevFlex; |
| 59 | protected OVRGrabbable m_grabbedObj = null; |
| 60 | protected Vector3 m_grabbedObjectPosOff; |
| 61 | protected Quaternion m_grabbedObjectRotOff; |
| 62 | protected Dictionary<OVRGrabbable, int> m_grabCandidates = new Dictionary<OVRGrabbable, int>(); |
| 63 | protected bool operatingWithoutOVRCameraRig = true; |
| 64 | |
| 65 | /// <summary> |
| 66 | /// The currently grabbed object. |
| 67 | /// </summary> |
| 68 | public OVRGrabbable grabbedObject |
| 69 | { |
| 70 | get { return m_grabbedObj; } |
| 71 | } |
| 72 | |
| 73 | public void ForceRelease(OVRGrabbable grabbable) |
| 74 | { |
| 75 | bool canRelease = ( |
| 76 | (m_grabbedObj != null) && |
| 77 | (m_grabbedObj == grabbable) |
| 78 | ); |
| 79 | if (canRelease) |
| 80 | { |
| 81 | GrabEnd(); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | protected virtual void Awake() |
| 86 | { |
| 87 | m_anchorOffsetPosition = transform.localPosition; |
| 88 | m_anchorOffsetRotation = transform.localRotation; |
| 89 | |
| 90 | // If we are being used with an OVRCameraRig, let it drive input updates, which may come from Update or FixedUpdate. |
| 91 | |
| 92 | OVRCameraRig rig = null; |
| 93 | if (transform.parent != null && transform.parent.parent != null) |
| 94 | rig = transform.parent.parent.GetComponent<OVRCameraRig>(); |
| 95 | |
| 96 | if (rig != null) |
| 97 | { |
| 98 | rig.UpdatedAnchors += (r) => {OnUpdatedAnchors();}; |
| 99 | operatingWithoutOVRCameraRig = false; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | protected virtual void Start() |
| 104 | { |
| 105 | m_lastPos = transform.position; |
| 106 | m_lastRot = transform.rotation; |
| 107 | if(m_parentTransform == null) |
| 108 | { |
| 109 | if(gameObject.transform.parent != null) |
| 110 | { |
| 111 | m_parentTransform = gameObject.transform.parent.transform; |
| 112 | } |
| 113 | else |
| 114 | { |
| 115 | m_parentTransform = new GameObject().transform; |
| 116 | m_parentTransform.position = Vector3.zero; |
| 117 | m_parentTransform.rotation = Quaternion.identity; |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | void FixedUpdate() |
| 123 | { |
| 124 | if (operatingWithoutOVRCameraRig) |
| 125 | OnUpdatedAnchors(); |
| 126 | } |
| 127 | |
| 128 | // Hands follow the touch anchors by calling MovePosition each frame to reach the anchor. |
| 129 | // This is done instead of parenting to achieve workable physics. If you don't require physics on |
| 130 | // your hands or held objects, you may wish to switch to parenting. |
| 131 | void OnUpdatedAnchors() |
| 132 | { |
| 133 | Vector3 handPos = OVRInput.GetLocalControllerPosition(m_controller); |
| 134 | Quaternion handRot = OVRInput.GetLocalControllerRotation(m_controller); |
| 135 | Vector3 destPos = m_parentTransform.TransformPoint(m_anchorOffsetPosition + handPos); |
| 136 | Quaternion destRot = m_parentTransform.rotation * handRot * m_anchorOffsetRotation; |
| 137 | GetComponent<Rigidbody>().MovePosition(destPos); |
| 138 | GetComponent<Rigidbody>().MoveRotation(destRot); |
| 139 | |
| 140 | if (!m_parentHeldObject) |
| 141 | { |
| 142 | MoveGrabbedObject(destPos, destRot); |
| 143 | } |
| 144 | m_lastPos = transform.position; |
| 145 | m_lastRot = transform.rotation; |
| 146 | |
| 147 | float prevFlex = m_prevFlex; |
| 148 | // Update values from inputs |
| 149 | m_prevFlex = OVRInput.Get(OVRInput.Axis1D.PrimaryHandTrigger, m_controller); |
| 150 | |
| 151 | CheckForGrabOrRelease(prevFlex); |
| 152 | } |
| 153 | |
| 154 | void OnDestroy() |
| 155 | { |
| 156 | if (m_grabbedObj != null) |
| 157 | { |
| 158 | GrabEnd(); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | void OnTriggerEnter(Collider otherCollider) |
| 163 | { |
| 164 | // Get the grab trigger |
| 165 | OVRGrabbable grabbable = otherCollider.GetComponent<OVRGrabbable>() ?? otherCollider.GetComponentInParent<OVRGrabbable>(); |
| 166 | if (grabbable == null) return; |
| 167 | |
| 168 | // Add the grabbable |
| 169 | int refCount = 0; |
| 170 | m_grabCandidates.TryGetValue(grabbable, out refCount); |
| 171 | m_grabCandidates[grabbable] = refCount + 1; |
| 172 | } |
| 173 | |
| 174 | void OnTriggerExit(Collider otherCollider) |
| 175 | { |
| 176 | OVRGrabbable grabbable = otherCollider.GetComponent<OVRGrabbable>() ?? otherCollider.GetComponentInParent<OVRGrabbable>(); |
| 177 | if (grabbable == null) return; |
| 178 | |
| 179 | // Remove the grabbable |
| 180 | int refCount = 0; |
| 181 | bool found = m_grabCandidates.TryGetValue(grabbable, out refCount); |
| 182 | if (!found) |
| 183 | { |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | if (refCount > 1) |
| 188 | { |
| 189 | m_grabCandidates[grabbable] = refCount - 1; |
| 190 | } |
| 191 | else |
| 192 | { |
| 193 | m_grabCandidates.Remove(grabbable); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | protected void CheckForGrabOrRelease(float prevFlex) |
| 198 | { |
| 199 | if ((m_prevFlex >= grabBegin) && (prevFlex < grabBegin)) |
| 200 | { |
| 201 | GrabBegin(); |
| 202 | } |
| 203 | else if ((m_prevFlex <= grabEnd) && (prevFlex > grabEnd)) |
| 204 | { |
| 205 | GrabEnd(); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | protected virtual void GrabBegin() |
| 210 | { |
| 211 | float closestMagSq = float.MaxValue; |
| 212 | OVRGrabbable closestGrabbable = null; |
| 213 | Collider closestGrabbableCollider = null; |
| 214 | |
| 215 | // Iterate grab candidates and find the closest grabbable candidate |
| 216 | foreach (OVRGrabbable grabbable in m_grabCandidates.Keys) |
| 217 | { |
| 218 | bool canGrab = !(grabbable.isGrabbed && !grabbable.allowOffhandGrab); |
| 219 | if (!canGrab) |
| 220 | { |
| 221 | continue; |
| 222 | } |
| 223 | |
| 224 | for (int j = 0; j < grabbable.grabPoints.Length; ++j) |
| 225 | { |
| 226 | Collider grabbableCollider = grabbable.grabPoints[j]; |
| 227 | // Store the closest grabbable |
| 228 | Vector3 closestPointOnBounds = grabbableCollider.ClosestPointOnBounds(m_gripTransform.position); |
| 229 | float grabbableMagSq = (m_gripTransform.position - closestPointOnBounds).sqrMagnitude; |
| 230 | if (grabbableMagSq < closestMagSq) |
| 231 | { |
| 232 | closestMagSq = grabbableMagSq; |
| 233 | closestGrabbable = grabbable; |
| 234 | closestGrabbableCollider = grabbableCollider; |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | // Disable grab volumes to prevent overlaps |
| 240 | GrabVolumeEnable(false); |
| 241 | |
| 242 | if (closestGrabbable != null) |
| 243 | { |
| 244 | if (closestGrabbable.isGrabbed) |
| 245 | { |
| 246 | closestGrabbable.grabbedBy.OffhandGrabbed(closestGrabbable); |
| 247 | } |
| 248 | |
| 249 | m_grabbedObj = closestGrabbable; |
| 250 | m_grabbedObj.GrabBegin(this, closestGrabbableCollider); |
| 251 | |
| 252 | m_lastPos = transform.position; |
| 253 | m_lastRot = transform.rotation; |
| 254 | |
| 255 | // Set up offsets for grabbed object desired position relative to hand. |
| 256 | if(m_grabbedObj.snapPosition) |
| 257 | { |
| 258 | m_grabbedObjectPosOff = m_gripTransform.localPosition; |
| 259 | if(m_grabbedObj.snapOffset) |
| 260 | { |
| 261 | Vector3 snapOffset = m_grabbedObj.snapOffset.position; |
| 262 | if (m_controller == OVRInput.Controller.LTouch) snapOffset.x = -snapOffset.x; |
| 263 | m_grabbedObjectPosOff += snapOffset; |
| 264 | } |
| 265 | } |
| 266 | else |
| 267 | { |
| 268 | Vector3 relPos = m_grabbedObj.transform.position - transform.position; |
| 269 | relPos = Quaternion.Inverse(transform.rotation) * relPos; |
| 270 | m_grabbedObjectPosOff = relPos; |
| 271 | } |
| 272 | |
| 273 | if (m_grabbedObj.snapOrientation) |
| 274 | { |
| 275 | m_grabbedObjectRotOff = m_gripTransform.localRotation; |
| 276 | if(m_grabbedObj.snapOffset) |
| 277 | { |
| 278 | m_grabbedObjectRotOff = m_grabbedObj.snapOffset.rotation * m_grabbedObjectRotOff; |
| 279 | } |
| 280 | } |
| 281 | else |
| 282 | { |
| 283 | Quaternion relOri = Quaternion.Inverse(transform.rotation) * m_grabbedObj.transform.rotation; |
| 284 | m_grabbedObjectRotOff = relOri; |
| 285 | } |
| 286 | |
| 287 | // Note: force teleport on grab, to avoid high-speed travel to dest which hits a lot of other objects at high |
| 288 | // speed and sends them flying. The grabbed object may still teleport inside of other objects, but fixing that |
| 289 | // is beyond the scope of this demo. |
| 290 | MoveGrabbedObject(m_lastPos, m_lastRot, true); |
| 291 | if(m_parentHeldObject) |
| 292 | { |
| 293 | m_grabbedObj.transform.parent = transform; |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | protected virtual void MoveGrabbedObject(Vector3 pos, Quaternion rot, bool forceTeleport = false) |
| 299 | { |
| 300 | if (m_grabbedObj == null) |
| 301 | { |
| 302 | return; |
| 303 | } |
| 304 | |
| 305 | Rigidbody grabbedRigidbody = m_grabbedObj.grabbedRigidbody; |
| 306 | Vector3 grabbablePosition = pos + rot * m_grabbedObjectPosOff; |
| 307 | Quaternion grabbableRotation = rot * m_grabbedObjectRotOff; |
| 308 | |
| 309 | if (forceTeleport) |
| 310 | { |
| 311 | grabbedRigidbody.transform.position = grabbablePosition; |
| 312 | grabbedRigidbody.transform.rotation = grabbableRotation; |
| 313 | } |
| 314 | else |
| 315 | { |
| 316 | grabbedRigidbody.MovePosition(grabbablePosition); |
| 317 | grabbedRigidbody.MoveRotation(grabbableRotation); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | protected void GrabEnd() |
| 322 | { |
| 323 | if (m_grabbedObj != null) |
| 324 | { |
| 325 | OVRPose localPose = new OVRPose { position = OVRInput.GetLocalControllerPosition(m_controller), orientation = OVRInput.GetLocalControllerRotation(m_controller) }; |
| 326 | OVRPose offsetPose = new OVRPose { position = m_anchorOffsetPosition, orientation = m_anchorOffsetRotation }; |
| 327 | localPose = localPose * offsetPose; |
| 328 | |
| 329 | OVRPose trackingSpace = transform.ToOVRPose() * localPose.Inverse(); |
| 330 | Vector3 linearVelocity = trackingSpace.orientation * OVRInput.GetLocalControllerVelocity(m_controller); |
| 331 | Vector3 angularVelocity = trackingSpace.orientation * OVRInput.GetLocalControllerAngularVelocity(m_controller); |
| 332 | |
| 333 | GrabbableRelease(linearVelocity, angularVelocity); |
| 334 | } |
| 335 | |
| 336 | // Re-enable grab volumes to allow overlap events |
| 337 | GrabVolumeEnable(true); |
| 338 | } |
| 339 | |
| 340 | protected void GrabbableRelease(Vector3 linearVelocity, Vector3 angularVelocity) |
| 341 | { |
| 342 | m_grabbedObj.GrabEnd(linearVelocity, angularVelocity); |
| 343 | if(m_parentHeldObject) m_grabbedObj.transform.parent = null; |
| 344 | m_grabbedObj = null; |
| 345 | } |
| 346 | |
| 347 | protected virtual void GrabVolumeEnable(bool enabled) |
| 348 | { |
| 349 | if (m_grabVolumeEnabled == enabled) |
| 350 | { |
| 351 | return; |
| 352 | } |
| 353 | |
| 354 | m_grabVolumeEnabled = enabled; |
| 355 | for (int i = 0; i < m_grabVolumes.Length; ++i) |
| 356 | { |
| 357 | Collider grabVolume = m_grabVolumes[i]; |
| 358 | grabVolume.enabled = m_grabVolumeEnabled; |
| 359 | } |
| 360 | |
| 361 | if (!m_grabVolumeEnabled) |
| 362 | { |
| 363 | m_grabCandidates.Clear(); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | protected virtual void OffhandGrabbed(OVRGrabbable grabbable) |
| 368 | { |
| 369 | if (m_grabbedObj == grabbable) |
| 370 | { |
| 371 | GrabbableRelease(Vector3.zero, Vector3.zero); |
| 372 | } |
| 373 | } |
| 374 | } |