public static Sprite ToSprite(Heightmap map)
        {
            var texture = ToTexture2D(map);
            var rect    = new Rect(0, 0, texture.width, texture.height);

            return(Sprite.Create(texture, rect, Vector2.one * 0.5f));
        }
Beispiel #2
0
 public void Substract(Heightmap other)
 {
     for (int i = 0; i < Values.Length; i++)
     {
         Values[i] -= other.Values[i];
     }
 }
Beispiel #3
0
 public void Add(Heightmap other)
 {
     for (int i = 0; i < Values.Length; i++)
     {
         Values[i] += other.Values[i];
     }
 }
Beispiel #4
0
        private void SmoothAt(Heightmap smoothedMap, int x, int y, int weighting)
        {
            float sum   = 0;
            float count = 0;

            for (int ySurroundings = y - 1; ySurroundings <= y + 1; ySurroundings++)
            {
                for (int xSurroundings = x - 1; xSurroundings <= x + 1; xSurroundings++)
                {
                    if (!IsWithinBounds(xSurroundings, ySurroundings))
                    {
                        continue;
                    }

                    sum += GetAt(xSurroundings, ySurroundings);
                    count++;
                }
            }

            sum   += (weighting - 1) * GetAt(x, y);
            count += weighting - 1;

            float smoothedValue = sum / count;

            smoothedMap.SetAt(x, y, smoothedValue);
        }
Beispiel #5
0
        public object Clone()
        {
            var clone = new Heightmap(Width, Height)
            {
                Values = Values.Clone() as float[]
            };

            return(clone);
        }
        public static Mesh ToMesh(Heightmap heightmap)
        {
            var mesh = new Mesh
            {
                vertices  = GenerateVertices(heightmap),
                triangles = GenerateTriangles(heightmap),
                uv        = GenerateUVs(heightmap)
            };

            mesh.RecalculateNormals();

            return(mesh);
        }
Beispiel #7
0
        public void Smooth(int weighting = 1)
        {
            Heightmap smoothedMap = new Heightmap(Width, Height);

            for (int y = 0; y < Height; y++)
            {
                for (int x = 0; x < Width; x++)
                {
                    SmoothAt(smoothedMap, x, y, weighting);
                }
            }

            Values = smoothedMap.Values;
        }
        private static Vector2[] GenerateUVs(Heightmap heightmap)
        {
            var uvs = new Vector2[heightmap.Values.Length];

            int i = 0;

            for (int y = 0; y < heightmap.Height; y++)
            {
                for (int x = 0; x < heightmap.Width; x++)
                {
                    var uv = new Vector2((float)x / heightmap.Width, (float)y / heightmap.Width);
                    uvs[i++] = uv;
                }
            }

            return(uvs);
        }
        public static Texture2D ToTexture2D(Heightmap heightmap)
        {
            var texture = new Texture2D(heightmap.Width, heightmap.Height, TextureFormat.ARGB32, false);

            for (int y = 0; y < heightmap.Height; y++)
            {
                for (int x = 0; x < heightmap.Width; x++)
                {
                    float value = heightmap.GetAt(x, y);
                    texture.SetPixel(x, heightmap.Height - y - 1, new Color(value, value, value));
                }
            }

            texture.filterMode = FilterMode.Point;
            texture.Apply();

            return(texture);
        }
        private static Vector3[] GenerateVertices(Heightmap heightmap)
        {
            var vertices = new Vector3[heightmap.Values.Length];

            int i = 0;

            for (int y = 0; y < heightmap.Height; y++)
            {
                for (int x = 0; x < heightmap.Width; x++)
                {
                    var vertice = new Vector3(x * DistanceBetweenVertices,
                                              heightmap.GetAt(x, y) * MaxHeight,
                                              y * DistanceBetweenVertices);

                    vertices[i++] = vertice;
                }
            }

            return(vertices);
        }
        private static int[] GenerateTriangles(Heightmap heightmap)
        {
            var triangles = new int[(heightmap.Width - 1) * (heightmap.Height - 1) * 2 * 3];

            int i = 0;

            for (int y = 0; y < heightmap.Height - 1; y++)
            {
                for (int x = 0; x < heightmap.Width - 1; x++)
                {
                    triangles[i++] = x + y * heightmap.Width;
                    triangles[i++] = x + y * heightmap.Width + heightmap.Width + 1;
                    triangles[i++] = x + y * heightmap.Width + 1;

                    triangles[i++] = x + y * heightmap.Width;
                    triangles[i++] = x + y * heightmap.Width + heightmap.Width;
                    triangles[i++] = x + y * heightmap.Width + heightmap.Width + 1;
                }
            }

            return(triangles);
        }
        public static void ToFile(Heightmap heightmap, string path)
        {
            var texture = ToTexture2D(heightmap);

            File.WriteAllBytes(path, texture.EncodeToPNG());
        }