Esempio n. 1
0
        public HeightmapQuadTreeNode GetOrAdd(int index, bool doSpread)
        {
            HeightmapQuadTreeNode result;

            if (Children[index] == null)
            {
                var rect = Range;
                rect.Width  /= 2;
                rect.Height /= 2;
                if ((index % 2) == 1)
                {
                    rect.X += rect.Width;
                }
                if (index > 1)
                {
                    rect.Y += rect.Height;
                }
                result           = new HeightmapQuadTreeNode(this, rect);
                result.ParentInd = index;
                Children[index]  = result;
                doSpread         = true;
            }
            else
            {
                result = Children[index];
            }

            if (doSpread)
            {
                if (Parent != null)
                {
                    //find adjacent quad to add to.
                    //for example if we are in index 0 (top left), make sure there is:
                    // - a subdivision in the top right (1) of the tile to our left,
                    // - a subdivision in the bottom left (2) of the tile above us,
                    // - a subdivision in the bottom right (3) of the tile above and left

                    //index 1 (top right
                    switch (index)
                    {
                    case 0:     //top left
                    {
                        var left = result.FindOrCreateQuadInDirection(3);
                        var up   = result.FindOrCreateQuadInDirection(0);
                        if (up != null)
                        {
                            up.FindOrCreateQuadInDirection(3);
                        }
                        break;
                    }

                    case 1:     //top right
                    {
                        var right = result.FindOrCreateQuadInDirection(1);
                        var up    = result.FindOrCreateQuadInDirection(0);
                        if (up != null)
                        {
                            up.FindOrCreateQuadInDirection(1);
                        }
                        break;
                    }

                    case 2:     //bottom left
                    {
                        var left   = result.FindOrCreateQuadInDirection(3);
                        var bottom = result.FindOrCreateQuadInDirection(2);
                        if (bottom != null)
                        {
                            bottom.FindOrCreateQuadInDirection(3);
                        }
                        break;
                    }

                    case 3:     //bottom right
                    {
                        var right  = result.FindOrCreateQuadInDirection(1);
                        var bottom = result.FindOrCreateQuadInDirection(2);
                        if (bottom != null)
                        {
                            bottom.FindOrCreateQuadInDirection(1);
                        }
                        break;
                    }
                    }
                }
            }
            return(result);
        }
Esempio n. 2
0
 public HeightmapQuadTreeNode(HeightmapQuadTreeNode parent, Rectangle range)
 {
     Parent   = parent;
     Range    = range;
     MipLevel = (parent?.MipLevel ?? 6) - 1;
 }