//====================================================== // Protected elements //====================================================== /// <summary> /// Subdivides the node into 4 children. /// </summary> protected void Subdivide() { if (Children != null) { //if (WorldChunk.gDebug) { //Debug.Log ("Already have children, not subdividing..."); } return; } // Subdivide into 4 equal sized bounded nodes Children = new QuadNode <TContent> [ChildrenCount]; Vector3 halfSize = Boundaries.size * 0.5f; halfSize.y = Boundaries.size.y; Vector3 offset = halfSize * 0.5f; for (int i = 0; i < Children.Length; i++) { int x = (i / 1) % 2; int z = (i / 2) % 2; Vector3 newCenter = Boundaries.min + new Vector3(x * halfSize.x, 0, z * halfSize.z) + offset; newCenter.y = 0f; //whuh? var c = Children [i] = new QuadNode <TContent> (this, new Bounds(newCenter, halfSize), MaxContent); } int maxDepth = 0; // Add each content element to the first child node which will accept it foreach (var content in Content) { bool added = false; for (int i = 0; i < Children.Length; i++) { if (Children [i].Add(content)) { added = true; break; } } if (!added) { /*throw new NotImplementedException*/ Debug.LogWarning("QuadTree nodes too small! This should not happen!"); } } Content = null; }
//====================================================== // Constructors //====================================================== /// <summary> /// Instanciates a new quadtree node. /// </summary> /// <param name="boundaries">The boundaries of the node.</param> /// <param name="maxContent">The maximum number of elements in Content before the node is subdivided.</param> /// <param name="parent">The parent node.</param> public QuadNode(QuadNode <TContent> parent, Bounds boundaries, int maxContent) { Parent = parent; MaxContent = maxContent; Boundaries = boundaries; }