cwisbg

Wrk

Dec 14th, 2018
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.25 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class QuadTree : MonoBehaviour
  6. {
  7.    
  8.     public int SubMaxCount = 2;
  9.  
  10.  
  11.     public bool DoListUpdate = true;
  12.     public bool DoDebugDraw = true;
  13.    
  14.  
  15.     private GameObject BoundingBoxTop;
  16.    
  17.     private Vector3 BoundingBoxPosition;
  18.     private Vector3 BoudningBoxScale;
  19.     private List<Transform> BBList = new List<Transform>();
  20.     private List<GameObject> BBObjList = new List<GameObject>();
  21.     private List<float[]> BBShowList = new List<float[]>();
  22.  
  23.     Vector3 ZeroYPosition = Vector3.zero;
  24.     Vector3 ZeroYScale = Vector3.zero;
  25.  
  26.     private float[] StartBoundingBox = new float[4];
  27.     private float BBX;
  28.     private float BBX_;
  29.     //private float BBY;
  30.     //private float BBY_;
  31.     private float BBZ;
  32.     private float BBZ_;
  33.     //private ConstructTree StartTree = new ConstructTree();
  34.    
  35.     class MakeQuadTree
  36.     {
  37.  
  38.         /*
  39.         public MakeQuadTree(float[] BoundingBoxNew, List<Thing> ThingListNew)
  40.         {
  41.  
  42.         }
  43.        
  44.         */
  45.         public List<Thing> ThingList = new List<Thing>();
  46.         public void InsertThing(Thing NewThing)
  47.         {
  48.             ThingList.Add(NewThing);
  49.         }
  50.         public void ConstructTree(float[] BoundingBox, List<float[]> BBShowList)
  51.         {
  52.  
  53.             //
  54.             List<QuadTreeObject> SaveAll = new List<QuadTreeObject>();
  55.  
  56.             QuadTreeObject newSub = new QuadTreeObject(BoundingBox);
  57.             newSub.DoSub();
  58.             {
  59.                 //newSub.ShowSub(BBShowList);// show debug bounds
  60.  
  61.                 BBShowList.Add(newSub.BoundingBox);// Draw top level bounding box
  62.  
  63.                 //return SaveAll;
  64.             }
  65.         }
  66.     }
  67.  
  68.     class TreeLists
  69.     {
  70.         public List<QuadTreeObject> SaveAll = new List<QuadTreeObject>();
  71.         public List<QuadTreeObject> CurrentSet = new List<QuadTreeObject>();
  72.         public List<QuadTreeObject> LastSet = new List<QuadTreeObject>();
  73.         public List<QuadTreeObject> SetSave = new List<QuadTreeObject>();
  74.         public List<Thing> SearchList = new List<Thing>();
  75.         public List<QuadTreeObject> ShowList = new List<QuadTreeObject>();
  76.        
  77.         //public List<float[]> test = new List<float[]>();
  78.     }
  79.  
  80.     class QuadTreeObject//subBB
  81.     {
  82.         // Instance constructo
  83.         public QuadTreeObject(float[] BoundingBoxNew)
  84.         {
  85.             BoundingBox = BoundingBoxNew;
  86.             BoundingBoxCenter.x = (BoundingBoxList[0] + BoundingBoxList[1]) / 2;
  87.             BoundingBoxCenter.z = (BoundingBoxList[2] + BoundingBoxList[3]) / 2;
  88.         }
  89.  
  90.         public float[] BoundingBox {get; set;}
  91.         public List<QuadTreeObject> BoundingBoxChildren = new List<QuadTreeObject>();
  92.         public List<Thing> BoundingBoxSearchObjects = new List<Thing>();
  93.         public Vector3 BoundingBoxCenter = new Vector3();
  94.         private float[] BoundingBoxList = new float[4];
  95.  
  96.  
  97.         public void DoSub()
  98.         {
  99.             List<QuadTreeObject> NewBoundingBoxList = new List<QuadTreeObject>();
  100.             List<Thing> SearchList = new List<Thing>();
  101.             List<QuadTreeObject> AddList = new List<QuadTreeObject>();
  102.             float[] bb = this.BoundingBox;  
  103.             float[] NewBBNW = new float[4];// North West box
  104.             NewBBNW[2] = bb[2];// North
  105.             NewBBNW[0] = (bb[0] + bb[1]) / 2;// East, NorthWest
  106.             NewBBNW[1] = bb[1];// West
  107.             NewBBNW[3] = (bb[2] + bb[3]) / 2;// South, SouthWest
  108.             QuadTreeObject NWTreeObject = new QuadTreeObject(NewBBNW);
  109.             NewBoundingBoxList.Add(NWTreeObject);    
  110.             float[] NewBBNE = new float[4];// North East box
  111.             NewBBNE[2] = bb[2];
  112.             NewBBNE[0] = (bb[0] + bb[1]) / 2;
  113.             NewBBNE[1] = bb[0];
  114.             NewBBNE[3] = (bb[2] + bb[3]) / 2;
  115.             QuadTreeObject NETreeObject = new QuadTreeObject(NewBBNE);
  116.             NewBoundingBoxList.Add(NETreeObject);        
  117.             float[] NewBBSW = new float[4];// South West box
  118.             NewBBSW[2] = (bb[2] + bb[3]) / 2;
  119.             NewBBSW[0] = (bb[0] + bb[1]) / 2;
  120.             NewBBSW[1] = bb[1];
  121.             NewBBSW[3] = bb[3];
  122.             QuadTreeObject SWTreeObject = new QuadTreeObject(NewBBSW);
  123.             NewBoundingBoxList.Add(SWTreeObject);      
  124.             float[] NewBBSE = new float[4];// South East box
  125.             NewBBSE[2] = (bb[2] + bb[3]) / 2;
  126.             NewBBSE[0] = bb[0];
  127.             NewBBSE[1] = (bb[1] + bb[0]) / 2;
  128.             NewBBSE[3] = bb[3];
  129.             QuadTreeObject SETreeObject = new QuadTreeObject(NewBBSE);
  130.             NewBoundingBoxList.Add(SETreeObject);
  131.  
  132.             foreach (QuadTreeObject NewBoundingBox in NewBoundingBoxList)
  133.             {
  134.                
  135.                 AddList.Add(NewBoundingBox);
  136.  
  137.             }
  138.             this.BoundingBoxChildren = AddList;
  139.  
  140.             //this.BoundingBoxChildren.Add(NWTreeObject);
  141.         }
  142.         public void ShowSub(List<float[]> BBList)
  143.         {
  144.  
  145.             foreach (QuadTreeObject child in this.BoundingBoxChildren)
  146.             {
  147.                 BBList.Add(child.BoundingBox);
  148.             }
  149.            
  150.         }
  151.     }
  152.  
  153.  
  154.     class Thing
  155.     {
  156.         public Thing(GameObject ThingObjectNew)
  157.         {
  158.             Position = ThingObjectNew.transform.position;
  159.             IsFound = false;
  160.             ThingObject = ThingObjectNew;
  161.         }
  162.  
  163.         public Vector3 Position { get; set; }
  164.         public bool IsFound { get; set; }
  165.         public GameObject ThingObject { get; set; }
  166.     }
  167.  
  168.  
  169.     public float[] GetBounds(List<Transform> BBList)
  170.     {
  171.         BBX = -100000f;
  172.         BBX_ = 100000f;
  173.         //BBY = -100000f;
  174.         //BBY_ = 100000f;
  175.         BBZ = -100000f;
  176.         BBZ_ = 100000f;
  177.  
  178.         foreach (Transform child in BBList)
  179.         {
  180.             if (child.position.x > BBX) // Bounds x+
  181.             {
  182.                 BBX = child.position.x;
  183.             }
  184.             if (child.position.x < BBX_) // Bounds x-
  185.             {
  186.                 BBX_ = child.position.x;
  187.             }
  188.             /*
  189.             if (child.position.y > BBY) // Bounds y+
  190.             {
  191.                 BBY = child.position.y;
  192.             }
  193.             if (child.position.y < BBY_) // Bounds y-
  194.             {
  195.                 BBY_ = child.position.y;
  196.             }
  197.             */
  198.             if (child.position.z > BBZ) // Bounds z+
  199.             {
  200.                 BBZ = child.position.z;
  201.             }
  202.             if (child.position.z < BBZ_) // Bounds z-
  203.             {
  204.                 BBZ_ = child.position.z;
  205.             }
  206.         }
  207.  
  208.         StartBoundingBox[0] = BBX;
  209.         StartBoundingBox[1] = BBX_;
  210.         StartBoundingBox[2] = BBZ;
  211.         StartBoundingBox[3] = BBZ_;
  212.         //BoundingBoxList[4] = BBY;
  213.         //BoundingBoxList[5] = BBY_;
  214.         //ConvertBoundsToCube();
  215.         return StartBoundingBox;
  216.     }
  217.    
  218.  
  219.     void ConvertBoundsToCube()
  220.     {
  221.         BoundingBoxPosition.x = (StartBoundingBox[0] + StartBoundingBox[1]) / 2;
  222.         BoundingBoxPosition.z = (StartBoundingBox[2] + StartBoundingBox[3]) / 2;
  223.         //BoundingBoxPosition.y = (BoundingBoxList[4] + BoundingBoxList[5]) / 2;
  224.         BoudningBoxScale.x = (StartBoundingBox[0] - StartBoundingBox[1]);
  225.         BoudningBoxScale.z = (StartBoundingBox[2] - StartBoundingBox[3]);
  226.         //BoudningBoxScale.y = (BoundingBoxList[4] - BoundingBoxList[5]);
  227.         //return StartBoundingBox;
  228.     }
  229.  
  230.     void OnDrawGizmos()
  231.     {
  232.         if (DoDebugDraw)
  233.         {
  234.             foreach(float[] bb in BBShowList)
  235.             {
  236.                 ZeroYPosition.x = (bb[0] + bb[1])/2;
  237.                 ZeroYPosition.z = (bb[2] + bb[3]) / 2;
  238.                 ZeroYScale.x = bb[0] - bb[1];
  239.                 ZeroYScale.z = bb[2] - bb[3];
  240.                 Gizmos.color = Color.red;
  241.                 Gizmos.DrawWireCube(ZeroYPosition, ZeroYScale);
  242.                 Gizmos.DrawSphere(ZeroYPosition, .1f);
  243.             }
  244.         }
  245.     }
  246.     void UpdadateBBList()
  247.     {
  248.         BBList.Clear();
  249.         foreach (GameObject child in GameObject.FindGameObjectsWithTag("InOctree"))
  250.         {
  251.             BBList.Add(child.transform);
  252.         }
  253.         DoListUpdate = false;
  254.     }
  255.     void Start()
  256.     {
  257.         //TreeLists TreeListsMain = new TreeLists();
  258.         MakeQuadTree QuadTree = new MakeQuadTree();
  259.  
  260.         foreach (GameObject child in GameObject.FindGameObjectsWithTag("InOctree"))
  261.          {
  262.             Thing NewThing = new Thing(child);
  263.             QuadTree.InsertThing(NewThing);
  264.             //TreeListsMain.SearchList.Add(NewThing);
  265.             //QuadTreeObject test = new QuadTreeObject(child.transform.position);
  266.             //test.BoundingBoxCenter = child.transform.position;
  267.             //BBList.Add(child.transform);
  268.             //BBObjList.Add(child);
  269.         }
  270.         StartBoundingBox = GetBounds(BBList); // starting bounding box
  271.         //StartTree.BuildTree(BoundingBoxList, BBShowList);
  272.  
  273.     }
  274.  
  275.     void Update()
  276.     {
  277.         if (DoListUpdate)
  278.         {
  279.             UpdadateBBList();
  280.         }
  281.         StartBoundingBox = GetBounds(BBList);
  282.         BBShowList.Clear();
  283.         //ConstructTree(StartBoundingBox, BBShowList);
  284.  
  285.         //BBShowList.Add(BoundingBoxList);
  286.  
  287.  
  288.     }
  289. }
Advertisement
Add Comment
Please, Sign In to add comment