public Texture2D generateTexture(MapProvider provider, BBox bbox, int i, int j, string OSMFileName)
        {
            string[] proj = OSMFileName.Split(new char[] {'/', '\\'});
            string projectName = proj[proj.Length - 1];

            if (File.Exists(tileFolder + "/final/" + provider.ToString("G") + "_" + projectName + "_" + i + "_" + j + ".png"))
            {
                byte[] fileData;
                fileData = File.ReadAllBytes(tileFolder + "/final/" + provider.ToString("G") + "_" + projectName + "_" + i + "_" + j + ".png");
                Texture2D texture = new Texture2D(2, 2);
                texture.LoadImage(fileData);
                return texture;
            }

            Texture2D uncroppedTexture = generateUncroppedTexture(bbox, provider, i, j);
            Rect cropWindow = generateCroppingRect(bbox);
            Texture2D finalTexture = CropTexture(uncroppedTexture, cropWindow);

            if (!Directory.Exists(tileFolder + "/final/"))
                Directory.CreateDirectory(tileFolder + "/final/");

            var tex = new Texture2D(finalTexture.width, finalTexture.height);
            tex.SetPixels32(finalTexture.GetPixels32());
            tex.Apply(false);
            File.WriteAllBytes(tileFolder + "/final/" + provider.ToString("G") + "_" + projectName + "_" + i + "_" + j + ".png", tex.EncodeToPNG());

            return finalTexture;
        }
