public static Heightmap Create <T>(Int2 size, HeightfieldTypes heightType, Vector2 heightRange, float heightScale, T[] data) { if (data == null) { throw new ArgumentNullException(nameof(data)); } HeightmapUtils.CheckHeightParameters(size, heightType, heightRange, heightScale, true); var length = size.X * size.Y; switch (data) { case float[] floats when floats.Length == length: break; case short[] shorts when shorts.Length == length: break; case byte[] bytes when bytes.Length == length: break; default: throw new ArgumentException($"{ typeof(T[]) } is not supported in { heightType } height type. Or { nameof(data) }.{ nameof(data).Length } doesn't match { nameof(size) }."); } var heightmap = new Heightmap { HeightType = heightType, Size = size, HeightRange = heightRange, HeightScale = heightScale, Floats = data as float[], Shorts = data as short[], Bytes = data as byte[], }; return(heightmap); }
public static bool IsValid([NotNull] this Heightmap heightmap) { if (heightmap == null) { throw new ArgumentNullException(nameof(heightmap)); } bool IsValidHeights() { var length = heightmap.Size.X * heightmap.Size.Y; switch (heightmap.HeightType) { case HeightfieldTypes.Float when heightmap.Floats != null && heightmap.Floats.Length == length: return(true); case HeightfieldTypes.Short when heightmap.Shorts != null && heightmap.Shorts.Length == length: return(true); case HeightfieldTypes.Byte when heightmap.Bytes != null && heightmap.Bytes.Length == length: return(true); } return(false); } return(HeightmapUtils.CheckHeightParameters(heightmap.Size, heightmap.HeightType, heightmap.HeightRange, heightmap.HeightScale, false) && IsValidHeights()); }
public bool IsValid() => HeightmapUtils.CheckHeightParameters(HeightStickSize, HeightType, HeightRange, HeightScale, false) && MathUtil.IsInRange(InitialByte, byte.MinValue, byte.MaxValue);
public static Texture CreateTexture([NotNull] this Heightmap heightmap, GraphicsDevice device) { if (heightmap == null) { throw new ArgumentNullException(nameof(heightmap)); } if (device == null || !heightmap.IsValid()) { return(null); } var min = heightmap.HeightRange.X / heightmap.HeightScale; var max = heightmap.HeightRange.Y / heightmap.HeightScale; switch (heightmap.HeightType) { case HeightfieldTypes.Float: return(Texture.New2D(device, heightmap.Size.X, heightmap.Size.Y, PixelFormat.R8_UNorm, HeightmapUtils.ConvertToByteHeights(heightmap.Floats, min, max))); case HeightfieldTypes.Short: return(Texture.New2D(device, heightmap.Size.X, heightmap.Size.Y, PixelFormat.R8_UNorm, heightmap.Shorts.Select((h) => (byte)MathUtil.Clamp(MathUtil.Lerp(byte.MinValue, byte.MaxValue, MathUtil.InverseLerp(min, max, h)), byte.MinValue, byte.MaxValue)).ToArray())); case HeightfieldTypes.Byte: return(Texture.New2D(device, heightmap.Size.X, heightmap.Size.Y, PixelFormat.R8_UNorm, heightmap.Bytes.Select((h) => (byte)MathUtil.Clamp(MathUtil.Lerp(byte.MinValue, byte.MaxValue, MathUtil.InverseLerp(min, max, h)), byte.MinValue, byte.MaxValue)).ToArray())); default: return(null); } }
public bool IsValid() => HeightmapUtils.CheckHeightParameters(HeightStickSize, HeightType, HeightRange, HeightScale, false) && MathUtil.IsInRange(InitialHeight, HeightRange.X, HeightRange.Y);