GestureMockInEditor.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using System;
  2. using System.Collections.Generic;
  3. using Rokid.UXR.Utility;
  4. using UnityEngine;
  5. using JsonUtils = Newtonsoft.Json.JsonConvert;
  6. namespace Rokid.UXR.Interaction
  7. {
  8. public struct TrackingHandData
  9. {
  10. public float[] verts_ndc;
  11. public float[] verts_cam;
  12. public float[] skeleton_ndc;
  13. public float[] quaternion;
  14. public float[] euler;
  15. public float[] rotation_axis;
  16. public int lr_hand;
  17. public int gesture_type;
  18. public int hand_orientation;
  19. public int is_pinch;
  20. };
  21. public struct TrackingFrameData
  22. {
  23. public TrackingHandData[] data;
  24. public int hand_num;
  25. };
  26. public class GestureMockInEditor : MonoBehaviour
  27. {
  28. [SerializeField]
  29. private TextAsset gestureDataCacheData;
  30. [SerializeField]
  31. private TextAsset gesFrameData;
  32. [SerializeField]
  33. private GesImplementation eventInput;
  34. private int gesFrame = 0;
  35. private List<TrackingFrameData> gestureFrame;
  36. [SerializeField]
  37. private List<GestureBean[]> gestureDataCache = new List<GestureBean[]>();
  38. private GestureBean[] gestureData = new GestureBean[2] { new GestureBean(), new GestureBean() };
  39. /// <summary>
  40. /// 模拟手势交互数据
  41. /// </summary>
  42. [SerializeField]
  43. private bool mockGesIntaction = true;
  44. /// <summary>
  45. /// 模拟手机数据
  46. /// </summary>
  47. [SerializeField]
  48. private bool mockPhoneData;
  49. private bool isRightHand = true;
  50. private bool mockGesIntactionRender;
  51. public List<TrackingFrameData> ReadTrackingFrameDataFromJSON(TextAsset gestureData)
  52. {
  53. return JsonUtils.DeserializeObject<List<TrackingFrameData>>(gestureData.ToString());
  54. }
  55. #if UNITY_EDITOR
  56. private void Start()
  57. {
  58. if (Utils.IsAndroidPlatfrom())
  59. {
  60. mockGesIntaction = false;
  61. mockPhoneData = false;
  62. }
  63. if (mockPhoneData)
  64. {
  65. gestureDataCacheData = Resources.Load<TextAsset>("3DHandData/gesDataRecord");
  66. gestureDataCache = JsonUtils.DeserializeObject<List<GestureBean[]>>(gestureDataCacheData.ToString());
  67. }
  68. if (eventInput == null)
  69. eventInput = GetComponent<GesImplementation>();
  70. ChangeHand();
  71. }
  72. private void LateUpdate()
  73. {
  74. if (mockGesIntaction)
  75. MockGesInteraction();
  76. if (mockPhoneData)
  77. MockPhoneData();
  78. }
  79. #endif
  80. private void ChangeHand()
  81. {
  82. isRightHand = !isRightHand;
  83. DataCache.Instance.Add("ThreeGesMockInEditor_ShowHandType",
  84. isRightHand ? HandType.RightHand : HandType.LeftHand, true);
  85. }
  86. /// <summary>
  87. /// 模拟手机数据
  88. /// </summary>
  89. private void MockPhoneData()
  90. {
  91. if (gesFrame >= gestureDataCache.Count - 1)
  92. {
  93. gesFrame = 0;
  94. }
  95. try
  96. {
  97. eventInput.ProcessData(gestureDataCache[gesFrame], 1);
  98. }
  99. catch (Exception e)
  100. {
  101. RKLog.Info(e.ToString());
  102. }
  103. // RKLog.Info("gesFrame:" + gesFrame);
  104. gesFrame++;
  105. }
  106. /// <summary>
  107. /// 模拟手势交互
  108. /// </summary>
  109. private void MockGesInteraction()
  110. {
  111. if (Input.GetKeyDown(KeyCode.LeftShift) || Input.GetKeyDown(KeyCode.RightShift))
  112. {
  113. ChangeHand();
  114. }
  115. gestureData[0].hand_type = isRightHand ? (int)HandType.RightHand : (int)HandType.LeftHand;
  116. gestureData[0].gesture_type = (int)GestureType.None;
  117. gestureData[0].hand_orientation = 1;
  118. GestureType gesType = GestureType.None;
  119. if (Input.GetKey(KeyCode.X))
  120. {
  121. //模拟Grip/Plam
  122. if (Input.GetMouseButtonDown(0))
  123. {
  124. gesType = GestureType.Grip;
  125. }
  126. else if (Input.GetMouseButton(0))
  127. {
  128. gesType = GestureType.Grip;
  129. }
  130. else if (Input.GetMouseButtonUp(0))
  131. {
  132. gesType = GestureType.Palm;
  133. }
  134. else
  135. {
  136. gesType = GestureType.Palm;
  137. }
  138. }
  139. else if (Input.GetKeyUp(KeyCode.X))
  140. {
  141. gesType = GestureType.Palm;
  142. }
  143. else
  144. {
  145. //模拟Pinch/UnPinch
  146. if (Input.GetMouseButtonDown(0))
  147. {
  148. gesType = GestureType.Pinch;
  149. }
  150. else if (Input.GetMouseButton(0))
  151. {
  152. gesType = GestureType.Pinch;
  153. }
  154. else if (Input.GetMouseButtonUp(0))
  155. {
  156. gesType = GestureType.None;
  157. }
  158. else
  159. {
  160. gesType = GestureType.None;
  161. }
  162. }
  163. if (Input.GetKey(KeyCode.Space))
  164. {
  165. eventInput.ProcessData(null, 0);
  166. }
  167. else
  168. {
  169. gestureData[0].gesture_type = (int)gesType;
  170. eventInput.ProcessData(gestureData, 1);
  171. }
  172. }
  173. }
  174. }