/// <summary> /// Convert Spherical Mercator Coordinates of scene BBox to Unity Coordinates /// </summary> /// <param name="bbox"> bbox obtained from OSM file </param> /// <returns></returns> private BBox editbbox(BBox bbox) { Geography proj = new Geography(); Vector2 bottomleft = proj.LatLontoMeters(bbox.bottom, bbox.left); Vector2 topright = proj.LatLontoMeters(bbox.top, bbox.right); bbox.meterLeft = bottomleft.y; bbox.meterBottom = bottomleft.x; bbox.meterTop = topright.x; bbox.meterRight = topright.y; return(bbox); }
/// <summary> /// Converts Spherical Mercator Coordinates of all nodes to Unity Coordinates /// </summary> private void assignNodePositions() { Geography proj = new Geography(); for (int k = 0; k < osmxml.nodeList.Count; k++) { Node nd = osmxml.nodeList[k]; Vector2 meterCoord = proj.LatLontoMeters(nd.lat, nd.lon); Vector2 shift = new Vector2(scenebbox.meterBottom, scenebbox.meterLeft); meterCoord = meterCoord - shift; float height = terrain.getTerrainHeight(nd.lat, nd.lon); nd.meterPosition = new Vector3(meterCoord.y, height, meterCoord.x); osmxml.nodeList[k] = nd; } for (int i = 0; i < osmxml.wayList.Count; i++) { for (int k = 0; k < osmxml.wayList[i].nodes.Count; k++) { Node nd = osmxml.wayList[i].nodes[k]; Vector2 meterCoord = proj.LatLontoMeters(nd.lat, nd.lon); Vector2 shift = new Vector2(scenebbox.meterBottom, scenebbox.meterLeft); meterCoord = meterCoord - shift; float height = terrain.getTerrainHeight(nd.lat, nd.lon); nd.meterPosition = new Vector3(meterCoord.y, height, meterCoord.x); osmxml.wayList[i].nodes[k] = nd; } } for (int i = 0; i < osmxml.defaultobject3DList.Count; i++) { Node nd = osmxml.defaultobject3DList[i]; Vector2 meterCoord = proj.LatLontoMeters(nd.lat, nd.lon); Vector2 shift = new Vector2(scenebbox.meterBottom, scenebbox.meterLeft); meterCoord = meterCoord - shift; float height = terrain.getTerrainHeight(nd.lat, nd.lon); nd.meterPosition = new Vector3(meterCoord.y, height, meterCoord.x); osmxml.defaultobject3DList[i] = nd; } }
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(); }