BoundingBox.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using SC.XR.Unity.Module_InputSystem;
  6. using UnityEngine.Events;
  7. using System;
  8. using SC.XR.Unity;
  9. [AddComponentMenu("SDK/BoundingBox")]
  10. public class BoundingBox : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler, IPointerUpHandler
  11. {
  12. [SerializeField]
  13. [Tooltip("Type of activation method for showing/hiding bounding box handles and controls")]
  14. private BoundingBoxActivationType activation = BoundingBoxActivationType.ActivateOnStart;
  15. public BoundingBoxActivationType ActivationType
  16. {
  17. get
  18. {
  19. return activation;
  20. }
  21. }
  22. [SerializeField]
  23. [Tooltip("Flatten bounds in the specified axis or flatten the smallest one if 'auto' is selected")]
  24. private FlattenModeType flattenAxis = FlattenModeType.DoNotFlatten;
  25. public FlattenModeType FlattenAxis
  26. {
  27. get
  28. {
  29. return flattenAxis;
  30. }
  31. set
  32. {
  33. flattenAxis = value;
  34. Redraw();
  35. }
  36. }
  37. [SerializeField]
  38. private HandleType activeHandle = ~HandleType.None;
  39. public HandleType ActiveHandle
  40. {
  41. get
  42. {
  43. return activeHandle;
  44. }
  45. set
  46. {
  47. activeHandle = value;
  48. Redraw();
  49. }
  50. }
  51. [SerializeField]
  52. private BoundingBoxAssets m_HandlerAssets;
  53. public BoundingBoxAssets BoundingBoxAssets
  54. {
  55. get
  56. {
  57. if (m_HandlerAssets == null)
  58. {
  59. m_HandlerAssets = Resources.Load<BoundingBoxAssets>("HandlerAssets/DefaultAssets");
  60. }
  61. return m_HandlerAssets;
  62. }
  63. }
  64. //[SerializeField]
  65. //[AssetPreAssign("Assets/SDK/Common/StandardAssets/Materials/BoundingBox.mat", typeof(Material))]
  66. private Material m_boxFocusDisplayMat;
  67. public Material boxFocusDisplayMat
  68. {
  69. get
  70. {
  71. if (m_boxFocusDisplayMat == null)
  72. {
  73. if (BoundingBoxAssets)
  74. {
  75. m_boxFocusDisplayMat = BoundingBoxAssets.boxFocusDisplayMat;
  76. }
  77. }
  78. return m_boxFocusDisplayMat;
  79. }
  80. }
  81. //[SerializeField]
  82. //[AssetPreAssign("Assets/SDK/Common/StandardAssets/Materials/BoundingBoxGrabbed.mat", typeof(Material))]
  83. private Material m_boxGrabDisplayMat;
  84. public Material boxGrabDisplayMat
  85. {
  86. get
  87. {
  88. if (m_boxGrabDisplayMat == null)
  89. {
  90. if (BoundingBoxAssets)
  91. {
  92. m_boxGrabDisplayMat = BoundingBoxAssets.boxGrabDisplayMat;
  93. }
  94. }
  95. return m_boxGrabDisplayMat;
  96. }
  97. }
  98. //[SerializeField]
  99. //[AssetPreAssign("Assets/SDK/Common/StandardAssets/Materials/BoundingBoxHandleWhite.mat", typeof(Material))]
  100. private Material m_HandleMaterial;
  101. public Material HandleMaterial
  102. {
  103. get
  104. {
  105. if (m_HandleMaterial == null)
  106. {
  107. if (BoundingBoxAssets)
  108. {
  109. m_HandleMaterial = BoundingBoxAssets.HandleMaterial;
  110. }
  111. }
  112. return m_HandleMaterial;
  113. }
  114. }
  115. //[SerializeField]
  116. //[AssetPreAssign("Assets/SDK/Common/StandardAssets/Materials/BoundingBoxHandleBlueGrabbed.mat", typeof(Material))]
  117. private Material m_HandleGrabMaterial;
  118. public Material HandleGrabMaterial
  119. {
  120. get
  121. {
  122. if (m_HandleGrabMaterial == null)
  123. {
  124. if (BoundingBoxAssets)
  125. {
  126. m_HandleGrabMaterial = BoundingBoxAssets.HandleGrabMaterial;
  127. }
  128. }
  129. return m_HandleGrabMaterial;
  130. }
  131. }
  132. //[SerializeField]
  133. //[Header("Scale Handles")]
  134. //[AssetPreAssign("Assets/SDK/Common/StandardAssets/Prefabs/BoundingBox_ScaleHandle.prefab", typeof(GameObject))]
  135. private GameObject m_CornerPrefab;
  136. public GameObject CornerPrefab
  137. {
  138. get
  139. {
  140. if (m_CornerPrefab == null)
  141. {
  142. if (BoundingBoxAssets)
  143. {
  144. m_CornerPrefab = BoundingBoxAssets.CornerPrefab;
  145. }
  146. }
  147. return m_CornerPrefab;
  148. }
  149. }
  150. //[SerializeField]
  151. //[AssetPreAssign("Assets/SDK/Common/StandardAssets/Prefabs/BoundingBox_ScaleHandle_Slate.prefab", typeof(GameObject))]
  152. private GameObject m_CornerSlatePrefab;
  153. public GameObject CornerSlatePrefab
  154. {
  155. get
  156. {
  157. if (m_CornerSlatePrefab == null)
  158. {
  159. if (BoundingBoxAssets)
  160. {
  161. m_CornerSlatePrefab = BoundingBoxAssets.CornerSlatePrefab;
  162. }
  163. }
  164. return m_CornerSlatePrefab;
  165. }
  166. }
  167. public bool activeScaleMinRestrict = true;
  168. [Tooltip("Minimum scaling allowed relative to the world size Unit of m")]
  169. public float scaleMinimum = 0.05f;
  170. public bool activeScaleMaxRestrict = false;
  171. [Tooltip("Maximum scaling allowed relative to the world size Unit of m")]
  172. public float scaleMaximum = 2.0f;
  173. public Vector3 boundingBoxInitScale { get; private set; }
  174. public Vector3 calculateMinScale
  175. {
  176. get
  177. {
  178. Vector3 temp = (CurrentBoundsExtents * 2 / scaleMinimum);
  179. return new Vector3(boundingBoxInitScale.x / temp.x, boundingBoxInitScale.y / temp.y, boundingBoxInitScale.z / temp.z);
  180. }
  181. }
  182. public Vector3 calculateMaxScale
  183. {
  184. get
  185. {
  186. Vector3 temp = (CurrentBoundsExtents * 2 / scaleMaximum);
  187. return new Vector3(boundingBoxInitScale.x / temp.x, boundingBoxInitScale.y / temp.y, boundingBoxInitScale.z / temp.z);
  188. }
  189. }
  190. //[SerializeField]
  191. //[Tooltip("Size of the cube collidable used in scale handles")]
  192. //private float scaleHandleSize = 0.016f; // 1.6cm default handle size
  193. //public float ScaleHandleSize
  194. //{
  195. // get
  196. // {
  197. // return scaleHandleSize;
  198. // }
  199. // set
  200. // {
  201. // scaleHandleSize = value;
  202. // Redraw();
  203. // }
  204. //}
  205. [HideInInspector]
  206. public BoxCollider BoundBoxCollider;
  207. // Half the size of the current bounds
  208. private Vector3 currentBoundsExtents;
  209. public Vector3 CurrentBoundsExtents
  210. {
  211. get
  212. {
  213. return currentBoundsExtents;
  214. }
  215. }
  216. //[SerializeField]
  217. //[Header("Rotation Handles")]
  218. //[AssetPreAssign("Assets/SDK/Common/StandardAssets/Prefabs/BoundingBox_RotateHandle.prefab", typeof(GameObject))]
  219. private GameObject m_SidePrefab;
  220. public GameObject SidePrefab
  221. {
  222. get
  223. {
  224. if (m_SidePrefab == null)
  225. {
  226. if (BoundingBoxAssets)
  227. {
  228. m_SidePrefab = BoundingBoxAssets.SidePrefab;
  229. }
  230. }
  231. return m_SidePrefab;
  232. }
  233. }
  234. //[SerializeField]
  235. //[Tooltip("Radius of the handle geometry of rotation handles")]
  236. //private float rotationHandleSize = 0.016f; // 1.6cm default handle size
  237. //public float RotationHandleSize
  238. //{
  239. // get
  240. // {
  241. // return rotationHandleSize;
  242. // }
  243. // set
  244. // {
  245. // rotationHandleSize = value;
  246. // Redraw();
  247. // }
  248. //}
  249. [SerializeField]
  250. [Tooltip("Check to show rotation handles for the X axis")]
  251. private bool showRotationHandleForX = true;
  252. public bool ShowRotationHandleForX
  253. {
  254. get
  255. {
  256. return showRotationHandleForX;
  257. }
  258. set
  259. {
  260. showRotationHandleForX = value;
  261. Redraw();
  262. }
  263. }
  264. [SerializeField]
  265. [Tooltip("Check to show rotation handles for the Y axis")]
  266. private bool showRotationHandleForY = true;
  267. public bool ShowRotationHandleForY
  268. {
  269. get
  270. {
  271. return showRotationHandleForY;
  272. }
  273. set
  274. {
  275. showRotationHandleForY = value;
  276. Redraw();
  277. }
  278. }
  279. [SerializeField]
  280. [Tooltip("Check to show rotation handles for the Z axis")]
  281. private bool showRotationHandleForZ = true;
  282. public bool ShowRotationHandleForZ
  283. {
  284. get
  285. {
  286. return showRotationHandleForZ;
  287. }
  288. set
  289. {
  290. showRotationHandleForZ = value;
  291. Redraw();
  292. }
  293. }
  294. //[SerializeField]
  295. //[Header("AxisScale Handles")]
  296. private GameObject m_facePrefab;
  297. public GameObject facePrefab
  298. {
  299. get
  300. {
  301. if (m_facePrefab == null)
  302. {
  303. if (BoundingBoxAssets)
  304. {
  305. m_facePrefab = BoundingBoxAssets.facePrefab;
  306. }
  307. }
  308. return m_facePrefab;
  309. }
  310. }
  311. //[SerializeField]
  312. //[Tooltip("Radius of the handle geometry of rotation handles")]
  313. //private float axisScaleHandleSize = 0.016f; // 1.6cm default handle size
  314. //public float AxisScaleHandleSize
  315. //{
  316. // get
  317. // {
  318. // return axisScaleHandleSize;
  319. // }
  320. // set
  321. // {
  322. // axisScaleHandleSize = value;
  323. // Redraw();
  324. // }
  325. //}
  326. [SerializeField]
  327. private AxisType activeAxis = ~AxisType.None;
  328. public AxisType ActiveAxis
  329. {
  330. get
  331. {
  332. return activeAxis;
  333. }
  334. set
  335. {
  336. activeAxis = value;
  337. Redraw();
  338. }
  339. }
  340. public Transform BoundingBoxContainer
  341. {
  342. get;
  343. set;
  344. }
  345. public BoundingBoxRoot BoundingBoxRoot
  346. {
  347. get;
  348. private set;
  349. }
  350. public CornerBoundingBoxRoot CornerBoundingBoxRoot
  351. {
  352. get;
  353. private set;
  354. }
  355. public SideBoundingBoxRoot SideBoundingBoxRoot
  356. {
  357. get;
  358. set;
  359. }
  360. private FaceBoundingBoxRoot FaceBoundingBoxRoot
  361. {
  362. get;
  363. set;
  364. }
  365. private List<IBoundingBoxRoot> BoundingBoxRootList
  366. {
  367. get;
  368. set;
  369. }
  370. [Header("Audio")]
  371. [SerializeField]
  372. public SCAudiosConfig.AudioType RotateStartAudio = SCAudiosConfig.AudioType.Manipulation_Start;
  373. [SerializeField]
  374. public SCAudiosConfig.AudioType RotateStopAudio = SCAudiosConfig.AudioType.Manipulation_End;
  375. [SerializeField]
  376. public SCAudiosConfig.AudioType ScaleStartAudio = SCAudiosConfig.AudioType.Manipulation_Start;
  377. [SerializeField]
  378. public SCAudiosConfig.AudioType ScaleStopAudio = SCAudiosConfig.AudioType.Manipulation_End;
  379. [Header("Events")]
  380. /// <summary>
  381. /// Event that gets fired when interaction with a rotation handle starts.
  382. /// </summary>
  383. public UnityEvent RotateStarted = new UnityEvent();
  384. /// <summary>
  385. /// Event that gets fired when interaction with a rotation handle stops.
  386. /// </summary>
  387. public UnityEvent RotateStopped = new UnityEvent();
  388. /// <summary>
  389. /// Event that gets fired when interaction with a scale handle starts.
  390. /// </summary>
  391. public UnityEvent ScaleStarted = new UnityEvent();
  392. /// <summary>
  393. /// Event that gets fired when interaction with a scale handle stops.
  394. /// </summary>
  395. public UnityEvent ScaleStopped = new UnityEvent();
  396. public UnityEvent Rotating = new UnityEvent();
  397. public UnityEvent Scaling = new UnityEvent();
  398. #region Class & Enum Define
  399. /// <summary>
  400. /// Enum which describes whether a BoundingBox handle which has been grabbed, is
  401. /// a Rotation Handle (sphere) or a Scale Handle( cube)
  402. /// </summary>
  403. [Flags]
  404. public enum HandleType
  405. {
  406. None = 1 << 0,
  407. Rotation = 1 << 1,
  408. Scale = 1 << 2,
  409. AxisScale = 1 << 3,
  410. }
  411. [Flags]
  412. public enum AxisType
  413. {
  414. None = 1 << 0,
  415. X = 1 << 1,
  416. Y = 1 << 2,
  417. Z = 1 << 3,
  418. NX = 1 << 4,
  419. NY = 1 << 5,
  420. NZ = 1 << 6,
  421. }
  422. /// <summary>
  423. /// Enum which describes how an object's BoundingBox is to be flattened.
  424. /// </summary>
  425. public enum FlattenModeType
  426. {
  427. DoNotFlatten = 0,
  428. /// <summary>
  429. /// Flatten the X axis
  430. /// </summary>
  431. FlattenX,
  432. /// <summary>
  433. /// Flatten the Y axis
  434. /// </summary>
  435. FlattenY,
  436. /// <summary>
  437. /// Flatten the Z axis
  438. /// </summary>
  439. FlattenZ,
  440. /// <summary>
  441. /// Flatten the smallest relative axis if it falls below threshold
  442. /// </summary>
  443. FlattenAuto,
  444. }
  445. /// <summary>
  446. /// This enum defines how the BoundingBox gets activated
  447. /// </summary>
  448. public enum BoundingBoxActivationType
  449. {
  450. ActivateOnStart = 0,
  451. ActivateByPointer,
  452. }
  453. public class Handle
  454. {
  455. /// <summary>
  456. /// Handle Type
  457. /// </summary>
  458. public HandleType type;
  459. /// <summary>
  460. /// Handle Root Gameobject
  461. /// </summary>
  462. public Transform root;
  463. /// <summary>
  464. /// Handle bounds
  465. /// </summary>
  466. public Bounds bounds;
  467. public Vector3 localPosition;
  468. public Transform visualsScale;
  469. public GameObject visual;
  470. public void SetActive(bool active)
  471. {
  472. root.gameObject.SetActive(active);
  473. }
  474. }
  475. #endregion
  476. #region Unity Lifecycle Function
  477. // Start is called before the first frame update
  478. void Start()
  479. {
  480. Init();
  481. }
  482. private void OnValidate()
  483. {
  484. Redraw();
  485. }
  486. #endregion
  487. private void Init()
  488. {
  489. CreatBoundingBoxRoot(flattenAxis);
  490. BoundingBoxRoot = new BoundingBoxRoot(this);
  491. CornerBoundingBoxRoot = new CornerBoundingBoxRoot(this);
  492. SideBoundingBoxRoot = new SideBoundingBoxRoot(this);
  493. FaceBoundingBoxRoot = new FaceBoundingBoxRoot(this);
  494. BoundingBoxRoot.Init();
  495. CornerBoundingBoxRoot.Init();
  496. SideBoundingBoxRoot.Init();
  497. FaceBoundingBoxRoot.Init();
  498. if (BoundingBoxRootList == null)
  499. {
  500. BoundingBoxRootList = new List<IBoundingBoxRoot>();
  501. BoundingBoxRootList.Add(BoundingBoxRoot);
  502. BoundingBoxRootList.Add(CornerBoundingBoxRoot);
  503. BoundingBoxRootList.Add(SideBoundingBoxRoot);
  504. BoundingBoxRootList.Add(FaceBoundingBoxRoot);
  505. }
  506. if (ActivationType == BoundingBoxActivationType.ActivateOnStart)
  507. {
  508. SetVisibility(true);
  509. }
  510. else
  511. {
  512. SetVisibility(false);
  513. }
  514. }
  515. private void CreatBoundingBoxRoot(FlattenModeType flattenMode)
  516. {
  517. RecaculateBounds();
  518. }
  519. public void Redraw()
  520. {
  521. if (BoundingBoxRootList == null)
  522. {
  523. return;
  524. }
  525. RecaculateBounds();
  526. for (int i = 0; i < BoundingBoxRootList.Count; i++)
  527. {
  528. IBoundingBoxRoot boundingBoxRoot = BoundingBoxRootList[i];
  529. boundingBoxRoot.ReDraw();
  530. }
  531. }
  532. private void RecaculateBounds()
  533. {
  534. // Make sure that the bounds of all child objects are up to date before we compute bounds
  535. Physics.SyncTransforms();
  536. BoundBoxCollider = GetComponentInChildren<BoxCollider>();
  537. if (BoundBoxCollider == null)
  538. {
  539. Debug.Log("Error! Please Add BoxCollider And Adjust Size For BoundingBoxGameobject");
  540. return;
  541. }
  542. // Store current rotation then zero out the rotation so that the bounds
  543. // are computed when the object is in its 'axis aligned orientation'.
  544. Quaternion currentRotation = transform.rotation;
  545. transform.rotation = Quaternion.identity;
  546. Physics.SyncTransforms(); // Update collider bounds
  547. currentBoundsExtents = BoundBoxCollider.bounds.extents;
  548. boundingBoxInitScale = BoundBoxCollider.transform.lossyScale;
  549. // After bounds are computed, restore rotation...
  550. transform.rotation = currentRotation;
  551. Physics.SyncTransforms();
  552. if (currentBoundsExtents != Vector3.zero)
  553. {
  554. if (FlattenAxis == FlattenModeType.FlattenAuto)
  555. {
  556. float min = Mathf.Min(currentBoundsExtents.x, Mathf.Min(currentBoundsExtents.y, currentBoundsExtents.z));
  557. flattenAxis = (min == currentBoundsExtents.x) ? FlattenModeType.FlattenX :
  558. ((min == currentBoundsExtents.y) ? FlattenModeType.FlattenY : FlattenModeType.FlattenZ);
  559. }
  560. currentBoundsExtents.x = (flattenAxis == FlattenModeType.FlattenX) ? 0.0f : currentBoundsExtents.x;
  561. currentBoundsExtents.y = (flattenAxis == FlattenModeType.FlattenY) ? 0.0f : currentBoundsExtents.y;
  562. currentBoundsExtents.z = (flattenAxis == FlattenModeType.FlattenZ) ? 0.0f : currentBoundsExtents.z;
  563. Transform existContainerTransform = this.transform.Find(GetType().ToString());
  564. if (existContainerTransform != null)
  565. {
  566. #if UNITY_EDITOR
  567. GameObject.Destroy(existContainerTransform.gameObject);
  568. #else
  569. GameObject.DestroyImmediate(existContainerTransform.gameObject);
  570. #endif
  571. }
  572. BoundingBoxContainer = new GameObject(GetType().ToString()).transform;
  573. BoundingBoxContainer.parent = transform;
  574. BoundingBoxContainer.position = BoundBoxCollider.bounds.center;
  575. BoundingBoxContainer.localRotation = Quaternion.identity;
  576. }
  577. }
  578. public void SetVisibility(bool isVisible)
  579. {
  580. for (int i = 0; i < BoundingBoxRootList.Count; i++)
  581. {
  582. IBoundingBoxRoot boundingBoxRoot = BoundingBoxRootList[i];
  583. boundingBoxRoot.SetVisible(isVisible);
  584. }
  585. }
  586. public void SetHighLight(Transform activeHandle, bool hideOtherHandle)
  587. {
  588. for (int i = 0; i < BoundingBoxRootList.Count; i++)
  589. {
  590. IBoundingBoxRoot boundingBoxRoot = BoundingBoxRootList[i];
  591. boundingBoxRoot.SetHighLight(activeHandle, hideOtherHandle);
  592. }
  593. }
  594. #region BoundingBox PointerEvent
  595. public void OnPointerEnter(PointerEventData eventData)
  596. {
  597. SCPointEventData scPointEventData = eventData as SCPointEventData;
  598. if (scPointEventData == null)
  599. {
  600. return;
  601. }
  602. if (scPointEventData.DownPressGameObject == null)
  603. {
  604. SetVisibility(true);
  605. }
  606. }
  607. public void OnPointerExit(PointerEventData eventData)
  608. {
  609. SCPointEventData scPointEventData = eventData as SCPointEventData;
  610. if (scPointEventData == null)
  611. {
  612. return;
  613. }
  614. if (scPointEventData.DownPressGameObject == null)
  615. {
  616. if (activation == BoundingBoxActivationType.ActivateOnStart)
  617. {
  618. SetVisibility(true);
  619. }
  620. else
  621. {
  622. SetVisibility(false);
  623. }
  624. }
  625. }
  626. public void OnPointerDown(PointerEventData eventData)
  627. {
  628. SCPointEventData scPointEventData = eventData as SCPointEventData;
  629. if (scPointEventData == null)
  630. {
  631. return;
  632. }
  633. SetHighLight(eventData.pointerCurrentRaycast.gameObject.transform, true);
  634. }
  635. public void OnPointerUp(PointerEventData eventData)
  636. {
  637. SCPointEventData scPointEventData = eventData as SCPointEventData;
  638. if (scPointEventData == null)
  639. {
  640. return;
  641. }
  642. if (activation == BoundingBoxActivationType.ActivateOnStart)
  643. {
  644. SetVisibility(true);
  645. }
  646. else
  647. {
  648. SetVisibility(true);
  649. }
  650. }
  651. #endregion
  652. }