SHARE
TWEET

CH6BoundingBox

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