Exemplo n.º 1
0
    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;
    }
Exemplo n.º 2
0
 public void RemoveTrees(LowPolyTerrainData terrainData)
 {
     foreach (var position in Positions)
     {
         terrainData.GetTile(position.x, position.y).HasTree = false;
     }
 }
Exemplo n.º 3
0
    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);
    }
Exemplo n.º 4
0
 public void RemoveTrees(LowPolyTerrainData terrainData)
 {
     foreach (var position in Positions)
     {
         terrainData.GetTile(position.x, position.y).HasTree = false;
     }
 }
Exemplo n.º 5
0
    void CreateTrees()
    {
        for (var y = 0; y < TerrainData.Height; y++)
        {
            for (var x = 0; x < TerrainData.Width; x++)
            {
                var tile = TerrainData.GetTile(x, y);

                if (tile.HasTree)
                {
                    AddTree(tile);
                }
            }
        }
    }
Exemplo n.º 6
0
    private void RenderTileTriangle(int x, int y)
    {
        var tile = terrainData.GetTile(x, y);

        var vi = verticies.Count;

        verticies.Add(tile.Corner1);
        verticies.Add(tile.Corner2);
        verticies.Add(tile.Corner3);

        for (var i = vi; i < vi + 3; i++)
        {
            triangles.Add(i);
        }
    }
Exemplo n.º 7
0
 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;
 }
Exemplo n.º 8
0
 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;
 }
Exemplo n.º 9
0
    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;
    }
Exemplo n.º 10
0
    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;
    }
Exemplo n.º 11
0
    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);
    }
Exemplo n.º 12
0
    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);
    }