public static LowPolyTerrainData GetRandomMap() { var result = new LowPolyTerrainData(111, 111); const float Depth = 30; for (var y = 0; y < result.Height; y++) { for (var x = 0; x < result.Width; x++) { var tile = result.GetTile(x, y); var height1 = CalcHeight(result, tile.Corner1, Depth); var height2 = CalcHeight(result, tile.Corner2, Depth); var height3 = CalcHeight(result, tile.Corner3, Depth); tile.SetCorner(height1, 0); tile.SetCorner(height2, 1); tile.SetCorner(height3, 2); var anyBelow = height1 < 4 || height2 < 4 || height3 < 4; var allAbove = height1 > 14 && height2 > 14 && height3 > 14; if (allAbove) { tile.HasTree = CalcHasTree(result, tile.Corner1); tile.TreeScale = CalcTreeScale(result, tile.Corner2); tile.TreeRotation = CalcTreeRotation(result, tile.Corner3); } result.SetTileType(x, y, anyBelow ? "sand" : "grass"); } } return(result); }
static Vector2 CalcOffsetPos(LowPolyTerrainData terrainData, Vector3 position) { var xpos = LowPolyTerrainTile.TriHalfWidth + position.x; var ypos = LowPolyTerrainTile.TriHeight + position.z; return(new Vector2(xpos, ypos)); }
public void RemoveTrees(LowPolyTerrainData terrainData) { foreach (var position in Positions) { terrainData.GetTile(position.x, position.y).HasTree = false; } }
public static LowPolyTerrainData GetRandomMap() { var result = new LowPolyTerrainData(111, 111); const float Depth = 30; for (var y = 0; y < result.Height; y++) { for (var x = 0; x < result.Width; x++) { var tile = result.GetTile(x, y); var height1 = CalcHeight(result, tile.Corner1, Depth); var height2 = CalcHeight(result, tile.Corner2, Depth); var height3 = CalcHeight(result, tile.Corner3, Depth); tile.SetCorner(height1, 0); tile.SetCorner(height2, 1); tile.SetCorner(height3, 2); var anyBelow = height1 < 4 || height2 < 4 || height3 < 4; var allAbove = height1 > 14 && height2 > 14 && height3 > 14; if (allAbove) { tile.HasTree = CalcHasTree(result, tile.Corner1); tile.TreeScale = CalcTreeScale(result, tile.Corner2); tile.TreeRotation = CalcTreeRotation(result, tile.Corner3); } result.SetTileType(x, y, anyBelow ? "sand" : "grass"); } } return result; }
static float CalcHeight(LowPolyTerrainData terrainData, Vector3 position, float depth) { var pos = CalcOffsetPos(terrainData, position); var subheight = Mathf.PerlinNoise(pos.x / 512.0f, pos.y / 512.0f) * depth * 5.0f - depth * 2.5f; var height = Mathf.PerlinNoise(pos.x / 128.0f, pos.y / 128.0f) * depth; return(Mathf.Round(subheight + height * 0.5f) * 2.0f - depth * 0.4f); }
public void Init(LowPolyTerrainData terrainData) { TerrainData = terrainData; foreach (var pair in Materials) { materialMap[pair.Name] = pair.Material; } CreateMesh(); }
public float GetLowestPoint(LowPolyTerrainData terrainData) { var lowest = float.MaxValue; foreach (var position in Positions) { var tile = terrainData.GetTile(position.x, position.y); if (tile.Corner1.y < lowest) { lowest = tile.Corner1.y; } if (tile.Corner2.y < lowest) { lowest = tile.Corner2.y; } if (tile.Corner3.y < lowest) { lowest = tile.Corner3.y; } } return lowest; }
public float GetHighestPoint(LowPolyTerrainData terrainData) { var highest = float.MinValue; foreach (var position in Positions) { var tile = terrainData.GetTile(position.x, position.y); if (tile.Corner1.y > highest) { highest = tile.Corner1.y; } if (tile.Corner2.y > highest) { highest = tile.Corner2.y; } if (tile.Corner3.y > highest) { highest = tile.Corner3.y; } } return highest; }
public void CreateMesh(LowPolyTerrainData terrainData, string tileType, Material material) { var meshFilter = gameObject.AddComponent <MeshFilter>(); var meshCollider = gameObject.AddComponent <MeshCollider>(); var meshRenderer = gameObject.AddComponent <MeshRenderer>(); meshRenderer.material = material; verticies = new List <Vector3>(); uvs = new List <Vector2>(); triangles = new List <int>(); this.terrainData = terrainData; var width = terrainData.Width - 1; var height = terrainData.Height - 1; for (var y = 0; y < height; y++) { for (var x = 0; x < width; x++) { var tile = terrainData.GetTile(x, y); if (tile.Type == tileType) { RenderTileTriangle(x, y); } } } var mesh = new Mesh(); meshFilter.mesh = mesh; mesh.vertices = verticies.ToArray(); mesh.uv = uvs.ToArray(); mesh.triangles = triangles.ToArray(); mesh.Optimize(); mesh.RecalculateNormals(); meshCollider.sharedMesh = mesh; }
public void CreateMesh(LowPolyTerrainData terrainData, string tileType, Material material) { var meshFilter = gameObject.AddComponent<MeshFilter>(); var meshCollider = gameObject.AddComponent<MeshCollider>(); var meshRenderer = gameObject.AddComponent<MeshRenderer>(); meshRenderer.material = material; verticies = new List<Vector3>(); uvs = new List<Vector2>(); triangles = new List<int>(); this.terrainData = terrainData; var width = terrainData.Width - 1; var height = terrainData.Height - 1; for (var y = 0; y < height; y++) { for (var x = 0; x < width; x++) { var tile = terrainData.GetTile(x, y); if (tile.Type == tileType) { RenderTileTriangle(x, y); } } } var mesh = new Mesh(); meshFilter.mesh = mesh; mesh.vertices = verticies.ToArray(); mesh.uv = uvs.ToArray(); mesh.triangles = triangles.ToArray(); mesh.Optimize(); mesh.RecalculateNormals(); meshCollider.sharedMesh = mesh; }
public float GetHighestPoint(LowPolyTerrainData terrainData) { var highest = float.MinValue; foreach (var position in Positions) { var tile = terrainData.GetTile(position.x, position.y); if (tile.Corner1.y > highest) { highest = tile.Corner1.y; } if (tile.Corner2.y > highest) { highest = tile.Corner2.y; } if (tile.Corner3.y > highest) { highest = tile.Corner3.y; } } return(highest); }
public float GetLowestPoint(LowPolyTerrainData terrainData) { var lowest = float.MaxValue; foreach (var position in Positions) { var tile = terrainData.GetTile(position.x, position.y); if (tile.Corner1.y < lowest) { lowest = tile.Corner1.y; } if (tile.Corner2.y < lowest) { lowest = tile.Corner2.y; } if (tile.Corner3.y < lowest) { lowest = tile.Corner3.y; } } return(lowest); }
// Use this for initialization void Start() { Map = new GameMap(); Map.TerrainData = LowPolyTerrainData.GetRandomMap(); var building = new Building(); building.Positions.Add(new Vector2Int(6, 1)); building.Positions.Add(new Vector2Int(7, 1)); building.Positions.Add(new Vector2Int(8, 1)); building.Positions.Add(new Vector2Int(9, 1)); building.Positions.Add(new Vector2Int(10, 1)); building.Positions.Add(new Vector2Int(11, 1)); building.Positions.Add(new Vector2Int(12, 1)); building.Positions.Add(new Vector2Int(13, 1)); building.Positions.Add(new Vector2Int(14, 1)); building.Positions.Add(new Vector2Int(11, 2)); building.Positions.Add(new Vector2Int(12, 2)); building.Positions.Add(new Vector2Int(12, 3)); building.Positions.Add(new Vector2Int(13, 3)); building.Positions.Add(new Vector2Int(13, 4)); building.Positions.Add(new Vector2Int(14, 4)); building.Positions.Add(new Vector2Int(14, 5)); building.Positions.Add(new Vector2Int(15, 5)); building.RemoveTrees(Map.TerrainData); //Map.Buildings.Add(building); building = new Building(); //building.Positions.Add(new Vector2Int(6, 2)); building.Positions.Add(new Vector2Int(7, 2)); building.Positions.Add(new Vector2Int(8, 2)); building.Positions.Add(new Vector2Int(9, 2)); building.Positions.Add(new Vector2Int(10, 2)); building.Positions.Add(new Vector2Int(5, 3)); building.Positions.Add(new Vector2Int(6, 3)); building.Positions.Add(new Vector2Int(7, 3)); building.Positions.Add(new Vector2Int(8, 3)); building.Positions.Add(new Vector2Int(9, 3)); building.Positions.Add(new Vector2Int(10, 3)); building.Positions.Add(new Vector2Int(11, 3)); //building.Positions.Add(new Vector2Int(5, 4)); building.Positions.Add(new Vector2Int(6, 4)); building.Positions.Add(new Vector2Int(7, 4)); building.Positions.Add(new Vector2Int(8, 4)); building.Positions.Add(new Vector2Int(9, 4)); building.Positions.Add(new Vector2Int(10, 4)); building.Positions.Add(new Vector2Int(11, 4)); building.Positions.Add(new Vector2Int(10, 6)); building.Positions.Add(new Vector2Int(11, 6)); building.Positions.Add(new Vector2Int(12, 6)); building.Type = "house"; building.RemoveTrees(Map.TerrainData); //Map.Buildings.Add(building); building = new Building(); var floor = new BuildingFloor(); floor.Positions.Add(new Vector2Int(13, 2)); floor.Positions.Add(new Vector2Int(14, 2)); floor.Positions.Add(new Vector2Int(15, 2)); floor.Positions.Add(new Vector2Int(16, 2)); floor.Positions.Add(new Vector2Int(14, 3)); floor.Positions.Add(new Vector2Int(15, 3)); floor.Positions.Add(new Vector2Int(16, 3)); floor.WallEdges.Add(new WallEdge(new Vector2Int(13, 2), 2)); floor.WallEdges.Add(new WallEdge(new Vector2Int(13, 2), 0)); floor.BaseHeight = 20.0f; floor.FloorHeight = 0.2f; floor.Height = 10.0f; floor.Map = Map; building.Floors.Add(floor); floor = new BuildingFloor(); floor.Positions.Add(new Vector2Int(14, 2)); floor.Positions.Add(new Vector2Int(15, 2)); floor.Positions.Add(new Vector2Int(16, 2)); floor.Positions.Add(new Vector2Int(14, 3)); floor.Positions.Add(new Vector2Int(15, 3)); floor.Positions.Add(new Vector2Int(16, 3)); floor.WallEdges.Add(new WallEdge(new Vector2Int(14, 2), 2)); floor.WallEdges.Add(new WallEdge(new Vector2Int(15, 2), 2)); floor.BaseHeight = 30.0f; floor.FloorHeight = 0.2f; floor.Height = 10.0f; floor.Map = Map; building.Floors.Add(floor); floor = new BuildingFloor(); floor.Positions.Add(new Vector2Int(15, 4)); floor.Positions.Add(new Vector2Int(16, 4)); floor.Positions.Add(new Vector2Int(17, 4)); floor.Positions.Add(new Vector2Int(17, 3)); floor.Positions.Add(new Vector2Int(18, 3)); floor.WallEdges.Add(new WallEdge(new Vector2Int(15, 4), 0)); floor.WallEdges.Add(new WallEdge(new Vector2Int(16, 4), 0)); floor.WallEdges.Add(new WallEdge(new Vector2Int(17, 4), 1, 2)); floor.BaseHeight = 22.0f; floor.FloorHeight = 0.2f; floor.Height = 10.0f; floor.Map = Map; building.Floors.Add(floor); building.Type = "generic"; building.RemoveTrees(Map.TerrainData); Map.Buildings.Add(building); Init(Map); var geometry = new InputGeometry(); //it is necessary to put a border around all the points in order to get triangulation to work correctly when holes are used var border = new List <Point>(); border.Add(new Point(10, 6)); border.Add(new Point(10, -6)); border.Add(new Point(-10, -6)); border.Add(new Point(-10, 6)); geometry.AddRing(border); border = new List <Point>(); border.Add(new Point(7, 4)); border.Add(new Point(7, 2)); border.Add(new Point(5, 2)); border.Add(new Point(5, 4)); geometry.AddRingAsHole(border); border = new List <Point>(); border.Add(new Point(3, 2)); border.Add(new Point(3, -6)); border.Add(new Point(-3, -6)); border.Add(new Point(-3, 2)); geometry.AddRingAsHole(border); var meshRepresentation = new TriangleNet.Mesh(); meshRepresentation.Triangulate(geometry); var triangleIndex = 0; var vertices = new List <Vector3>(meshRepresentation.triangles.Count * 3); var triangleIndices = new List <int>(meshRepresentation.triangles.Count * 3); foreach (var pair in meshRepresentation.triangles) { var triangle = pair.Value; var vertex0 = triangle.GetVertex(0); var vertex1 = triangle.GetVertex(1); var vertex2 = triangle.GetVertex(2); var p0 = new Vector3(vertex0.x, vertex0.y, 0); var p1 = new Vector3(vertex1.x, vertex1.y, 0); var p2 = new Vector3(vertex2.x, vertex2.y, 0); vertices.Add(p0); vertices.Add(p1); vertices.Add(p2); triangleIndices.Add(triangleIndex + 2); triangleIndices.Add(triangleIndex + 1); triangleIndices.Add(triangleIndex); triangleIndex += 3; } var mesh = new Mesh(); mesh.vertices = vertices.ToArray(); mesh.triangles = triangleIndices.ToArray(); mesh.RecalculateNormals(); var newObj = new GameObject(); newObj.name = "Test"; newObj.transform.parent = transform; newObj.AddComponent <MeshFilter>(); newObj.AddComponent <MeshRenderer>(); newObj.GetComponent <MeshFilter>().mesh = mesh; /* * var path = new List<ClipperLib.IntPoint>(); * path.Add(new ClipperLib.IntPoint(-3000, 0)); * path.Add(new ClipperLib.IntPoint(1000, 0)); * path.Add(new ClipperLib.IntPoint(6000, 3000)); * path.Add(new ClipperLib.IntPoint(6000, -3000)); * path.Add(new ClipperLib.IntPoint(1000, 0)); * //path.Add(new ClipperLib.IntPoint(5000, 0)); * //path.Add(new ClipperLib.IntPoint(1000, -3500)); * //path.Add(new ClipperLib.IntPoint(-3000, 0)); * * var co = new ClipperLib.ClipperOffset(); * co.AddPath(path, ClipperLib.JoinType.jtSquare, ClipperLib.EndType.etClosedLine); * * var result = new List<List<ClipperLib.IntPoint>>(); * co.Execute(ref result, 500); * foreach (var intpoint in path) * { * var point = new Vector3((float)intpoint.X / 1000.0f, 0, (float)intpoint.Y / 1000.0f); * Debug.DrawLine(point, point + Vector3.up * 3, Color.cyan, 100); * } * * foreach (var intpath in result) * { * foreach (var intpoint in intpath) * { * var point = new Vector3((float)intpoint.X / 1000.0f, 0, (float)intpoint.Y / 1000.0f); * Debug.DrawLine(point, point + Vector3.up * 3, Color.red, 100); * } * } */ }
static bool CalcHasTree(LowPolyTerrainData terrainData, Vector3 position) { var pos = CalcOffsetPos(terrainData, position); return(Mathf.PerlinNoise(pos.x, pos.y) > 0.5f); }
static float CalcTreeRotation(LowPolyTerrainData terrainData, Vector3 position) { var pos = CalcOffsetPos(terrainData, position); return(Mathf.PerlinNoise(pos.x, pos.y) * 360.0f); }
static float CalcHeight(LowPolyTerrainData terrainData, Vector3 position, float depth) { var pos = CalcOffsetPos(terrainData, position); var subheight = Mathf.PerlinNoise(pos.x / 512.0f, pos.y / 512.0f) * depth * 5.0f - depth * 2.5f; var height = Mathf.PerlinNoise(pos.x / 128.0f, pos.y / 128.0f) * depth; return Mathf.Round(subheight + height * 0.5f) * 2.0f - depth * 0.4f; }
static Vector2 CalcOffsetPos(LowPolyTerrainData terrainData, Vector3 position) { var xpos = LowPolyTerrainTile.TriHalfWidth + position.x; var ypos = LowPolyTerrainTile.TriHeight + position.z; return new Vector2(xpos, ypos); }
public void Init(LowPolyTerrainData terrainData) { TerrainData = terrainData; CreateTrees(); }
static float CalcTreeRotation(LowPolyTerrainData terrainData, Vector3 position) { var pos = CalcOffsetPos(terrainData, position); return Mathf.PerlinNoise(pos.x, pos.y) * 360.0f; }
static bool CalcHasTree(LowPolyTerrainData terrainData, Vector3 position) { var pos = CalcOffsetPos(terrainData, position); return Mathf.PerlinNoise(pos.x, pos.y) > 0.5f; }