BBox3DHandler.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. #if ZED_HDRP || ZED_URP
  6. using UnityEngine.Rendering;
  7. #endif
  8. /// <summary>
  9. /// For the ZED 3D Object Detection sample. Handles the cube objects that are moved and resized
  10. /// to represent objects detected in 3D.
  11. /// This was designed specifically for the 3D Bounding Box prefab, and expects it to use the default
  12. /// Unity cube model, the TopBottomBBoxMat, and a label object that floats adjacently.
  13. /// </summary>
  14. public class BBox3DHandler : MonoBehaviour
  15. {
  16. /// <summary>
  17. /// Root transform of the child object that's attached nearby and holds information on the object's ID and distance.
  18. /// </summary>
  19. [Header("Label")]
  20. public Transform labelRoot;
  21. /// <summary>
  22. /// Text component that displays the object's ID and distance from the camera.
  23. /// </summary>
  24. [Tooltip("Text component that displays the object's ID and distance from the camera. ")]
  25. public Text text2D;
  26. /// <summary>
  27. /// Background image on the label.
  28. /// </summary>
  29. [Tooltip("Background image on the label.")]
  30. public Image backgroundImage;
  31. /// <summary>
  32. /// Outline component around the background image on the label. Should reference the same object as backgroundImage.
  33. /// </summary>
  34. [Tooltip("Outline component around the background image on the label. Should reference the same object as backgroundImage.")]
  35. public Outline backgroundOutline;
  36. /// <summary>
  37. /// The Y difference between the center of the label and the top of the bounding box.
  38. /// This is used to keep the box at a consistent position, even as the box itself changes in scale.
  39. /// </summary>
  40. [Tooltip("The Y difference between the center of the label and the top of the bounding box. " +
  41. "This is used to keep the box at a consistent position, even as the box itself changes in scale.")]
  42. public float heightFromBoxCeiling = -0.05f;
  43. /// <summary>
  44. /// If true, the text2D's color will be changed when you call SetColor().
  45. /// </summary>
  46. [Space(2)]
  47. [Tooltip("If true, the text2D's color will be changed when you call SetColor().")]
  48. public bool applyColorToText2D = true;
  49. /// <summary>
  50. /// If true, the backgroundImage's color will be changed when you call SetColor().
  51. /// </summary>
  52. [Tooltip("If true, the backgroundImage's color will be changed when you call SetColor().")]
  53. public bool applyColorToBackgroundImage = false;
  54. /// <summary>
  55. /// If true, the backgroundOutline's color will be changed when you call SetColor().
  56. /// </summary>
  57. [Tooltip("If true, the backgroundOutline's color will be changed when you call SetColor().")]
  58. public bool applyColorToBackgroundOutline = true;
  59. /// <summary>
  60. /// If true, the object's ID will be displayed in text2D, assuming it's been updated.
  61. /// </summary>
  62. [Space(5)]
  63. [Tooltip("If true, the object's ID will be displayed in text2D, assuming it's been updated.")]
  64. public bool showID = true;
  65. /// <summary>
  66. /// If true, the object's distance from the detecting camera will be diplayed in text2D, assuming it's been updated.
  67. /// </summary>
  68. [Tooltip("If true, the object's distance from the detecting camera will be diplayed in text2D, assuming it's been updated.")]
  69. public bool showDistance = true;
  70. /// <summary>
  71. /// If true, the label will increase size when further than maxDistBeforeScaling from each rendering camera so it's never too small.
  72. /// </summary>
  73. [Space(5)]
  74. [Tooltip("If true, the label will increase size when further than maxDistBeforeScaling from each rendering camera so it's never too small.")]
  75. public bool useLabelMaxDistScaling = true;
  76. /// <summary>
  77. /// If useLabelMaxDistScaling is true, this defines how far the label must be from a rendering camera to scale up.
  78. /// </summary>
  79. [Tooltip("If useLabelMaxDistScaling is true, this defines how far the label must be from a rendering camera to scale up.")]
  80. public float maxDistBeforeScaling = 12f;
  81. /// <summary>
  82. /// Cache for the label's calculated scale. If useLabelMaxDistScaling is enabled, this holds the scale until Camera.onPreRender where the scale is applied.
  83. /// </summary>
  84. private Vector3 thisFrameScale;
  85. /// <summary>
  86. /// Lossy (world) scale of the label on start. Used to offset changes in the parent bounding box transform each frame.
  87. /// </summary>
  88. private Vector3 labelStartLossyScale;
  89. /// <summary>
  90. /// ID of the object that this instance is currently representing.
  91. /// </summary>
  92. private string currentID = "";
  93. /// <summary>
  94. /// Distance from this object to the ZED camera that detected it.
  95. /// </summary>
  96. private float currentDistance = -1f;
  97. private float currentX;
  98. private float currentY;
  99. private float currentZ;
  100. /// <summary>
  101. /// MeshRenderer attached to this bounding box.
  102. /// </summary>
  103. private MeshRenderer rend;
  104. #if ZED_HDRP
  105. /// <summary>
  106. /// If using HDRP, you can't move or modify objects on a per-camera basis: all "beginCameraRendering" behavior appears to run before the first camera starts rendering,
  107. /// so the object state during the last camera to render is how it appears in all cameras.
  108. /// As such, we just pick one single camera (by default the left camera of the first ZEDManager we find) and make labels face that one.
  109. /// </summary>
  110. private Camera labelFacingCamera;
  111. #endif
  112. #region Shader ID caches
  113. //We look up the IDs of shader properties once, to save a lookup (and performance) each time we access them.
  114. private static int boxBGColorIndex;
  115. private static int boxTexColorIndex;
  116. private static int edgeColorIndex;
  117. private static int xScaleIndex;
  118. private static int yScaleIndex;
  119. private static int zScaleIndex;
  120. private static int floorHeightIndex;
  121. private static bool shaderIndexIDsSet = false;
  122. #endregion
  123. // Use this for initialization
  124. void Awake ()
  125. {
  126. if (!text2D) text2D = labelRoot.GetComponentInChildren<Text>();
  127. thisFrameScale = labelRoot.localScale;
  128. labelStartLossyScale = labelRoot.lossyScale;
  129. if(!shaderIndexIDsSet)
  130. {
  131. FindShaderIndexes();
  132. }
  133. #if !ZED_HDRP && !ZED_URP
  134. Camera.onPreCull += OnCameraPreRender;
  135. #elif ZED_URP
  136. RenderPipelineManager.beginCameraRendering += URPBeginCamera;
  137. #elif ZED_HDRP
  138. ZEDManager manager = FindObjectOfType<ZEDManager>();
  139. labelFacingCamera = manager.GetLeftCamera();
  140. RenderPipelineManager.beginFrameRendering += HDRPBeginFrame;
  141. #endif
  142. }
  143. private void Update()
  144. {
  145. //Position the label so that it stays at a consistent place relative to the box's scale.
  146. UpdateLabelScaleAndPosition();
  147. UpdateBoxUVScales();
  148. }
  149. /// <summary>
  150. /// Sets the ID value that will be displayed on the box's label.
  151. /// Usually set when the box first starts representing a detected object.
  152. /// </summary>
  153. public void SetID(string id)
  154. {
  155. currentID = id;
  156. UpdateText(currentID, currentDistance, currentX, currentY, currentZ);
  157. }
  158. /// <summary>
  159. /// Sets the distance value that will be displayed on the box's label.
  160. /// Designed to indicate the distance from the camera that saw the object.
  161. /// Value is expected in meters. Should be updated with each new detection.
  162. /// </summary>
  163. public void SetDistance(float dist)
  164. {
  165. currentDistance = dist;
  166. UpdateText(currentID, currentDistance, currentX, currentY, currentZ);
  167. }
  168. public void SetX(float x)
  169. {
  170. currentX = x;
  171. UpdateText(currentID, currentDistance, currentX, currentY, currentZ);
  172. }
  173. public void SetY(float y)
  174. {
  175. currentY = y;
  176. UpdateText(currentID, currentDistance, currentX, currentY, currentZ);
  177. }
  178. public void SetZ(float z)
  179. {
  180. currentZ = z;
  181. UpdateText(currentID, currentDistance, currentX, currentY, currentZ);
  182. }
  183. /// <summary>
  184. /// Sets the color of the box and label elements.
  185. /// Use this to alternate between several colors if you have multiple detected objects, and
  186. /// keep them distinguished from one another. Or use to easily customize the visuals however you'd like.
  187. /// </summary>
  188. public void SetColor(Color col)
  189. {
  190. if (text2D && applyColorToText2D)
  191. {
  192. text2D.color = new Color(col.r, col.g, col.b, text2D.color.a);
  193. }
  194. if (backgroundImage && applyColorToBackgroundImage)
  195. {
  196. backgroundImage.color = new Color(col.r, col.g, col.b, backgroundImage.color.a);
  197. }
  198. if (backgroundOutline && applyColorToBackgroundOutline)
  199. {
  200. backgroundOutline.effectColor = new Color(col.r, col.g, col.b, backgroundOutline.effectColor.a);
  201. }
  202. ApplyColorToBoxMats(col);
  203. }
  204. /// <summary>
  205. /// Tells the TopBottomBBoxMat material attached to the box what the transform's current scale is,
  206. /// so that the UVs can be scaled appropriately and avoid stretching.
  207. /// </summary>
  208. public void UpdateBoxUVScales() //TODO: Cache shader IDs.
  209. {
  210. MeshRenderer rend = GetComponentInChildren<MeshRenderer>();
  211. if (rend)
  212. {
  213. foreach (Material mat in rend.materials)
  214. {
  215. if (mat.HasProperty(xScaleIndex))
  216. {
  217. mat.SetFloat(xScaleIndex, transform.lossyScale.x);
  218. }
  219. if (mat.HasProperty(yScaleIndex))
  220. {
  221. mat.SetFloat(yScaleIndex, transform.lossyScale.y);
  222. }
  223. if (mat.HasProperty(zScaleIndex))
  224. {
  225. mat.SetFloat(zScaleIndex, transform.lossyScale.z);
  226. }
  227. if (mat.HasProperty(floorHeightIndex))
  228. {
  229. float height = transform.position.y - transform.lossyScale.y / 2f;
  230. mat.SetFloat(floorHeightIndex, height);
  231. }
  232. }
  233. }
  234. }
  235. /// <summary>
  236. /// Adjusts the label's scale and position to compensate for any changes in the bounding box's scale.
  237. /// </summary>
  238. public void UpdateLabelScaleAndPosition()
  239. {
  240. float lossyxdif = labelStartLossyScale.x / labelRoot.lossyScale.x;
  241. float lossyydif = labelStartLossyScale.y / labelRoot.lossyScale.y;
  242. float lossyzdif = labelStartLossyScale.z / labelRoot.lossyScale.z;
  243. thisFrameScale = new Vector3(labelRoot.localScale.x * lossyxdif,
  244. labelRoot.localScale.y * lossyydif,
  245. labelRoot.localScale.z * lossyzdif);
  246. labelRoot.localPosition = new Vector3(labelRoot.localPosition.x, 0.5f + heightFromBoxCeiling / transform.localScale.y, labelRoot.localPosition.z);
  247. if(!useLabelMaxDistScaling) //If we're using this, we don't apply the scale until the OnPreRender event to add additional scaling effects.
  248. {
  249. labelRoot.localScale = thisFrameScale;
  250. }
  251. }
  252. /// <summary>
  253. /// Updates the text in the label based on the given ID and distance values.
  254. /// </summary>
  255. private void UpdateText(string id, float dist, float x, float y, float z)
  256. {
  257. if (text2D)
  258. {
  259. text2D.text = $"ID: {id}\r\nDistance: {dist:0.00}\r\nX: {x:0.00}\r\nY: {y:0.00}\r\nZ: {z:0.00}";
  260. }
  261. }
  262. /// <summary>
  263. /// Updates the colors of the 3D box materials to the given color.
  264. /// </summary>
  265. private void ApplyColorToBoxMats(Color col)
  266. {
  267. if (!rend) rend = GetComponent<MeshRenderer>();
  268. Material[] mats = rend.materials;
  269. if(!shaderIndexIDsSet)
  270. {
  271. FindShaderIndexes();
  272. }
  273. for (int m = 0; m < mats.Length; m++)
  274. {
  275. Material mat = new Material(rend.materials[m]);
  276. if (mat.HasProperty(boxTexColorIndex))
  277. {
  278. float texalpha = mat.GetColor(boxTexColorIndex).a;
  279. mat.SetColor(boxTexColorIndex, new Color(col.r, col.g, col.b, texalpha));
  280. }
  281. if (mat.HasProperty(boxBGColorIndex))
  282. {
  283. float bgalpha = mat.GetColor(boxBGColorIndex).a;
  284. mat.SetColor(boxBGColorIndex, new Color(col.r, col.g, col.b, bgalpha));
  285. }
  286. if (mat.HasProperty(edgeColorIndex))
  287. {
  288. float bgalpha = mat.GetColor(edgeColorIndex).a;
  289. mat.SetColor(edgeColorIndex, new Color(col.r, col.g, col.b, bgalpha));
  290. }
  291. mats[m] = mat;
  292. }
  293. rend.materials = mats;
  294. }
  295. #if ZED_URP
  296. private void URPBeginCamera(ScriptableRenderContext context, Camera rendcam)
  297. {
  298. OnCameraPreRender(rendcam);
  299. }
  300. #elif ZED_HDRP
  301. private void HDRPBeginFrame(ScriptableRenderContext context, Camera[] rendcams)
  302. {
  303. OnCameraPreRender(labelFacingCamera);
  304. }
  305. #endif
  306. private void OnCameraPreRender(Camera cam)
  307. {
  308. if (!useLabelMaxDistScaling) return;
  309. if (cam.name.Contains("Scene")) return;
  310. if (float.IsInfinity(thisFrameScale.x) || float.IsInfinity(thisFrameScale.y) || float.IsInfinity(thisFrameScale.z))
  311. {
  312. return;
  313. }
  314. //float dist = Vector3.Distance(cam.transform.position, labelRoot.transform.position);
  315. float depth = cam.WorldToScreenPoint(labelRoot.transform.position).z;
  316. if (depth > maxDistBeforeScaling)
  317. {
  318. labelRoot.localScale = thisFrameScale * (depth / maxDistBeforeScaling);
  319. }
  320. else
  321. {
  322. labelRoot.localScale = thisFrameScale;
  323. }
  324. }
  325. /// <summary>
  326. /// Finds and sets the static shader indexes for the properties that we'll set.
  327. /// Used so we can call those indexes when we set the properties, which avoids a lookup
  328. /// and increases performance.
  329. /// </summary>
  330. private static void FindShaderIndexes()
  331. {
  332. boxBGColorIndex = Shader.PropertyToID("_BGColor");
  333. boxTexColorIndex = Shader.PropertyToID("_Color");
  334. edgeColorIndex = Shader.PropertyToID("_EdgeColor");
  335. xScaleIndex = Shader.PropertyToID("_XScale");
  336. yScaleIndex = Shader.PropertyToID("_YScale");
  337. zScaleIndex = Shader.PropertyToID("_ZScale");
  338. floorHeightIndex = Shader.PropertyToID("_FloorHeight");
  339. shaderIndexIDsSet = true;
  340. }
  341. private void OnDestroy()
  342. {
  343. #if !ZED_HDRP && !ZED_URP
  344. Camera.onPreCull -= OnCameraPreRender;
  345. #elif ZED_URP
  346. RenderPipelineManager.beginCameraRendering -= URPBeginCamera;
  347. #elif ZED_HDRP
  348. RenderPipelineManager.beginFrameRendering -= HDRPBeginFrame;
  349. #endif
  350. }
  351. }