Exemple #1
0
        public bool ReadTree( Stream ins )
        {
            root = null;
              width = height = 0;
              if ( ins == null ) return false;

              // !!!{{ TODO: add the Q-tree reading code here

              StreamReader r = new StreamReader( ins );
              string line = r.ReadLine();
              if ( line == null || line.Length < 1 )
            return false;

              string[] header = line.Split( ' ' );
              width = height = 0;

              if ( header.Length < 3 ||
               !header[ 0 ].Equals( "QTREE" ) ||
               !int.TryParse( header[ 1 ], out width ) ||
               !int.TryParse( header[ 2 ], out height ) )
            return false;

              if ( width > 0 && height > 0 )
            root = ReadNode( r );

              return true;

              // !!!}}
        }
Exemple #2
0
        public QTNode(float size, Vector2 origin)
        {
            this.size = size;
            this.origin = origin;

            this.bl = null;
            this.tl = null;
            this.br = null;
            this.tr = null;
        }
Exemple #3
0
        public void EncodeTree( Bitmap inp )
        {
            root = null;
              width = height = 0;
              if ( inp == null ) return;

              // !!!{{ TODO: add the Q-tree encoding code here

              width  = inp.Width;
              height = inp.Height;

              // !!!}}
        }
Exemple #4
0
        protected QTNode ReadNode(StreamReader r)
        {
            string ln = r.ReadLine();

            if (ln == null ||
                ln.Length < 1)
            {
                return(null);
            }
            string[] line = ln.Split(' ');
            if (line.Length < 1)
            {
                return(null);
            }

            if (line[0] == "R") // inner node
            {
                QTNode inner = new QTNode();
                inner.ul = ReadNode(r);
                inner.ur = ReadNode(r);
                inner.ll = ReadNode(r);
                inner.lr = ReadNode(r);
                return(inner);
            }

            if (line.Length < 3)
            {
                return(null);
            }
            else                       // leaf node
            {
                int R, G, B;
                if (!int.TryParse(line[0], out R) ||
                    !int.TryParse(line[1], out G) ||
                    !int.TryParse(line[2], out B))
                {
                    return(null);
                }

                return(new QTNode(Color.FromArgb(R, G, B)));
            }
        }
Exemple #5
0
 void OnDrawGizmos()
 {
     if (activeNodeListArray == null || !showGizmos)
     {
         return;
     }
     tempNode = null;
     for (int i = 0; i < activeNodeListArray.Length; i++)
     {
         //Debug.Log ("LOD为"+i.ToString()+"的节点个数为"+activeNodeListArray[i].Count.ToString());
         for (int m = 0; m < activeNodeListArray[i].Count; m++)
         {
             tempNode = activeNodeListArray [i] [m];
             if (tempNode.isDisplay)
             {
                 Gizmos.DrawWireCube(transform.TransformPoint(new Vector3(tempNode.center.x, 0f, tempNode.center.z)), new Vector3(tempNode.length, 1f, tempNode.length));
             }
         }
     }
 }
Exemple #6
0
        private void AddQuadTreeQuads(QTNode node, IMap map)
        {
            var r = node.Bounds;

            var feature = new Feature {
                Geometry = geometryFactory.ToGeometry(new Envelope(r.X, r.X + r.Width, r.Y, r.Y + r.Height))
            };

            VectorLayer vectorLayer = null;

            if (!quadTreeQuadLayers.TryGetValue((int)node.Level, out vectorLayer))
            {
                var featureCollection = new FeatureCollection {
                    Features = new List <Feature> {
                        feature
                    }
                };
                vectorLayer = new VectorLayer
                {
                    DataSource = featureCollection, Style = { Fill = Brushes.Transparent, Outline = { Width = 2, Color = Color.FromArgb(100, 100, 100, 200) } },
                    Name       = Name + " quad tree, level: " + node.Level,
                    Selectable = false
                };
                map.Layers.Insert(0, vectorLayer);
                map.BringToFront(vectorLayer);
                quadTreeQuadLayers[node.Level] = vectorLayer;
            }
            else
            {
                vectorLayer.DataSource.Features.Add(feature);
            }

            if (node.Children != null)
            {
                AddQuadTreeQuads(node.Children[0], map);
                AddQuadTreeQuads(node.Children[1], map);
                AddQuadTreeQuads(node.Children[2], map);
                AddQuadTreeQuads(node.Children[3], map);
            }
        }
