Tanoda
HackedHand.cs
Go to the documentation of this file.
2
3using System.Collections;
4using System.Collections.Generic;
5using UnityEngine;
7#if DANA
8using Leap.Unity;
9#endif
10#if !UNITY_WEBGL
11using Valve.VR;
12using Valve.VR.InteractionSystem;
13using ViveHandTracking;
14#endif
15
16public class HackedHand
17#if UNITY_WEBGL // && !UNITY_EDITOR
18 : MonoBehaviour
19#else
20 : Valve.VR.InteractionSystem.Hand
21#endif
22{
23 public Transform FingerJointBoneParent;
24 public Camera VRCamera;
25 public new bool isPoseValid = true;
26 public float timeout = 0.0f;
27 public UnityEvent onDetachObject, onAttachObject;
28 public bool returnItem = true;
29 public Transform forceAttachPosition;
30 public Transform forceAttachParent;
31 public bool flyInHand = false;
32 public bool dropFive = false;
33 private float flyIn = 0.0f;
34 private float time = 0.0f;
35
36
37 private Vector3 objectStart, rightHandPos, leftHandPos;
38 private Quaternion objectRot;
39 private bool leftSpawn = false;
40
41 public bool HandExchange = false;
42 public bool exchangeSuccess = true;
43 private Vector3 originalRotation;
44 private Vector3 attachOriginalRotation;
45 private Vector3 OriginalHandPosition;
46 private Vector3 OriginalHandRotation;
47 public GameObject handHudLed;
48 internal List<GameObject> collidedFingers = new List<GameObject>();
49 internal List<GameObject> SavedCollidedFingers = new List<GameObject>();
50 internal bool attachedToHand = false;
51 internal bool handAnim = false;
52 internal bool twoHand = false;
53 internal bool delay = false;
54 internal bool animIsOver = false;
55 public GameObject InstantiatedHand;
56 internal bool realToolInHand = false;
57
58
59
60#if !UNITY_WEBGL
61 protected override IEnumerator Start()
62 {
63#if !DANA
64 if (SavedUser.instance.courseType != SavedUser.CourseType.Dobot)
65 {
66 grabPinchAction = null;
67 grabGripAction = null;
68 }
69#endif
70 hapticAction = null;
71 uiInteractAction = null;
72 if (GetComponentInChildren<HandCopyAvg>())
73 {
74 OriginalHandPosition = GetComponentInChildren<HandCopyAvg>(true).gameObject.transform.localPosition;
75 OriginalHandRotation = GetComponentInChildren<HandCopyAvg>(true).gameObject.transform.localEulerAngles;
76 }
77
78 // save off player instance
79 //playerInstance = Player.instance;
80 //if (!playerInstance)
81 //{
82 // Debug.LogError("<b>[SteamVR Interaction]</b> No player instance found in Hand Start()");
83 //}
84
85 // allocate array for colliders
86 overlappingColliders = new Collider[ColliderArraySize];
87
88 // We are a "no SteamVR fallback hand" if we have this camera set
89 // we'll use the right mouse to look around and left mouse to interact
90 // - don't need to find the device
91 if (noSteamVRFallbackCamera)
92 {
93 yield break;
94 }
95 while (true)
96 {
97 if (isPoseValid)
98 {
100 break;
101 }
102
103 yield return null;
104 }
105 HideController(true);
106 if (GetComponentInChildren<HandAnimHelper>())
107 originalRotation = GetComponentInChildren<HandAnimHelper>().gameObject.transform.localEulerAngles;
108
109 if (objectAttachmentPoint)
110 attachOriginalRotation = objectAttachmentPoint.localEulerAngles;
111 }
112
113 protected new void InitController()
114 {
115 bool hadOldRendermodel = mainRenderModel != null;
116 EVRSkeletalMotionRange oldRM_rom = EVRSkeletalMotionRange.WithController;
117 if (hadOldRendermodel)
118 oldRM_rom = mainRenderModel.GetSkeletonRangeOfMotion;
119
120
121 foreach (RenderModel r in renderModels)
122 {
123 if (r != null)
124 Destroy(r.gameObject);
125 }
126
127 renderModels.Clear();
128 if (renderModelPrefab)
129 {
130 GameObject renderModelInstance = null;
131 if (!InstantiatedHand)
132 {
133 renderModelInstance = GameObject.Instantiate(renderModelPrefab);
134 renderModelInstance.layer = gameObject.layer;
135 renderModelInstance.tag = gameObject.tag;
136 renderModelInstance.transform.parent = this.transform;
137 renderModelInstance.transform.localPosition = Vector3.zero;
138 renderModelInstance.transform.localRotation = Quaternion.identity;
139 renderModelInstance.transform.localScale = renderModelPrefab.transform.localScale;
140 }
141 else
142 {
143 renderModelInstance = InstantiatedHand;
144 renderModelInstance.transform.localPosition = Vector3.zero;
145 renderModelInstance.transform.localRotation = Quaternion.identity;
146 renderModelInstance.transform.localScale = renderModelPrefab.transform.localScale;
147 }
148
149
150 //TriggerHapticPulse(800); //pulse on controller init
151
152
153 mainRenderModel = renderModelInstance.GetComponent<RenderModel>();
154 renderModels.Add(mainRenderModel);
155 }
156
157 if (hadOldRendermodel)
158 mainRenderModel.SetSkeletonRangeOfMotion(oldRM_rom);
159
160 //int deviceIndex = trackedObject.GetDeviceIndex();
161 this.BroadcastMessage("SetInputSource", handType, SendMessageOptions.DontRequireReceiver); // let child objects know we've initialized
162 //this.BroadcastMessage("OnHandInitialized", deviceIndex, SendMessageOptions.DontRequireReceiver); // let child objects know we've initialized
163 }
164 //protected void FixedUpdate()
165 //{
166 // base.FixedUpdate();
167 // var rigidBodies = GetComponentsInChildren<Rigidbody>();
168 // foreach (var rb in rigidBodies)
169 // {
170 // if (rb.velocity != Vector3.zero)
171 // {
172 // rb.velocity = Vector3.zero;
173 // }
174 // }
175 //}
176 protected override void Update()
177 {
178 //if (!handHudLed.activeSelf)
179 // return;
180
181 base.Update();
182 if (timeout > 0)
183 {
184 timeout -= Time.deltaTime;
185 }
186
187 if (!leftSpawn && objectStart != default(Vector3) && Vector3.Distance(objectStart, transform.position) > 0.2f)
188 {
189 leftSpawn = true;
190 }
191
192 if (leftSpawn && Vector3.Distance(objectStart, transform.position) < 0.2f)
193 {
194 DetachObject(currentAttachedObject);
195 leftSpawn = false;
196 }
197
198#if !UNITY_WEBGL
199 if (dropFive && currentAttachedObject)
200 {
201 if (GestureProvider.Current.engine.State.LeftHand != null)
202 if (handType == SteamVR_Input_Sources.LeftHand &&
203 GestureProvider.Current.engine.State.LeftHand.gesture == GestureType.Five)
204 {
205 DetachObject(currentAttachedObject);
206 }
207
208 if (GestureProvider.Current.engine.State.RightHand != null)
209 if (handType == SteamVR_Input_Sources.RightHand &&
210 GestureProvider.Current.engine.State.RightHand.gesture == GestureType.Five)
211 {
212 DetachObject(currentAttachedObject);
213 }
214 }
215#endif
216 if (flyInHand && flyIn > 0)
217 {
218 flyIn -= Time.deltaTime;
219 if (flyIn < 0)
220 {
221 flyIn = 0;
222 Debug.Log("flyIn done!");
223 //var grabbedobj = currentAttachedObject;
224 //base.DetachObject(grabbedobj, false);
225 //base.AttachObject(grabbedobj, GrabTypes.Grip, defaultAttachmentFlags);
226 //var cache = currentAttachedObject;
227 //DetachObject(cache);
228 //AttachObject(cache, GrabTypes.Grip);
229 }
230
231 //var attachpoint = currentAttachedObject.GetComponent<ThrowableCanDisable>().attachmentOffset;
232 //currentAttachedObject.transform.position = Vector3.Lerp(currentAttachedObject.transform.position,
233 // objectAttachmentPoint.position, 1 - flyIn);
234 }
235 //if (attachedToHand)
236 //{
237 // StartCoroutine(LateReOrient());
238 //}
239#if DANA
240 if (attachedToHand)
241 {
242 if (GetComponentInChildren<TwoHandGrab>())
243 {
244
245 if (!otherHand.GetComponentInChildren<HandCopySkelet>().isCollision && delay)
246 {
247 time += Time.deltaTime;
248 if (time > 1)
249 {
250 var tcd = GetComponentInChildren<ThrowableCanDisable>();
251 var rigidBody = tcd.GetComponent<Rigidbody>();
252 StartCoroutine(DelayKinematicTurnOff(tcd, rigidBody));
253 tcd.ForceDrop();
254 }
255
256 }
257 else time = 0.0f;
258
259
260 }
261 }
262#endif
263 //var handCopyAVG = GetComponentInChildren<HandCopyAvg>(true);
264 //if (!animIsOver && !handAnim && handCopyAVG && !GetComponentInChildren<HandCopyAvg>(true).gameObject.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).IsName("idle"))
265 //{
266 // var anim = GetComponentInChildren<HandCopyAvg>(true).gameObject.GetComponent<Animator>();
267 // anim.enabled = true;
268 // anim.SetTrigger("idle");
269 //}
270 //if (!animIsOver && !handAnim && handCopyAVG && GetComponentInChildren<HandCopyAvg>(true).gameObject.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).IsName("idle"))
271 //{
272 // animIsOver = true;
273 // var anim = GetComponentInChildren<HandCopyAvg>(true).gameObject.GetComponent<Animator>();
274 // anim.enabled = false;
275 // handCopyAVG.enabled = true;
276
277 //}
278
279 }
280
281 public override GrabTypes GetGrabStarting(GrabTypes explicitType = GrabTypes.None)
282 {
283#if !DANA
284 if (SavedUser.instance.courseType == SavedUser.CourseType.Dobot)
285 {
286 return base.GetGrabStarting(explicitType);
287 }
288#endif
289 if (timeout <= 0)
290 {
291 return GrabTypes.Grip;
292 }
293 return GrabTypes.None;
294 }
295
296 public override bool IsGrabbingWithType(GrabTypes type)
297 {
298#if !DANA
299 if (SavedUser.instance.courseType == SavedUser.CourseType.Dobot)
300 {
301 return base.IsGrabbingWithType(type);
302 }
303#endif
304 if (timeout <= 0)
305 {
306 return true;
307 }
308 return false;
309 }
310 internal void CheckNow()
311 {
312 delay = true;
313 }
314 public override void DetachObject(GameObject objectToDetach, bool restoreOriginalParent = true)
315 {
316 int index = attachedObjects.FindIndex(l => l.attachedObject == objectToDetach);
317 if (index == -1) return;
318
319 //Turn to isKinematic the rigidbody on the hand --> else the hand fly in after attached the object
320 if (GetComponentInChildren<HandCopyAvg>() && GetComponentInChildren<HandCopyAvg>().GetComponent<Rigidbody>())
321 {
322 //GetComponentInChildren<HandCopyAvg>().GetComponent<Rigidbody>().isKinematic = false;
323 //GetComponentInChildren<HandCopyAvg>().GetComponent<Rigidbody>().collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
324 GetComponentInChildren<HandCopyAvg>().GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;
325 }
326
327 // attachedToHand = false;
328#if DANA
329 if (handAnim)
330 {
331
332 handAnim = false;
333 PinchAnimOff();
334 GetComponentInChildren<HandCopyAvg>(true).enabled = true;
335 //StartCoroutine(DelayedAnimOff());
336
337
338 }
339#endif
340 attachedToHand = false;
341 //StartCoroutine(DelayedDetachHand());
342 collidersSet(true);
343 flyIn = 0;
344 exchangeSuccess = true;
345 var info = currentAttachedObjectInfo;
346 var tcd = objectToDetach.GetComponent<ThrowableCanDisable>();
347 var tempUE = new UnityEvent();
348 if (index != -1)
349 {
350 timeout = 1.0f;
351 if (otherHand && (otherHand as HackedHand).HandExchange)
352 {
353 //var connectedJoints = otherHand.GetComponentsInChildren<ConfigurableJoint>();
354 //var connectedJoint = connectedJoints[0];
355 //if(connectedJoint != null)
356 // connectedJoint.connectedBody = null;
357
358 if (tcd)
359 {
360 tempUE = tcd.onDetachFromHand;
361 tcd.onDetachFromHand = new UnityEvent();
362 }
363 }
365
366 //if (tcd)
367 //{
368 // var cloneHand = Instantiate(gameObject, tcd.transform, true);
369 // DestroyImmediate(cloneHand.GetComponent<HackedHand>());
370 // DestroyImmediate(cloneHand.GetComponent<ModelRenderer>());
371 // DestroyImmediate(cloneHand.GetComponentInChildren<Animator>());
372 //}
373#if DANA
375 if (objectToDetach.name.Contains("Csavar"))
376 {
377 FindObjectOfType<PromoHelper>().PlaySound(5);
378 }
379 if (objectToDetach.name.Contains("oszlop"))
380 {
381 FindObjectOfType<PromoHelper>().PlaySound(1);
382 }
383 if (objectToDetach.name.Contains("base"))
384 {
385 FindObjectOfType<PromoHelper>().PlaySound(3);
386 }
387 if (objectToDetach.name.Contains("topBack"))
388 {
389 FindObjectOfType<PromoHelper>().PlaySound(3);
390 }
391#endif
392 }
393 if (objectToDetach.GetComponent<TwoHandGrab>())
394 {
395 base.setTwoHand(false);
396 delay = false;
397 objectToDetach.GetComponent<ThrowableCanDisable>().onDetachFromHand.RemoveListener(CheckNow);
398#if !DANA
399 otherHand.GetComponentInChildren<SkinnedMeshRenderer>().enabled = true;
400#endif
401 //otherHand.GetComponentInChildren<ConfigurableJoint>().connectedBody = null;
402 }
403
404 var rb = objectToDetach.GetComponentInChildren<Rigidbody>();
405 if (rb != null)
406 {
407 rb.collisionDetectionMode = CollisionDetectionMode.Discrete;
408 }
409
410 var ph = GetComponent<PhysicsHand>();
411 if (ph != null)
412 {
413 ph.SetHoldItemRadius(0);
414 ph.SetHoldItemBounds(default);
415 ph.SetHoldItemRenderer(null);
416 Macro.SetLayerRecursive(objectToDetach, "Default");
417 }
418
419 base.DetachObject(objectToDetach, restoreOriginalParent);
420
421 if (index != -1)
422 {
423 if (otherHand && (otherHand as HackedHand).HandExchange)
424 {
425 //var connectedJoints = otherHand.GetComponentsInChildren<ConfigurableJoint>();
426 //var connectedJoint = connectedJoints[0];
427 //if (connectedJoint != null)
428 // connectedJoint.connectedBody = null;
429
430 if (tcd) tcd.onDetachFromHand = tempUE;
431 if (objectToDetach.transform.parent)
432 {
433 }
434 else
435 {
436 exchangeSuccess = false;
437 }
438 }
439 onDetachObject.Invoke();
440 }
441
442 if (returnItem && objectStart != default(Vector3) && objectRot != default(Quaternion))
443 {
444 objectToDetach.transform.position = objectStart;
445 objectToDetach.transform.rotation = objectRot;
446 }
447
448 if (GetComponentInChildren<HandAnimHelper>())
449 GetComponentInChildren<HandAnimHelper>().gameObject.transform.localEulerAngles = originalRotation;
450
451 objectStart = default(Vector3);
452 objectRot = default(Quaternion);
453
454 if (forceAttachParent && info.HasValue)
455 {
456 objectToDetach.transform.SetParent(info.Value.originalParent.transform, true);
457
458 }
459
460 //if (GetComponentInChildren<HandCopyAvg>())
461 //{
462 // GetComponentInChildren<HandCopyAvg>(true).gameObject.transform.localPosition = OriginalHandPosition;
463 // GetComponentInChildren<HandCopyAvg>(true).gameObject.transform.localEulerAngles = OriginalHandRotation;
464 //}
465
466 //objectToDetach.transform.localScale = new Vector3(Mathf.Abs(objectToDetach.transform.localScale.x), Mathf.Abs(objectToDetach.transform.localScale.y), Mathf.Abs(objectToDetach.transform.localScale.z));
467 if (objectToDetach.GetComponent<TwoHandGrab>())
468 {
469 //StartCoroutine(DelayColliderTurnOff(objectToDetach));
470 }
471 }
472
473
474 public override void AttachObject(GameObject objectToAttach, GrabTypes grabbedWithType,
475 AttachmentFlags flags = defaultAttachmentFlags, Transform attachmentOffset = null)
476
477 {
478 AttachedObject attachedObject = new AttachedObject();
479 attachedObject.attachmentFlags = flags;
480 attachedObject.attachedOffsetTransform = attachmentOffset;
481 attachedObject.attachTime = Time.time;
482 attachedObject.attachedObject = objectToAttach;
483 var savedrot = objectToAttach.transform.rotation;
484 if (flags == 0)
485 {
486 flags = defaultAttachmentFlags;
487 }
488 if (otherHand && otherHand.attachedObjects.Count > 0 && attachedObjects.Count > 0)
489 {
490 return;
491 }
492 //if (!isGrabbed()) return;
493
494 attachedToHand = true;
495
496 float volume = 0.0f;
497 if (objectToAttach.GetComponentInChildren<MeshRenderer>())
498 {
499 var size = objectToAttach.GetComponentInChildren<MeshRenderer>().bounds.size;
500 volume = size.x * size.y * size.z;
501 }
502 //so that the hand animation does not get stuck above anything
503 //if (attachedObject.HasAttachFlag(AttachmentFlags.SnapOnAttach) || flags.HasFlag(AttachmentFlags.SnapOnAttach))
504 //{
505 collidersSet(false);
506 //}
507
508
509 if (GetComponentInChildren<HandCopyAvg>() && GetComponentInChildren<HandCopyAvg>().GetComponent<Rigidbody>())
510 {
511 GetComponentInChildren<HandCopyAvg>().GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll;
512 }
513 if (GetComponentInChildren<HandCopyAvg>())
514 {
515 if (!SavedUser.instance.isVarjo || ToolPositioner.instance.IsTool(objectToAttach) || volume <= 0.0004f || flags.HasFlag(AttachmentFlags.SnapOnAttach))
516 {
517 GetComponentInChildren<HandCopyAvg>(true).gameObject.transform.localPosition = OriginalHandPosition;
518 GetComponentInChildren<HandCopyAvg>(true).gameObject.transform.localEulerAngles = OriginalHandRotation;
519 }
520
521 }
522 if (Vector3.Distance(objectToAttach.transform.position, hoverSphereTransform.position) > 0.1f && objectToAttach.GetComponent<TwoHandGrab>() == null)
523 {
524#if UNITY_EDITOR
525 Debug.LogError("mi a fene " + name);
526#endif
527 }
528 Transform trans = null;
529 var tcd = objectToAttach.GetComponent<ThrowableCanDisable>();
530 var rb = objectToAttach.GetComponentInParent<Rigidbody>();
531#if !UNITY_WEBGL
532 if (objectToAttach.GetComponent<TwoHandGrab>())
533 {
534 base.setTwoHand(true);
535 objectToAttach.GetComponent<TwoHandGrab>().enabled = true;
536 twoHand = true;
537 tcd.DelayedOnPickUp?.AddListener(CheckNow);
538#if !DANA
539 otherHand.GetComponentInChildren<SkinnedMeshRenderer>().enabled = false;
540
541 //flags ^= AttachmentFlags.ParentToHand;
542#endif
543 }
544#endif
545
546 if (SavedUser.instance.courseType == SavedUser.CourseType.Dobot)
547 {
548 if (objectToAttach.name != "DobotGrabSphere")
549 return;
550 }
551
552 leftSpawn = false;
553 if (objectStart == default(Vector3) && objectRot == default(Quaternion))
554 {
555 objectStart = objectToAttach.transform.position;
556 objectRot = objectToAttach.transform.rotation;
557 }
558 timeout = 0;
559 GameObject origiParent = null;
560 if (otherHand && otherHand.ObjectIsAttached(objectToAttach))
561 {
562 HandExchange = true;
563 origiParent = otherHand.AttachedObjects[0].originalParent;
564
565 //var connectedJoints = otherHand.GetComponentsInChildren<ConfigurableJoint>();
566 //var connectedJoint = connectedJoints[0];
567 //if (connectedJoint != null)
568 // connectedJoint.connectedBody = null;
569 }
571 var offsetHolder = objectToAttach.GetComponent<OffsetHolder>();
572
573 if (!SavedUser.instance.isVarjo || ToolPositioner.instance.IsTool(objectToAttach) || volume <= 0.0004f || flags.HasFlag(AttachmentFlags.SnapOnAttach))
574 if (objectAttachmentPoint)
575 {
576 objectToAttach.transform.position = objectAttachmentPoint.position;
577 if (handType == SteamVR_Input_Sources.RightHand)
578 {
579 if (tcd && tcd.attachmentOffset)
580 {
581 Quaternion rotDiff = Quaternion.Inverse(tcd.attachmentOffset.transform.rotation) * objectToAttach.transform.rotation;
582 objectToAttach.transform.rotation = objectAttachmentPoint.rotation * rotDiff;
583 objectToAttach.transform.position += objectAttachmentPoint.position - tcd.attachmentOffset.position;
584 }
585 }
586 else
587 {
588 if (tcd && tcd.attachmentOffsetLeft)
589 {
590 Quaternion rotDiff = Quaternion.Inverse(tcd.attachmentOffsetLeft.transform.rotation) * objectToAttach.transform.rotation;
591 objectToAttach.transform.rotation = objectAttachmentPoint.rotation * rotDiff;
592 objectToAttach.transform.position += objectAttachmentPoint.position - tcd.attachmentOffsetLeft.position;
593 }
594 }
595 }
596
597//#if DANA
598 if (offsetHolder && (offsetHolder.Offset != default || offsetHolder.Rotation != default) && handType == SteamVR_Input_Sources.RightHand/* && !objectToAttach.GetComponent<TwoHandGrab>()*/)
599 {
600 if (ToolPositioner.instance.IsTool(objectToAttach) || !SavedUser.instance.isVarjo || volume <= 0.0004f || tcd.IsSmall() || flags.HasFlag(AttachmentFlags.SnapOnAttach))
601 {
602 trans = new GameObject("offset").transform;
603 trans.SetParent(objectToAttach.transform);
604 //if (offsetHolder.Offset != default)
605 trans.localPosition = offsetHolder.Offset;
606 if (offsetHolder.Rotation != default)
607 trans.localEulerAngles = offsetHolder.Rotation;
608 attachmentOffset = trans;
609 }
610 }
611 if (offsetHolder && (offsetHolder.LeftOffset != default || offsetHolder.LeftRotation != default) && handType == SteamVR_Input_Sources.LeftHand/* && !objectToAttach.GetComponent<TwoHandGrab>()*/)
612 {
613 if (ToolPositioner.instance.IsTool(objectToAttach) || !SavedUser.instance.isVarjo || volume <= 0.0004f || tcd.IsSmall() || flags.HasFlag(AttachmentFlags.SnapOnAttach))
614 {
615 trans = new GameObject("offset").transform;
616 //StartCoroutine(LateReScale(objectToAttach));
617 //objectToAttach.transform.localScale = new Vector3(Mathf.Abs(objectToAttach.transform.localScale.x), Mathf.Abs(objectToAttach.transform.localScale.y), Mathf.Abs(objectToAttach.transform.localScale.z));
618 trans.SetParent(objectToAttach.transform);
619 //if (offsetHolder.LeftOffset != default)
620 trans.localPosition = offsetHolder.LeftOffset;
621 if (offsetHolder.LeftRotation != default)
622 trans.localEulerAngles = offsetHolder.LeftRotation;
623 attachmentOffset = trans;
624 //StartCoroutine(LateReScale(objectToAttach));
625 //objectToAttach.transform.localScale = new Vector3(Mathf.Abs(objectToAttach.transform.localScale.x), Mathf.Abs(objectToAttach.transform.localScale.y), Mathf.Abs(objectToAttach.transform.localScale.z));
626 }
627 }
628//#endif
629
630 var ph = GetComponent<PhysicsHand>();
631 if (ph != null/* && !objectToAttach.GetComponent<TwoHandGrab>()*/)
632 {
633 var itemBounds = Macro.GetBounds(objectToAttach);
634 ph.SetHoldItemRadius(itemBounds.extents.magnitude);
635 ph.SetHoldItemBounds(itemBounds);
636 ph.SetHoldItemRenderer(objectToAttach.GetComponentInChildren<Renderer>());
637 Macro.SetLayerRecursive(objectToAttach, "HoldItemLayer");
638 }
639
640 if (forceAttachParent/* && !objectToAttach.GetComponent<TwoHandGrab>()*/)
641 {
642 flags ^= AttachmentFlags.ParentToHand;
643
644 if (!SavedUser.instance.isVarjo || ToolPositioner.instance.IsTool(objectToAttach) || volume <= 0.0004f)
645 {
646 flags |= AttachmentFlags.SnapOnAttach;
647 objectToAttach.GetComponent<ThrowableCanDisable>().attachmentFlags |= AttachmentFlags.SnapOnAttach;
648 }
649
650 }
651 base.AttachObject(objectToAttach, grabbedWithType, flags, attachmentOffset);
652
653 if (!SavedUser.instance.isVarjo || ToolPositioner.instance.IsTool(objectToAttach) || volume <= 0.0004f)
655 {
656 objectToAttach.transform.position = forceAttachPosition.position;
657 }
658
659 //var rb = objectToAttach.GetComponentInParent<Rigidbody>();
660 if (MiscLogicManager.instance.UseItemCollision && rb)
661 {
662 rb.useGravity = false;
663 rb.isKinematic = false;
664
665 //var connectedJoints = otherHand.GetComponentsInChildren<ConfigurableJoint>();
666 //var connectedJoint = connectedJoints[0];
667 //connectedJoint.connectedBody = rb;
668 //rb.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
669 //connectedJoint.autoConfigureConnectedAnchor = true;
670 }
671
672 if (HandExchange && otherHand && !(otherHand as HackedHand).exchangeSuccess/* && !objectToAttach.GetComponent<TwoHandGrab>()*/)
673 {
674 var temp = attachedObjects[0];
675 temp.originalParent = origiParent;
676 attachedObjects.Clear();
677 attachedObjects.Add(temp);
678 }
679 onAttachObject.Invoke();
680 HandExchange = false;
681 flyIn = 1;
682#if !UNITY_WEBGL
683 if (tcd/* && !objectToAttach.GetComponent<TwoHandGrab>()*/)
684 {
685 if (tcd.handRotate != default && tcd.handRotate != Vector3.zero)
686 {
687 var tcdt = GetComponentInChildren<HandAnimHelper>().gameObject.transform;
688
689 if (objectAttachmentPoint)
690 {
691 objectAttachmentPoint.localEulerAngles = attachOriginalRotation;
692 objectAttachmentPoint.localEulerAngles += tcd.handRotate;
693 }
694 tcdt.localEulerAngles = originalRotation;
695 tcdt.localEulerAngles += tcd.handRotate;
696 }
697 }
698#if !DANA
699 if (objectToAttach.GetComponent<TwoHandGrab>())
700 {
701 objectToAttach.transform.SetParent(GetComponentInChildren<FollowOtherHand>().transform, true);
702 objectToAttach.transform.localPosition = Vector3.zero;
703 StartCoroutine(LateRotate(objectToAttach.transform, savedrot));
704 }
705#endif
706#endif
707
708 //objectToAttach.transform.localScale = new Vector3(Mathf.Abs(objectToAttach.transform.localScale.x), Mathf.Abs(objectToAttach.transform.localScale.y), Mathf.Abs(objectToAttach.transform.localScale.z));
709 if (forceAttachParent/* && !objectToAttach.GetComponent<TwoHandGrab>()*/)
710 {
711 if (objectToAttach.GetComponent<TwoHandGrab>())
712 {
713 objectToAttach.transform.SetParent(GetComponentInChildren<FollowOtherHand>().transform, true);
714 objectToAttach.transform.localPosition = Vector3.zero;
715 StartCoroutine(LateRotate(objectToAttach.transform, savedrot));
716 }
717
718 else objectToAttach.transform.SetParent(forceAttachParent, true);
719 //objectToAttach.transform.SetParent(transform, true);
720 //StartCoroutine(LateReparent(objectToAttach));
721 //objectToAttach.transform.localScale = new Vector3(Mathf.Abs(objectToAttach.transform.localScale.x), Mathf.Abs(objectToAttach.transform.localScale.y), Mathf.Abs(objectToAttach.transform.localScale.z));
722 }
723 if (trans)
724 {
725 //Destroy(trans.gameObject);
726 }
727
728 //if (GetComponentInChildren<HandCopyAvg>())
729 //{
730 // GetComponentInChildren<HandCopyAvg>(true).gameObject.transform.localPosition = OriginalHandPosition;
731 // GetComponentInChildren<HandCopyAvg>(true).gameObject.transform.localEulerAngles = OriginalHandRotation;
732 //}
733 if (attachedObject.HasAttachFlag(AttachmentFlags.SnapOnAttach) || flags.HasFlag(AttachmentFlags.SnapOnAttach))
734 {
735
736 collidersSet(false);
737 if (!ToolPositioner.instance.IsTool(objectToAttach))
738 {
739 handAnim = true;
740 PinchAnimOn();
741 animIsOver = false;
742 //StartCoroutine(DelayedAnimOn());
743 }
744 }
745 if (objectToAttach.GetComponent<TwoHandGrab>())
746 {
747 //rb.isKinematic = false;
748 //rb.constraints = RigidbodyConstraints.FreezePosition;
749 }
750 //objectToAttach.transform.localScale = new Vector3(Mathf.Abs(objectToAttach.transform.localScale.x), Mathf.Abs(objectToAttach.transform.localScale.y), Mathf.Abs(objectToAttach.transform.localScale.z));
751
752 }
753
754 IEnumerator LateRotate(Transform t, Quaternion rot)
755 {
756 yield return null;
757 t.rotation = rot;
758 yield return null;
759 t.rotation = rot;
760 yield return null;
761 t.rotation = rot;
762 }
763 IEnumerator LateReparent(GameObject objectToAttach)
764 {
765 yield return new WaitForSeconds(1);
766 objectToAttach.transform.SetParent(forceAttachParent, true);
767 }
768 IEnumerator DelayColliderTurnOff(GameObject objectToDetach)
769 {
770 objectToDetach.GetComponentInChildren<Collider>().enabled = false;
771 objectToDetach.GetComponent<Rigidbody>().isKinematic = false;
772 yield return new WaitForSeconds(1);
773 objectToDetach.GetComponentInChildren<Collider>().enabled = true;
774 objectToDetach.GetComponent<Rigidbody>().isKinematic = true;
775
776 }
777
778 IEnumerator DelayKinematicTurnOff(ThrowableCanDisable tcd, Rigidbody rigidBody)
779 {
780 yield return null;
781 rigidBody.isKinematic = false;
782 rigidBody.useGravity = true;
783 }
784 IEnumerator LateReOrient()
785 {
786
787 float time = 0;
788 float duration = 0.75f;
789
790 while (time <= duration)
791 {
792 time += Time.deltaTime;
793 if (GetComponentInChildren<HandCopyAvg>())
794 {
795 var GOLocalPos = GetComponentInChildren<HandCopyAvg>(true).gameObject.transform.localPosition;
796 var GOLocalRot = GetComponentInChildren<HandCopyAvg>(true).gameObject.transform.localEulerAngles;
797
798 GetComponentInChildren<HandCopyAvg>(true).gameObject.transform.localPosition = Vector3.Lerp(GOLocalPos, OriginalHandPosition, time / duration);
799 GetComponentInChildren<HandCopyAvg>(true).gameObject.transform.localEulerAngles = Vector3.Lerp(GOLocalRot, OriginalHandRotation, time / duration);
800
801 }
802 yield return null;
803 }
804 }
805
806 IEnumerator LateReScale(GameObject objectToAttach)
807 {
808 objectToAttach.transform.localScale = new Vector3(Mathf.Abs(objectToAttach.transform.localScale.x), Mathf.Abs(objectToAttach.transform.localScale.y), Mathf.Abs(objectToAttach.transform.localScale.z));
809 yield return null;
810 }
811
812 public override void HideGrabHint()
813 {
814 //nothin'
815 }
816
817 public override void TriggerHapticPulse(ushort microSecondsDuration)
818 {
819 float seconds = (float)microSecondsDuration / 1000000f;
820 if (hapticAction != null)
821 hapticAction.Execute(0, seconds, 1f / seconds, 1, handType);
822 }
823
824 public void ForceDrop()
825 {
826 if (currentAttachedObject == null) return;
827 DetachObject(currentAttachedObject);
828 }
829
830 private bool isInFieldOfView(GameObject interactedGO)
831 {
832 Vector3 screenPoint = VRCamera.WorldToViewportPoint(interactedGO.transform.position);
833 bool onScreen = screenPoint.z > 0 && screenPoint.x > 0 && screenPoint.x < 1 && screenPoint.y > 0 && screenPoint.y < 1;
834 return onScreen;
835 }
836
837 protected override void UpdateHovering()
838 {
839#if DANA
840 var handEnabled = GetComponent<HandEnableDisable>().Hand.activeInHierarchy;
841 if (SavedUser.instance.isEditor || !handEnabled) return;
842#endif
843 hoveringInteractable = null;
844 if ((noSteamVRFallbackCamera == null) && (isActive == false))
845 {
846 return;
847 }
848
849 if (hoverLocked)
850 return;
851
852 //if (!handHudLed.activeSelf)
853 // return;
854
855 if (applicationLostFocusObject.activeSelf)
856 return;
857
858 float closestDistance = float.MaxValue;
859 Interactable closestInteractable = null;
860
861 if (useHoverSphere)
862 {
863 float scaledHoverRadius = hoverSphereRadius * Mathf.Abs(SteamVR_Utils.GetLossyScale(hoverSphereTransform));
864 CheckHoveringForTransform(hoverSphereTransform.position, scaledHoverRadius, ref closestDistance, ref closestInteractable, Color.green);
865 }
866
867 if (useControllerHoverComponent && mainRenderModel != null && mainRenderModel.IsControllerVisibile())
868 {
869 float scaledHoverRadius = controllerHoverRadius * Mathf.Abs(SteamVR_Utils.GetLossyScale(this.transform));
870 CheckHoveringForTransform(mainRenderModel.GetControllerPosition(controllerHoverComponent), scaledHoverRadius / 2f, ref closestDistance, ref closestInteractable, Color.blue);
871 }
872
873 if (useFingerJointHover && /*mainRenderModel != null && mainRenderModel.IsHandVisibile() &&*/ FingerJointBoneParent)
874 {
875#if DANA
876 var hca = FingerJointBoneParent.GetComponentInParent<HandCopyAvg>();
877 var hcl = FingerJointBoneParent.GetComponentInParent<HandCopyLeap>();
878 if (!hca && !hcl)
879 {
880 Debug.LogWarning("HandCopyAvg and HandCopyLeap is NULL but using useFingerJointHover!");
881 return;
882 }
883 var thumbCollider = hca?.thumb2[2].GetComponent<Collider>();
884 if (!thumbCollider)
885 thumbCollider = hcl?.thumb2[2].GetComponent<Collider>();
886
887 bool isThumbCollided = false;
888 var allboneColliders = FingerJointBoneParent.GetComponentsInChildren<Collider>();
889 var numOfColliders = 0;
890 collidedFingers.Clear();
891 var lastDistance = float.MaxValue;
892 //System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
893 foreach (var collider in allboneColliders)
894 {
895 float scaledHoverRadius = fingerJointHoverRadius * collider.bounds.size.x;
896 CheckHoveringForTransform(collider.transform.position, scaledHoverRadius / 2f, ref closestDistance, ref closestInteractable, Color.yellow);
897 if (lastDistance != closestDistance)
898 {
899 numOfColliders++;
900 if (collider.gameObject.name == thumbCollider.gameObject.name)
901 {
902 isThumbCollided = true;
903 }
904 if (!collidedFingers.Contains(collider.gameObject))
905 {
906 collidedFingers.Add(collider.gameObject);
907 collidedFingers.Add(collider.gameObject.transform.parent.gameObject);
908 collidedFingers.Add(collider.gameObject.transform.parent.transform.parent.gameObject);
909 }
910
911
912 }
913
914 lastDistance = closestDistance;
915 }
916#endif
917#if !UNITY_WEBGL
918 //if (attachedToHand && Macro.CollidesWithTwoHandGrab(GetComponentInChildren<HandCopySkelet>().gameObject, false) || (otherHand as HackedHand).attachedToHand && Macro.CollidesWithTwoHandGrab(GetComponentInChildren<HandCopySkelet>().gameObject, false))
919 //{
920 // return;
921 //}
922#if DANA
923 if (attachedToHand && Macro.CollidesWithTwoHandGrab(GetComponentInChildren<HandCopySkelet>().gameObject, false) || (otherHand as HackedHand).attachedToHand && Macro.CollidesWithTwoHandGrab(GetComponentInChildren<HandCopySkelet>().gameObject, false))
924 {
925 return;
926 }
927 //if we reach into something too quickly you can’t pick it up
928
929 if (GetComponentInChildren<HandCopyAvg>(true).speedometer() > 1.0f) return;
930#else
931 if (attachedToHand && Macro.CollidesWithTwoHandGrab(gameObject, false) || (otherHand as HackedHand).attachedToHand && Macro.CollidesWithTwoHandGrab(gameObject, false))
932 {
933 return;
934 }
935#endif
936 if (GetComponentInChildren<HandCopySkelet>() && !attachedToHand && !Macro.CollidesWithSnapOnAttach(GetComponentInChildren<HandCopySkelet>().gameObject, false) && ConveyorBeltManager.instance.started && !GetComponentInChildren<HandCopySkelet>().isPOI)
937 {
938#if DANA
939 if (attachedToHand) return;
940 //return if you reach with less than 3 fingers
941 if (Macro.CollidesWithFingers(GetComponentInChildren<HandCopySkelet>().gameObject, false).Count < 3)
942 {
943 return;
944 }
945 var thumb = Macro.CollidesWithFingers(GetComponentInChildren<HandCopySkelet>().gameObject, false);
946
947 //return if you don't reach with your thumb finger
948 if (!thumb.Contains(GetComponentInChildren<HandCopyAvg>().thumb2[2]) && !thumb.Contains(GetComponentInChildren<HandCopyAvg>().thumb1[2].transform.GetChild(0).gameObject)) return;
949 //return if you don't reach with your index finger
950 if (!thumb.Contains(GetComponentInChildren<HandCopyAvg>().to[2]) && !thumb.Contains(GetComponentInChildren<HandCopyAvg>().from[2].transform.GetChild(0).gameObject)) return;
951 if (Macro.CollidesWithTwoHandGrab(GetComponentInChildren<HandCopySkelet>().gameObject, false))
952 {
953 if (!otherHand.GetComponentInChildren<HandCopySkelet>().isCollision)
954 {
955 return;
956 }
957 }
958#else
959 if (Macro.CollidesWithTwoHandGrab(gameObject, false))
960 {
961 if (!otherHand.GetComponentInChildren<HandCopySkelet>().isCollision)
962 {
963 return;
964 }
965 }
966#endif
967
968 }
969
970#endif
971
972 }
973
974 // Hover on this one
975 if (closestInteractable && closestInteractable.hideHighlight == null)
976 {
977 closestInteractable.hideHighlight = new GameObject[0];
978 }
979 if (VRCamera != null && closestInteractable != null)
980 {
981 if (!isInFieldOfView(closestInteractable.gameObject))
982 {
983 return;
984 }
985 }
986
987 hoveringInteractable = closestInteractable;
988 }
989 IEnumerator DelayedAnimOff()
990 {
991 var anim = GetComponentInChildren<HandCopyAvg>(true).gameObject.GetComponent<Animator>();
992 anim.enabled = true;
993 anim.SetTrigger("idle");
994 collidersSet(true);
995 yield return new WaitForSeconds(0.6f);
996 anim.enabled = false;
997 GetComponentInChildren<HandCopyAvg>(true).enabled = true;
998
999
1000 }
1001 IEnumerator DelayedDetachHand()
1002 {
1003 yield return null;
1004 attachedToHand = false;
1005 }
1006 internal void PinchAnimOff()
1007 {
1008 GetComponentInChildren<HandCopyAvg>(true).gameObject.GetComponent<Animator>().SetTrigger("idle");
1009 StartCoroutine(DelayedAnimOff());
1010
1011 }
1012 internal void PinchAnimOn()
1013 {
1014 GetComponentInChildren<HandCopyAvg>().GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll;
1015#if DANA
1016 GetComponentInChildren<HandCopyAvg>(true).gameObject.GetComponent<Animator>().enabled = true;
1017 GetComponentInChildren<HandCopyAvg>(true).gameObject.GetComponent<Animator>().SetTrigger("pinch");
1018 GetComponentInChildren<HandCopyAvg>(true).enabled = false;
1019#endif
1020 }
1021 IEnumerator DelayedAnimOn()
1022 {
1023 GetComponentInChildren<HandCopyAvg>().GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll;
1024 yield return null;
1025#if DANA
1026 GetComponentInChildren<HandCopyAvg>(true).gameObject.GetComponent<Animator>().enabled = true;
1027 GetComponentInChildren<HandCopyAvg>(true).gameObject.GetComponent<Animator>().SetTrigger("pinch");
1028 GetComponentInChildren<HandCopyAvg>(true).enabled = false;
1029#endif
1030
1031
1032 }
1033 internal void collidersSet(bool set)
1034 {
1035 var colliders = GetComponentsInChildren<Collider>();
1036 var FastIK = GetComponentsInChildren<FastIKFabric>();
1037 var Tracer = GetComponentsInChildren<FastIKTargetTracer>();
1038 var physicsHand = GetComponentInChildren<PhysicsHand>(true);
1039 if (physicsHand)
1040 {
1041 physicsHand.enabled = set;
1042 }
1043 foreach (var coll in colliders)
1044 {
1045 if (!coll.gameObject.GetComponent<MeshCollider>() && !coll.gameObject.GetComponent<ThrowableCanDisable>())
1046 {
1047 coll.enabled = set;
1048 }
1049
1050 }
1051 foreach (var item in FastIK)
1052 {
1053 item.enabled = set;
1054 }
1055 foreach (var t in Tracer)
1056 {
1057 t.enabled = set;
1058 }
1059 }
1060 protected bool isGrabbed()
1061 {
1062 float closestDistance = float.MaxValue;
1063 Interactable closestInteractable = null;
1064
1065 if (useHoverSphere)
1066 {
1067 float scaledHoverRadius = hoverSphereRadius * Mathf.Abs(SteamVR_Utils.GetLossyScale(hoverSphereTransform));
1068 CheckHoveringForTransform(hoverSphereTransform.position, scaledHoverRadius, ref closestDistance, ref closestInteractable, Color.green);
1069 }
1070
1071 if (useControllerHoverComponent && mainRenderModel != null && mainRenderModel.IsControllerVisibile())
1072 {
1073 float scaledHoverRadius = controllerHoverRadius * Mathf.Abs(SteamVR_Utils.GetLossyScale(this.transform));
1074 CheckHoveringForTransform(mainRenderModel.GetControllerPosition(controllerHoverComponent), scaledHoverRadius / 2f, ref closestDistance, ref closestInteractable, Color.blue);
1075 }
1076 if (useFingerJointHover && /*mainRenderModel != null && mainRenderModel.IsHandVisibile() &&*/ FingerJointBoneParent)
1077 {
1078 var thumbCollider = FingerJointBoneParent.GetComponentInParent<HandCopyAvg>().thumb2[2].GetComponent<Collider>();
1079 bool isThumbCollided = false;
1080 var allboneColliders = FingerJointBoneParent.GetComponentsInChildren<Collider>();
1081 var numOfColliders = 0;
1082 var lastDistance = float.MaxValue;
1083 foreach (var collider in allboneColliders)
1084 {
1085 float scaledHoverRadius = fingerJointHoverRadius * collider.bounds.size.x;
1086 CheckHoveringForTransform(collider.transform.position, scaledHoverRadius / 2f, ref closestDistance, ref closestInteractable, Color.yellow);
1087 if (lastDistance != closestDistance)
1088 {
1089 numOfColliders++;
1090 if (collider.gameObject.name == thumbCollider.gameObject.name)
1091 {
1092 isThumbCollided = true;
1093 }
1094 }
1095 lastDistance = closestDistance;
1096 }
1097 //Debug.Log("num of colliders: " + numOfColliders);
1098 //return;
1099 if (numOfColliders > 2 && isThumbCollided)
1100 {
1101 return true;
1102 }
1103 }
1104 return false;
1105 }
1106
1107#endif
1108 }
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
UnityEngine.Color Color
Definition: TestScript.cs:32
override void HideGrabHint()
Definition: HackedHand.cs:812
override void UpdateHovering()
Definition: HackedHand.cs:837
GameObject InstantiatedHand
Definition: HackedHand.cs:55
bool isGrabbed()
Definition: HackedHand.cs:1060
Transform forceAttachParent
Definition: HackedHand.cs:30
bool returnItem
Definition: HackedHand.cs:28
bool flyInHand
Definition: HackedHand.cs:31
override void Update()
Definition: HackedHand.cs:176
Transform FingerJointBoneParent
Definition: HackedHand.cs:23
override GrabTypes GetGrabStarting(GrabTypes explicitType=GrabTypes.None)
Definition: HackedHand.cs:281
override void AttachObject(GameObject objectToAttach, GrabTypes grabbedWithType, AttachmentFlags flags=defaultAttachmentFlags, Transform attachmentOffset=null)
Definition: HackedHand.cs:474
override void DetachObject(GameObject objectToDetach, bool restoreOriginalParent=true)
Definition: HackedHand.cs:314
bool HandExchange
Definition: HackedHand.cs:41
bool dropFive
Definition: HackedHand.cs:32
Camera VRCamera
Definition: HackedHand.cs:24
override void TriggerHapticPulse(ushort microSecondsDuration)
Definition: HackedHand.cs:817
Transform forceAttachPosition
Definition: HackedHand.cs:29
void ForceDrop()
Definition: HackedHand.cs:824
new void InitController()
Definition: HackedHand.cs:113
UnityEvent onDetachObject
Definition: HackedHand.cs:27
new bool isPoseValid
Definition: HackedHand.cs:25
GameObject handHudLed
Definition: HackedHand.cs:47
override bool IsGrabbingWithType(GrabTypes type)
Definition: HackedHand.cs:296
float timeout
Definition: HackedHand.cs:26
UnityEvent onAttachObject
Definition: HackedHand.cs:27
override IEnumerator Start()
Definition: HackedHand.cs:61
bool exchangeSuccess
Definition: HackedHand.cs:42
GameObject[] thumb2
Definition: HandCopyLeap.cs:8
Definition: Macro.cs:12
static Bounds GetBounds(GameObject go)
Definition: Macro.cs:482
static void SetLayerRecursive(GameObject go, string layerName)
Definition: Macro.cs:534
static List< GameObject > CollidesWithFingers(GameObject bounds, bool skipHand=true)
Definition: Macro.cs:777
static bool CollidesWithSnapOnAttach(GameObject bounds, bool skipHand=true)
Definition: Macro.cs:689
static bool CollidesWithTwoHandGrab(GameObject bounds, bool skipHand=true)
Definition: Macro.cs:637
bool IsTool(GameObject tool)
static ToolPositioner instance