Ejemplo n.º 1
0
        public Minimap(Device device, DeviceContext dc, int minimapWidth, int minimapHeight, Terrain terrain, CameraBase viewCam)
        {
            _dc = dc;

            _minimapViewport = new Viewport(0, 0, minimapWidth, minimapHeight);

            CreateMinimapTextureViews(device, minimapWidth, minimapHeight);

            _terrain = terrain;

            SetupOrthoCamera();
            _viewCam = viewCam;

            // frustum vb will contain four corners of view frustum, with first vertex repeated as the last
            var vbd = new BufferDescription(
                VertexPC.Stride * 5,
                ResourceUsage.Dynamic,
                BindFlags.VertexBuffer,
                CpuAccessFlags.Write,
                ResourceOptionFlags.None,
                0
            );
            _frustumVB = new Buffer(device, vbd);

            _edgePlanes = new[] {
            new Plane(1, 0, 0, -_terrain.Width / 2),
            new Plane(-1, 0, 0, _terrain.Width / 2),
            new Plane(0, 1, 0, -_terrain.Depth / 2),
            new Plane(0, -1, 0, _terrain.Depth / 2)
            };

            ScreenPosition = new Vector2(0.25f, 0.75f);
            Size = new Vector2(0.25f, 0.25f);
        }
Ejemplo n.º 2
0
        public TerrainRenderer(Material material, Terrain terrain) {
            _material = material;
            _patches = new List<Patch>();
            _terrain = terrain;
            World = Matrix.Identity;

        }
Ejemplo n.º 3
0
        public Unit(BasicModelInstance model, MapTile mp, Terrain terrain) {
            _modelInstance = model;
            MapTile = mp;
            _terrain = terrain;
            _position = mp.WorldPos;
            _position.Y += HeightOffset;
            Time = 0.0f;
            _activeWP = 0;
            Moving = false;
            MovePrc = 0;

            Speed = 1.0f;
        }
Ejemplo n.º 4
0
        public void CreateMesh(Terrain terrain, Rectangle r, Device device)
        {
            _patchBounds = r;
            if (_vb != null) {
                Util.ReleaseCom(ref _vb);
                _vb = null;
            }

            var halfWidth = 0.5f * terrain.Width;
            var halfDepth = 0.5f * terrain.Depth;

            var patchWidth = terrain.Width / (terrain.NumPatchVertCols - 1);
            var patchDepth = terrain.Depth / (terrain.NumPatchVertRows - 1);
            var vertWidth = terrain.Info.CellSpacing;
            var vertDepth = terrain.Info.CellSpacing;
            var du = 1.0f / (terrain.NumPatchVertCols - 1) / Terrain.CellsPerPatch;
            var dv = 1.0f / (terrain.NumPatchVertRows - 1) / Terrain.CellsPerPatch;

            _verts = new List<TerrainCP>();
            var min = new Vector3(float.MaxValue);
            var max = new Vector3(float.MinValue);
            for (int z = r.Top, z0 = 0; z <= r.Bottom; z++, z0++) {
                var zp = halfDepth - r.Top / Terrain.CellsPerPatch * patchDepth - z0 * vertDepth;

                for (int x = r.Left, x0 = 0; x <= r.Right; x++, x0++) {
                    var xp = -halfWidth + r.Left / Terrain.CellsPerPatch * patchWidth + x0 * vertWidth;
                    var pos = new Vector3(xp, terrain.Height(xp, zp), zp);

                    min = Vector3.Minimize(min, pos);
                    max = Vector3.Maximize(max, pos);

                    var uv = new Vector2(r.Left * du + x0 * du, r.Top * dv + z0 * dv);
                    var v = new TerrainCP(pos, uv, new Vector2());
                    _verts.Add(v);
                }
            }

            Bounds = new BoundingBox(min, max);

            var vbd = new BufferDescription(
                TerrainCP.Stride * _verts.Count,
                ResourceUsage.Immutable,
                BindFlags.VertexBuffer,
                CpuAccessFlags.None,
                ResourceOptionFlags.None,
                0
            );
            _vb = new Buffer(device, new DataStream(_verts.ToArray(), false, false), vbd);
        }