Exemple #7
0
 // Update is called once per frame
 public void Execute()
 {
     tempNode = null;
     for (int i = 0; i < activeNodeListArray.Length; i++)
     {
         for (int m = 0; m < activeNodeListArray[i].Count; m++)
         {
             tempNode = activeNodeListArray [i] [m];
             if (tempNode.lodLevel > 0 && QTManager.Instance.CanGenerate(tempNode))
             {
                 tempNode.Generate();
             }
             else
             {
                 if (tempNode.lodLevel < maxLodLevel && QTManager.Instance.NeedBack(tempNode.parent))
                 {
                     tempNode.parent.Back();
                 }
             }
         }
     }
 }
Exemple #8
0
        public bool ReadTree(Stream ins)
        {
            root  = null;
            width = height = 0;
            if (ins == null)
            {
                return(false);
            }

            // !!!{{ TODO: add the Q-tree reading code here

            StreamReader r    = new StreamReader(ins);
            string       line = r.ReadLine();

            if (line == null || line.Length < 1)
            {
                return(false);
            }

            string[] header = line.Split(' ');
            width = height = 0;

            if (header.Length < 3 ||
                header[0] != "QTREE" ||
                !int.TryParse(header[1], out width) ||
                !int.TryParse(header[2], out height))
            {
                return(false);
            }

            if (width > 0 && height > 0)
            {
                root = ReadNode(r);
            }

            return(true);

            // !!!}}
        }
Exemple #9
0
        public bool AllocateSpace(Vector2 size, ref ulong address, ref Rect space)
        {
            if (!allocated) {
                //check if size is bigger than children can hold
                Vector2 dim = new Vector2(this.size * 0.5f, this.size * 0.5f);

                if (size.x > dim.x || size.y > dim.y) {
                    //size to big for children
                    if (Empty(bl) && Empty(tl) && Empty(br) && Empty(tr)) {
                        allocated = true;
                        space = new Rect(origin - dim, new Vector2(this.size, this.size));
                        return true;
                    }
                    else {
                        address >>= 2;
                        return false;
                    }
                }
                else if (!Mathf.Approximately(this.size, MINIMUM_SIZE)) {

                    float sizeHalf = this.size * 0.5f;

                    //allocate space in a node that is a child of this node
                    //try bottom left
                    PushAddress(ref address, 0);
                    if (bl == null) {
                        bl = new QTNode(sizeHalf, origin - (dim * 0.5f));
                        return bl.AllocateSpace(size, ref address, ref space);
                    } else if (bl.AllocateSpace(size, ref address, ref space)) {
                        return true;
                    }

                    PushAddress(ref address, 1);
                    if (tl == null) {
                        tl = new QTNode(sizeHalf, origin + new Vector2(this.size * -0.5f, this.size * 0.5f));
                        return tl.AllocateSpace(size, ref address, ref space);
                    } else if (tl.AllocateSpace(size, ref address, ref space)) {
                        return true;
                    }

                    PushAddress(ref address, 2);
                    if (br == null) {
                        br = new QTNode(sizeHalf, origin + new Vector2(this.size * 0.5f, this.size * -0.5f));
                        return br.AllocateSpace(size, ref address, ref space);
                    } else if (br.AllocateSpace(size, ref address, ref space)) {
                        return true;
                    }

                    PushAddress(ref address, 3);
                    if (tr == null) {
                        tr = new QTNode(sizeHalf, origin + (dim * 0.5f));
                        return tr.AllocateSpace(size, ref address, ref space);
                    } else if (tr.AllocateSpace(size, ref address, ref space)) {
                        return true;
                    } else {
                        address >>= 2;
                        return false;
                    }

                }
                else {
                    //allocate the space of this whole node
                    allocated = true;
                    space = new Rect(origin - dim, new Vector2(this.size, this.size));
                    return true;
                }
            } else {
                address >>= 2;
                return false;
            }
        }
Exemple #10
0
 public QuadTree()
 {
     root = null;
       width = height = 0;
 }
Exemple #11
0
 private static bool Empty(QTNode node)
 {
     return node == null || node.Empty();
 }
