cwisbg

wrk3

Jan 5th, 2019
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.93 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 SubdivideMaxCount = 2; // Max objects in cell, then stop subdividing
  9. public int SubDepthCount = 10; // Max times it will subdivide
  10.  
  11. public bool DoListUpdate = true;
  12. public bool DoDebugDraw = true;
  13.  
  14.  
  15. private GameObject BoundingBoxTop;
  16. private ConstructQuadTree QuadTreeMain = new ConstructQuadTree();
  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 ConstructQuadTree
  36. {
  37.  
  38. public List<Thing> ThingList = new List<Thing>();
  39. float[] StartBoundingBox = new float[4];
  40.  
  41. public void ClearThings()
  42. {
  43. this.ThingList.Clear();
  44. }
  45. public void InsertThing(Thing NewThing)
  46. {
  47. ThingList.Add(NewThing);
  48. }
  49.  
  50. public List<QuadTreeObject> CopyQuadList(List<QuadTreeObject> ToCopyList)
  51. {
  52. List<QuadTreeObject> CopiedList = new List<QuadTreeObject>();
  53. foreach (QuadTreeObject ToCopyObj in ToCopyList)
  54. {
  55. QuadTreeObject CopiedObj = new QuadTreeObject(ToCopyObj);
  56. Debug.Log("HERE");
  57. CopiedList.Add(CopiedObj);
  58. }
  59. return CopiedList;
  60. }
  61.  
  62. public void ConstructTree(float[] BBox, List<float[]> BBShowList, int SubDepthInput, int SubDepth, bool DoDebugDraw)
  63. {
  64.  
  65. List<QuadTreeObject> SaveAll = new List<QuadTreeObject>();
  66. List<QuadTreeObject> CurrentSet = new List<QuadTreeObject>();
  67.  
  68. List<QuadTreeObject> SetSave = new List<QuadTreeObject>();
  69. //List<List<QuadTreeObject>> SetSave = new List<List<QuadTreeObject>>();
  70. int SubMaxCountAmnt = SubDepthInput;
  71.  
  72. QuadTreeObject newSub = new QuadTreeObject(BBox);
  73. newSub.SubMaxCount = SubMaxCountAmnt;
  74. newSub.BoundingBoxSearchObjects = this.ThingList;
  75. newSub.BoundingBoxParentSearchObjects = this.ThingList;
  76. newSub.DoSave = false;
  77. //newSub.DoSub();
  78. //newSub.DrawBounds();
  79. CurrentSet.Add(newSub);
  80.  
  81. int NewSubCount = 1;
  82.  
  83. for (int i = 0; i < SubDepth; i++)
  84. {
  85.  
  86. //int CurrentSetLen = CurrentSet.Count;
  87. //Debug.Log(CurrentSetLen);
  88.  
  89. //for (int y = 0; y < CurrentSetLen; y++)
  90. int Catcher = 1000;
  91. foreach(QuadTreeObject CurrentObj in CurrentSet)
  92. {
  93. //Debug.Log(Catcher);
  94. if(Catcher == 0)
  95. {
  96. Debug.Log("Broken! Caught!");
  97. break;
  98. }
  99. else{
  100. if (CurrentObj.DoSubActive)
  101. {
  102. CurrentObj.DoSub();
  103. CurrentObj.DoSubActive = false;
  104. foreach (QuadTreeObject bbChild in CurrentObj.BoundingBoxChildren)
  105. {
  106. SetSave.Add(bbChild);
  107. }
  108. }
  109. if (DoDebugDraw && CurrentObj.BBActive)
  110. {
  111. CurrentObj.DrawBounds();
  112. }
  113. }
  114. Catcher--;
  115. }
  116.  
  117.  
  118. NewSubCount = 0;
  119. foreach (QuadTreeObject NewFromSet in SetSave)
  120. {
  121. if (NewFromSet.DoSave)
  122. {
  123. CurrentSet.Add(NewFromSet);
  124. //Debug.Log(NewFromSet);
  125. NewFromSet.DrawBounds();
  126. NewFromSet.DoSave = false;
  127. NewSubCount++;
  128. }
  129. }
  130. //Debug.Log(NewSubCount);
  131. //SetSave.Clear();
  132.  
  133. //SetSave = new List<QuadTreeObject>();
  134.  
  135. //CurrentSet = SetSave;
  136. //List<Book> books_2 = books_1.ConvertAll(book => new Book(book.title));
  137. //CurrentSet.AddRange(SetSave);
  138. //targetList.AddRange(sourceList);
  139. //SetSave.CopyTo(CurrentSet);
  140. /*
  141.  
  142. foreach (QuadTreeObject ss in SetSave)
  143. {
  144. CurrentSet.Add(ss);
  145. }
  146.  
  147. //for (int i = 0; i < SubDepth; i++)
  148. for (int ii = 0; ii < SetSave.Count; ii++)
  149. {
  150. //CurrentSet.Add(SetSave[ii]);
  151. }
  152. */
  153.  
  154.  
  155.  
  156. }
  157.  
  158. }
  159. }
  160.  
  161.  
  162.  
  163. class QuadTreeObject//subBB
  164. {
  165.  
  166. public QuadTreeObject(QuadTreeObject lastQuad)
  167. {
  168. BoundingBox = lastQuad.BoundingBox;
  169. BoundingBoxSearchObjects = lastQuad.BoundingBoxSearchObjects;
  170. //BoundingBoxChildren = lastQuad.BoundingBoxChildren;
  171. BBIsInObjs = lastQuad.BBIsInObjs;
  172. BoundingBoxCenter = lastQuad.BoundingBoxCenter;
  173. BoundingBoxList = lastQuad.BoundingBoxList;
  174. BBActive = lastQuad.BBActive;
  175. SubMaxCount = lastQuad.SubMaxCount;
  176. DoSubActive = lastQuad.DoSubActive;
  177. }
  178.  
  179. public QuadTreeObject(float[] BoundingBoxNew)
  180. {
  181. BoundingBox = BoundingBoxNew;
  182. BoundingBoxCenter.x = (BoundingBox[0] + BoundingBox[1]) / 2;
  183. BoundingBoxCenter.z = (BoundingBox[2] + BoundingBox[3]) / 2;
  184. }
  185.  
  186.  
  187. public float[] BoundingBox { get; set; }
  188. public List<Thing> BoundingBoxSearchObjects = new List<Thing>();//{ get; set; }
  189. public List<Thing> BoundingBoxParentSearchObjects { get; set; }
  190. public List<QuadTreeObject> BoundingBoxChildren = new List<QuadTreeObject>();
  191. public List<Thing> BBIsInObjs = new List<Thing>();
  192. public Vector3 BoundingBoxCenter = new Vector3();
  193. private float[] BoundingBoxList = new float[4];
  194. public bool BBActive = false;
  195. public bool DoSubActive = true;
  196. public int SubMaxCount = 0;
  197. public bool DoSave = true;
  198.  
  199. public void DoSub()
  200. {
  201. //this.DoSubActive = false;
  202. //this.BoundingBoxChildren.Clear();
  203. List<QuadTreeObject> NewBoundingBoxList = new List<QuadTreeObject>();
  204. //List<Thing> SearchList = new List<Thing>();
  205. //List<QuadTreeObject> AddList = new List<QuadTreeObject>();
  206. float[] bb = this.BoundingBox;
  207. float[] NewBBNW = new float[4];// North West box
  208. NewBBNW[2] = bb[2];// North
  209. NewBBNW[0] = (bb[0] + bb[1]) / 2;// East, NorthWest
  210. NewBBNW[1] = bb[1];// West
  211. NewBBNW[3] = (bb[2] + bb[3]) / 2;// South, SouthWest
  212. QuadTreeObject NWTreeObject = new QuadTreeObject(NewBBNW);
  213. //Debug.Log(NWTreeObject.BoundingBoxSearchObjects.Count);
  214. //NWTreeObject.BoundingBox = NewBBNW;
  215.  
  216. NewBoundingBoxList.Add(NWTreeObject);
  217.  
  218. //this.BoundingBoxChildren.Add(NWTreeObject);
  219. float[] NewBBNE = new float[4];// North East box
  220. NewBBNE[2] = bb[2];
  221. NewBBNE[0] = (bb[0] + bb[1]) / 2;
  222. NewBBNE[1] = bb[0];
  223. NewBBNE[3] = (bb[2] + bb[3]) / 2;
  224. QuadTreeObject NETreeObject = new QuadTreeObject(NewBBNE);
  225. //NETreeObject.BoundingBox = NewBBNE;
  226.  
  227. NewBoundingBoxList.Add(NETreeObject);
  228.  
  229. //this.BoundingBoxChildren.Add(NETreeObject);
  230. float[] NewBBSW = new float[4];// South West box
  231. NewBBSW[2] = (bb[2] + bb[3]) / 2;
  232. NewBBSW[0] = (bb[0] + bb[1]) / 2;
  233. NewBBSW[1] = bb[1];
  234. NewBBSW[3] = bb[3];
  235. QuadTreeObject SWTreeObject = new QuadTreeObject(NewBBSW);
  236. // SWTreeObject.BoundingBox = NewBBSW;
  237.  
  238. NewBoundingBoxList.Add(SWTreeObject);
  239.  
  240. //this.BoundingBoxChildren.Add(SWTreeObject);
  241. float[] NewBBSE = new float[4];// South East box
  242. NewBBSE[2] = (bb[2] + bb[3]) / 2;
  243. NewBBSE[0] = bb[0];
  244. NewBBSE[1] = (bb[1] + bb[0]) / 2;
  245. NewBBSE[3] = bb[3];
  246. QuadTreeObject SETreeObject = new QuadTreeObject(NewBBSE);
  247. //SETreeObject.BoundingBox = NewBBSE;
  248.  
  249. NewBoundingBoxList.Add(SETreeObject);
  250.  
  251. //this.BoundingBoxChildren.Add(SETreeObject);
  252.  
  253. List<QuadTreeObject> addList = new List<QuadTreeObject>();
  254. foreach (QuadTreeObject NewBoundingBox in NewBoundingBoxList)
  255. {
  256. NewBoundingBox.BoundingBoxParentSearchObjects = this.BoundingBoxSearchObjects;
  257. NewBoundingBox.SubMaxCount = this.SubMaxCount;
  258. //NewBoundingBox.BoundingBoxSearchObjects = this.BoundingBoxSearchObjects;
  259. //Debug.Log(this.BoundingBoxSearchObjects.Count);
  260. //foreach(Thing SearchThing in this.BoundingBoxSearchObjects)
  261. //{
  262. // NewBoundingBox.BoundingBoxSearchObjects.Add(SearchThing);
  263. //}
  264. NewBoundingBox.IsIn();
  265.  
  266. //NewBoundingBox.DrawBounds();
  267. //this.BoundingBoxChildren.Add(NewBoundingBox);
  268. if (NewBoundingBox.BBActive == true)
  269. {
  270.  
  271. this.BoundingBoxChildren.Add(NewBoundingBox);
  272.  
  273. //NewBoundingBox.BoundingBoxChildren = this.BoundingBoxChildren;
  274. }
  275. }
  276. //this.BoundingBoxChildren = new List<QuadTreeObject>();
  277.  
  278. }
  279.  
  280. public void IsIn()
  281. {
  282. List<Thing> IsInList = new List<Thing>();
  283. float[] BB = this.BoundingBox;
  284. bool doAdd = false;
  285.  
  286. //this.DrawBounds();
  287.  
  288. int ListLen = this.BoundingBoxParentSearchObjects.Count;
  289. //Debug.Log(ListLen);
  290. for (int x = 0; x < ListLen; x++)
  291. {
  292. Vector3 objPos = this.BoundingBoxParentSearchObjects[x].Position;
  293.  
  294. //if (objPos.x < BB[1] || objPos.x > BB[0] || objPos.z < BB[3] || objPos.z > BB[2])
  295. if (objPos.x <= BB[0] && objPos.x >= BB[1] && objPos.z <= BB[2] && objPos.z >= BB[3])
  296. {
  297. //if (objPos.x <= BB[1] || objPos.x >= BB[0] || objPos.z <= BB[3] || objPos.z >= BB[2])
  298.  
  299. doAdd = true;
  300. }
  301. if (doAdd == true)
  302. {
  303. //this.BBIsInObjs.Add(this.BoundingBoxParentSearchObjects[x]);
  304. //this.BoundingBoxSearchObjects.Add(this.BoundingBoxParentSearchObjects[x]);
  305. this.BoundingBoxSearchObjects.Add(this.BoundingBoxParentSearchObjects[x]);
  306.  
  307. //Debug.Log("ListLen");
  308. Debug.Log(ListLen);
  309. if (ListLen <= this.SubMaxCount)
  310. {
  311. this.BBActive = false;
  312.  
  313. }
  314. else
  315. {
  316. this.BBActive = true;
  317. }
  318.  
  319. }
  320. }
  321. this.BoundingBoxSearchObjects = new List<Thing>(IsInList);
  322. //this.BoundingBoxSearchObjects.Clear();
  323.  
  324. /*
  325. for (int iii = 0; iii < IsInList.Count; iii++)
  326. {
  327. //this.BoundingBoxSearchObjects.Add(IsInList[iii]);
  328. }
  329. */
  330. }
  331.  
  332. public void ShowSub(List<float[]> BBList)
  333. {
  334. BBList.Add(this.BoundingBox);
  335. /*
  336. foreach (QuadTreeObject child in this.BoundingBoxChildren)
  337. {
  338. BBList.Add(child.BoundingBox);
  339. }
  340. */
  341.  
  342. }
  343. public Vector3 ConvertBoundsToCube(float[] BoundsList)
  344. {
  345. Vector3 BoxPos = Vector2.zero;
  346. BoxPos.x = (BoundsList[0] + BoundsList[1]) / 2;
  347. BoxPos.z = (BoundsList[2] + BoundsList[3]) / 2;
  348. //BoundingBoxPosition.y = (BoundingBoxList[4] + BoundingBoxList[5]) / 2;
  349. BoxPos.x = (BoundsList[0] - BoundsList[1]);
  350. BoxPos.z = (BoundsList[2] - BoundsList[3]);
  351. //BoudningBoxScale.y = (BoundingBoxList[4] - BoundingBoxList[5]);
  352. return BoxPos;
  353. }
  354. public void DrawBounds()
  355. {
  356. float Offset = 0;
  357. float[] bb = this.BoundingBox;
  358. Vector3 NorthWLine = new Vector3(bb[1] - Offset, 0, bb[2] + Offset);
  359. Vector3 NorthELine = new Vector3(bb[0] + Offset, 0, bb[2] + Offset);
  360. Vector3 SouthELine = new Vector3(bb[0] + Offset, 0, bb[3] - Offset);
  361. Vector3 SouthWLine = new Vector3(bb[1] - Offset, 0, bb[3] - Offset);
  362. Debug.DrawLine(NorthWLine, NorthELine, Color.red);// NorthLine
  363. Debug.DrawLine(NorthELine, SouthELine, Color.green);
  364. Debug.DrawLine(SouthELine, SouthWLine, Color.blue);
  365. Debug.DrawLine(SouthWLine, NorthWLine, Color.yellow);
  366. }
  367.  
  368.  
  369. }
  370.  
  371. class Thing
  372. {
  373. public Thing(GameObject ThingObjectNew)
  374. {
  375. Position = ThingObjectNew.transform.position;
  376. //IsFound = false;
  377. ThingObject = ThingObjectNew;
  378. }
  379.  
  380. public Vector3 Position { get; set; }
  381. //public bool IsFound { get; set; }
  382. public GameObject ThingObject { get; set; }
  383. }
  384.  
  385. public float[] GetBounds(List<Transform> BBList)
  386. {
  387. BBX = -100000f;
  388. BBX_ = 100000f;
  389. //BBY = -100000f;
  390. //BBY_ = 100000f;
  391. BBZ = -100000f;
  392. BBZ_ = 100000f;
  393.  
  394. foreach (Transform child in BBList)
  395. {
  396. if (child.position.x > BBX) // Bounds x+
  397. {
  398. BBX = child.position.x;
  399. }
  400. if (child.position.x < BBX_) // Bounds x-
  401. {
  402. BBX_ = child.position.x;
  403. }
  404. /*
  405. if (child.position.y > BBY) // Bounds y+
  406. {
  407. BBY = child.position.y;
  408. }
  409. if (child.position.y < BBY_) // Bounds y-
  410. {
  411. BBY_ = child.position.y;
  412. }
  413. */
  414. if (child.position.z > BBZ) // Bounds z+
  415. {
  416. BBZ = child.position.z;
  417. }
  418. if (child.position.z < BBZ_) // Bounds z-
  419. {
  420. BBZ_ = child.position.z;
  421. }
  422. }
  423.  
  424. StartBoundingBox[0] = BBX;
  425. StartBoundingBox[1] = BBX_;
  426. StartBoundingBox[2] = BBZ;
  427. StartBoundingBox[3] = BBZ_;
  428. //BoundingBoxList[4] = BBY;
  429. //BoundingBoxList[5] = BBY_;
  430. //ConvertBoundsToCube();
  431. return StartBoundingBox;
  432. }
  433.  
  434.  
  435.  
  436. void OnDrawGizmos()
  437. {
  438. if (DoDebugDraw)
  439. {
  440. foreach(float[] bb in BBShowList)
  441. {
  442. ZeroYPosition.x = (bb[0] + bb[1])/2;
  443. ZeroYPosition.z = (bb[2] + bb[3]) / 2;
  444. ZeroYScale.x = bb[0] - bb[1];
  445. ZeroYScale.z = bb[2] - bb[3];
  446. Gizmos.color = Color.red;
  447. Gizmos.DrawWireCube(ZeroYPosition, ZeroYScale);
  448. Gizmos.DrawSphere(ZeroYPosition, .01f);
  449. }
  450. }
  451. }
  452. void UpdadateBBList()
  453. {
  454. BBList.Clear();
  455.  
  456. foreach (GameObject child in GameObject.FindGameObjectsWithTag("InOctree"))
  457. {
  458. BBList.Add(child.transform);
  459. }
  460. DoListUpdate = false;
  461. }
  462. void UpdateTreeList()
  463. {
  464. QuadTreeMain.ClearThings();
  465. foreach (GameObject child in GameObject.FindGameObjectsWithTag("InOctree"))
  466. {
  467. Thing NewThing = new Thing(child);
  468. QuadTreeMain.InsertThing(NewThing);
  469. }
  470. DoListUpdate = false;
  471.  
  472. }
  473. void Start()
  474. {
  475. //TreeLists TreeListsMain = new TreeLists();
  476.  
  477.  
  478. foreach (GameObject child in GameObject.FindGameObjectsWithTag("InOctree"))
  479. {
  480. Thing NewThing = new Thing(child);
  481. QuadTreeMain.InsertThing(NewThing);
  482. BBObjList.Add(child);
  483. BBList.Add(child.transform);
  484. //TreeListsMain.SearchList.Add(NewThing);
  485. //QuadTreeObject test = new QuadTreeObject(child.transform.position);
  486. //test.BoundingBoxCenter = child.transform.position;
  487. //BBList.Add(child.transform);
  488.  
  489. }
  490. StartBoundingBox = GetBounds(BBList); // starting bounding box
  491.  
  492. //StartTree.BuildTree(BoundingBoxList, BBShowList);
  493.  
  494. }
  495.  
  496. void Update()
  497. {
  498. if (DoListUpdate)
  499. {
  500. UpdadateBBList();
  501. UpdateTreeList();
  502. }
  503. BBShowList.Clear();
  504. StartBoundingBox = GetBounds(BBList);
  505. QuadTreeMain.ConstructTree(StartBoundingBox, BBShowList, SubdivideMaxCount, SubDepthCount, DoDebugDraw);
  506.  
  507. //ConstructTree(StartBoundingBox, BBShowList);
  508.  
  509. //BBShowList.Add(BoundingBoxList);
  510.  
  511.  
  512. }
  513. }
Add Comment
Please, Sign In to add comment