Ejemplo n.º 5
0
        private void AddUIElements()
        {
            _panel = new FlowLayoutPanel {
                Dock = DockStyle.Top,
                AutoSize = true,
                FlowDirection = FlowDirection.LeftToRight
            };

            _generateButton = new Button {
                Text = "Generate Terrain",
                AutoSize = true
            };
            _generateButton.Click += (sender, args) => {
                Window.Cursor = Cursors.WaitCursor;
                Util.ReleaseCom(ref _terrain);
                _terrain = new Terrain();
                var tii = new InitInfo {
                    HeightMapFilename = null,
                    LayerMapFilename0 = "textures/grass.dds",
                    LayerMapFilename1 = "textures/darkdirt.dds",
                    LayerMapFilename2 = "textures/stone.dds",
                    LayerMapFilename3 = "Textures/lightdirt.dds",
                    LayerMapFilename4 = "textures/snow.dds",
                    BlendMapFilename = null,
                    HeightScale = 50.0f,
                    HeightMapWidth = 2049,
                    HeightMapHeight = 2049,
                    CellSpacing = 0.5f,

                    Seed = (int)_txtSeed.Value,
                    NoiseSize1 = (float) _txtNoise1.Value,
                    Persistence1 =(float) _txtPersistence1.Value,
                    Octaves1 = (int) _txtOctaves1.Value,
                    NoiseSize2 = (float) _txtNoise2.Value,
                    Persistence2 = (float) _txtPersistence2.Value,
                    Octaves2 = (int) _txtOctaves2.Value
                };
                _terrain.Init(Device, ImmediateContext, tii);
                _camera.Height = _terrain.Height;
                _hmImg.Image = _terrain.HeightMapImg;
                Window.Cursor = Cursors.Default;
            };

            var labelPadding = new Padding(0, 6, 0, 0);
            _lblSeed = new Label {
                Text = "Seed:",
                AutoSize = true,
                Padding = labelPadding

            };
            _txtSeed = new NumericUpDown() {
                Value = 0,
                AutoSize = true
            };

            _lblNoise1 = new Label {
                Text = "Noise:",
                AutoSize = true,
                Padding = labelPadding

            };
            _txtNoise1 = new NumericUpDown {
                Value = 1.0m,
                DecimalPlaces = 2,
                Minimum = 0m,
                Maximum = 10m,
                Increment = 0.1m,
                AutoSize = true
            };
            _lblPersistence1 = new Label {
                Text = "Persistence:",
                AutoSize = true,
                Padding = labelPadding
            };
            _txtPersistence1 = new NumericUpDown {
                Value = 0.7m,
                DecimalPlaces = 2,
                Minimum = 0m,
                Maximum = 10m,
                Increment = 0.1m,
                AutoSize = true
            };
            _lblOctaves1 = new Label {
                Text = "Octaves:",
                AutoSize = true,
                Padding = labelPadding

            };
            _txtOctaves1 = new NumericUpDown() {
                Value =7,
                AutoSize = true,
                Minimum = 1,
                Maximum = 20,

            };

            _lblNoise2 = new Label {
                Text = "Noise:",
                AutoSize = true,
                Padding = labelPadding

            };
            _txtNoise2 = new NumericUpDown {
                Value = 2.5m,
                DecimalPlaces = 2,
                Minimum = 0m,
                Maximum = 10m,
                Increment = 0.1m,
                AutoSize = true
            };
            _lblPersistence2 = new Label {
                Text = "Persistence:",
                AutoSize = true,
                Padding = labelPadding
            };
            _txtPersistence2 = new NumericUpDown {
                Value = 0.8m,
                DecimalPlaces = 2,
                Minimum = 0m,
                Maximum = 10m,
                Increment = 0.1m,
                AutoSize = true
            };
            _lblOctaves2 = new Label {
                Text = "Octaves:",
                AutoSize = true,
                Padding = labelPadding

            };
            _txtOctaves2 = new NumericUpDown() {
                Value = 3,
                AutoSize = true,
                Minimum = 1,
                Maximum = 20
            };

            _hmImg = new PictureBox() {
                Image = _terrain.HeightMapImg,
                MaximumSize = new Size(64,64),
                MinimumSize = new Size(64,64),
                SizeMode = PictureBoxSizeMode.StretchImage,
                BackColor = Color.White
            };

            _panel.Controls.Add(_lblNoise1);
            _panel.Controls.Add(_txtNoise1);
            _panel.Controls.Add(_lblPersistence1);
            _panel.Controls.Add(_txtPersistence1);
            _panel.Controls.Add(_lblOctaves1);
            _panel.Controls.Add(_txtOctaves1);

            _panel.Controls.Add(_lblNoise2);
            _panel.Controls.Add(_txtNoise2);
            _panel.Controls.Add(_lblPersistence2);
            _panel.Controls.Add(_txtPersistence2);
            _panel.Controls.Add(_lblOctaves2);
            _panel.Controls.Add(_txtOctaves2);

            _panel.SetFlowBreak(_txtOctaves2, true);

            _panel.Controls.Add(_lblSeed);
            _panel.Controls.Add(_txtSeed);

            _panel.Controls.Add(_generateButton);

            _tblLayout = new TableLayoutPanel {
                Dock = DockStyle.Top,
                AutoSize = true
            };
            _tblLayout.RowStyles.Add(new RowStyle(SizeType.AutoSize));
            _tblLayout.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
            _tblLayout.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));

            _tblLayout.Controls.Add(_panel, 0,0);
            _tblLayout.Controls.Add(_hmImg, 1, 0);

            Window.Controls.Add(_tblLayout);
        }
