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 is null) { throw new ArgumentNullException(nameof(heightmap)); } var length = heightmap.Size.X * heightmap.Size.Y; bool hasValidHeights = false; switch (heightmap.HeightType) { case HeightfieldTypes.Float when heightmap.Floats != null && heightmap.Floats.Length == length: hasValidHeights = true; break; case HeightfieldTypes.Short when heightmap.Shorts != null && heightmap.Shorts.Length == length: hasValidHeights = true; break; case HeightfieldTypes.Byte when heightmap.Bytes != null && heightmap.Bytes.Length == length: hasValidHeights = true; break; } return(HeightmapUtils.CheckHeightParameters(heightmap.Size, heightmap.HeightType, heightmap.HeightRange, heightmap.HeightScale, false) && hasValidHeights); }
public bool IsValid() => HeightmapUtils.CheckHeightParameters(HeightStickSize, HeightType, HeightRange, HeightScale, false) && MathUtil.IsInRange(InitialShort, -short.MaxValue, short.MaxValue);
public bool IsValid() => HeightmapUtils.CheckHeightParameters(HeightStickSize, HeightType, HeightRange, HeightScale, false) && MathUtil.IsInRange(InitialHeight, HeightRange.X, HeightRange.Y);
public static Texture CreateTexture([NotNull] this Heightmap heightmap, GraphicsDevice device) { if (heightmap is null) { throw new ArgumentNullException(nameof(heightmap)); } if (device is 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); } }