Exemple #12
0
        public void Insert(T T)
        {
            int      i = 0;
            QTBound  r = bounds;
            TSVector p = T.position;

            T.next = null;

            maxRadius = TSMath.Max(T.neighbourRadius, maxRadius);

            int      depth = 0;
            TSVector min   = TSVector.zero;
            TSVector max   = TSVector.zero;

            while (true)
            {
                depth++;

                if (nodes[i].childNode1 == i)
                {
                    // Leaf node.
                    if (nodes[i].count < LeafSize || depth > 10)
                    {
                        nodes[i].Add(T);
                        //  nodes[i].count++;
                        break;
                    }
                    else
                    {
                        // Split
                        QTNode node = nodes[i];
                        node.childNode1 = GetNodeIndex();
                        node.childNode2 = GetNodeIndex();
                        node.childNode3 = GetNodeIndex();
                        node.childNode4 = GetNodeIndex();
                        nodes[i]        = node;

                        nodes[i].Distribute(nodes, r);
                    }
                }
                // Note, no else
                if (nodes[i].childNode1 != i)
                {
                    // Not a leaf node
                    TSVector c = r.center;
                    if (p.x > c.x)
                    {
                        if (p.z > c.z)
                        {
                            i = nodes[i].childNode4;
                            r = QTBound.MinMaxQTBound(c, r.max);
                        }
                        else
                        {
                            i = nodes[i].childNode3;
                            min.Set(c.x, 0, r.min.z);
                            max.Set(r.max.x, 0, c.z);
                            r = QTBound.MinMaxQTBound(min, max);
                        }
                    }
                    else
                    {
                        if (p.z > c.z)
                        {
                            i = nodes[i].childNode2;
                            min.Set(r.min.x, 0, c.z);
                            max.Set(c.x, 0, r.max.z);
                            r = QTBound.MinMaxQTBound(min, max);
                        }
                        else
                        {
                            i = nodes[i].childNode1;
                            r = QTBound.MinMaxQTBound(r.min, c);
                        }
                    }
                }
            }
        }
 public bool CanGenerate(QTNode node)
 {
     return(MathExtra.GetV3L(QTManager.Instance.playerTrans.position - node.center) / node.length < QTManager.Instance.activeTerrain.cl);
 }
Exemple #14
0
 public QTNode(Color initCol)
 {
     ul  = ur = ll = lr = null;
     col = initCol;
 }
Exemple #15
0
 public void RemoveFromUpdateList(QTNode node)
 {
     updateList.Remove(node);
 }
Exemple #16
0
 public void AddToUpdateList(QTNode node)
 {
     updateList.Add(node);
 }