Ejemplo n.º 6
0
        public override bool Init()
        {
            if (!base.Init()) return false;

            Effects.InitAll(Device);
            InputLayouts.InitAll(Device);
            RenderStates.InitAll(Device);
            Patch.InitPatchData(Terrain.CellsPerPatch, Device);

            _sky = new Sky(Device, "Textures/grasscube1024.dds", 5000.0f);

            var tii = new InitInfo {
                HeightMapFilename = null,
                LayerMapFilename0 = "textures/grass.dds",
                LayerMapFilename1 = "textures/darkdirt.dds",
                LayerMapFilename2 = "textures/stone.dds",
                LayerMapFilename3 = "Textures/lightdirt.dds",
                LayerMapFilename4 = "textures/snow.dds",
                BlendMapFilename = null,
                HeightScale = 50.0f,
                HeightMapWidth = 2049,
                HeightMapHeight = 2049,
                CellSpacing = 0.5f,

                Seed = 0,
                NoiseSize1 = 1.0f,
                Persistence1 = 0.7f,
                Octaves1 = 7,
                NoiseSize2 = 2.5f,
                Persistence2 = 0.8f,
                Octaves2 = 3
            };
            _terrain = new Terrain();
            _terrain.Init(Device, ImmediateContext, tii);

            _camera.Height = _terrain.Height;

            AddUIElements();

            return true;
        }
