public static Mesh Construct(MeshFromHeightmap2 metaResource) { var indexedPlane = Construct(metaResource.Grid); /*indexedPlane.Transform(metaResource.CombinedWorld); * var invWorld = Matrix.Invert(metaResource.CombinedWorld);*/ var invCombinedWorld = Matrix.Invert(metaResource.CombinedWorld); int heightMapHeight = metaResource.Heightmap.Data.GetLength(0); int heightMapWidth = metaResource.Heightmap.Data.GetLength(1); Vector2 gridStep = new Vector2(metaResource.HeightmapLayout.Width / heightMapWidth, metaResource.HeightmapLayout.Height / heightMapHeight); for (int i = 0; i < indexedPlane.VertexBuffer.Count; i++) { var v = indexedPlane.VertexBuffer[i]; var transformedPos = Vector3.TransformCoordinate(v.Position, metaResource.CombinedWorld); // 0 -> Heightmap size Vector2 relativePosition = Common.Math.ToVector2(transformedPos) - new Vector2(metaResource.HeightmapLayout.X, metaResource.HeightmapLayout.Y); // 0 -> 1 relativePosition.X /= metaResource.HeightmapLayout.Width; relativePosition.Y /= metaResource.HeightmapLayout.Height; Texel.R32F height; if (metaResource.PointSample) { height = TextureUtil.HeightmapPointSample(metaResource.Heightmap.Data, relativePosition); } //{ // int iu = (int)(relativePosition.X * heightMapWidth); // int iv = (int)(relativePosition.Y * heightMapHeight); // height = metaResource.Heightmap.Data[iv, iu]; // /*height = TextureUtil.PointSample(metaResource.Heightmap.Data, relativePosition.X, // relativePosition.Y);*/ //} else { height = TextureUtil.HeightmapSample(metaResource.Heightmap.Data, relativePosition, true); } float h = metaResource.Grid.Position.Z + height.R * metaResource.Height - transformedPos.Z //+ metaResource.CombinedWorld.M43 ; //h = Vector3.TransformCoordinate(Vector3.UnitZ * h, invCombinedWorld).Z; v.Position = new Vector3(v.Position.X, v.Position.Y, h); v.Normal = TextureUtil.NormalFromHeightmap(metaResource.Heightmap.Data, relativePosition, gridStep, new Size(heightMapWidth, heightMapHeight)); indexedPlane.VertexBuffer[i] = v; } return(indexedPlane); }
public MeshFromHeightmap2(MeshFromHeightmap2 copy) { Heightmap = (Content.DataLink <Texel.R32F[, ]>)copy.Heightmap.Clone(); HeightmapLayout = copy.HeightmapLayout; Grid = (Grid)copy.Grid.Clone(); Height = copy.Height; PointSample = copy.PointSample; CombinedWorld = copy.CombinedWorld; }
public static Mesh Construct(MeshFromHeightmap2 metaResource) { var indexedPlane = Construct(metaResource.Grid); /*indexedPlane.Transform(metaResource.CombinedWorld); var invWorld = Matrix.Invert(metaResource.CombinedWorld);*/ var invCombinedWorld = Matrix.Invert(metaResource.CombinedWorld); int heightMapHeight = metaResource.Heightmap.Data.GetLength(0); int heightMapWidth = metaResource.Heightmap.Data.GetLength(1); Vector2 gridStep = new Vector2(metaResource.HeightmapLayout.Width / heightMapWidth, metaResource.HeightmapLayout.Height / heightMapHeight); for (int i = 0; i < indexedPlane.VertexBuffer.Count; i++) { var v = indexedPlane.VertexBuffer[i]; var transformedPos = Vector3.TransformCoordinate(v.Position, metaResource.CombinedWorld); // 0 -> Heightmap size Vector2 relativePosition = Common.Math.ToVector2(transformedPos) - new Vector2(metaResource.HeightmapLayout.X, metaResource.HeightmapLayout.Y); // 0 -> 1 relativePosition.X /= metaResource.HeightmapLayout.Width; relativePosition.Y /= metaResource.HeightmapLayout.Height; Texel.R32F height; if (metaResource.PointSample) height = TextureUtil.HeightmapPointSample(metaResource.Heightmap.Data, relativePosition); //{ // int iu = (int)(relativePosition.X * heightMapWidth); // int iv = (int)(relativePosition.Y * heightMapHeight); // height = metaResource.Heightmap.Data[iv, iu]; // /*height = TextureUtil.PointSample(metaResource.Heightmap.Data, relativePosition.X, // relativePosition.Y);*/ //} else height = TextureUtil.HeightmapSample(metaResource.Heightmap.Data, relativePosition, true); float h = metaResource.Grid.Position.Z + height.R * metaResource.Height - transformedPos.Z //+ metaResource.CombinedWorld.M43 ; //h = Vector3.TransformCoordinate(Vector3.UnitZ * h, invCombinedWorld).Z; v.Position = new Vector3(v.Position.X, v.Position.Y, h); v.Normal = TextureUtil.NormalFromHeightmap(metaResource.Heightmap.Data, relativePosition, gridStep, new Size(heightMapWidth, heightMapHeight)); indexedPlane.VertexBuffer[i] = v; } return indexedPlane; }
public MeshFromHeightmap2(MeshFromHeightmap2 copy) { Heightmap = (Content.DataLink<Texel.R32F[,]>)copy.Heightmap.Clone(); HeightmapLayout = copy.HeightmapLayout; Grid = (Grid)copy.Grid.Clone(); Height = copy.Height; PointSample = copy.PointSample; CombinedWorld = copy.CombinedWorld; }