Node.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class Node : INode
  6. {
  7. public Bounds bound { get; set; }
  8. private int id;
  9. private int depth;
  10. private Tree belongTree;
  11. private Node[] childList;
  12. private List<SceneObjData> objList;
  13. public List<SceneObjData> ObjList
  14. {
  15. get { return objList; }
  16. }
  17. public Node(Bounds bound, int depth, Tree belongTree)
  18. {
  19. this.id = Tree.nodeInstanceId++;
  20. this.belongTree = belongTree;
  21. this.bound = bound;
  22. this.depth = depth;
  23. objList = new List<SceneObjData>();
  24. }
  25. public void InsertObj(SceneObjData obj)
  26. {
  27. if (!obj.mbIsUsed)
  28. return;
  29. Node node = null;
  30. bool bChild = false;
  31. if (depth < belongTree.maxDepth && childList == null)
  32. {
  33. //如果还没到叶子节点,可以拥有儿子且儿子未创建,则创建儿子
  34. CerateChild();
  35. }
  36. if (childList != null)
  37. {
  38. for (int i = 0; i < childList.Length; ++i)
  39. {
  40. Node item = childList[i];
  41. if (item == null)
  42. {
  43. break;
  44. }
  45. if (item.bound.Contains(obj.Center))
  46. {
  47. if (node != null)
  48. {
  49. bChild = false;
  50. break;
  51. }
  52. node = item;
  53. bChild = true;
  54. }
  55. }
  56. }
  57. if (bChild)
  58. {
  59. //只有一个儿子可以包含该物体,则该物体
  60. node.InsertObj(obj);
  61. }
  62. else
  63. {
  64. objList.Add(obj);
  65. }
  66. }
  67. public void TriggerMove(Camera camera, BaseBattleScene scene)
  68. {
  69. for (int i = 0; i < objList.Count; ++i)
  70. {
  71. scene.SetSceneOjbVis(objList[i]);
  72. }
  73. //刷新子节点
  74. if (childList != null)
  75. {
  76. for (int i = 0; i < childList.Length; ++i)
  77. {
  78. if (childList[i].bound.CheckBoundIsInCamera(camera))
  79. {
  80. childList[i].TriggerMove(camera,scene);
  81. }
  82. else
  83. {
  84. childList[i].HideSceneObj();
  85. }
  86. }
  87. }
  88. }
  89. public void HideSceneObj()
  90. {
  91. for (int i = 0; i < objList.Count; ++i)
  92. {
  93. CommonUtil.SetGameObjectLayer(objList[i].gameObject, BattleCamera.hideLayer);
  94. }
  95. if (childList != null)
  96. {
  97. for (int i = 0; i < childList.Length; ++i)
  98. {
  99. childList[i].HideSceneObj();
  100. }
  101. }
  102. }
  103. private void CerateChild()
  104. {
  105. childList = new Node[belongTree.maxChildCount];
  106. int index = 0;
  107. for (int i = -1; i <= 1; i += 2)
  108. {
  109. for (int j = -1; j <= 1; j += 2)
  110. {
  111. Vector3 centerOffset = new Vector3(bound.size.x / 4 * i, 0, bound.size.z / 4 * j);
  112. Vector3 cSize = new Vector3(bound.size.x / 2, bound.size.y, bound.size.z / 2);
  113. Bounds cBound = new Bounds(bound.center + centerOffset, cSize);
  114. childList[index++] = new Node(cBound, depth + 1, belongTree);
  115. }
  116. }
  117. }
  118. public void DrawBound()
  119. {
  120. //if(this.depth == 0)
  121. //{
  122. // Gizmos.color = Color.red;
  123. //}else if(this.depth == 1)
  124. //{
  125. // Gizmos.color = Color.green;
  126. //}else if(this.depth == 2)
  127. //{
  128. // Gizmos.color = Color.blue;
  129. //}
  130. //Gizmos.DrawWireCube(bound.center, bound.size - Vector3.one * 0.1f);
  131. if (childList != null)
  132. {
  133. for (int i = 0; i < childList.Length; ++i)
  134. {
  135. childList[i].DrawBound();
  136. }
  137. }
  138. }
  139. }