Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using HoloToolkit.Unity.InputModule;
- using System.Collections.Generic;
- using UnityEngine;
- public class BoundingBox : MonoBehaviour, IFocusable
- {
- Material BoundingBoxMat; //BoundingBox Material
- GameObject SRSBoundingBox; // Our BoundingBox
- Bounds SRSBounds; // What will be Our Bounds collection
- bool isActive; //is our BoundingBox Active?
- public bool BoundingBoxCreated; //Has our BoundingBox been created?
- public bool isRootObject; //is it our Root object?
- void Start()
- {
- BoundingBoxMat = NRSRManager.Instance.BoundingBoxMat;
- }
- void Update()
- {
- //if the BoundingBoxCreated bool is false and it is the root object,(for a few edge
- //cases where non root objects found themselves with BoundingBox class attached)
- //CreateBoundingBox and set the bool to true
- if (!BoundingBoxCreated && isRootObject)
- {
- CreateBoundingBox();
- BoundingBoxCreated = true;
- return;
- }
- //now test where our bounding box is null, if not test whether it is
- //active or not. Set true or false accordingly.
- if (SRSBoundingBox != null)
- {
- if (!isActive)
- {
- SRSBoundingBox.SetActive(false);
- return;
- }
- if (isActive)
- {
- SRSBoundingBox.SetActive(true);
- }
- }
- }
- void CreateBoundingBox()
- {
- //lets creat a copy of the object that we are making a bounding box of.
- SRSBoundingBox = Instantiate(gameObject);
- // name it bounding box
- SRSBoundingBox.name = "BoundingBox";
- //the object we copied should be have the boundingbox class attached, so if it doesn't, destroy the object.
- if (SRSBoundingBox.GetComponent<BoundingBox>() == null)
- {
- Destroy(SRSBoundingBox);
- return;
- }
- else
- {
- //the bounding box class should only be attached to the root object, otherwise it can create infinite loops
- // so once the copy is instantiated check and see if we have a bounding box class and if we do destroy it.
- Destroy(SRSBoundingBox.GetComponent<BoundingBox>());
- // Make sure we tag it correctly
- SRSBoundingBox.tag = "NRSRTools";
- //set the scale .1f up from its normal scale.
- SRSBoundingBox.transform.localScale *= 1.1f;
- //make sure it is parented to the object we copied.
- SRSBoundingBox.transform.parent = gameObject.transform;
- //find all of the children attached to our bounding box object.
- List<Transform> children = new List<Transform>(SRSBoundingBox.GetComponentsInChildren<Transform>());
- Debug.Log("Children");
- //loop through all of the children and if there have a meshRenderer, use our Bounding Box Material on the objects
- //then make sure they are all parented to the Bounding Box Object.
- foreach (Transform child in children)
- {
- child.tag = "NRSRTools";
- if (child.GetComponent<MeshRenderer>() != null)
- {
- Debug.Log("Yes! Chef!");
- child.GetComponent<MeshRenderer>().material = BoundingBoxMat;
- child.transform.parent = SRSBoundingBox.transform;
- }
- }
- }
- //Time to add the Endpoint Handles.
- //check and see if the object has children - this will work find for objects that do not.
- List<MeshFilter> childrenBounds = new List<MeshFilter>(SRSBoundingBox.GetComponentsInChildren<MeshFilter>());
- //loop through each child, check if the meshfilter exists and then add its bounds to the SRSBounds.
- foreach (MeshFilter meshRen in childrenBounds)
- {
- if (meshRen.GetComponent<MeshFilter>() != null)
- {
- SRSBounds.Encapsulate(meshRen.GetComponent<MeshFilter>().mesh.bounds);
- }
- }
- //8.Create 8 points around the object
- Vector3 SRSPoint0 = SRSBounds.min * gameObject.transform.localScale.x * 1.1f;
- Vector3 SRSPoint1 = SRSBounds.max * gameObject.transform.localScale.z * 1.1f;
- Vector3 SRSPoint2 = new Vector3(SRSPoint0.x, SRSPoint0.y, SRSPoint1.z);
- Vector3 SRSPoint3 = new Vector3(SRSPoint0.x, SRSPoint1.y, SRSPoint0.z);
- Vector3 SRSPoint4 = new Vector3(SRSPoint1.x, SRSPoint0.y, SRSPoint0.z);
- Vector3 SRSPoint5 = new Vector3(SRSPoint0.x, SRSPoint1.y, SRSPoint1.z);
- Vector3 SRSPoint6 = new Vector3(SRSPoint1.x, SRSPoint0.y, SRSPoint1.z);
- Vector3 SRSPoint7 = new Vector3(SRSPoint1.x, SRSPoint1.y, SRSPoint0.z);
- //9.Create the scaling handles.
- CreateEndPoints(SRSPoint0 + transform.position);
- CreateEndPoints(SRSPoint1 + transform.position);
- CreateEndPoints(SRSPoint2 + transform.position);
- CreateEndPoints(SRSPoint3 + transform.position);
- CreateEndPoints(SRSPoint4 + transform.position);
- CreateEndPoints(SRSPoint5 + transform.position);
- CreateEndPoints(SRSPoint6 + transform.position);
- CreateEndPoints(SRSPoint7 + transform.position);
- }
- void CreateEndPoints(Vector3 position)
- {
- //Create Handle Object
- GameObject CornerHandle = GameObject.CreatePrimitive(PrimitiveType.Sphere);
- CornerHandle.name = "Handle";
- CornerHandle.tag = "NRSRTools";
- //Set The Scale
- CornerHandle.transform.localScale = new Vector3(0.15f, 0.15f, 0.15f);
- //Set The Position
- CornerHandle.transform.position = position;
- //Set The Parent
- CornerHandle.transform.parent = SRSBoundingBox.transform;
- //Set The Material
- CornerHandle.GetComponent<Renderer>().material = BoundingBoxMat;
- }
- public void OnFocusEnter()
- {
- //if the BoundingBox has not been created return
- if (!BoundingBoxCreated)
- {
- return;
- }
- //if it is currently active return
- if (isActive)
- {
- return;
- }
- NRSRManager.SendFocusedObjectToManager(gameObject);
- //if those conditions do not apply - set isActive to true
- isActive = true;
- }
- public void OnFocusExit()
- {
- //if the BoundingBox has not been created return
- if (!BoundingBoxCreated)
- {
- return;
- }
- //if it is not currently active return
- if (!isActive)
- {
- return;
- }
- if (NRSRManager.holdSelectedObject_UsingTransformTool) { return; }
- if (NRSRManager.holdSelectedObject_LookingAtTransformTool)
- {
- return;
- }
- //if those conditions do not apply - set isActive to false
- NRSRManager.ClearFocusedObjectFromManager();
- isActive = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement