subere23

ch8 - BoundingBox Update

Sep 29th, 2017
341
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using HoloToolkit.Unity.InputModule;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5.  
  6. public class BoundingBox : MonoBehaviour, IFocusable
  7. {
  8.  
  9. Material BoundingBoxMat; //BoundingBox Material
  10. GameObject SRSBoundingBox; // Our BoundingBox
  11. Bounds SRSBounds; // What will be Our Bounds collection
  12. bool isActive; //is our BoundingBox Active?
  13.  
  14. public bool BoundingBoxCreated; //Has our BoundingBox been created?
  15. public bool isRootObject; //is it our Root object?
  16.  
  17. void Start()
  18. {
  19. BoundingBoxMat = NRSRManager.Instance.BoundingBoxMat;
  20. }
  21.  
  22. void Update()
  23. {
  24. //if the BoundingBoxCreated bool is false and it is the root object,(for a few edge
  25. //cases where non root objects found themselves with BoundingBox class attached)
  26. //CreateBoundingBox and set the bool to true
  27.  
  28. if (!BoundingBoxCreated && isRootObject)
  29. {
  30. CreateBoundingBox();
  31.  
  32. BoundingBoxCreated = true;
  33. return;
  34. }
  35.  
  36.  
  37. //now test where our bounding box is null, if not test whether it is
  38. //active or not. Set true or false accordingly.
  39. if (SRSBoundingBox != null)
  40. {
  41. if (!isActive)
  42. {
  43. SRSBoundingBox.SetActive(false);
  44.  
  45.  
  46. return;
  47. }
  48. if (isActive)
  49. {
  50. SRSBoundingBox.SetActive(true);
  51.  
  52.  
  53. }
  54. }
  55. }
  56.  
  57. void CreateBoundingBox()
  58. {
  59. //lets creat a copy of the object that we are making a bounding box of.
  60. SRSBoundingBox = Instantiate(gameObject);
  61. // name it bounding box
  62. SRSBoundingBox.name = "BoundingBox";
  63.  
  64. //the object we copied should be have the boundingbox class attached, so if it doesn't, destroy the object.
  65. if (SRSBoundingBox.GetComponent<BoundingBox>() == null)
  66. {
  67. Destroy(SRSBoundingBox);
  68. return;
  69. }
  70. else
  71. {
  72. //the bounding box class should only be attached to the root object, otherwise it can create infinite loops
  73. // so once the copy is instantiated check and see if we have a bounding box class and if we do destroy it.
  74. Destroy(SRSBoundingBox.GetComponent<BoundingBox>());
  75. // Make sure we tag it correctly
  76. SRSBoundingBox.tag = "NRSRTools";
  77. //set the scale .1f up from its normal scale.
  78. SRSBoundingBox.transform.localScale *= 1.1f;
  79.  
  80. //make sure it is parented to the object we copied.
  81. SRSBoundingBox.transform.parent = gameObject.transform;
  82.  
  83. //find all of the children attached to our bounding box object.
  84. List<Transform> children = new List<Transform>(SRSBoundingBox.GetComponentsInChildren<Transform>());
  85. Debug.Log("Children");
  86.  
  87. //loop through all of the children and if there have a meshRenderer, use our Bounding Box Material on the objects
  88. //then make sure they are all parented to the Bounding Box Object.
  89. foreach (Transform child in children)
  90. {
  91. child.tag = "NRSRTools";
  92. if (child.GetComponent<MeshRenderer>() != null)
  93. {
  94. Debug.Log("Yes! Chef!");
  95. child.GetComponent<MeshRenderer>().material = BoundingBoxMat;
  96. child.transform.parent = SRSBoundingBox.transform;
  97. }
  98. }
  99. }
  100.  
  101. //Time to add the Endpoint Handles.
  102.  
  103. //check and see if the object has children - this will work find for objects that do not.
  104. List<MeshFilter> childrenBounds = new List<MeshFilter>(SRSBoundingBox.GetComponentsInChildren<MeshFilter>());
  105.  
  106. //loop through each child, check if the meshfilter exists and then add its bounds to the SRSBounds.
  107. foreach (MeshFilter meshRen in childrenBounds)
  108. {
  109. if (meshRen.GetComponent<MeshFilter>() != null)
  110. {
  111.  
  112. SRSBounds.Encapsulate(meshRen.GetComponent<MeshFilter>().mesh.bounds);
  113. }
  114. }
  115.  
  116.  
  117. //8.Create 8 points around the object
  118. Vector3 SRSPoint0 = SRSBounds.min * gameObject.transform.localScale.x * 1.1f;
  119. Vector3 SRSPoint1 = SRSBounds.max * gameObject.transform.localScale.z * 1.1f;
  120. Vector3 SRSPoint2 = new Vector3(SRSPoint0.x, SRSPoint0.y, SRSPoint1.z);
  121. Vector3 SRSPoint3 = new Vector3(SRSPoint0.x, SRSPoint1.y, SRSPoint0.z);
  122. Vector3 SRSPoint4 = new Vector3(SRSPoint1.x, SRSPoint0.y, SRSPoint0.z);
  123. Vector3 SRSPoint5 = new Vector3(SRSPoint0.x, SRSPoint1.y, SRSPoint1.z);
  124. Vector3 SRSPoint6 = new Vector3(SRSPoint1.x, SRSPoint0.y, SRSPoint1.z);
  125. Vector3 SRSPoint7 = new Vector3(SRSPoint1.x, SRSPoint1.y, SRSPoint0.z);
  126.  
  127. //9.Create the scaling handles.
  128. CreateEndPoints(SRSPoint0 + transform.position);
  129. CreateEndPoints(SRSPoint1 + transform.position);
  130. CreateEndPoints(SRSPoint2 + transform.position);
  131. CreateEndPoints(SRSPoint3 + transform.position);
  132. CreateEndPoints(SRSPoint4 + transform.position);
  133. CreateEndPoints(SRSPoint5 + transform.position);
  134. CreateEndPoints(SRSPoint6 + transform.position);
  135. CreateEndPoints(SRSPoint7 + transform.position);
  136.  
  137. }
  138.  
  139.  
  140. void CreateEndPoints(Vector3 position)
  141. {
  142. //Create Handle Object
  143. GameObject CornerHandle = GameObject.CreatePrimitive(PrimitiveType.Sphere);
  144.  
  145. CornerHandle.name = "Handle";
  146. CornerHandle.tag = "NRSRTools";
  147.  
  148. //Set The Scale
  149. CornerHandle.transform.localScale = new Vector3(0.15f, 0.15f, 0.15f);
  150.  
  151. //Set The Position
  152. CornerHandle.transform.position = position;
  153.  
  154. //Set The Parent
  155. CornerHandle.transform.parent = SRSBoundingBox.transform;
  156.  
  157. //Set The Material
  158. CornerHandle.GetComponent<Renderer>().material = BoundingBoxMat;
  159.  
  160. }
  161.  
  162.  
  163. public void OnFocusEnter()
  164. {
  165.  
  166.  
  167. //if the BoundingBox has not been created return
  168. if (!BoundingBoxCreated)
  169. {
  170. return;
  171. }
  172. //if it is currently active return
  173. if (isActive)
  174. {
  175. return;
  176. }
  177.  
  178. NRSRManager.SendFocusedObjectToManager(gameObject);
  179. //if those conditions do not apply - set isActive to true
  180. isActive = true;
  181. }
  182.  
  183. public void OnFocusExit()
  184. {
  185.  
  186. //if the BoundingBox has not been created return
  187. if (!BoundingBoxCreated)
  188. {
  189. return;
  190. }
  191. //if it is not currently active return
  192. if (!isActive)
  193. {
  194. return;
  195. }
  196. if (NRSRManager.holdSelectedObject_UsingTransformTool) { return; }
  197.  
  198. if (NRSRManager.holdSelectedObject_LookingAtTransformTool)
  199. {
  200. return;
  201. }
  202.  
  203. //if those conditions do not apply - set isActive to false
  204. NRSRManager.ClearFocusedObjectFromManager();
  205. isActive = false;
  206.  
  207.  
  208. }
  209.  
  210.  
  211. }
RAW Paste Data