HandSimulator.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. //using EZXR.Glass.SixDof;
  5. using EZXR.Glass.Inputs;
  6. using System.Runtime.InteropServices;
  7. using System;
  8. namespace EZXR.Glass.Simulator.Hand
  9. {
  10. public class HandSimulator : MonoBehaviour
  11. {
  12. bool useLeftHand = true;
  13. public float moveSpeed = 0.1f;
  14. public static IntPtr ptr_HandTrackingData;
  15. public static IntPtr ptr_HandTrackingData0;
  16. int handTrackingDataSize;
  17. NativeSwapManager.HandTrackingData[] handTrackingData;
  18. Vector3 offset;
  19. NativeSwapManager nativeSwapManager;
  20. // Start is called before the first frame update
  21. void Start()
  22. {
  23. nativeSwapManager = GetComponent<NativeSwapManager>();
  24. //为了接收算法的结果,在非托管内存区域开辟长度为2个HandTrackingData的内存
  25. handTrackingDataSize = Marshal.SizeOf(typeof(NativeSwapManager.HandTrackingData));
  26. ptr_HandTrackingData = Marshal.AllocHGlobal(handTrackingDataSize * 2);
  27. ptr_HandTrackingData0 = new IntPtr(ptr_HandTrackingData.ToInt64() + handTrackingDataSize);
  28. //用于将Marshal开辟的非托管区域的所有值清零以避免内存区域原数据造成的影响
  29. NativeSwapManager.HandTrackingData temp = new NativeSwapManager.HandTrackingData();
  30. Marshal.StructureToPtr(temp, ptr_HandTrackingData, false);
  31. Marshal.StructureToPtr(temp, ptr_HandTrackingData0, false);
  32. ChangeHandGestureType(GestureType.OpenHand);
  33. }
  34. // Update is called once per frame
  35. void Update()
  36. {
  37. if (nativeSwapManager != null)
  38. {
  39. nativeSwapManager.enabled = false;
  40. }
  41. #if UNITY_EDITOR
  42. if (Input.GetMouseButtonDown(0))
  43. {
  44. ChangeHandGestureType(GestureType.Pinch, true);
  45. }
  46. if (Input.GetMouseButtonUp(0))
  47. {
  48. ChangeHandGestureType(GestureType.OpenHand, true);
  49. }
  50. if (Input.GetKey(KeyCode.A))
  51. {
  52. SetJointPose(Vector3.left);
  53. }
  54. if (Input.GetKey(KeyCode.D))
  55. {
  56. SetJointPose(Vector3.right);
  57. }
  58. if (Input.GetKey(KeyCode.W))
  59. {
  60. SetJointPose(Vector3.up);
  61. }
  62. if (Input.GetKey(KeyCode.S))
  63. {
  64. SetJointPose(Vector3.down);
  65. }
  66. #endif
  67. }
  68. void ChangeHandGestureType(GestureType gestureType, bool doNotChangePose = false)
  69. {
  70. byte[] leftHandData = Resources.Load<TextAsset>("RecordHandData/leftHand_" + gestureType.ToString()).bytes;
  71. byte[] rightHandData = Resources.Load<TextAsset>("RecordHandData/rightHand_" + gestureType.ToString()).bytes;
  72. Marshal.Copy(leftHandData, 0, ptr_HandTrackingData, leftHandData.Length);
  73. Marshal.Copy(rightHandData, 0, ptr_HandTrackingData0, rightHandData.Length);
  74. handTrackingData = new NativeSwapManager.HandTrackingData[2];
  75. if (useLeftHand)
  76. {
  77. handTrackingData[0] = (NativeSwapManager.HandTrackingData)Marshal.PtrToStructure(ptr_HandTrackingData, typeof(NativeSwapManager.HandTrackingData));
  78. }
  79. else
  80. {
  81. handTrackingData[1] = (NativeSwapManager.HandTrackingData)Marshal.PtrToStructure(ptr_HandTrackingData0, typeof(NativeSwapManager.HandTrackingData));
  82. }
  83. ARHandManager.Instance.UpdateHandTrackingDataManually(handTrackingData);
  84. if (doNotChangePose)
  85. {
  86. for (int i = 0; i < 25; i++)
  87. {
  88. HandJointType handJointType = (HandJointType)i;
  89. Pose temp = (useLeftHand ? ARHandManager.leftHand : ARHandManager.rightHand).GetJointData(handJointType);
  90. (useLeftHand ? ARHandManager.leftHand : ARHandManager.rightHand).jointsPose[handJointType] = new Pose(temp.position + offset, temp.rotation);
  91. }
  92. }
  93. }
  94. void SetJointPose(Vector3 dir)
  95. {
  96. offset += dir * Time.deltaTime * moveSpeed;
  97. for (int i = 0; i < 25; i++)
  98. {
  99. HandJointType handJointType = (HandJointType)i;
  100. Pose temp = (useLeftHand ? ARHandManager.leftHand : ARHandManager.rightHand).GetJointData(handJointType);
  101. (useLeftHand ? ARHandManager.leftHand : ARHandManager.rightHand).jointsPose[handJointType] = new Pose(temp.position + dir * Time.deltaTime * moveSpeed, temp.rotation);
  102. }
  103. }
  104. }
  105. }