SetControllerSkin.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Linq;
  5. #if ZED_STEAM_VR
  6. using Valve.VR;
  7. #endif
  8. /// <summary>
  9. /// Creates a model to match whatever tracked object it's assigned to. Used alongsize ZEDControllerTracker.
  10. /// In SteamVR, uses SteamVR_RenderModel to handle the model. In Oculus, it loads a prefab from Resources.
  11. /// </summary>
  12. public class SetControllerSkin : MonoBehaviour
  13. {
  14. /// <summary>
  15. /// Associated ZEDControllerTracker with this object. If checkDeviceIndexEachUpdate is true, this is
  16. /// where we check the index.
  17. /// </summary>
  18. [Tooltip("Associated ZEDControllerTracker with this object. If checkDeviceIndexEachUpdate is true, this is " +
  19. "where we check the index. ")]
  20. public ZEDControllerTracker controllerTracker;
  21. /// <summary>
  22. /// Whether to regularly check the ZEDControllerTracker to see if its tracked index has changed.
  23. /// </summary>
  24. [Tooltip("Whether to regularly check the ZEDControllerTracker to see if its tracked index has changed.")]
  25. public bool checkDeviceIndexEachUpdate = true;
  26. #if ZED_OCULUS
  27. public GameObject controllerModel;
  28. #endif
  29. /// <summary>
  30. /// Returns the first MeshFilter attached to or parented to this object.
  31. /// Used when other scripts want to draw copies of it with Graphics.DrawMesh.
  32. /// </summary>
  33. public Mesh GetFirstControllerMesh()
  34. {
  35. MeshFilter filter = GetComponentInChildren<MeshFilter>();
  36. if (filter != null) return filter.mesh;
  37. else return null;
  38. }
  39. /// <summary>
  40. /// Returns the Meshes of all MeshFilters attached to or parented to this object.
  41. /// </summary>
  42. public Mesh[] GetControllerMeshes()
  43. {
  44. return GetComponentsInChildren<MeshFilter>().Select(x => x.mesh).ToArray();
  45. }
  46. #if ZED_STEAM_VR
  47. private SteamVR_RenderModel renderModel;
  48. #endif
  49. void Start ()
  50. {
  51. //controllerTracker = GetComponent<ZEDControllerTracker>();
  52. #if ZED_STEAM_VR
  53. renderModel = GetComponent<SteamVR_RenderModel>();
  54. if(!renderModel)
  55. {
  56. renderModel = gameObject.AddComponent<SteamVR_RenderModel>();
  57. }
  58. #endif
  59. }
  60. // Update is called once per frame
  61. void Update ()
  62. {
  63. if (checkDeviceIndexEachUpdate)
  64. {
  65. #if ZED_STEAM_VR
  66. if ((int)controllerTracker.index != (int)renderModel.index)
  67. {
  68. //renderModel.SetDeviceIndex((int)controllerTracker.index);
  69. SetRenderModelIndex((int)controllerTracker.index);
  70. }
  71. #endif
  72. #if ZED_OCULUS
  73. if(controllerTracker.deviceToTrack == ZEDControllerTracker.Devices.LeftController)
  74. {
  75. SetRenderModelIndex(ChooseTrackedObjectMenu.TOUCH_INDEX_LEFT);
  76. }
  77. else if (controllerTracker.deviceToTrack == ZEDControllerTracker.Devices.RightController)
  78. {
  79. SetRenderModelIndex(ChooseTrackedObjectMenu.TOUCH_INDEX_RIGHT);
  80. }
  81. #endif
  82. }
  83. }
  84. /// <summary>
  85. /// Makes the controller update its model based on the specified controller index.
  86. /// This index is the actual tracked object index in SteamVR, and in Oculus, corresponds to
  87. /// ChooseTrackedObjectMenu.TOUCH_INDEX_LEFT and _RIGHT.
  88. /// </summary>
  89. public void SetRenderModelIndex(int newindex)
  90. {
  91. #if ZED_STEAM_VR
  92. if (!renderModel)
  93. {
  94. renderModel = gameObject.AddComponent<SteamVR_RenderModel>();
  95. }
  96. renderModel.SetDeviceIndex(newindex);
  97. #elif ZED_OCULUS
  98. if (newindex == ChooseTrackedObjectMenu.TOUCH_INDEX_LEFT)
  99. {
  100. //if(controllerModel != null && controllerModel.name != "TouchControl_L")
  101. if (controllerModel != null)
  102. {
  103. Destroy(controllerModel);
  104. controllerModel = null;
  105. }
  106. GameObject lcontrolprefab = Resources.Load<GameObject>("TouchControl_L");
  107. controllerModel = Instantiate(lcontrolprefab, transform, false);
  108. }
  109. else if (newindex == ChooseTrackedObjectMenu.TOUCH_INDEX_RIGHT)
  110. {
  111. //if (controllerModel != null && controllerModel.name != "TouchControl_R")
  112. if (controllerModel != null)
  113. {
  114. Destroy(controllerModel);
  115. controllerModel = null;
  116. }
  117. GameObject rcontrolprefab = Resources.Load<GameObject>("TouchControl_R");
  118. controllerModel = Instantiate(rcontrolprefab, transform, false);
  119. }
  120. else
  121. {
  122. Debug.LogError("Passed invalid index to SetControllerSkin: Valid options are " + ChooseTrackedObjectMenu.TOUCH_INDEX_LEFT +
  123. " (left controller) or " + ChooseTrackedObjectMenu.TOUCH_INDEX_RIGHT + " (right controller).");
  124. }
  125. #endif
  126. }
  127. }