Example #1
0
        bool LODQuery(Octree node, int LOD, int[] ranges)
        {
            if (LOD < 0)
            {
                Debug.LogError("LOD query less than zero: " + LOD);
            }
            if (LOD > ranges.Length)
            {
                Debug.LogError("LOD overflow: " + LOD);
            }
            if (node == null)
            {
                Debug.LogError("null Node: " + node);
            }
            if (!node.IntersectSphere(player.position, ranges[LOD]))
            {
                return(false);
            }
            //if (LOD != 0 && !GeometryUtility.TestPlanesAABB(frustumPlanes, node.bounds)) return false;

            if (node.level > LOD)
            {
                node.Subdivide();
                foreach (Octree child in node.children)
                {
                    LODQuery(child, LOD, ranges);
                }
                return(true);
            }
            if (LOD == 0)
            {
                queries.Add(node.bounds.center);
                chunks.Display(node, false);
                return(true);
            }
            else
            {
                if (!node.IntersectSphere(player.position, ranges[LOD - 1]))
                {
                    chunks.Display(node, false);
                }
                else
                {
                    //Debug.Log("!");
                    node.Subdivide();
                    foreach (Octree child in node.children)
                    {
                        if (!LODQuery(child, LOD - 1, ranges))
                        {
                            chunks.Display(child, true);
                        }
                    }
                }
                return(true);
            }
        }
Example #2
0
        public Octree GetNodeAt(float3 location, int level)
        {
            float3 offset = location - chunk.center;
            //bitmap for octant indexing
            int  index      = (offset.x > 0 ? 1 : 0) | ((offset.y > 0 ? 1 : 0)) << 1 | ((offset.z > 0 ? 1 : 0) << 2);
            bool insideNode = InsideNode(location);

            //if offset within nodeSize
            if (insideNode && this.level >= level)
            {
                //if node is of interest
                if (this.level == level)
                {
                    //if (chunk != null) {
                    //    //Debug.Log("EXISTING CHUNK " + chunk.center);
                    //    return chunk;
                    //} else {
                    //    this.chunk = new Chunk(center);
                    return(this);
                    //}
                }
                else                             //else node is not specified level
                {
                    if (children[index] == null) //child node does not exist
                    //subdivide
                    {
                        Subdivide();
                    }
                    return(children[index].GetNodeAt(location, level));
                }
            }
            else
            {
                //make new root
                float3 newCenter = chunk.center;
                newCenter.x += (offset.x > 0 ? 1 : -1) * (chunk.size / 2);
                newCenter.y += (offset.y > 0 ? 1 : -1) * (chunk.size / 2);
                newCenter.z += (offset.z > 0 ? 1 : -1) * (chunk.size / 2);
                Octree newRoot = new Octree(chunk.size * 2, newCenter);
                int    idx;
                idx                   = ((offset.x < 0 ? 1 : 0) << 0) | ((offset.y < 0 ? 1 : 0) << 1) | ((offset.z < 0 ? 1 : 0) << 2);
                newRoot.level         = this.level + 1;
                this.parent           = newRoot;
                newRoot.children[idx] = this;
                newRoot.Subdivide();
                //return this;
                newRoot.isLeaf = false;
                return(newRoot.GetNodeAt(location, level));
            }
        }