Ejemplo n.º 7
0
        public override bool Init() {
            if (!base.Init()) return false;

            Effects.InitAll(Device);
            InputLayouts.InitAll(Device);
            RenderStates.InitAll(Device);
            Patch.InitPatchData(Terrain.CellsPerPatch, Device);

            _sky = new Sky(Device, "Textures/grasscube1024.dds", 5000.0f);

            var tii = new InitInfo {
                HeightMapFilename = null,
                LayerMapFilename0 = "textures/grass.png",
                LayerMapFilename1 = "textures/hills.png",
                LayerMapFilename2 = "textures/stone.png",
                LayerMapFilename3 = "Textures/lightdirt.dds",
                LayerMapFilename4 = "textures/snow.png",
                Material = new Material() {
                    Ambient = Color.LightGray,
                    Diffuse = Color.LightGray,
                    Specular = new Color4(64, 0, 0, 0)
                },
                BlendMapFilename = null,
                HeightScale = 50.0f,
                HeightMapWidth = 2049,
                HeightMapHeight = 2049,
                CellSpacing = 0.5f,

                Seed = MathF.Rand(),
                NoiseSize1 = 3.0f,
                Persistence1 = 0.7f,
                Octaves1 = 7,
                NoiseSize2 = 2.5f,
                Persistence2 = 0.8f,
                Octaves2 = 3,


            };
            _terrain = new Terrain();
            //_terrain.DebugQuadTree = true;
            _terrain.Init(Device, ImmediateContext, tii);

            _camera.Height = _terrain.Height;


            _camera.SetLens(0.25f * MathF.PI, AspectRatio, 1.0f, 1000.0f);
            _ssao = new Ssao(Device, ImmediateContext, ClientWidth, ClientHeight, _camera.FovY, _camera.FarZ);

            _whiteTex = ShaderResourceView.FromFile(Device, "Textures/white.dds");

            _sMap = new ShadowMap(Device, SMapSize, SMapSize);

            _sceneBounds = new BoundingSphere(new Vector3(), MathF.Sqrt(_terrain.Width * _terrain.Width + _terrain.Depth * _terrain.Depth) / 2);

            _minimap = new Minimap(Device, ImmediateContext, MinimapSize, MinimapSize, _terrain, _camera);

            _sphereModel = new BasicModel();
            _sphereModel.CreateSphere(Device, 0.25f, 10, 10);
            _sphereModel.Materials[0] = new Material {
                Ambient = new Color4(63, 0, 0),
                Diffuse = Color.Red,
                Specular = new Color4(32, 1.0f, 1.0f, 1.0f)
            };
            _sphereModel.DiffuseMapSRV[0] = _whiteTex;

            _sphere = new BasicModelInstance(_sphereModel);

            _unit = new Unit(_sphere, _terrain.GetTile(511, 511), _terrain);

            FontCache.RegisterFont("bold", 16, "Courier New", FontWeight.Bold);


            return true;
        }
Ejemplo n.º 8
0
        public override bool Init() {
            if ( ! base.Init()) return false;

            Effects.InitAll(Device);
            InputLayouts.InitAll(Device);
            RenderStates.InitAll(Device);

            _sky = new Sky(Device, "Textures/grasscube1024.dds", 5000.0f);
            Patch.InitPatchData(Terrain.CellsPerPatch, Device);
            var tii = new InitInfo {
                HeightMapFilename = "Textures/terrain.raw",
                LayerMapFilename0 = "textures/grass.dds",
                LayerMapFilename1 = "textures/darkdirt.dds",
                LayerMapFilename2 = "textures/stone.dds",
                LayerMapFilename3 = "Textures/lightdirt.dds",
                LayerMapFilename4 = "textures/snow.dds",
                BlendMapFilename = "textures/blend.dds",
                HeightScale = 50.0f,
                HeightMapWidth = 2049,
                HeightMapHeight = 2049,
                CellSpacing = 0.5f
            };
            _terrain = new Terrain();
            _terrain.Init(Device, ImmediateContext, tii);

            _camera.Height = _terrain.Height;
            _txMgr = new TextureManager();
            _txMgr.Init(Device);
            _treeModel = new BasicModel(Device, _txMgr, "Models/tree.x", "Textures");
            _treeInstances = new List<BasicModelInstance>();
            
            
            for (var i = 0; i < NumTrees; i++) {
                var good = false;
                var x = MathF.Rand(0, _terrain.Width);
                var z = MathF.Rand(0, _terrain.Depth);
                while (!good) {

                    
                    if (_terrain.Height(x, z) < 12.0f) {
                        good = true;
                    }
                    x = MathF.Rand(-_terrain.Width/2, _terrain.Width/2);
                    z = MathF.Rand(-_terrain.Depth/2, _terrain.Depth/2);
                }
                var treeInstance = new BasicModelInstance(_treeModel) {
                    
                    World = Matrix.RotationX(MathF.PI / 2) * Matrix.Translation(x, _terrain.Height(x, z), z)
                };
                _treeInstances.Add(treeInstance);
            }
            BuildInstancedBuffer();

            return true;
        }
