public void prepareData() { switch (type) { case GODataType.DEM: case GODataType.Normals: case GODataType.Texture: case GODataType.Satellite: case GODataType.Satellite4X: textureData = new GODEMTexture2D(data, goTile.tileSize, goTile.elevationType, goTile.elevationMultiplier); break; } }
public GODEMTexture2D getTerrainData() { if (demData != null) { return(demData); } foreach (GOTileData data in tileData) { if (data.type == GOTileData.GODataType.DEM) { demData = data.textureData; } return(data.textureData); } return(null); }
public GOMesh elevatedTerrainMesh() { GODEMTexture2D terrainTex = getTerrainData(); GODEMTexture2D normalTex = getNormalsData(); GOMesh terrainMesh = new GOMesh(); terrainMesh.name = "Elevated terrain - " + name; Vector3[] vertices; Vector3[] normals; Color[] colors; Vector2 offset = Vector2.zero; vertices = new Vector3[(((int)resolution.x + 1) * ((int)resolution.y + 1))]; colors = new Color[vertices.Length]; normals = new Vector3[vertices.Length]; Vector2[] uv = new Vector2[vertices.Length]; float stepSizeWidth = tileSize.x / resolution.x; float stepSizeHeight = tileSize.y / resolution.y; for (int v = 0, z = 0; z < (int)resolution.y + 1; z++) { for (int x = 0; x < (int)resolution.x + 1; x++) { Color32 c32 = terrainTex.calculateColor(new Vector2(x, z), offset, resolution); float height = terrainTex.ConvertColorToAltitude(c32); height = height * elevationMultiplier; vertices[v] = new Vector3(x * stepSizeWidth + offset.x, height, z * stepSizeHeight + offset.y); vertices [v] -= new Vector3(tileSize.x / 2, 0, tileSize.y / 2); colors [v] = c32; if (normalTex != null) { Color32 c32Normal = normalTex.calculateColor(new Vector2(x, z), offset, resolution); normals [v] = new Vector3(c32Normal.r, c32Normal.g, c32Normal.b); } else { normals [v] = Vector3.up; } uv[v] = new Vector2(x / resolution.x, z / resolution.y); v++; } } for (int v = 0, z = 0; z < (int)resolution.y + 1; z++) { for (int x = 0; x < (int)resolution.x + 1; x++) { if (topTile != null && topTile.goMesh != null && z == (int)resolution.y) { int pos = x; vertices [v] = TrasformVectorReferences(this, topTile, topTile.goMesh.vertices[pos]); } if (leftTile != null && leftTile.goMesh != null && x == 0) { int pos = (z + 1) * (int)resolution.x + z; vertices [v] = TrasformVectorReferences(this, leftTile, leftTile.goMesh.vertices[pos]); } if (rightTile != null && rightTile.goMesh != null && x == (int)resolution.x) { int pos = z * ((int)resolution.x + 1); vertices [v] = TrasformVectorReferences(this, rightTile, rightTile.goMesh.vertices[pos]); } if (bottomTile != null && bottomTile.goMesh != null && z == 0) { int pos = bottomTile.goMesh.vertices.Count() - ((int)resolution.x + 1) + x; vertices [v] = TrasformVectorReferences(this, bottomTile, bottomTile.goMesh.vertices[pos]); } v++; } } int[] triangles = new int[((int)resolution.x) * ((int)resolution.y) * 2 * 3]; float[] heightCheck = new float [6]; for (int v = 0, t = 0, z = 0; z < (int)resolution.y; z++, t++) { for (int x = 0; x < (int)resolution.x; x++, v += 6, t++) { triangles [v] = t; triangles [v + 1] = t + (int)resolution.x + 1; triangles [v + 2] = t + 1; triangles [v + 3] = t + 1; triangles [v + 4] = t + (int)resolution.x + 1; triangles [v + 5] = t + (int)resolution.x + 2; //Checks spike map errors heightCheck[0] = vertices [triangles [v]].y; heightCheck[1] = vertices [triangles [v + 1]].y; heightCheck[2] = vertices [triangles [v + 2]].y; heightCheck[3] = vertices [triangles [v + 3]].y; heightCheck[4] = vertices [triangles [v + 4]].y; heightCheck[5] = vertices [triangles [v + 5]].y; float mean = 0.0f; float meanAbs = 0.0f; float standardDeviation = 0.0f; foreach (float height in heightCheck) { mean += height / 6.0f; meanAbs += Mathf.Abs(height) / 6.0f; standardDeviation += (height * height); } standardDeviation = standardDeviation / 6.0f; float variance = standardDeviation; standardDeviation = Mathf.Sqrt(variance); float newMean = 0.0f; int meanCount = 0; for (int i = 0; i < heightCheck.Count(); i++) { if (variance > 100 && Mathf.Abs(heightCheck [i]) > standardDeviation + meanAbs) { // CreateDebugSphere ( vertices[triangles[v+i]] + Vector3.up,"spikes STD" + standardDeviation + " " + variance,10,null); } else { meanCount++; newMean += heightCheck [i]; } } newMean = newMean / meanCount; for (int i = 0; i < heightCheck.Count(); i++) { if (variance > 200 && Mathf.Abs(heightCheck [i]) > standardDeviation + meanAbs) { vertices [triangles [v + i]] = new Vector3(vertices [triangles [v + i]].x, newMean, vertices [triangles [v + i]].z); } } } } terrainMesh.vertices = vertices; terrainMesh.normals = normals; terrainMesh.uv = uv; terrainMesh.triangles = triangles; goMesh = terrainMesh; return(goMesh); }