private void GenerateChildNodes() { // children index // ------ // 2 | 3 // ------ // 0 | 1 // ------ if (this.currentDepth >= CityQuadTree.maxDepth) { //Debug.Log("maxDepth node can not generate children"); return; } float childNodeSize = this.nodeSize / 2.0f; for (int i = 0; i < 4; i++) { float deltaX = childNodeSize * ((i == 0 || i == 2) ? 0 : 1); float deltaY = childNodeSize * ((i == 0 || i == 1) ? 0 : 1); Vector2 childLeftBottom = new Vector2(this.nodeBounds.x + deltaX, this.nodeBounds.y + deltaY); Rect childRect = new Rect(childLeftBottom, new Vector2(childNodeSize, childNodeSize)); CityQuadTree child = new CityQuadTree(childRect, this.currentDepth + 1, this); this.childNodes[i] = child; } this.isLeaf = false; }
private void InitQuadTree() { InitWorldRect(); terrainRoot = new GameObject("terrain-root"); terrainRoot.transform.position = new Vector3(0, 0, 0); terrainRoot.transform.localScale = Vector3.one * BuildingGeoList.GetWorldScaleFactor(); qTree = new CityQuadTree(worldRect, 0, null); qTree.InitSearchTarget(new Rect(BuildingGeoList.GetRerenceRect().x, BuildingGeoList.GetRerenceRect().y, BuildingGeoList.GetRerenceRect().width + 100, BuildingGeoList.GetRerenceRect().height - 100), terrainRoot, planeMeshPrefab); }
public CityQuadTree(Rect worldRect, int depth, CityQuadTree parent) { this.nodeSize = worldRect.width; this.nodeCenter = worldRect.center; this.currentDepth = depth; this.childNodes = new CityQuadTree[4]; this.isLeaf = true; this.LODTile = null; //this.mapTile = new List<GameObject>(); if (parent != null) { this.nodeParent = parent; } this.nodeBounds = worldRect; }
// 每个leaf都有以下属性 // private List<GameObject> mapTile; //public GameObject planeMeshPrefab; // 构造函数 public CityQuadTree(float size, int depth, Vector2 center, CityQuadTree parent) { this.nodeSize = size; this.currentDepth = depth; this.nodeCenter = center; this.childNodes = new CityQuadTree[4]; this.isLeaf = true; this.LODTile = null; //this.mapTile = new List<GameObject>(); if (parent != null) { this.nodeParent = parent; } // else is root this.nodeBounds = new Rect(center.x - size / 2.0f, center.y - size / 2.0f, size, size); // 左上角 + 正height }