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 HeightfieldColliderShapeDesc() { InitialHeights = null; HeightfieldType = HeightfieldTypes.Float; HeightStickSize = new Int2(65, 65); HeightRange = new Vector2(-10, 10); HeightScale = new CustomHeightScale(); FlipQuadEdges = false; LocalOffset = new Vector3(0, 0, 0); LocalRotation = Quaternion.Identity; }
public static bool CheckHeightParameters(Int2 size, HeightfieldTypes heightType, Vector2 heightRange, float heightScale, bool throwExceptionWhenInvalid) { try { // Size if (size.X < HeightfieldColliderShape.MinimumHeightStickWidth || size.Y < HeightfieldColliderShape.MinimumHeightStickLength) { throw new ArgumentException($"{ nameof(size) } parameters should be greater than or equal to 2."); } // HeightScale if (MathUtil.IsZero(heightScale)) { throw new ArgumentException($"{ nameof(heightScale) } is 0."); } if ((heightType == HeightfieldTypes.Float) && !MathUtil.IsOne(heightScale)) { throw new ArgumentException($"{ nameof(heightScale) } should be 1 in { heightType } type."); } // HeightRange if (heightRange.Y < heightRange.X) { throw new ArgumentException($"{ nameof(heightRange) }.{ nameof(heightRange.Y) } should be greater than { nameof(heightRange.X) }."); } if (Math.Abs((heightRange.Y / heightScale) - (heightRange.X / heightScale)) < 1) { throw new ArgumentException($"{ nameof(heightRange) } is too short."); } if ((heightType == HeightfieldTypes.Byte) && (Math.Sign(heightRange.X) + Math.Sign(heightRange.Y)) == 0) { throw new ArgumentException($"Can't handle { nameof(heightRange) } included both of positive and negative in { heightType } type."); } } catch (ArgumentException) { if (throwExceptionWhenInvalid) { throw; } return(false); } return(true); }
internal HeightfieldColliderShape ( int heightStickWidth, int heightStickLength, HeightfieldTypes heightType, object dynamicFieldData, float heightScale, float minHeight, float maxHeight, bool flipQuadEdges ) { Type = ColliderShapeTypes.Heightfield; Is2D = false; HeightStickWidth = heightStickWidth; HeightStickLength = heightStickLength; HeightType = heightType; HeightScale = heightScale; MinHeight = minHeight; MaxHeight = maxHeight; cachedScaling = Vector3.One; switch (HeightType) { case HeightfieldTypes.Short: ShortArray = dynamicFieldData as UnmanagedArray <short>; InternalShape = new BulletSharp.HeightfieldTerrainShape(HeightStickWidth, HeightStickLength, ShortArray.Pointer, HeightScale, MinHeight, MaxHeight, 1, BulletSharp.PhyScalarType.Int16, flipQuadEdges) { LocalScaling = cachedScaling, }; break; case HeightfieldTypes.Byte: ByteArray = dynamicFieldData as UnmanagedArray <byte>; InternalShape = new BulletSharp.HeightfieldTerrainShape(HeightStickWidth, HeightStickLength, ByteArray.Pointer, HeightScale, MinHeight, MaxHeight, 1, BulletSharp.PhyScalarType.Byte, flipQuadEdges) { LocalScaling = cachedScaling, }; break; case HeightfieldTypes.Float: FloatArray = dynamicFieldData as UnmanagedArray <float>; InternalShape = new BulletSharp.HeightfieldTerrainShape(HeightStickWidth, HeightStickLength, FloatArray.Pointer, HeightScale, MinHeight, MaxHeight, 1, BulletSharp.PhyScalarType.Single, flipQuadEdges) { LocalScaling = cachedScaling, }; break; } var halfRange = (MaxHeight - MinHeight) * 0.5f; var offset = -(MinHeight + halfRange); DebugPrimitiveMatrix = Matrix.Translation(new Vector3(0, offset, 0)) * Matrix.Scaling(Vector3.One * DebugScaling); }