private MeshDataArray CreateBaseMesh(UnityTile tile, int sampleCount)
        {
            //TODO use arrays instead of lists
            _newVertexList.Clear();
            _newNormalList.Clear();
            _newUvList.Clear();
            _newTriangleList.Clear();

            for (float y = 0; y < sampleCount; y++)
            {
                var yrat = y / (sampleCount - 1);
                for (float x = 0; x < sampleCount; x++)
                {
                    var xrat = x / (sampleCount - 1);

                    var xx = Mathd.Lerp(tile.Rect.Min.x, tile.Rect.Max.x, xrat);
                    var yy = Mathd.Lerp(tile.Rect.Min.y, tile.Rect.Max.y, yrat);

                    _newVertexList.Add(new Vector3(
                                           (float)(xx - tile.Rect.Center.x) * tile.TileScale,
                                           0,
                                           (float)(yy - tile.Rect.Center.y) * tile.TileScale));
                    _newNormalList.Add(Mapbox.Unity.Constants.Math.Vector3Up);
                    _newUvList.Add(new Vector2(x * 1f / (sampleCount - 1), 1 - (y * 1f / (sampleCount - 1))));
                }
            }

            int vertA, vertB, vertC;

            for (int y = 0; y < sampleCount - 1; y++)
            {
                for (int x = 0; x < sampleCount - 1; x++)
                {
                    vertA = (y * sampleCount) + x;
                    vertB = (y * sampleCount) + x + sampleCount + 1;
                    vertC = (y * sampleCount) + x + sampleCount;
                    _newTriangleList.Add(vertA);
                    _newTriangleList.Add(vertB);
                    _newTriangleList.Add(vertC);

                    vertA = (y * sampleCount) + x;
                    vertB = (y * sampleCount) + x + 1;
                    vertC = (y * sampleCount) + x + sampleCount + 1;
                    _newTriangleList.Add(vertA);
                    _newTriangleList.Add(vertB);
                    _newTriangleList.Add(vertC);
                }
            }

            var mesh = new MeshDataArray();

            mesh.Vertices  = _newVertexList.ToArray();
            mesh.Normals   = _newNormalList.ToArray();
            mesh.Uvs       = _newUvList.ToArray();
            mesh.Triangles = _newTriangleList.ToArray();
            return(mesh);
        }
Esempio n. 2
0
        private void BuildQuad(UnityTile tile)
        {
            var unityMesh = tile.MeshFilter.sharedMesh;
            var verts     = new Vector3[4];
            var norms     = new Vector3[4];

            verts[0] = tile.TileScale * ((tile.Rect.Min - tile.Rect.Center).ToVector3xz());
            verts[1] = tile.TileScale * (new Vector3((float)(tile.Rect.Max.x - tile.Rect.Center.x), 0, (float)(tile.Rect.Min.y - tile.Rect.Center.y)));
            verts[2] = tile.TileScale * ((tile.Rect.Max - tile.Rect.Center).ToVector3xz());
            verts[3] = tile.TileScale * (new Vector3((float)(tile.Rect.Min.x - tile.Rect.Center.x), 0, (float)(tile.Rect.Max.y - tile.Rect.Center.y)));
            norms[0] = Constants.Math.Vector3Up;
            norms[1] = Constants.Math.Vector3Up;
            norms[2] = Constants.Math.Vector3Up;
            norms[3] = Constants.Math.Vector3Up;

            unityMesh.vertices = verts;
            unityMesh.normals  = norms;

            var trilist = new int[6] {
                0, 1, 2, 0, 2, 3
            };

            unityMesh.triangles = trilist;

            var uvlist = new Vector2[4]
            {
                new Vector2(0, 1),
                new Vector2(1, 1),
                new Vector2(1, 0),
                new Vector2(0, 0)
            };

            unityMesh.uv = uvlist;
            _cachedQuad  = new MeshDataArray()
            {
                Vertices  = verts,
                Normals   = norms,
                Triangles = trilist,
                Uvs       = uvlist
            };
        }