Exemple #17
0
    public void CreatMesh(QTNode node, bool[] transformArray, int splitCount)
    {
        if (node.lodLevel == 0)
        {
            gameObject.SetActive(false);
        }
        heightMap               = QTManager.Instance.activeTerrain.heightMap;
        _vectorToPosTable       = QTManager.Instance.activePlanet.vectorToPosTable;
        _vectorToHeightMapTable = QTManager.Instance.activePlanet.vectorToHeightMapTable;
        mesh.Clear();

        float offSet    = node.length / splitCount;
        int   lineCount = 0;

        if (verts == null || verts.Length != (splitCount + 1) * (splitCount + 1))
        {
            verts  = new Vector3[(splitCount + 1) * (splitCount + 1)];
            height = new float[verts.Length];
        }

        verts [0] = new Vector3(node.center.x - node.length * 0.5f, node.center.y, node.center.z - node.length * 0.5f);
        int max = (splitCount + 1) * (splitCount + 1);

        for (int i = 1; i < max; i++)
        {
            if (lineCount < splitCount)
            {
                verts [i] = new Vector3(verts [i - 1].x + offSet, node.center.y, verts [i - 1].z);

                lineCount++;
            }
            else
            {
                verts [i] = new Vector3(verts[i - splitCount - 1].x, node.center.y, verts [i - splitCount - 1].z + offSet);
                lineCount = 0;
            }
        }
        float   radius = QTManager.Instance.activePlanet.sphereRadius;
        Vector2 origin = node.fullGenerateOrigin * QTManager.Instance.activePlanet.mapScale;
        float   hx     = 0f;
        float   hy     = origin.y;

        lineCount = 0;
        offSet    = node.fullGenerateOffset * QTManager.Instance.activePlanet.mapScale;
        float heightScale = QTManager.Instance.activePlanet.heightScale;

        for (int i = 0; i < max; i++)
        {
            hx = origin.x + offSet * lineCount;
            if (QTManager.Instance.activePlanet.mapScale == 1f)
            {
                height [i] = heightMap[_vectorToHeightMapTable[(int)hx, (int)hy]];
            }
            else
            {
                height [i] = GetSmoothHeight(hx, hy);
            }
            verts [i] = MathExtra.FastNormalize(verts [i]) * radius * (1f + height[i] * heightScale);
            lineCount++;
            if (lineCount >= splitCount + 1)
            {
                hy       += offSet;
                lineCount = 0;
            }
        }

        if (splitCount == 1)
        {
            mesh.vertices  = verts;
            mesh.triangles = new int[6] {
                0, 2, 1, 2, 3, 1
            };
            mesh.RecalculateNormals();
            return;
        }
        int reduceCount = 0;
        int pos         = 0;

        for (int i = 0; i < transformArray.Length; i++)
        {
            if (transformArray [i] == true)
            {
                reduceCount += (splitCount >> 1);
            }
        }
        if (tris == null || tris.Length != splitCount * splitCount * 6)
        {
            tris         = new int[splitCount * splitCount * 6];
            vertsLowPoly = new Vector3[tris.Length];
            normals      = new Vector3[tris.Length];
            uv           = new Vector2[tris.Length];
        }
        else
        {
            max = reduceCount * 3;
            pos = splitCount * splitCount * 6 - 1;
            for (int i = 0; i < max; i++)
            {
                tris [pos] = 0;
                pos--;
            }
        }
        //tris = new int[(splitCount*splitCount*2-reduceCount)*3];//Cause GC

        pos = 0;
        int x = 0;
        int z = 0;

        //Up
        x = 0;
        z = splitCount - 1;
        if (transformArray [0])
        {
            max = splitCount >> 1;
            if (splitCount == 2)
            {
                tris [pos]     = GetVertsPos(x + 1, z);
                tris [pos + 1] = GetVertsPos(x, z + 1);
                tris [pos + 2] = GetVertsPos(x + 2, z + 1);
                pos           += 3;
            }
            else
            {
                for (int i = 0; i < max; i++)
                {
                    if (i == 0)
                    {
                        tris [pos]     = GetVertsPos(x + 1, z);
                        tris [pos + 1] = GetVertsPos(x, z + 1);
                        tris [pos + 2] = GetVertsPos(x + 2, z + 1);

                        tris [pos + 3] = tris [pos];
                        tris [pos + 4] = tris [pos + 2];
                        tris [pos + 5] = GetVertsPos(x + 2, z);
                        pos           += 6;
                    }
                    else if (i == max - 1)
                    {
                        tris [pos]     = GetVertsPos(x, z);
                        tris [pos + 1] = GetVertsPos(x, z + 1);
                        tris [pos + 2] = GetVertsPos(x + 1, z);

                        tris [pos + 3] = tris [pos + 2];
                        tris [pos + 4] = tris [pos + 1];
                        tris [pos + 5] = GetVertsPos(x + 2, z + 1);
                        pos           += 6;
                    }
                    else
                    {
                        tris [pos]     = GetVertsPos(x, z);
                        tris [pos + 1] = GetVertsPos(x, z + 1);
                        tris [pos + 2] = GetVertsPos(x + 1, z);

                        tris [pos + 3] = tris [pos + 2];
                        tris [pos + 4] = tris [pos + 1];
                        tris [pos + 5] = GetVertsPos(x + 2, z + 1);

                        tris [pos + 6] = tris [pos + 2];
                        tris [pos + 7] = tris [pos + 5];
                        tris [pos + 8] = GetVertsPos(x + 2, z);
                        pos           += 9;
                    }
                    x += 2;
                }
            }
        }
        else
        {
            max = splitCount;
            if (splitCount == 2)
            {
                tris [pos]     = GetVertsPos(x + 1, z);
                tris [pos + 1] = GetVertsPos(x, z + 1);
                tris [pos + 2] = GetVertsPos(x + 1, z + 1);
                tris [pos + 3] = tris [pos];
                tris [pos + 4] = tris [pos + 2];
                tris [pos + 5] = GetVertsPos(x + 2, z + 1);
                pos           += 6;
            }
            else
            {
                for (int i = 0; i < max; i++)
                {
                    if (i == 0)
                    {
                        tris [pos]     = GetVertsPos(x + 1, z);
                        tris [pos + 1] = GetVertsPos(x, z + 1);
                        tris [pos + 2] = GetVertsPos(x + 1, z + 1);
                        pos           += 3;
                    }
                    else if (i == max - 1)
                    {
                        tris [pos]     = GetVertsPos(x, z);
                        tris [pos + 1] = GetVertsPos(x, z + 1);
                        tris [pos + 2] = GetVertsPos(x + 1, z + 1);
                        pos           += 3;
                    }
                    else
                    {
                        tris [pos]     = GetVertsPos(x, z);
                        tris [pos + 1] = GetVertsPos(x, z + 1);
                        tris [pos + 2] = GetVertsPos(x + 1, z);
                        tris [pos + 3] = tris [pos + 2];
                        tris [pos + 4] = tris [pos + 1];
                        tris [pos + 5] = GetVertsPos(x + 1, z + 1);
                        pos           += 6;
                    }
                    x += 1;
                }
            }
        }
        //Right

        x = splitCount - 1;
        z = 0;
        if (transformArray [1])
        {
            max = splitCount >> 1;
            if (splitCount == 2)
            {
                tris [pos]     = GetVertsPos(x + 1, z);
                tris [pos + 1] = GetVertsPos(x, z + 1);
                tris [pos + 2] = GetVertsPos(x + 1, z + 2);
                pos           += 3;
            }
            else
            {
                for (int i = 0; i < max; i++)
                {
                    if (i == 0)
                    {
                        tris [pos]     = GetVertsPos(x + 1, z);
                        tris [pos + 1] = GetVertsPos(x, z + 1);
                        tris [pos + 2] = GetVertsPos(x + 1, z + 2);

                        tris [pos + 3] = tris [pos + 2];
                        tris [pos + 4] = tris [pos + 1];
                        tris [pos + 5] = GetVertsPos(x, z + 2);
                        pos           += 6;
                    }
                    else if (i == max - 1)
                    {
                        tris [pos]     = GetVertsPos(x + 1, z);
                        tris [pos + 1] = GetVertsPos(x, z);
                        tris [pos + 2] = GetVertsPos(x, z + 1);

                        tris [pos + 3] = tris [pos];
                        tris [pos + 4] = tris [pos + 2];
                        tris [pos + 5] = GetVertsPos(x + 1, z + 2);
                        pos           += 6;
                    }
                    else
                    {
                        tris [pos]     = GetVertsPos(x + 1, z);
                        tris [pos + 1] = GetVertsPos(x, z);
                        tris [pos + 2] = GetVertsPos(x, z + 1);

                        tris [pos + 3] = tris [pos];
                        tris [pos + 4] = tris [pos + 2];
                        tris [pos + 5] = GetVertsPos(x + 1, z + 2);

                        tris [pos + 6] = tris [pos + 5];
                        tris [pos + 7] = tris [pos + 2];
                        tris [pos + 8] = GetVertsPos(x, z + 2);
                        pos           += 9;
                    }
                    z += 2;
                }
            }
        }
        else
        {
            max = splitCount;
            if (splitCount == 2)
            {
                tris [pos]     = GetVertsPos(x + 1, z);
                tris [pos + 1] = GetVertsPos(x, z + 1);
                tris [pos + 2] = GetVertsPos(x + 1, z + 1);
                tris [pos + 3] = tris [pos + 2];
                tris [pos + 4] = tris [pos + 1];
                tris [pos + 5] = GetVertsPos(x + 1, z + 2);
                pos           += 6;
            }
            else
            {
                for (int i = 0; i < max; i++)
                {
                    if (i == 0)
                    {
                        tris [pos]     = GetVertsPos(x + 1, z);
                        tris [pos + 1] = GetVertsPos(x, z + 1);
                        tris [pos + 2] = GetVertsPos(x + 1, z + 1);
                        pos           += 3;
                    }
                    else if (i == max - 1)
                    {
                        tris [pos]     = GetVertsPos(x + 1, z);
                        tris [pos + 1] = GetVertsPos(x, z);
                        tris [pos + 2] = GetVertsPos(x + 1, z + 1);
                        pos           += 3;
                    }
                    else
                    {
                        tris [pos]     = GetVertsPos(x + 1, z);
                        tris [pos + 1] = GetVertsPos(x, z);
                        tris [pos + 2] = GetVertsPos(x, z + 1);
                        tris [pos + 3] = tris [pos];
                        tris [pos + 4] = tris [pos + 2];
                        tris [pos + 5] = GetVertsPos(x + 1, z + 1);
                        pos           += 6;
                    }
                    z += 1;
                }
            }
        }


        //Down
        x = 0;
        z = 0;
        if (transformArray [2])
        {
            max = splitCount >> 1;
            if (splitCount == 2)
            {
                tris [pos]     = GetVertsPos(x, z);
                tris [pos + 1] = GetVertsPos(x + 1, z + 1);
                tris [pos + 2] = GetVertsPos(x + 2, z);
                pos           += 3;
            }
            else
            {
                for (int i = 0; i < max; i++)
                {
                    if (i == 0)
                    {
                        tris [pos]     = GetVertsPos(x, z);
                        tris [pos + 1] = GetVertsPos(x + 1, z + 1);
                        tris [pos + 2] = GetVertsPos(x + 2, z);

                        tris [pos + 3] = tris [pos + 2];
                        tris [pos + 4] = tris [pos + 1];
                        tris [pos + 5] = GetVertsPos(x + 2, z + 1);
                        pos           += 6;
                    }
                    else if (i == max - 1)
                    {
                        tris [pos]     = GetVertsPos(x, z);
                        tris [pos + 1] = GetVertsPos(x, z + 1);
                        tris [pos + 2] = GetVertsPos(x + 1, z + 1);

                        tris [pos + 3] = tris [pos];
                        tris [pos + 4] = tris [pos + 2];
                        tris [pos + 5] = GetVertsPos(x + 2, z);
                        pos           += 6;
                    }
                    else
                    {
                        tris [pos]     = GetVertsPos(x, z);
                        tris [pos + 1] = GetVertsPos(x, z + 1);
                        tris [pos + 2] = GetVertsPos(x + 1, z + 1);

                        tris [pos + 3] = tris [pos];
                        tris [pos + 4] = tris [pos + 2];
                        tris [pos + 5] = GetVertsPos(x + 2, z);

                        tris [pos + 6] = tris [pos + 5];
                        tris [pos + 7] = tris [pos + 2];
                        tris [pos + 8] = GetVertsPos(x + 2, z + 1);
                        pos           += 9;
                    }
                    x += 2;
                }
            }
        }
        else
        {
            max = splitCount;
            if (splitCount == 2)
            {
                tris [pos]     = GetVertsPos(x, z);
                tris [pos + 1] = GetVertsPos(x + 1, z + 1);
                tris [pos + 2] = GetVertsPos(x + 1, z);
                tris [pos + 3] = tris [pos + 2];
                tris [pos + 4] = tris [pos + 1];
                tris [pos + 5] = GetVertsPos(x + 2, z);
                pos           += 6;
            }
            else
            {
                for (int i = 0; i < max; i++)
                {
                    if (i == 0)
                    {
                        tris [pos]     = GetVertsPos(x, z);
                        tris [pos + 1] = GetVertsPos(x + 1, z + 1);
                        tris [pos + 2] = GetVertsPos(x + 1, z);
                        pos           += 3;
                    }
                    else if (i == max - 1)
                    {
                        tris [pos]     = GetVertsPos(x, z);
                        tris [pos + 1] = GetVertsPos(x, z + 1);
                        tris [pos + 2] = GetVertsPos(x + 1, z);
                        pos           += 3;
                    }
                    else
                    {
                        tris [pos]     = GetVertsPos(x, z);
                        tris [pos + 1] = GetVertsPos(x, z + 1);
                        tris [pos + 2] = GetVertsPos(x + 1, z);
                        tris [pos + 3] = tris [pos + 2];
                        tris [pos + 4] = tris [pos + 1];
                        tris [pos + 5] = GetVertsPos(x + 1, z + 1);
                        pos           += 6;
                    }
                    x += 1;
                }
            }
        }
        //Left

        x = 0;
        z = 0;
        if (transformArray [3])
        {
            max = splitCount >> 1;
            if (splitCount == 2)
            {
                tris [pos]     = GetVertsPos(x, z);
                tris [pos + 1] = GetVertsPos(x, z + 2);
                tris [pos + 2] = GetVertsPos(x + 1, z + 1);
                pos           += 3;
            }
            else
            {
                for (int i = 0; i < max; i++)
                {
                    if (i == 0)
                    {
                        tris [pos]     = GetVertsPos(x, z);
                        tris [pos + 1] = GetVertsPos(x, z + 2);
                        tris [pos + 2] = GetVertsPos(x + 1, z + 1);

                        tris [pos + 3] = tris [pos + 1];
                        tris [pos + 4] = GetVertsPos(x + 1, z + 2);
                        tris [pos + 5] = tris [pos + 2];
                        pos           += 6;
                    }
                    else if (i == max - 1)
                    {
                        tris [pos]     = GetVertsPos(x, z);
                        tris [pos + 1] = GetVertsPos(x + 1, z + 1);
                        tris [pos + 2] = GetVertsPos(x + 1, z);

                        tris [pos + 3] = tris [pos];
                        tris [pos + 4] = GetVertsPos(x, z + 2);
                        tris [pos + 5] = tris [pos + 1];
                        pos           += 6;
                    }
                    else
                    {
                        tris [pos]     = GetVertsPos(x, z);
                        tris [pos + 1] = GetVertsPos(x + 1, z + 1);
                        tris [pos + 2] = GetVertsPos(x + 1, z);

                        tris [pos + 3] = tris [pos];
                        tris [pos + 4] = GetVertsPos(x, z + 2);
                        tris [pos + 5] = tris [pos + 1];

                        tris [pos + 6] = tris [pos + 4];
                        tris [pos + 7] = GetVertsPos(x + 1, z + 2);
                        tris [pos + 8] = tris [pos + 1];
                        pos           += 9;
                    }
                    z += 2;
                }
            }
        }
        else
        {
            max = splitCount;
            if (splitCount == 2)
            {
                tris [pos]     = GetVertsPos(x, z);
                tris [pos + 1] = GetVertsPos(x, z + 1);
                tris [pos + 2] = GetVertsPos(x + 1, z + 1);
                tris [pos + 3] = tris [pos + 1];
                tris [pos + 4] = GetVertsPos(x, z + 2);
                tris [pos + 5] = tris [pos + 2];
                pos           += 6;
            }
            else
            {
                for (int i = 0; i < max; i++)
                {
                    if (i == 0)
                    {
                        tris [pos]     = GetVertsPos(x, z);
                        tris [pos + 1] = GetVertsPos(x, z + 1);
                        tris [pos + 2] = GetVertsPos(x + 1, z + 1);
                        pos           += 3;
                    }
                    else if (i == max - 1)
                    {
                        tris [pos]     = GetVertsPos(x, z);
                        tris [pos + 1] = GetVertsPos(x, z + 1);
                        tris [pos + 2] = GetVertsPos(x + 1, z);
                        pos           += 3;
                    }
                    else
                    {
                        tris [pos]     = GetVertsPos(x, z);
                        tris [pos + 1] = GetVertsPos(x, z + 1);
                        tris [pos + 2] = GetVertsPos(x + 1, z);
                        tris [pos + 3] = tris [pos + 1];
                        tris [pos + 4] = GetVertsPos(x + 1, z + 1);
                        tris [pos + 5] = tris [pos + 2];
                        pos           += 6;
                    }
                    z += 1;
                }
            }
        }



        //Mid
        x         = 1;
        z         = 1;
        lineCount = splitCount - 1;
        int rowCount = splitCount - 2;

        max = rowCount * rowCount;
        for (int i = 0; i < max; i++)
        {
            tris[pos]     = GetVertsPos(x, z);
            tris[pos + 1] = GetVertsPos(x, z + 1);
            tris[pos + 2] = GetVertsPos(x + 1, z);

            tris[pos + 3] = tris[pos + 2];
            tris[pos + 4] = tris[pos + 1];
            tris[pos + 5] = GetVertsPos(x + 1, z + 1);
            pos          += 6;
            x++;
            if (x >= lineCount)
            {
                x = 1;
                z++;
            }
        }
        float heightTemp;

        lineCount = 0;
        for (int i = 0; i < tris.Length; i++)
        {
            vertsLowPoly[i] = verts[tris[i]];
            if (lineCount == 0)
            {
                heightTemp = MathExtra.Min(height[tris[i]], height[tris[i + 1]], height[tris[i + 2]]);
                if (heightTemp >= 0.6f)
                {
                    uv [i] = uv [i + 1] = uv [i + 2] = new Vector2(0f, 0f);
                }
                else if (heightTemp >= 0.3f)
                {
                    uv [i] = uv [i + 1] = uv [i + 2] = new Vector2(0.2f, 0.14f);
                }
                else
                {
                    uv [i] = uv [i + 1] = uv [i + 2] = new Vector2(0.2f, 0.25f);
                }
                lineCount = 3;
            }
            lineCount--;
            tris [i] = i;
        }


        Vector3 p1;
        Vector3 p2;

        for (int i = 0; i < vertsLowPoly.Length; i += 3)
        {
            // Calculate the normal of the triangle
            p1         = vertsLowPoly[i + 1] - vertsLowPoly[i];
            p2         = vertsLowPoly[i + 2] - vertsLowPoly[i];
            normals[i] = normals[i + 1] = normals[i + 2] = MathExtra.FastNormalize(Vector3.Cross(p1, p2));
        }
        mesh.vertices  = vertsLowPoly;
        mesh.triangles = tris;
        mesh.normals   = normals;
        mesh.uv        = uv;
        if (node.lodLevel == 0)
        {
            meshCollider.enabled = true;
        }
        else
        {
            meshCollider.enabled = false;
        }
        gameObject.SetActive(true);
    }
