public void Resize(SlimDX.Direct3D9.Device device, System.Drawing.RectangleF rectangle) { var hm = Ground.Heightmap; //var oldSm1 = Ground.SplatMap1; //var oldSm2 = Ground.SplatMap2; Settings.Size = new System.Drawing.SizeF(rectangle.Width, rectangle.Height); InitGround(); int yOldWorldStart = (int)(Ground.Translation.Y / Constants.GroundPolySize); int xOldWorldStart = (int)(Ground.Translation.X / Constants.GroundPolySize); int yNewWorldStart = (int)(rectangle.Y / Constants.GroundPolySize); int xNewWorldStart = (int)(rectangle.X / Constants.GroundPolySize); int xNewOffs = xNewWorldStart - xOldWorldStart; int yNewOffs = yNewWorldStart - yOldWorldStart; int yReadOffset = yNewOffs; int xReadOffset = xNewOffs; for (int y = 0; y < Ground.Heightmap.GetLength(0); y++) { for (int x = 0; x < Ground.Heightmap.GetLength(1); x++) { if (y + yReadOffset >= 0 && x + xReadOffset >= 0 && y + yReadOffset < hm.GetLength(0) && x + xReadOffset < hm.GetLength(1)) { Ground.Heightmap[y, x] = hm[y + yReadOffset, x + xReadOffset]; } } } //var oldSmR1 = Graphics.TextureUtil.ReadTexture<Graphics.Software.Texel.A8R8G8B8>(oldSm1, 0); //var oldSmR2 = Graphics.TextureUtil.ReadTexture<Graphics.Software.Texel.A8R8G8B8>(oldSm2, 0); Ground.SplatMap1.Resource9 = new SlimDX.Direct3D9.Texture(device, (int)Settings.Size.Width * 4, (int)Settings.Size.Height * 4, 1, SlimDX.Direct3D9.Usage.Dynamic, SlimDX.Direct3D9.Format.A8R8G8B8, SlimDX.Direct3D9.Pool.Default); Ground.SplatMap2.Resource9 = new SlimDX.Direct3D9.Texture(device, (int)Settings.Size.Width * 4, (int)Settings.Size.Height * 4, 1, SlimDX.Direct3D9.Usage.Dynamic, SlimDX.Direct3D9.Format.A8R8G8B8, SlimDX.Direct3D9.Pool.Default); /*var ld = Ground.SplatMap.GetLevelDescription(0); * * int ySMOldWorldStart = (int)((Ground.Translation.Y / Constants.GroundPolySize) * 4f); * int xSMOldWorldStart = (int)((Ground.Translation.X / Constants.GroundPolySize) * 4f); * int ySMNewWorldStart = (int)((rectangle.Y / Constants.GroundPolySize) * 4f); * int xSMNewWorldStart = (int)((rectangle.X / Constants.GroundPolySize) * 4f); * int xSMNewOffs = xSMNewWorldStart - xSMOldWorldStart; * int ySMNewOffs = ySMNewWorldStart - ySMOldWorldStart; * * Graphics.Software.Texel.A8R8G8B8[,] p = new Graphics.Software.Texel.A8R8G8B8[ * (int)Settings.Size.Height * 4, (int)Settings.Size.Width * 4]; * * for (int y = 0; y < p.GetLength(0); y++) * for (int x = 0; x < p.GetLength(1); x++) * { * if (y + ySMNewOffs >= 0 && x + xSMNewOffs >= 0 && * y + ySMNewOffs < oldSmR.GetLength(0) && x + xSMNewOffs < oldSmR.GetLength(1)) * p[y, x] = oldSmR[y + ySMNewOffs, x + xSMNewOffs]; * } * * Graphics.TextureUtil.WriteTexture(Ground.SplatMap, p, p[0, 0].Size); */ Settings.Position = new System.Drawing.PointF(rectangle.X, rectangle.Y); Ground.Translation = new Vector3(rectangle.X, rectangle.Y, 0); Ground.ConstructPieces(this); Ground.Init(); }
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); } }