Esempio n. 3
0
        private void BuildQuadWithSides(UnityTile tile)
        {
            var unityMesh = tile.MeshFilter.sharedMesh;
            var verts     = new Vector3[20];
            var norms     = new Vector3[20];

            verts[0] = tile.TileScale * ((tile.Rect.Min - tile.Rect.Center).ToVector3xz());
            verts[1] = tile.TileScale * (new Vector3((float)(tile.Rect.Max.x - tile.Rect.Center.x), 0, (float)(tile.Rect.Min.y - tile.Rect.Center.y)));
            verts[2] = tile.TileScale * ((tile.Rect.Max - tile.Rect.Center).ToVector3xz());
            verts[3] = tile.TileScale * (new Vector3((float)(tile.Rect.Min.x - tile.Rect.Center.x), 0, (float)(tile.Rect.Max.y - tile.Rect.Center.y)));
            norms[0] = Mapbox.Unity.Constants.Math.Vector3Up;
            norms[1] = Mapbox.Unity.Constants.Math.Vector3Up;
            norms[2] = Mapbox.Unity.Constants.Math.Vector3Up;
            norms[3] = Mapbox.Unity.Constants.Math.Vector3Up;

            //verts goes
            //01
            //32
            unityMesh.subMeshCount = 2;
            Vector3 norm = Mapbox.Unity.Constants.Math.Vector3Up;

            for (int i = 0; i < 4; i++)
            {
                verts[4 * (i + 1)]     = verts[i];
                verts[4 * (i + 1) + 1] = verts[i + 1];
                verts[4 * (i + 1) + 2] = verts[i] + new Vector3(0, -_elevationOptions.sideWallOptions.wallHeight, 0);
                verts[4 * (i + 1) + 3] = verts[i + 1] + new Vector3(0, -_elevationOptions.sideWallOptions.wallHeight, 0);

                norm = Vector3.Cross(verts[4 * (i + 1) + 1] - verts[4 * (i + 1) + 2], verts[4 * (i + 1)] - verts[4 * (i + 1) + 1]).normalized;
                norms[4 * (i + 1)]     = norm;
                norms[4 * (i + 1) + 1] = norm;
                norms[4 * (i + 1) + 2] = norm;
                norms[4 * (i + 1) + 3] = norm;
            }

            unityMesh.vertices = verts;
            unityMesh.normals  = norms;

            var trilist = new List <int>(6)
            {
                0, 1, 2, 0, 2, 3
            };

            unityMesh.SetTriangles(trilist, 0);

            trilist = new List <int>(8);
            for (int i = 0; i < 4; i++)
            {
                trilist.Add(4 * (i + 1));
                trilist.Add(4 * (i + 1) + 2);
                trilist.Add(4 * (i + 1) + 1);

                trilist.Add(4 * (i + 1) + 1);
                trilist.Add(4 * (i + 1) + 2);
                trilist.Add(4 * (i + 1) + 3);
            }
            unityMesh.SetTriangles(trilist, 1);

            var uvlist = new Vector2[20];

            uvlist[0] = new Vector2(0, 1);
            uvlist[1] = new Vector2(1, 1);
            uvlist[2] = new Vector2(1, 0);
            uvlist[3] = new Vector2(0, 0);
            for (int i = 4; i < 20; i += 4)
            {
                uvlist[i]     = new Vector2(1, 1);
                uvlist[i + 1] = new Vector2(0, 1);
                uvlist[i + 2] = new Vector2(1, 0);
                uvlist[i + 3] = new Vector2(0, 0);
            }
            unityMesh.uv = uvlist;
            _cachedQuad  = new MeshDataArray()
            {
                Vertices  = verts,
                Normals   = norms,
                Triangles = trilist.ToArray(),
                Uvs       = uvlist
            };
        }