示例#1
0
        private bool InsertToNode(T obj, int depth, float centerx, float centerz, float sizex, float sizez)
        {
            if (depth == m_MaxDepth)
            {
                uint m = Morton2FromWorldPos(centerx, centerz);
                if (m_Nodes.ContainsKey(m) == false)
                {
                    m_Nodes[m] = new LinearSceneTreeLeaf <T>();
                }
                var node = m_Nodes[m].Insert(obj);
                obj.SetLinkedListNode <T>(m, node);
                return(true);
            }
            else
            {
                int   colider = 0;
                float minx    = obj.Bounds.min.x;
                float minz    = obj.Bounds.min.z;
                float maxx    = obj.Bounds.max.x;
                float maxz    = obj.Bounds.max.z;

                if (minx <= centerx && minz <= centerz)
                {
                    colider |= 1;
                }
                if (minx <= centerx && maxz >= centerz)
                {
                    colider |= 2;
                }
                if (maxx >= centerx && minz <= centerz)
                {
                    colider |= 4;
                }
                if (maxx >= centerx && maxz >= centerz)
                {
                    colider |= 8;
                }
                float sx = sizex * 0.5f, sz = sizez * 0.5f;

                bool insertresult = false;
                if ((colider & 1) != 0)
                {
                    insertresult = insertresult | InsertToNode(obj, depth + 1, centerx - sx * 0.5f, centerz - sz * 0.5f, sx, sz);
                }
                if ((colider & 2) != 0)
                {
                    insertresult = insertresult | InsertToNode(obj, depth + 1, centerx - sx * 0.5f, centerz + sz * 0.5f, sx, sz);
                }
                if ((colider & 4) != 0)
                {
                    insertresult = insertresult | InsertToNode(obj, depth + 1, centerx + sx * 0.5f, centerz - sz * 0.5f, sx, sz);
                }
                if ((colider & 8) != 0)
                {
                    insertresult = insertresult | InsertToNode(obj, depth + 1, centerx + sx * 0.5f, centerz + sz * 0.5f, sx, sz);
                }
                return(insertresult);
            }
        }
示例#2
0
 public override void Add(T item)
 {
     if (item == null)
     {
         return;
     }
     if (m_Bounds.Intersects(item.Bounds))
     {
         if (m_MaxDepth == 0)
         {
             if (m_Nodes.ContainsKey(0) == false)
             {
                 m_Nodes[0] = new LinearSceneTreeLeaf <T>();
             }
             var node = m_Nodes[0].Insert(item);
             item.SetLinkedListNode <T>(0, node);
         }
         else
         {
             InsertToNode(item, 0, m_Bounds.center.x, m_Bounds.center.z, m_Bounds.size.x, m_Bounds.size.z);
         }
     }
 }