Exemple #18
0
 public QTNode()
 {
     ul = ur = ll = lr = null;
 }
Exemple #19
0
        protected QTNode ReadNode( StreamReader r )
        {
            string ln = r.ReadLine();
              if ( ln == null ||
               ln.Length < 1 )
            return null;
              string[] line = ln.Split( ' ' );
              if ( line.Length < 1 ) return null;

              if ( line[ 0 ].Equals( "R" ) )   // inner node
              {
            QTNode inner = new QTNode();
            inner.ul = ReadNode( r );
            inner.ur = ReadNode( r );
            inner.ll = ReadNode( r );
            inner.lr = ReadNode( r );
            return inner;
              }

              if ( line.Length < 3 ) return null;
              else                             // leaf node
              {
            int R, G, B;
            if ( !int.TryParse( line[ 0 ], out R ) ||
             !int.TryParse( line[ 1 ], out G ) ||
             !int.TryParse( line[ 2 ], out B ) )
              return null;

            return new QTNode( Color.FromArgb( R, G, B ) );
              }
        }
Exemple #20
0
 public QuadTree()
 {
     root  = null;
     width = height = 0;
 }
Exemple #21
0
 // !!!{{ You probably don't want to modify this code..?!
 protected void WriteNode( StreamWriter w, QTNode n )
 {
     if ( n.ul == null )              // leaf node
     w.WriteLine( "{0} {1} {2}", n.col.R, n.col.G, n.col.B );
       else
       {
     w.WriteLine( 'R' );            // inner node
     WriteNode( w, n.ul );
     WriteNode( w, n.ur );
     WriteNode( w, n.ll );
     WriteNode( w, n.lr );
       }
 }
