InteractableDebug.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. //
  3. // Purpose: This object will get hover events and can be attached to the hands
  4. //
  5. //=============================================================================
  6. using UnityEngine;
  7. using UnityEngine.Events;
  8. using System.Collections;
  9. using System.Collections.Generic;
  10. namespace Valve.VR.InteractionSystem
  11. {
  12. //-------------------------------------------------------------------------
  13. public class InteractableDebug : MonoBehaviour
  14. {
  15. [System.NonSerialized]
  16. public Hand attachedToHand;
  17. public float simulateReleasesForXSecondsAroundRelease = 0;
  18. public float simulateReleasesEveryXSeconds = 0.005f;
  19. public bool setPositionsForSimulations = false;
  20. private Renderer[] selfRenderers;
  21. private Collider[] colliders;
  22. private Color lastColor;
  23. private Throwable throwable;
  24. private bool isThrowable { get { return throwable != null; } }
  25. private const bool onlyColorOnChange = true;
  26. public new Rigidbody rigidbody;
  27. private void Awake()
  28. {
  29. selfRenderers = this.GetComponentsInChildren<Renderer>();
  30. throwable = this.GetComponent<Throwable>();
  31. rigidbody = this.GetComponent<Rigidbody>();
  32. colliders = this.GetComponentsInChildren<Collider>();
  33. }
  34. private void OnAttachedToHand( Hand hand )
  35. {
  36. attachedToHand = hand;
  37. CreateMarker(Color.green);
  38. }
  39. protected virtual void HandAttachedUpdate(Hand hand)
  40. {
  41. Color grabbedColor;
  42. switch (hand.currentAttachedObjectInfo.Value.grabbedWithType)
  43. {
  44. case GrabTypes.Grip:
  45. grabbedColor = Color.blue;
  46. break;
  47. case GrabTypes.Pinch:
  48. grabbedColor = Color.green;
  49. break;
  50. case GrabTypes.Trigger:
  51. grabbedColor = Color.yellow;
  52. break;
  53. case GrabTypes.Scripted:
  54. grabbedColor = Color.red;
  55. break;
  56. case GrabTypes.None:
  57. default:
  58. grabbedColor = Color.white;
  59. break;
  60. }
  61. if ((onlyColorOnChange && grabbedColor != lastColor) || onlyColorOnChange == false)
  62. ColorSelf(grabbedColor);
  63. lastColor = grabbedColor;
  64. }
  65. private void OnDetachedFromHand( Hand hand )
  66. {
  67. if (isThrowable)
  68. {
  69. Vector3 velocity;
  70. Vector3 angularVelocity;
  71. throwable.GetReleaseVelocities(hand, out velocity, out angularVelocity);
  72. CreateMarker(Color.cyan, velocity.normalized);
  73. }
  74. CreateMarker(Color.red);
  75. attachedToHand = null;
  76. if (isSimulation == false && simulateReleasesForXSecondsAroundRelease != 0)
  77. {
  78. float startTime = -simulateReleasesForXSecondsAroundRelease;
  79. float endTime = simulateReleasesForXSecondsAroundRelease;
  80. List<InteractableDebug> list = new List<InteractableDebug>();
  81. list.Add(this);
  82. for (float offset = startTime; offset <= endTime; offset += simulateReleasesEveryXSeconds)
  83. {
  84. float lerp = Mathf.InverseLerp(startTime, endTime, offset);
  85. InteractableDebug copy = CreateSimulation(hand, offset, Color.Lerp(Color.red, Color.green, lerp));
  86. list.Add(copy);
  87. }
  88. for (int index = 0; index < list.Count; index++)
  89. {
  90. for (int otherIndex = 0; otherIndex < list.Count; otherIndex++)
  91. {
  92. list[index].IgnoreObject(list[otherIndex]);
  93. }
  94. }
  95. }
  96. }
  97. public Collider[] GetColliders()
  98. {
  99. return colliders;
  100. }
  101. public void IgnoreObject(InteractableDebug otherInteractable)
  102. {
  103. Collider[] otherColliders = otherInteractable.GetColliders();
  104. for (int myIndex = 0; myIndex < colliders.Length; myIndex++)
  105. {
  106. for (int otherIndex = 0; otherIndex < otherColliders.Length; otherIndex++)
  107. {
  108. Physics.IgnoreCollision(colliders[myIndex], otherColliders[otherIndex]);
  109. }
  110. }
  111. }
  112. private bool isSimulation = false;
  113. public void SetIsSimulation()
  114. {
  115. isSimulation = true;
  116. }
  117. private InteractableDebug CreateSimulation(Hand fromHand, float timeOffset, Color copyColor)
  118. {
  119. GameObject copy = GameObject.Instantiate(this.gameObject);
  120. InteractableDebug debugCopy = copy.GetComponent<InteractableDebug>();
  121. debugCopy.SetIsSimulation();
  122. debugCopy.ColorSelf(copyColor);
  123. copy.name = string.Format("{0} [offset: {1:0.000}]", copy.name, timeOffset);
  124. Vector3 velocity = fromHand.GetTrackedObjectVelocity(timeOffset);
  125. velocity *= throwable.scaleReleaseVelocity;
  126. debugCopy.rigidbody.velocity = velocity;
  127. return debugCopy;
  128. }
  129. private void CreateMarker(Color markerColor, float destroyAfter = 10)
  130. {
  131. CreateMarker(markerColor, attachedToHand.GetTrackedObjectVelocity().normalized, destroyAfter);
  132. }
  133. private void CreateMarker(Color markerColor, Vector3 forward, float destroyAfter = 10)
  134. {
  135. GameObject baseMarker = GameObject.CreatePrimitive(PrimitiveType.Cube);
  136. DestroyImmediate(baseMarker.GetComponent<Collider>());
  137. baseMarker.transform.localScale = new Vector3(0.02f, 0.02f, 0.02f);
  138. GameObject line = GameObject.Instantiate(baseMarker);
  139. line.transform.localScale = new Vector3(0.01f, 0.01f, 0.25f);
  140. line.transform.parent = baseMarker.transform;
  141. line.transform.localPosition = new Vector3(0, 0, line.transform.localScale.z / 2f);
  142. baseMarker.transform.position = attachedToHand.transform.position;
  143. baseMarker.transform.forward = forward;
  144. ColorThing(markerColor, baseMarker.GetComponentsInChildren<Renderer>());
  145. if (destroyAfter > 0)
  146. Destroy(baseMarker, destroyAfter);
  147. }
  148. private void ColorSelf(Color newColor)
  149. {
  150. ColorThing(newColor, selfRenderers);
  151. }
  152. private void ColorThing(Color newColor, Renderer[] renderers)
  153. {
  154. for (int rendererIndex = 0; rendererIndex < renderers.Length; rendererIndex++)
  155. {
  156. renderers[rendererIndex].material.color = newColor;
  157. }
  158. }
  159. }
  160. }