/// <summary> /// Crea la malla de un terreno en base a un Heightmap /// </summary> /// <param name="heightmapPath">Imagen de Heightmap</param> /// <param name="scaleXZ">Escala para los ejes X y Z</param> /// <param name="scaleY">Escala para el eje Y</param> /// <param name="center">Centro de la malla del terreno</param> public void loadHeightmap(string heightmapPath, float scaleXZ, float scaleY, TGCVector3 center) { var d3dDevice = D3DDevice.Instance.Device; this.center = center; ScaleXZ = scaleXZ; ScaleY = scaleY; //Dispose de VertexBuffer anterior, si habia if (vbTerrain != null && !vbTerrain.Disposed) { vbTerrain.Dispose(); } //cargar heightmap HeightmapData = loadHeightMap(d3dDevice, heightmapPath); //Crear vertexBuffer TotalVertices = 2 * 3 * (HeightmapData.GetLength(0) - 1) * (HeightmapData.GetLength(1) - 1); vbTerrain = new VertexBuffer(typeof(CustomVertex.PositionColoredTextured), TotalVertices, d3dDevice, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionColoredTextured.Format, Pool.Default); float width = HeightmapData.GetLength(0); float length = HeightmapData.GetLength(1); traslation.X = center.X - width / 2; traslation.Y = center.Y; traslation.Z = center.Z - length / 2; //Cargar vertices loadVertices(); }
public void SetHeightmapData(float[,] heightmapData) { if (heightmapData.GetLength(0) == HeightmapData.GetLength(0) && HeightmapData.GetLength(1) == heightmapData.GetLength(1)) { HeightmapData = heightmapData; } }
private void ParseFile() { try { JsonBaseImportData data = JsonConvert.DeserializeObject <JsonBaseImportData>(File.ReadAllText(PathFile)); if (data != null) { Console.WriteLine("[INFO] GeneratorType: " + data.GeneratorType); switch (data.GeneratorType) { case GeneratorType.Terrain: string directoryName = System.IO.Path.GetDirectoryName(PathFile); WorldTerrainData worldTerrainData = data as WorldTerrainData; worldTerrainData.DirectoryPath = directoryName; mGenerator = new TerrainGenerator(worldTerrainData); break; case GeneratorType.Heightmap: HeightmapData heightmapData = data as HeightmapData; mGenerator = new HeightmapGenerator(heightmapData, mSchematic); break; case GeneratorType.Shader: ShaderData shaderData = data as ShaderData; mGenerator = new ShaderGenerator(shaderData, mSchematic); break; } } } catch (Exception e) { Console.WriteLine("[ERROR] Failed to parse the JSON file: " + e.Message); } }
/// <summary> /// Crea los vertices /// </summary> private void loadVertices() { var dataIdx = 0; float width = HeightmapData.GetLength(0); float length = HeightmapData.GetLength(1); var color = Color.White.ToArgb(); vertices = new CustomVertex.PositionColoredTextured[TotalVertices]; maxIntensity = 0; minIntensity = -1; for (var i = 0; i < width - 1; i++) { for (var j = 0; j < length - 1; j++) { if (HeightmapData[i, j] > maxIntensity) { maxIntensity = HeightmapData[i, j]; } if (minIntensity == -1 || HeightmapData[i, j] < minIntensity) { minIntensity = HeightmapData[i, j]; } //Vertices var v1 = new TGCVector3(i, HeightmapData[i, j], j); var v2 = new TGCVector3(i, HeightmapData[i, j + 1], j + 1); var v3 = new TGCVector3(i + 1, HeightmapData[i + 1, j], j); var v4 = new TGCVector3(i + 1, HeightmapData[i + 1, j + 1], j + 1); //Coordendas de textura var t1 = new TGCVector2(i / width, j / length); var t2 = new TGCVector2(i / width, (j + 1) / length); var t3 = new TGCVector2((i + 1) / width, j / length); var t4 = new TGCVector2((i + 1) / width, (j + 1) / length); //Cargar triangulo 1 vertices[dataIdx] = new CustomVertex.PositionColoredTextured(v1, color, t1.X, t1.Y); vertices[dataIdx + 1] = new CustomVertex.PositionColoredTextured(v2, color, t2.X, t2.Y); vertices[dataIdx + 2] = new CustomVertex.PositionColoredTextured(v4, color, t4.X, t4.Y); //Cargar triangulo 2 vertices[dataIdx + 3] = new CustomVertex.PositionColoredTextured(v1, color, t1.X, t1.Y); vertices[dataIdx + 4] = new CustomVertex.PositionColoredTextured(v4, color, t4.X, t4.Y); vertices[dataIdx + 5] = new CustomVertex.PositionColoredTextured(v3, color, t3.X, t3.Y); dataIdx += 6; } vbTerrain.SetData(vertices, 0, LockFlags.None); aabb.setExtremes(new TGCVector3(0, minIntensity, 0), new TGCVector3(HeightmapData.GetLength(0), maxIntensity, HeightmapData.GetLength(1))); } }
public float Height(float x, float z) { var width = HeightmapData.GetLength(0); var length = HeightmapData.GetLength(1); var pos_i = x / m_scaleXZ + width / 2.0f; var pos_j = z / m_scaleXZ + length / 2.0f; var pi = (int)pos_i; var fracc_i = pos_i - pi; var pj = (int)pos_j; var fracc_j = pos_j - pj; if (pi < 0) { pi = 0; } else if (pi >= width) { pi = width - 1; } if (pj < 0) { pj = 0; } else if (pj >= length) { pj = length - 1; } var pi1 = pi + 1; var pj1 = pj + 1; if (pi1 >= width) { pi1 = width - 1; } if (pj1 >= length) { pj1 = length - 1; } // 2x2 percent closest filtering usual: var H0 = HeightmapData[pi, pj]; var H1 = HeightmapData[pi1, pj]; var H2 = HeightmapData[pi, pj1]; var H3 = HeightmapData[pi1, pj1]; var H = (H0 * (1 - fracc_i) + H1 * fracc_i) * (1 - fracc_j) + (H2 * (1 - fracc_i) + H3 * fracc_i) * fracc_j; return(H * m_scaleY); }
private bool XZToHeightmapCoords(float x, float z, out TGCVector2 coords) { float i = x / ScaleXZ - Traslation.X; float j = z / ScaleXZ - Traslation.Z; coords = new TGCVector2(i, j); if (coords.X >= HeightmapData.GetLength(0) || coords.Y >= HeightmapData.GetLength(1) || coords.Y < 0 || coords.X < 0) { return(false); } return(true); }
public void SaveAsCurrent() { //usage: GameControl.control.SaveAsCurrent() BinaryFormatter bf = new BinaryFormatter(); FileStream file = File.Create(Application.persistentDataPath + "/currentHeightsInfo.dat"); HeightmapData data = new HeightmapData(); data.heightmap = currentHeightmap; data.zoomLevel = currentZoom; data.centerCoords = currentCenter; bf.Serialize(file, data); file.Close(); Debug.Log("file created: " + Application.persistentDataPath + "/currentHeightsInfo.dat"); }
/// <summary> /// Transforma coordenadas del mundo en coordenadas del heightmap. /// </summary> public bool xzToHeightmapCoords(float x, float z, out Vector2 coords) { float i, j; i = x / ScaleXZ - traslation.X; j = z / ScaleXZ - traslation.Z; coords = new Vector2(i, j); if (coords.X >= HeightmapData.GetLength(0) || coords.Y >= HeightmapData.GetLength(1) || coords.Y < 0 || coords.X < 0) { return(false); } return(true); }
public bool LoadCurrent() { if (File.Exists(Application.persistentDataPath + "/currentHeightsInfo.dat")) { BinaryFormatter bf = new BinaryFormatter(); FileStream file = File.Open(Application.persistentDataPath + "/currentHeightsInfo.dat", FileMode.Open); HeightmapData data = (HeightmapData)bf.Deserialize(file); file.Close(); currentHeightmap = data.heightmap; currentZoom = data.zoomLevel; currentCenter = data.centerCoords; return(true); } else { Debug.Log("ALERT: no current heightmap found"); } return(false); }
/// <summary> /// Actualiza los vertices segun los valores de HeightmapData /// </summary> public void updateVertices() { minIntensity = -1; maxIntensity = 0; for (int i = 0; i < vertices.Length; i++) { CustomVertex.PositionColoredTextured v = vertices[i]; float intensity = heightmapData[(int)vertices[i].X, (int)vertices[i].Z]; vertices[i].Y = intensity; if (intensity > maxIntensity) { maxIntensity = intensity; } if (minIntensity == -1 || intensity < minIntensity) { minIntensity = intensity; } } vbTerrain.SetData(vertices, 0, LockFlags.None); aabb.setExtremes(new Vector3(0, minIntensity, 0), new Vector3(HeightmapData.GetLength(0), maxIntensity, HeightmapData.GetLength(1))); }
/// <summary> /// Retorna la intensidad del heightmap en ese punto utilizando interpolacion bilineal. /// </summary> /// <param name="u"></param> /// <param name="v"></param> /// <param name="i"></param> /// <returns></returns> public bool interpoledIntensity(float u, float v, out float i) { i = 0; float maxX = HeightmapData.GetLength(0); float maxZ = HeightmapData.GetLength(1); if (u >= maxX || v >= maxZ || v < 0 || u < 0) { return(false); } int x1, x2, z1, z2; float s, t; x1 = (int)FastMath.Floor(u); x2 = x1 + 1; s = u - x1; z1 = (int)FastMath.Floor(v); z2 = z1 + 1; t = v - z1; if (z2 >= maxZ) { z2--; } if (x2 >= maxX) { x2--; } var i1 = HeightmapData[x1, z1] + s * (HeightmapData[x2, z1] - HeightmapData[x1, z1]); var i2 = HeightmapData[x1, z2] + s * (HeightmapData[x2, z2] - HeightmapData[x1, z2]); i = i1 + t * (i2 - i1); return(true); }
/// <summary> /// Actualiza los vertices segun los valores de HeightmapData /// </summary> public void updateVertices() { minIntensity = -1; maxIntensity = 0; for (var i = 0; i < vertices.Length; i++) { var v = vertices[i]; var intensity = HeightmapData[(int)vertices[i].X, (int)vertices[i].Z]; vertices[i].Y = intensity; if (intensity > maxIntensity) { maxIntensity = intensity; } if (minIntensity == -1 || intensity < minIntensity) { minIntensity = intensity; } } vbTerrain.SetData(vertices, 0, LockFlags.None); aabb.setExtremes(new TGCVector3(0, minIntensity, 0), new TGCVector3(HeightmapData.GetLength(0), maxIntensity, HeightmapData.GetLength(1))); }
public void LoadHeightmap(string heightmapPath, float scaleXZ, float scaleY, TGCVector3 center) { Center = center; ScaleXZ = scaleXZ; ScaleY = scaleY; if (VertexTerrain != null && !VertexTerrain.Disposed) { VertexTerrain.Dispose(); } HeightmapData = LoadHeightMap(heightmapPath); Width = HeightmapData.GetLength(0); Length = HeightmapData.GetLength(1); var totalvertices = 2 * 3 * (Width - 1) * (Length - 1); VertexTotal = (int)totalvertices; VertexTerrain = new VertexBuffer(typeof(CustomVertex.PositionTextured), VertexTotal, D3DDevice.Instance.Device, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionTextured.Format, Pool.Default); LoadVertices(); }
/// <summary> /// Crea la malla de un terreno en base a un Heightmap /// </summary> /// <param name="heightmapPath">Imagen de Heightmap</param> /// <param name="scaleXZ">Escala para los ejes X y Z</param> /// <param name="scaleY">Escala para el eje Y</param> /// <param name="center">Centro de la malla del terreno</param> public void loadHeightmap(string heightmapPath, float scaleXZ, float scaleY, TGCVector3 center) { Center = center; //Dispose de VertexBuffer anterior, si habia if (vbTerrain != null && !vbTerrain.Disposed) { vbTerrain.Dispose(); } //cargar heightmap HeightmapData = loadHeightMap(D3DDevice.Instance.Device, heightmapPath); float width = HeightmapData.GetLength(0); float length = HeightmapData.GetLength(1); //Crear vertexBuffer totalVertices = 2 * 3 * (HeightmapData.GetLength(0) - 1) * (HeightmapData.GetLength(1) - 1); vbTerrain = new VertexBuffer(typeof(CustomVertex.PositionTextured), totalVertices, D3DDevice.Instance.Device, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionTextured.Format, Pool.Default); //Cargar vertices var dataIdx = 0; var data = new CustomVertex.PositionTextured[totalVertices]; center.X = center.X * scaleXZ - width / 2 * scaleXZ; center.Y = center.Y * scaleY; center.Z = center.Z * scaleXZ - length / 2 * scaleXZ; for (var i = 0; i < width - 1; i++) { for (var j = 0; j < length - 1; j++) { //Vertices var v1 = new TGCVector3(center.X + i * scaleXZ, center.Y + HeightmapData[i, j] * scaleY, center.Z + j * scaleXZ); var v2 = new TGCVector3(center.X + i * scaleXZ, center.Y + HeightmapData[i, j + 1] * scaleY, center.Z + (j + 1) * scaleXZ); var v3 = new TGCVector3(center.X + (i + 1) * scaleXZ, center.Y + HeightmapData[i + 1, j] * scaleY, center.Z + j * scaleXZ); var v4 = new TGCVector3(center.X + (i + 1) * scaleXZ, center.Y + HeightmapData[i + 1, j + 1] * scaleY, center.Z + (j + 1) * scaleXZ); //Coordendas de textura var t1 = new TGCVector2(i / width, j / length); var t2 = new TGCVector2(i / width, (j + 1) / length); var t3 = new TGCVector2((i + 1) / width, j / length); var t4 = new TGCVector2((i + 1) / width, (j + 1) / length); //Cargar triangulo 1 data[dataIdx] = new CustomVertex.PositionTextured(v1, t1.X, t1.Y); data[dataIdx + 1] = new CustomVertex.PositionTextured(v2, t2.X, t2.Y); data[dataIdx + 2] = new CustomVertex.PositionTextured(v4, t4.X, t4.Y); //Cargar triangulo 2 data[dataIdx + 3] = new CustomVertex.PositionTextured(v1, t1.X, t1.Y); data[dataIdx + 4] = new CustomVertex.PositionTextured(v4, t4.X, t4.Y); data[dataIdx + 5] = new CustomVertex.PositionTextured(v3, t3.X, t3.Y); dataIdx += 6; } } vbTerrain.SetData(data, 0, LockFlags.None); }
public HeightmapGenerator(HeightmapData heightmapData, Schematic schematic) { mHeightmapData = heightmapData; mSchematic = schematic; }
/// <summary> /// Crea la malla de un terreno en base a un Heightmap /// </summary> /// <param name="heightmap">Imagen de Heightmap</param> /// <param name="scaleXZ">Escala para los ejes X y Z</param> /// <param name="scaleY">Escala para el eje Y</param> /// <param name="center">Centro de la malla del terreno</param> public void LoadHeightmap(GraphicsDevice graphicsDevice, Texture2D heightmap, float scaleXZ, float scaleY, Vector3 center) { Center = center; m_scaleXZ = scaleXZ; m_scaleY = scaleY; float tx_scale = 1; // 50f; //cargar heightmap HeightmapData = LoadHeightMap(heightmap); var width = HeightmapData.GetLength(0); var length = HeightmapData.GetLength(1); float min_h = 256; float max_h = 0; for (var i = 0; i < width; i++) { for (var j = 0; j < length; j++) { //HeightmapData[i, j] = 256 - HeightmapData[i, j]; if (HeightmapData[i, j] > max_h) { max_h = HeightmapData[i, j]; } if (HeightmapData[i, j] < min_h) { min_h = HeightmapData[i, j]; } } } //Cargar vertices var totalVertices = 2 * 3 * (HeightmapData.GetLength(0) - 1) * (HeightmapData.GetLength(1) - 1); var dataIdx = 0; var data = new VertexPositionNormalTexture[totalVertices]; center.X = center.X * scaleXZ - width / 2f * scaleXZ; center.Y = center.Y * scaleY; center.Z = center.Z * scaleXZ - length / 2f * scaleXZ; var N = new Vector3[width, length]; for (var i = 0; i < width - 1; i++) { for (var j = 0; j < length - 1; j++) { var v1 = new Vector3(center.X + i * scaleXZ, center.Y + HeightmapData[i, j] * scaleY, center.Z + j * scaleXZ); var v2 = new Vector3(center.X + i * scaleXZ, center.Y + HeightmapData[i, j + 1] * scaleY, center.Z + (j + 1) * scaleXZ); var v3 = new Vector3(center.X + (i + 1) * scaleXZ, center.Y + HeightmapData[i + 1, j] * scaleY, center.Z + j * scaleXZ); N[i, j] = Vector3.Normalize(Vector3.Cross(v2 - v1, v3 - v1)); } } for (var i = 0; i < width - 1; i++) { for (var j = 0; j < length - 1; j++) { //Vertices var v1 = new Vector3(center.X + i * scaleXZ, center.Y + HeightmapData[i, j] * scaleY, center.Z + j * scaleXZ); var v2 = new Vector3(center.X + i * scaleXZ, center.Y + HeightmapData[i, j + 1] * scaleY, center.Z + (j + 1) * scaleXZ); var v3 = new Vector3(center.X + (i + 1) * scaleXZ, center.Y + HeightmapData[i + 1, j] * scaleY, center.Z + j * scaleXZ); var v4 = new Vector3(center.X + (i + 1) * scaleXZ, center.Y + HeightmapData[i + 1, j + 1] * scaleY, center.Z + (j + 1) * scaleXZ); //Coordendas de textura var t1 = new Vector2(i / (float)width, j / (float)length) * tx_scale; var t2 = new Vector2(i / (float)width, (j + 1) / (float)length) * tx_scale; var t3 = new Vector2((i + 1) / (float)width, j / (float)length) * tx_scale; var t4 = new Vector2((i + 1) / (float)width, (j + 1) / (float)length) * tx_scale; //Cargar triangulo 1 data[dataIdx] = new VertexPositionNormalTexture(v1, N[i, j], t1); data[dataIdx + 1] = new VertexPositionNormalTexture(v2, N[i, j + 1], t2); data[dataIdx + 2] = new VertexPositionNormalTexture(v4, N[i + 1, j + 1], t4); //Cargar triangulo 2 data[dataIdx + 3] = new VertexPositionNormalTexture(v1, N[i, j], t1); data[dataIdx + 4] = new VertexPositionNormalTexture(v4, N[i + 1, j + 1], t4); data[dataIdx + 5] = new VertexPositionNormalTexture(v3, N[i + 1, j], t3); dataIdx += 6; } } //Crear vertexBuffer vbTerrain = new VertexBuffer(graphicsDevice, VertexPositionNormalTexture.VertexDeclaration, totalVertices, BufferUsage.WriteOnly); vbTerrain.SetData(data); }
private void LoadVertices() { Traslation.X = Center.X - Width / 2; Traslation.Y = Center.Y; Traslation.Z = Center.Z - Length / 2; var dataIdx = 0; Vertex = new CustomVertex.PositionTextured[VertexTotal]; for (var i = 0; i < Width - 1; i++) { for (var j = 0; j < Length - 1; j++) { if (HeightmapData[i, j] > MaxIntensity) { MaxIntensity = HeightmapData[i, j]; } if (MinIntensity == -1 || HeightmapData[i, j] < MinIntensity) { MinIntensity = HeightmapData[i, j]; } //Vertices var v1 = new TGCVector3((Traslation.X + i) * ScaleXZ, (Traslation.Y + HeightmapData[i, j]) * ScaleY, (Traslation.Z + j) * ScaleXZ); var v2 = new TGCVector3((Traslation.X + i) * ScaleXZ, (Traslation.Y + HeightmapData[i, j + 1]) * ScaleY, (Traslation.Z + (j + 1)) * ScaleXZ); var v3 = new TGCVector3((Traslation.X + i + 1) * ScaleXZ, (Traslation.Y + HeightmapData[i + 1, j]) * ScaleY, (Traslation.Z + j) * ScaleXZ); var v4 = new TGCVector3((Traslation.X + i + 1) * ScaleXZ, (Traslation.Y + HeightmapData[i + 1, j + 1]) * ScaleY, (Traslation.Z + j + 1) * ScaleXZ); //Coordendas de textura var t1 = new TGCVector2(i / Width, j / Length); var t2 = new TGCVector2(i / Width, (j + 1) / Length); var t3 = new TGCVector2((i + 1) / Width, j / Length); var t4 = new TGCVector2((i + 1) / Width, (j + 1) / Length); //Cargar triangulo 1 Vertex[dataIdx + 0] = new CustomVertex.PositionTextured(v1, t1.X, t1.Y); Vertex[dataIdx + 1] = new CustomVertex.PositionTextured(v2, t2.X, t2.Y); Vertex[dataIdx + 2] = new CustomVertex.PositionTextured(v4, t4.X, t4.Y); //Cargar triangulo 2 Vertex[dataIdx + 3] = new CustomVertex.PositionTextured(v1, t1.X, t1.Y); Vertex[dataIdx + 4] = new CustomVertex.PositionTextured(v4, t4.X, t4.Y); Vertex[dataIdx + 5] = new CustomVertex.PositionTextured(v3, t3.X, t3.Y); dataIdx += 6; } VertexTerrain.SetData(Vertex, 0, LockFlags.None); var size = HeightmapData.GetLength(0) / 2; var pMin = new TGCVector3(size * -ScaleXZ, MinIntensity * ScaleY, size * -ScaleXZ); var pMax = new TGCVector3(size * ScaleXZ, MaxIntensity * ScaleY, size * ScaleXZ); BoundingBox.setExtremes(pMin, pMax); } }