Exemplo n.º 2
0
        //Highway Constructor
        public Highway(Way w, List<HighwayConfigurations> config, myTerrain _terrain)
        {
            id = w.id;
            way = w;
            type = getHighwayType(w.tags);
            name = getHighwayName(w.tags);

            if (type == highwayType.HighwayFootway)
                return;

            getConfiguration(config);
            bbox = _terrain.scenebbox;

            streetLampList = new List<Object3D>();
            generateInitial3Dway(_terrain);
            //colliderMesh = generateColliderMesh();

            highwayGameObject = new GameObject("Highway" + way.id, typeof(MeshRenderer), typeof(MeshFilter), typeof(MeshCollider));
            highwayGameObject.transform.localScale = new Vector3(1, 1, 1);

            if(type != highwayType.River)
                highwayGameObject.tag = "Highway";
        }
        public myTerrain(HeightmapLoader _heightmap, BBox _bbox, string _OSMfileName, MapProvider _provider)
        {
            OSMfileName = _OSMfileName;
            heightmap = _heightmap;
            scenebbox = _bbox;
            textureType = _provider;

            terrainObject = new GameObject("Terrain");
            gridList = new List<GameObject>();

            int leftIndex = (int)Math.Floor((scenebbox.left - (float)Math.Floor(scenebbox.left)) * 1200.0f);
            int rightIndex = (int)Math.Ceiling((scenebbox.right - (float)Math.Floor(scenebbox.right)) * 1200.0f);

            if ((rightIndex - leftIndex) % 2 != 0)
                rightIndex++;

            int topIndex, bottomIndex;

            topIndex = (int)Math.Floor(((float)Math.Ceiling(scenebbox.top) - scenebbox.top) * 1200.0f);
            bottomIndex = (int)Math.Ceiling(((float)Math.Ceiling(scenebbox.bottom) - scenebbox.bottom) * 1200.0f);

            if ((bottomIndex - topIndex) % 2 != 0)
                topIndex -= 1;

            Debug.Log("<color=yellow>TERRAIN:</color>" + "left:" + leftIndex + " right:" + rightIndex
                       + " bottom:" + bottomIndex + " top:" + topIndex);

            float[,] myTerrainHeights = new float[1 + bottomIndex - topIndex, 1 + rightIndex - leftIndex ];
            Vector2[,] meterPositions = new Vector2[1 + bottomIndex - topIndex, 1 + rightIndex - leftIndex ];
            Geography geo = new Geography();

            float left = (float)Math.Floor(scenebbox.left) + (leftIndex / 1200.0f);
            float right = (float)Math.Floor(scenebbox.left) + (rightIndex / 1200.0f);
            float top = (float)Math.Ceiling(scenebbox.top) - (topIndex / 1200.0f);
            float bottom = (float)Math.Ceiling(scenebbox.top) - (bottomIndex / 1200.0f);

            for (int i = 0; i <= bottomIndex - topIndex; i++)
            {
                for (int j = 0; j <= rightIndex - leftIndex; j++)
                {
                    myTerrainHeights[i, j] = heightmap.heightmap[topIndex + i, leftIndex + j];
                    meterPositions[i , j] =  geo.LatLontoMeters(top - (i / 1200.0f), left + (j / 1200.0f));
                }
            }

            terrainInfo.leftIndex = leftIndex;
            terrainInfo.rightIndex = rightIndex;
            terrainInfo.bottomIndex = bottomIndex;
            terrainInfo.topIndex = topIndex;
            terrainInfo.terrainHeights = myTerrainHeights;
            terrainInfo.meterPositions = meterPositions;
            terrainInfo.ColumnCount = 1 + rightIndex - leftIndex;
            terrainInfo.RowCount = 1 + bottomIndex - topIndex;

            terrainInfo.terrainBBox = new BBox();
            terrainInfo.terrainBBox.left = left;
            terrainInfo.terrainBBox.top = top;
            terrainInfo.terrainBBox.bottom = bottom;
            terrainInfo.terrainBBox.right = right;
            Vector2 bottmleft = geo.LatLontoMeters(bottom, left);
            Vector2 topright = geo.LatLontoMeters(top, right);
            terrainInfo.terrainBBox.meterBottom = bottmleft.x;
            terrainInfo.terrainBBox.meterLeft = bottmleft.y;
            terrainInfo.terrainBBox.meterTop = topright.x;
            terrainInfo.terrainBBox.meterRight = topright.y;

            terrainInfo.shiftX = scenebbox.meterLeft;
            terrainInfo.shiftZ = scenebbox.meterBottom;

            Debug.Log("<color=yellow>TERRAIN:</color> ColumnCount:" + terrainInfo.ColumnCount + " RowCount:" + terrainInfo.RowCount);

               // drawBoundsforDebug();

            for (int i = 0; i < terrainInfo.RowCount-1; i += 2)
            {
                for (int j = 0; j < terrainInfo.ColumnCount-1; j += 2)
                    createGrid(i,j);
            }

            drawUnderPlates();
        }
        //Creates 2x2 subgrid of Terrain
        private void createGrid(int i, int j)
        {
            BBox bbox = new BBox();
            bbox.meterLeft = terrainInfo.meterPositions[i, j].y;
            bbox.meterTop = terrainInfo.meterPositions[i, j].x;
            bbox.meterRight = terrainInfo.meterPositions[i, j + 2].y;
            bbox.meterBottom = terrainInfo.meterPositions[i + 2, j].x;

            //Debug.Log("<color=yellow>INDEX</color> i:" + i + " j:" + j);
            //Debug.Log("<color=red>Grid BBOX:</color> left:" + bbox.meterLeft + " right:" + bbox.meterRight +
            //         " bottom:" + bbox.meterBottom + " top:" + bbox.meterTop);

            GameObject grid = new GameObject("Grid"+ i + "_" + j , typeof(MeshRenderer), typeof(MeshFilter), typeof(MeshCollider));
            grid.transform.parent = terrainObject.transform;
            grid.tag = "Terrain";
            grid.AddComponent<TerrainMouseHandler>();

            Mesh mesh = new Mesh();

            Vector3[] Vertices = new Vector3[9];

            for (int m = i, itr = 0; m <= i+2; m++)
            {
                for (int n = j; n <= j+2 ; n++)
                {
                    Vertices[itr] = new Vector3(terrainInfo.meterPositions[m, n].y - terrainInfo.shiftX,
                                               terrainInfo.terrainHeights[m, n],
                                               terrainInfo.meterPositions[m, n].x - terrainInfo.shiftZ);
                    itr++;
                }
            }

            int[] Triangles = new int[] { 0, 1, 3, 1, 4, 3, //Quad1
                                          1, 2, 4, 2, 5, 4, //Quad2
                                          3, 4, 6, 4, 7, 6, //Quad3
                                          4, 5, 7, 5, 8, 7  //Quad4
                                         };

            Vector2[] UVCoords = new Vector2[] {
                                                new Vector2(0.0f,1.0f), new Vector2(0.5f,1.0f), new Vector2(1.0f,1.0f),
                                                new Vector2(0.0f,0.5f), new Vector2(0.5f,0.5f), new Vector2(1.0f,0.5f),
                                                new Vector2(0.0f,0.0f), new Vector2(0.5f,0.0f), new Vector2(1.0f,0.0f)
                                               };

            Vector3[] Normals = calculateNormals(i,j);

            mesh.vertices = Vertices;
            mesh.triangles = Triangles;
            mesh.uv = UVCoords;
            mesh.normals = Normals;

            MeshFilter meshfilter =  grid.GetComponent<MeshFilter>();
            meshfilter.mesh = mesh;

            Material matTerrain = (Material)Resources.Load("Materials/Terrain/Mat_Terrain", typeof(Material));
            Material mat = new Material(matTerrain);
            //mat.globalIlluminationFlags = MaterialGlobalIlluminationFlags.None;

            TerrainTextureHandler texturehandler = new TerrainTextureHandler();
            mat.mainTexture = texturehandler.generateTexture(textureType, bbox,i,j,OSMfileName);
            MeshRenderer meshrenderer = grid.GetComponent<MeshRenderer>();
            meshrenderer.material = mat;

            MeshCollider meshCollider = grid.GetComponent<MeshCollider>();
            meshCollider.sharedMesh = mesh;

            gridList.Add(grid);
        }
        private Texture2D generateUncroppedTexture(BBox bbox, MapProvider provider, int ii, int jj)
        {
            Vector2 mintileCoord = new Vector2();
            Vector2 maxtileCoord = new Vector2();

            FindTiles(bbox, ref mintileCoord, ref maxtileCoord);

            Debug.Log("<color=green>TEXTURE:</color> mintile X:" + mintileCoord.x + " Y:" + mintileCoord.y +
                " maxtile X:" + maxtileCoord.x + " Y:" + maxtileCoord.y);

            int ColumnCount = (int)(1 + maxtileCoord.x - mintileCoord.x);
            int RowCount = (int)(1 + mintileCoord.y - maxtileCoord.y);

            Texture2D[,] textures = new Texture2D[RowCount, ColumnCount];

            for (int i = (int)maxtileCoord.y, m=0; i <= (int)mintileCoord.y ; i++,m++)
            {
                for(int j= (int) mintileCoord.x, n=0 ; j <= (int) maxtileCoord.x ; j++, n++)
                {
                    textures[m,n] = DownloadTile(j, i, provider);
                }
            }

            Texture2D finalTexture = ConcatTexture(textures, ColumnCount, RowCount);
            return finalTexture;
        }
        private Rect generateCroppingRect(BBox bbox)
        {
            Geography geo = new Geography();

            Vector2 mintileCoord = new Vector2();
            Vector2 maxtileCoord = new Vector2();
            FindTiles(bbox, ref mintileCoord, ref maxtileCoord);

            float left, bottom, right, top;
            double res = geo.Resolution(zoomLevel);

            left = (float)Math.Round((bbox.meterLeft + geo.originShift) / res) - (mintileCoord.x * tileSize);
            right = (float)Math.Round((bbox.meterRight + geo.originShift) / res) - (mintileCoord.x * tileSize);

            bottom = (float)Math.Round((-bbox.meterBottom + geo.originShift) / res) - (maxtileCoord.y * tileSize);
            top = (float)Math.Round((-bbox.meterTop + geo.originShift) / res) - (maxtileCoord.y * tileSize);

            Rect croppingRect = new Rect(left, top, right - left, bottom - top);

            return croppingRect;
        }
        private void FindTiles(BBox bbox, ref Vector2 mintileCoord, ref Vector2 maxtileCoord)
        {
            Geography geo = new Geography();

            mintileCoord = geo.MetersToTile(bbox.meterBottom,bbox.meterLeft,zoomLevel);
            maxtileCoord = geo.MetersToTile(bbox.meterTop, bbox.meterRight,zoomLevel);
        }
        private BBox readBounds(XmlNode node)
        {
            BBox bb = new BBox();

            foreach (XmlAttribute attr in node.Attributes)
            {
            if (attr.Name == "minlat")
                bb.bottom = float.Parse(attr.Value);
            else if (attr.Name == "minlon")
                bb.left = float.Parse(attr.Value);
            else if (attr.Name == "maxlat")
                bb.top = float.Parse(attr.Value);
            else if (attr.Name == "maxlon")
                bb.right = float.Parse(attr.Value);
            }
            return bb;
        }