コード例 #1
0
ファイル: Heightmap.cs プロジェクト: glepag1/stride
        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);
        }
コード例 #2
0
ファイル: HeightmapExtensions.cs プロジェクト: glepag1/stride
        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());
        }
コード例 #3
0
 public bool IsValid() => HeightmapUtils.CheckHeightParameters(HeightStickSize, HeightType, HeightRange, HeightScale, false) &&
 MathUtil.IsInRange(InitialByte, byte.MinValue, byte.MaxValue);
コード例 #4
0
ファイル: HeightmapExtensions.cs プロジェクト: glepag1/stride
        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);
            }
        }
コード例 #5
0
 public bool IsValid() => HeightmapUtils.CheckHeightParameters(HeightStickSize, HeightType, HeightRange, HeightScale, false) &&
 MathUtil.IsInRange(InitialHeight, HeightRange.X, HeightRange.Y);