Ejemplo n.º 9
0
        public override bool Init() {
            if (!base.Init()) return false;
            Enable4XMsaa = true;
            Effects.InitAll(Device);
            InputLayouts.InitAll(Device);
            RenderStates.InitAll(Device);

            _sky = new Sky(Device, "Textures/grasscube1024.dds", 5000.0f);

            var tii = new InitInfo {
                HeightMapFilename = "Textures/terrain.raw",
                LayerMapFilename0 = "textures/grass.dds",
                LayerMapFilename1 = "textures/darkdirt.dds",
                LayerMapFilename2 = "textures/stone.dds",
                LayerMapFilename3 = "Textures/lightdirt.dds",
                LayerMapFilename4 = "textures/snow.dds",
                BlendMapFilename = "textures/blend.dds",
                HeightScale = 50.0f,
                HeightMapWidth = 2049,
                HeightMapHeight = 2049,
                CellSpacing = 0.5f
            };
            Patch.InitPatchData(Terrain.CellsPerPatch, Device);
            _terrain = new Terrain();
            _terrain.Init(Device, ImmediateContext, tii);

            _randomTex = Util.CreateRandomTexture1DSRV(Device);

            _flareTexSRV = Util.CreateTexture2DArraySRV(Device, ImmediateContext, new[] {"Textures/flare0.dds"}, Format.R8G8B8A8_UNorm);
            _fire = new ParticleSystem();
            _fire.Init(Device, Effects.FireFX, _flareTexSRV, _randomTex, 500);
            _fire.EmitPosW = new Vector3(0, 1.0f, 120.0f);

            _rainTexSRV = Util.CreateTexture2DArraySRV(Device, ImmediateContext, new[] {"Textures/raindrop.dds"}, Format.R8G8B8A8_UNorm);
            _rain = new ParticleSystem();
            _rain.Init(Device, Effects.RainFX, _rainTexSRV, _randomTex, 10000);

            return true;
        }
Ejemplo n.º 10
0
        public override bool Init() {
            if (!base.Init()) return false;

            Effects.InitAll(Device);
            InputLayouts.InitAll(Device);
            RenderStates.InitAll(Device);
            Patch.InitPatchData(Terrain.CellsPerPatch, Device);

            _sky = new Sky(Device, "Textures/grasscube1024.dds", 5000.0f);

            var tii = new InitInfo {
                HeightMapFilename = null,
                LayerMapFilename0 = "textures/grass.dds",
                LayerMapFilename1 = "textures/darkdirt.dds",
                LayerMapFilename2 = "textures/stone.dds",
                LayerMapFilename3 = "Textures/lightdirt.dds",
                LayerMapFilename4 = "textures/snow.dds",
                BlendMapFilename = null,
                HeightScale = 50.0f,
                HeightMapWidth = 2049,
                HeightMapHeight = 2049,
                CellSpacing = 0.5f,

                Seed = 0,
                NoiseSize1 = 3.0f,
                Persistence1 = 0.7f,
                Octaves1 = 7,
                NoiseSize2 = 2.5f,
                Persistence2 = 0.8f,
                Octaves2 = 3,


            };
            _terrain = new Terrain();
            _terrain.Init(Device, ImmediateContext, tii);

            _camera.Height = _terrain.Height;

            AddUIElements();

            _camera.SetLens(0.25f * MathF.PI, AspectRatio, 1.0f, 1000.0f);
            _ssao = new Ssao(Device, ImmediateContext, ClientWidth, ClientHeight, _camera.FovY, _camera.FarZ);

            _whiteTex = ShaderResourceView.FromFile(Device, "Textures/white.dds");


            _sMap = new ShadowMap(Device, SMapSize, SMapSize);

            _sceneBounds = new BoundingSphere(new Vector3(), MathF.Sqrt(_terrain.Width * _terrain.Width + _terrain.Depth * _terrain.Depth) / 2);

            _minimap = new Minimap(Device, ImmediateContext, MinimapSize, MinimapSize, _terrain, _camera);

            return true;
        }
