cwisbg

wrk2

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