Advertisement
subere23

BoundingBox.cs Update

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