Ejemplo n.º 11
0
        public override bool Init()
        {
            if (!base.Init()) return false;

            Effects.InitAll(Device);
            InputLayouts.InitAll(Device);
            RenderStates.InitAll(Device);
            Patch.InitPatchData(Terrain.CellsPerPatch, Device);

            _sky = new Sky(Device, "Textures/grasscube1024.dds", 5000.0f);

            var tii = new InitInfo {
                HeightMapFilename = null,
                LayerMapFilename0 = "textures/grass.dds",
                LayerMapFilename1 = "textures/darkdirt.dds",
                LayerMapFilename2 = "textures/stone.dds",
                LayerMapFilename3 = "Textures/lightdirt.dds",
                LayerMapFilename4 = "textures/snow.dds",
                BlendMapFilename = null,
                HeightScale = 50.0f,
                HeightMapWidth = 2049,
                HeightMapHeight = 2049,
                CellSpacing = 0.5f,

                Seed = 0,
                NoiseSize1 = 3.0f,
                Persistence1 = 0.7f,
                Octaves1 = 7,
                NoiseSize2 = 2.5f,
                Persistence2 = 0.8f,
                Octaves2 = 3,

            };
            _terrain = new Terrain();
            _terrain.Init(Device, ImmediateContext, tii);

            _camera.Height = _terrain.Height;

            _camera.SetLens(0.25f * MathF.PI, AspectRatio, 1.0f, 1000.0f);
            _ssao = new Ssao(Device, ImmediateContext, ClientWidth, ClientHeight, _camera.FovY, _camera.FarZ);

            _whiteTex = ShaderResourceView.FromFile(Device, "Textures/white.dds");
            BuildScreenQuadGeometryBuffers();

            _sMap = new ShadowMap(Device, SMapSize, SMapSize);

            _sceneBounds = new BoundingSphere(new Vector3(), MathF.Sqrt(_terrain.Width * _terrain.Width + _terrain.Depth * _terrain.Depth) / 2);

            _minimap = new Minimap(Device, ImmediateContext, MinimapSize, MinimapSize, _terrain, _camera);

            _sphereModel = BasicModel.CreateSphere(Device, 0.25f, 10, 10);
            _sphereModel.Materials[0] = new Material {
                Ambient = Color.Red,
                Diffuse = Color.Red,
                Specular = new Color4(32, 1.0f, 1.0f, 1.0f)
            };
            _sphereModel.DiffuseMapSRV[0] = _whiteTex;

            _sphere = new BasicModelInstance {Model = _sphereModel};
            _spherePos = new Vector3(float.MaxValue);

            return true;
        }
Ejemplo n.º 12
0
        public void Init(Device device, DeviceContext dc, Terrain terrain) {
            _device = device;
            NumPatchVertRows = ((terrain.Info.HeightMapHeight - 1) / Terrain.CellsPerPatch) + 1;
            NumPatchVertCols = ((terrain.Info.HeightMapWidth - 1) / Terrain.CellsPerPatch) + 1;
            _numPatchVertices = NumPatchVertRows * NumPatchVertCols;
            _numPatchQuadFaces = (NumPatchVertRows - 1) * (NumPatchVertCols - 1);
            if (device.FeatureLevel == FeatureLevel.Level_11_0) {
                _useTessellation = true;
            }

            if (terrain.Info.Material.HasValue) {
                _material = terrain.Info.Material.Value;
            }

            D3DApp.GD3DApp.ProgressUpdate.Draw(0.60f, "Building terrain patches");
            if (_useTessellation) {
                CalcAllPatchBoundsY();
                BuildQuadPatchVB(device);
                BuildQuadPatchIB(device);
            } else {
                BuildPatches(device);
            }
            D3DApp.GD3DApp.ProgressUpdate.Draw(0.65f, "Loading textures");
            _heightMapSRV = terrain.HeightMap.BuildHeightmapSRV(device);

            var layerFilenames = new List<string> {
                terrain.Info.LayerMapFilename0 ?? "textures/null.bmp", 
                terrain.Info.LayerMapFilename1 ?? "textures/null.bmp", 
                terrain.Info.LayerMapFilename2 ?? "textures/null.bmp", 
                terrain.Info.LayerMapFilename3 ?? "textures/null.bmp", 
                terrain.Info.LayerMapFilename4 ?? "textures/null.bmp"
            };
            _layerMapArraySRV = Util.CreateTexture2DArraySRV(device, dc, layerFilenames.ToArray(), Format.R8G8B8A8_UNorm);
            if (!string.IsNullOrEmpty(terrain.Info.BlendMapFilename)) {
                D3DApp.GD3DApp.ProgressUpdate.Draw(0.70f, "Loading blendmap from file");
                _blendMapSRV = ShaderResourceView.FromFile(device, terrain.Info.BlendMapFilename);
                _blendMapSRV.Resource.DebugName = terrain.Info.BlendMapFilename;
            } else {
                _blendMapSRV = CreateBlendMap(terrain.HeightMap, device, terrain);
            }

            if (DebugQuadTree) {
                D3DApp.GD3DApp.ProgressUpdate.Draw(0.95f, "Building quadtree debug vertex buffers");
                BuildQuadTreeDebugBuffers(device);
            }
            D3DApp.GD3DApp.ProgressUpdate.Draw(1.0f, "Terrain initialized");
            _walkMap = new WalkMap(this);
        }