Exemple #22
0
        private void AddQuadTreeEnvelopes(QTNode node, IMap map)
        {
            if (node.indexList != null)
            {
                //log.Debug(node.Bounds + ": " + string.Join(", ", node.indexList));

                var i = 0;
                foreach (var bound in node.boundsList)
                {
                    var id = node.indexList[i];

                    if (quadTreeEnvelopesLayer == null)
                    {
                        var featureCollection = new FeatureCollection {
                            Features = new List <Feature> {
                            }
                        };
                        quadTreeEnvelopesLayer = new VectorLayer
                        {
                            DataSource = featureCollection,
                            Style      = { Fill = Brushes.Transparent, Outline = { Width = 1, Color = Color.DarkGreen } },

                            Name       = Name + " quad tree, level: " + node.Level,
                            Selectable = false,
                            //UseQuadTree = true
                        };

                        var theme = new CategorialTheme("IsSelected", null);
                        theme.AddThemeItem(new CategorialThemeItem("true", new VectorStyle {
                            Fill = Brushes.Transparent, Outline = { Width = 3, Color = Color.FromArgb(200, 200, 100, 100) }
                        }, null));
                        theme.AddThemeItem(new CategorialThemeItem("false", new VectorStyle {
                            Fill = Brushes.Transparent, Outline = { Width = 3, Color = Color.FromArgb(50, 100, 200, 100) }
                        }, null));
                        quadTreeEnvelopesLayer.Theme = theme;

                        map.Layers.Insert(0, quadTreeEnvelopesLayer);
                        map.BringToFront(quadTreeEnvelopesLayer);
                    }

                    //if (!quadTreeEnvelopesLayer.DataSource.Features.Cast<IFeature>().Any(f => f.Attributes["ID"].Equals(id)))
                    {
                        var feature = new Feature {
                            Geometry = geometryFactory.ToGeometry(new Envelope(bound.X, bound.X + bound.Width, bound.Y, bound.Y + bound.Height))
                        };
                        feature.Attributes = new DictionaryFeatureAttributeCollection();
                        feature.Attributes["IsSelected"] = "false";
                        feature.Attributes["ID"]         = id;

                        quadTreeEnvelopesLayer.DataSource.Features.Add(feature);
                    }

                    i++;
                }
            }

            if (node.Children != null)
            {
                AddQuadTreeEnvelopes(node.Children[0], map);
                AddQuadTreeEnvelopes(node.Children[1], map);
                AddQuadTreeEnvelopes(node.Children[2], map);
                AddQuadTreeEnvelopes(node.Children[3], map);
            }
        }
Exemple #23
0
 public QTNode()
 {
     ul = ur = ll = lr = null;
 }
 public bool NeedBack(QTNode node)
 {
     return(MathExtra.GetV3L(QTManager.Instance.playerTrans.position - node.center) / node.length >= (QTManager.Instance.activeTerrain.cl + backBuffer));
 }
Exemple #25
0
 public QTNode( Color initCol )
 {
     ul = ur = ll = lr = null;
     col = initCol;
 }
Exemple #26
0
 public QuadTree()
 {
     root = new QTNode(new Bounds(Vector3.zero, new Vector3(MAX_RANGE, MAX_Y, MAX_RANGE)), 0);
 }
Exemple #27
0
 public QTSpaceAllocator(int levels)
 {
     root = new QTNode(MINIMUM_SIZE * (1 << levels), Vector2.zero);
 }