protected override void OnConstruct() { base.OnConstruct(); if (Ground != null) { float gs = Ground.HeightmapHeightStep; float scalingSnapped = gs * (float)Math.Round(scaling / gs); var gridSize = new Vector2(scalingSnapped, scalingSnapped); var center = Position - Common.Math.ToVector3(gridSize / 2f); Translation = Ground.SnapToHeightmap(center); var transOffset = center - Translation; if (heightmapLink == null) { heightmapLink = new DataLink <Graphics.Software.Texel.R32F[, ]>(Ground.Heightmap); heightmapLink.Version = Ground.CurrentHeightMapVersion; } else { UpdateMesh(); } Matrix?uvTransform = null; uvTransform = Matrix.Invert( Matrix.Translation(-0.5f, -0.5f, 0) * Matrix.RotationZ(orientation) * Matrix.Scaling(0.707106781f * scaling / scalingSnapped, 0.707106781f * scaling / scalingSnapped, 1) * Matrix.Translation(transOffset.X / gridSize.X, transOffset.Y / gridSize.Y, 0) * Matrix.Translation(0.5f, 0.5f, 0) ); ((MetaModel)MainGraphic).XMesh = new MeshConcretize { MeshDescription = new Graphics.Software.Meshes.MeshFromHeightmap3 { Grid = new Graphics.Software.Meshes.Grid { Position = Vector3.UnitZ * EditorHeight, MeshType = MeshType.Indexed, Size = gridSize, NWidth = (int)Math.Round(gridSize.X / Ground.HeightmapWidthStep), NHeight = (int)Math.Round(gridSize.Y / Ground.HeightmapHeightStep), UVMin = new Vector2(0, 0), UVMax = new Vector2(1, 1), UVTransform = uvTransform }, Height = Ground.Height, Heightmap = heightmapLink, HeightmapLayout = new RectangleF(0, 0, Ground.Size.Width, Ground.Size.Height), PointSample = PointSampleHeightMap, HeightmapReadPosition = translation }, Layout = global::Graphics.Software.Vertex.PositionNormalTexcoord.Instance }; VisibilityLocalBounding = CreateBoundingBoxFromModel((MetaModel)MainGraphic); PickingLocalBounding = CreateBoundingMeshFromModel((MetaModel)MainGraphic); } }
protected override void OnConstruct() { base.OnConstruct(); if (Ground != null) { Matrix combinedWorld = ((MetaModel)MainGraphic).World * CombinedWorldMatrix; Matrix?uvTransform = null; Vector3 positionOffset = Vector3.Zero; Matrix?gridTransform = null; if (RotateUV) { gridTransform = Matrix.Translation(-0.5f, -0.5f, 0) * Matrix.RotationZ(-orientation) * Matrix.Translation(0.5f, 0.5f, 0); combinedWorld = gridTransform.Value * combinedWorld; } Matrix invCombinedWorld = Matrix.Invert(combinedWorld); Vector3 rawPosition = Vector3.TransformCoordinate(Vector3.Zero, combinedWorld); if (SnapPositionToHeightmap == DecalSnapping.Snap || SnapPositionToHeightmap == DecalSnapping.SnapAndUVAntiSnap) { Vector3 snappedPosition = Ground.SnapToHeightmap(rawPosition); snappedPosition.Z = 0; positionOffset = Vector3.TransformCoordinate(snappedPosition, invCombinedWorld); positionOffset.Z = Common.Math.Position(((MetaModel)MainGraphic).World).Z; if (SnapPositionToHeightmap == DecalSnapping.SnapAndUVAntiSnap) { var d = new Vector3(positionOffset.X, positionOffset.Y, 0); uvTransform = Matrix.Translation(d); } } Vector2 planeSize = new Vector2(1, 1); Vector3 size; Quaternion rotation; Vector3 position; combinedWorld.Decompose(out size, out rotation, out position); if (SnapSizeToHeightmap != DecalSnapping.None) { Vector3 snappedSize = Ground.SnapToHeightmap(size); planeSize = new Vector2(snappedSize.X / size.X, snappedSize.Y / size.Y); if (SnapSizeToHeightmap == DecalSnapping.SnapAndUVAntiSnap) { uvTransform = Matrix.Scaling(planeSize.X, planeSize.Y, 0) * (uvTransform.HasValue ? uvTransform.Value : Matrix.Identity); } size = snappedSize; } if (SnapGridResolutionToHeightmap) { GridResolution = new Size( (int)Math.Floor(size.X / Ground.HeightmapWidthStep), (int)Math.Floor(size.Y / Ground.HeightmapHeightStep) ); } if (heightmapLink == null) { heightmapLink = new DataLink <Graphics.Software.Texel.R32F[, ]>(Ground.Heightmap); heightmapLink.Version = Ground.CurrentHeightMapVersion; } else { UpdateMesh(); } Vector2 uvmin_ = UVMin, uvmax_ = UVMax; if (RotateUV) { uvTransform = (uvTransform.HasValue ? uvTransform.Value : Matrix.Identity) * Matrix.Translation(-0.5f, -0.5f, 0) * Matrix.RotationZ(orientation) * Matrix.Translation(0.5f, 0.5f, 0); } if (GridResolution.Width < 1 || GridResolution.Height < 1) { ((MetaModel)MainGraphic).XMesh = null; return; } ((MetaModel)MainGraphic).XMesh = new MeshConcretize { MeshDescription = new Graphics.Software.Meshes.MeshFromHeightmap2 { Grid = new Graphics.Software.Meshes.Grid { Position = positionOffset, MeshType = MeshType.Indexed, Size = planeSize, NWidth = GridResolution.Width, NHeight = GridResolution.Height, UVMin = uvmin_, UVMax = uvmax_, UVTransform = uvTransform, Transform = gridTransform }, Height = Ground.Height, Heightmap = heightmapLink, HeightmapLayout = new RectangleF(0, 0, Ground.Size.Width, Ground.Size.Height), PointSample = PointSampleHeightMap, CombinedWorld = (gridTransform.HasValue ? Matrix.Invert(gridTransform.Value) : Matrix.Identity) * combinedWorld }, Layout = global::Graphics.Software.Vertex.PositionNormalTexcoord.Instance }; VisibilityLocalBounding = CreateBoundingBoxFromModel((MetaModel)MainGraphic); PickingLocalBounding = CreateBoundingMeshFromModel((MetaModel)MainGraphic); } }
protected override void OnConstruct() { base.OnConstruct(); if (Ground != null) { Matrix uvMove = Matrix.Identity; if (SnapSizeToHeightmap) { size = Common.Math.ToVector2(Ground.SnapToHeightmap(Common.Math.ToVector3(Size))); } Vector3 positionOffset = Vector3.Zero; if (SnapPositionToHeightmap == DecalSnapping.Snap || SnapPositionToHeightmap == DecalSnapping.SnapAndUVAntiSnap) { positionOffset = Translation - Ground.SnapToHeightmap(Translation); if (SnapPositionToHeightmap == DecalSnapping.SnapAndUVAntiSnap) { var d = new Vector3(positionOffset.X / Size.X, positionOffset.Y / Size.Y, 0); uvMove = Matrix.Translation(-d); } GridPosition = Ground.SnapToHeightmap(GridPosition); } if (SnapGridResolutionToHeightmap) { GridResolution = new Size( (int)Math.Round(Size.X / Ground.HeightmapWidthStep), (int)Math.Round(Size.Y / Ground.HeightmapHeightStep) ); } if (heightmapLink == null) { heightmapLink = new DataLink <Graphics.Software.Texel.R32F[, ]>(Ground.Heightmap); heightmapLink.Version = Ground.CurrentHeightMapVersion; } else { UpdateMesh(); } Vector2 uvmin_ = UVMin, uvmax_ = UVMax; Matrix? uvTransform = null; if (RotateUV) { uvTransform = uvMove * Matrix.Invert(Matrix.Translation(-0.5f, -0.5f, 0) * Matrix.RotationZ(orientation) * Matrix.Translation(0.5f, 0.5f, 0)); } ((MetaModel)MainGraphic).XMesh = new MeshConcretize { MeshDescription = new Graphics.Software.Meshes.MeshFromHeightmap2 { Grid = new Graphics.Software.Meshes.Grid { Position = GridPosition - positionOffset, MeshType = MeshType.Indexed, Size = Size, NWidth = GridResolution.Width, NHeight = GridResolution.Height, UVMin = uvmin_, UVMax = uvmax_, UVTransform = uvTransform }, Height = Ground.Height, Heightmap = heightmapLink, HeightmapLayout = new RectangleF(0, 0, Ground.Size.Width, Ground.Size.Height), PointSample = PointSampleHeightMap, CombinedWorld = CombinedWorldMatrix }, Layout = global::Graphics.Software.Vertex.PositionNormalTexcoord.Instance }; VisibilityLocalBounding = CreateBoundingBoxFromModel((MetaModel)MainGraphic); PickingLocalBounding = CreateBoundingMeshFromModel((MetaModel)MainGraphic); } }