Ejemplo n.º 13
0
 private static void SmoothBlendMap(HeightMap hm, List<Color4> colors, Terrain terrain) {
     for (var y = 0; y < terrain.HeightMap.HeightMapHeight; y++) {
         for (var x = 0; x < terrain.HeightMap.HeightMapWidth; x++) {
             var sum = colors[x + y * hm.HeightMapHeight];
             var num = 0;
             for (var y1 = y - 1; y1 < y + 2; y1++) {
                 for (var x1 = x - 1; x1 < x + 1; x1++) {
                     if (!hm.InBounds(y1, x1)) {
                         continue;
                     }
                     sum += colors[x1 + y1 * hm.HeightMapHeight];
                     num++;
                 }
             }
             colors[x + y * hm.HeightMapHeight] = new Color4(sum.Alpha / num, sum.Red / num, sum.Green / num, sum.Blue / num);
         }
     }
 }
Ejemplo n.º 14
0
        private static ShaderResourceView CreateBlendMap(HeightMap hm, Device device, Terrain terrain) {
            var colors = new List<Color4>();
            for (var y = 0; y < terrain.HeightMap.HeightMapHeight; y++) {
                for (var x = 0; x < terrain.HeightMap.HeightMapWidth; x++) {
                    var elev = terrain.HeightMap[y, x];
                    var color = new Color4(0);
                    if (elev > hm.MaxHeight * (0.05f + MathF.Rand(-0.05f, 0.05f))) {
                        // dark green grass texture
                        color.Red = elev / (hm.MaxHeight) + MathF.Rand(-0.05f, 0.05f);
                    }
                    if (elev > hm.MaxHeight * (0.4f + MathF.Rand(-0.15f, 0.15f))) {
                        // stone texture
                        color.Green = elev / hm.MaxHeight + MathF.Rand(-0.05f, 0.05f);
                    }
                    if (elev > hm.MaxHeight * (0.75f + MathF.Rand(-0.1f, 0.1f))) {
                        // snow texture
                        color.Alpha = elev / hm.MaxHeight + MathF.Rand(-0.05f, 0.05f);
                    }
                    colors.Add(color);
                }
                D3DApp.GD3DApp.ProgressUpdate.Draw(0.70f + 0.05f * ((float)y / terrain.HeightMap.HeightMapHeight), "Generating blendmap");
            }
            SmoothBlendMap(hm, colors, terrain);
            SmoothBlendMap(hm, colors, terrain);
            var texDec = new Texture2DDescription {
                ArraySize = 1,
                BindFlags = BindFlags.ShaderResource,
                CpuAccessFlags = CpuAccessFlags.None,
                Format = Format.R32G32B32A32_Float,
                SampleDescription = new SampleDescription(1, 0),
                Height = terrain.HeightMap.HeightMapHeight,
                Width = terrain.HeightMap.HeightMapWidth,
                MipLevels = 1,
                OptionFlags = ResourceOptionFlags.None,
                Usage = ResourceUsage.Default
            };
            var blendTex = new Texture2D(device, texDec, new DataRectangle(terrain.HeightMap.HeightMapWidth * Marshal.SizeOf(typeof(Color4)), new DataStream(colors.ToArray(), false, false))) { DebugName = "terrain blend texture" };
            var srvDesc = new ShaderResourceViewDescription { Format = texDec.Format, Dimension = ShaderResourceViewDimension.Texture2D, MostDetailedMip = 0, MipLevels = -1 };

            var srv = new ShaderResourceView(device, blendTex, srvDesc);

            Util.ReleaseCom(ref blendTex);
            return srv;
        }