Ejemplo n.º 1
0
 public float GetHeight(int x, int z)
 {
     if (x < 0 || x >= HeightMapData.GetLength(0))
     {
         return(0);
     }
     else if (z < 0 || z >= HeightMapData.GetLength(1))
     {
         return(0);
     }
     else
     {
         return(HeightMapData[x, z]);
     }
 }
Ejemplo n.º 2
0
        public float SampleHeight(Vector3 position)
        {
            float x = ((position.x - OffsetX) * (((float)HeightMapData.GetLength(0) - 1f) / (float)Width));
            float z = ((position.z - OffsetZ) * (((float)HeightMapData.GetLength(1) - 1f) / (float)Length));

            var hx0z0 = GetHeight(Mathf.FloorToInt(x), Mathf.FloorToInt(z));
            var hx1z0 = GetHeight(Mathf.CeilToInt(x), Mathf.FloorToInt(z));
            var hx0z1 = GetHeight(Mathf.FloorToInt(x), Mathf.CeilToInt(z));
            var hx1z1 = GetHeight(Mathf.CeilToInt(x), Mathf.CeilToInt(z));

            if (hx0z0 == hx1z0 && hx1z0 == hx0z1 && hx0z1 == hx1z1)
            {
                return(((hx0z0) * Height) + OffsetY);
            }

            var u0v0 = hx0z0 * (Mathf.CeilToInt(x) - x) * (Mathf.CeilToInt(z) - z);
            var u1v0 = hx1z0 * (x - Mathf.FloorToInt(x)) * (Mathf.CeilToInt(z) - z);
            var u0v1 = hx0z1 * (Mathf.CeilToInt(x) - x) * (z - Mathf.FloorToInt(z));
            var u1v1 = hx1z1 * (x - Mathf.FloorToInt(x)) * (z - Mathf.FloorToInt(z));

            return(((u0v0 + u1v0 + u0v1 + u1v1) * Height) + OffsetY);
        }