ZEDSkeletonTrackingViewer.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. //======= Copyright (c) Stereolabs Corporation, All rights reserved. ===============
  2. using UnityEngine;
  3. using System.Collections.Generic;
  4. #if ZED_URP
  5. using UnityEngine.Rendering.Universal;
  6. #endif
  7. /// <summary>
  8. /// Contols the ZEDSkeletonTracking . Links the SDK to Unity
  9. /// </summary>
  10. [DisallowMultipleComponent]
  11. public class ZEDSkeletonTrackingViewer : MonoBehaviour
  12. {
  13. /// <summary>
  14. /// The scene's ZEDManager.
  15. /// If you want to visualize detections from multiple ZEDs at once you will need multiple ZED3DSkeletonVisualizer commponents in the scene.
  16. /// </summary>
  17. [Tooltip("The scene's ZEDManager.\r\n" +
  18. "If you want to visualize detections from multiple ZEDs at once you will need multiple ZED3DSkeletonVisualizer commponents in the scene. ")]
  19. public ZEDManager zedManager;
  20. [Tooltip("The camera view to display ZED images")]
  21. public Camera viewCamera;
  22. /// <summary>
  23. /// Activate skeleton tracking when play mode is on and ZED ready
  24. /// </summary>
  25. [Header("Game Control")]
  26. public bool startObjectDetectionAutomatically = true;
  27. /// <summary>
  28. /// Vizualisation mode. Use a 3D model or only display the skeleton
  29. /// </summary>
  30. [Header("Vizualisation Mode")]
  31. /// <summary>
  32. /// Display 3D avatar. If set to false, only display bones and joint
  33. /// </summary>
  34. [Tooltip("Display 3D avatar. If set to false, only display bones and joint")]
  35. public bool useAvatar = true;
  36. [Space(5)]
  37. [Header("State Filters")]
  38. [Tooltip("Display objects that are actively being tracked by object tracking, where valid positions are known. ")]
  39. public bool showON = true;
  40. /// <summary>
  41. /// Display objects that were actively being tracked by object tracking, but that were lost very recently.
  42. /// </summary>
  43. [Tooltip("Display objects that were actively being tracked by object tracking, but that were lost very recently.")]
  44. public bool showSEARCHING = false;
  45. /// <summary>
  46. /// Display objects that are visible but not actively being tracked by object tracking (usually because object tracking is disabled in ZEDManager).
  47. /// </summary>
  48. [Tooltip("Display objects that are visible but not actively being tracked by object tracking (usually because object tracking is disabled in ZEDManager).")]
  49. public bool showOFF = false;
  50. [Header("Avatar Control")]
  51. /// <summary>
  52. /// Avatar game object
  53. /// </summary>
  54. public GameObject Avatar;
  55. [Space(5)]
  56. [Range(-2.0f, 2.0f)]
  57. public float heightOffset = 0.0f;
  58. public Dictionary<int,SkeletonHandler> avatarControlList;
  59. /// <summary>
  60. /// Start this instance.
  61. /// </summary>
  62. private void Start()
  63. {
  64. QualitySettings.vSyncCount = 1; // Activate vsync
  65. avatarControlList = new Dictionary<int,SkeletonHandler> ();
  66. if (!zedManager)
  67. {
  68. zedManager = FindObjectOfType<ZEDManager>();
  69. }
  70. if (zedManager)
  71. {
  72. #if ZED_URP
  73. UniversalAdditionalCameraData urpCamData = zedManager.GetLeftCamera().GetComponent<UniversalAdditionalCameraData>();
  74. urpCamData.renderPostProcessing = true;
  75. urpCamData.renderShadows = false;
  76. #endif
  77. zedManager.OnZEDReady += OnZEDReady;
  78. zedManager.OnObjectDetection += updateSkeletonData;
  79. }
  80. if (zedManager.objectDetectionModel == sl.DETECTION_MODEL.MULTI_CLASS_BOX || zedManager.objectDetectionModel == sl.DETECTION_MODEL.MULTI_CLASS_BOX_ACCURATE || zedManager.objectDetectionModel == sl.DETECTION_MODEL.MULTI_CLASS_BOX_MEDIUM )
  81. {
  82. Debug.LogWarning("MULTI_CLASS_BOX model can't be used for skeleton tracking, please use either HUMAN_BODY_FAST or HUMAN_BODY_ACCURATE");
  83. }
  84. if (zedManager.objectDetectionBodyFormat == sl.BODY_FORMAT.POSE_18)
  85. {
  86. Debug.LogWarning(" BODY_FORMAT must be set to POSE_34 to animate 3D Avatars !");
  87. return;
  88. }
  89. }
  90. private void OnZEDReady()
  91. {
  92. if (startObjectDetectionAutomatically && !zedManager.IsObjectDetectionRunning)
  93. {
  94. zedManager.StartObjectDetection();
  95. }
  96. }
  97. private void OnDestroy()
  98. {
  99. if (zedManager)
  100. {
  101. zedManager.OnObjectDetection -= updateSkeletonData;
  102. zedManager.OnZEDReady -= OnZEDReady;
  103. }
  104. }
  105. /// <summary>
  106. /// Updates the skeleton data from ZEDCamera call and send it to Skeleton Handler script.
  107. /// </summary>
  108. private void updateSkeletonData(DetectionFrame dframe)
  109. {
  110. List<int> remainingKeyList = new List<int>(avatarControlList.Keys);
  111. List<DetectedObject> newobjects = dframe.GetFilteredObjectList(showON, showSEARCHING, showOFF);
  112. foreach (DetectedObject dobj in newobjects)
  113. {
  114. int person_id = dobj.rawObjectData.id;
  115. //Avatar controller already exist --> update position
  116. if (avatarControlList.ContainsKey(person_id))
  117. {
  118. SkeletonHandler handler = avatarControlList[person_id];
  119. UpdateAvatarControl(handler,dobj.rawObjectData, useAvatar);
  120. // remove keys from list
  121. remainingKeyList.Remove(person_id);
  122. }
  123. else
  124. {
  125. SkeletonHandler handler = ScriptableObject.CreateInstance<SkeletonHandler>();
  126. Vector3 spawnPosition = zedManager.GetZedRootTansform().TransformPoint(dobj.rawObjectData.rootWorldPosition);
  127. handler.Create(Avatar);
  128. handler.initSkeleton(person_id);
  129. avatarControlList.Add(person_id, handler);
  130. UpdateAvatarControl(handler, dobj.rawObjectData, useAvatar);
  131. }
  132. }
  133. foreach (int index in remainingKeyList)
  134. {
  135. SkeletonHandler handler = avatarControlList[index];
  136. handler.Destroy();
  137. avatarControlList.Remove(index);
  138. }
  139. }
  140. public void Update()
  141. {
  142. if (Input.GetKeyDown(KeyCode.Space))
  143. {
  144. useAvatar = !useAvatar;
  145. if (useAvatar)
  146. {
  147. if (zedManager.objectDetectionBodyFitting)
  148. Debug.Log("<b><color=green> Switch to Avatar mode</color></b>");
  149. }
  150. else
  151. Debug.Log("<b><color=green> Switch to Skeleton mode</color></b>");
  152. }
  153. if (Input.GetKeyDown(KeyCode.DownArrow))
  154. {
  155. heightOffset -= 0.02f;
  156. }
  157. else if (Input.GetKeyDown(KeyCode.UpArrow))
  158. {
  159. heightOffset += 0.02f;
  160. }
  161. if (useAvatar)
  162. {
  163. foreach (var skelet in avatarControlList)
  164. {
  165. skelet.Value.Move();
  166. }
  167. }
  168. UpdateViewCameraPosition();
  169. }
  170. /// <summary>
  171. /// Function to update avatar control with data from ZED SDK.
  172. /// </summary>
  173. /// <param name="handler">Handler.</param>
  174. /// <param name="p">P.</param>
  175. private void UpdateAvatarControl(SkeletonHandler handler, sl.ObjectDataSDK data, bool useAvatar)
  176. {
  177. Vector3[] worldJointsPos = new Vector3[34];
  178. Quaternion[] worldJointsRot = new Quaternion[34];
  179. for (int i = 0; i < 34; i++)
  180. {
  181. worldJointsPos[i] = zedManager.GetZedRootTansform().TransformPoint(data.skeletonJointPosition[i]);
  182. worldJointsRot[i] = data.localOrientationPerJoint[i].normalized;
  183. }
  184. handler.setControlWithJointPosition(worldJointsPos, worldJointsRot, zedManager.GetZedRootTansform().rotation * data.globalRootOrientation, useAvatar);
  185. handler.SetHeightOffset(heightOffset);
  186. }
  187. void UpdateViewCameraPosition()
  188. {
  189. viewCamera.transform.position = zedManager.transform.localPosition;
  190. viewCamera.transform.rotation = zedManager.transform.localRotation;
  191. }
  192. }