private void BuildVertices(Terrain t) { meshHeight = t.Length + 5000; meshWidth = t.Width + 5000; float squareHeight = meshHeight / (float)(meshDivisions + 1); float squareWidth = meshWidth / (float)(meshDivisions + 1); vertices = new VertexPositionNormalTexture[(meshDivisions + 2) * (meshDivisions + 2)]; int i = 0; for (int y = 0; y < meshDivisions + 2; y++) { for (int x = 0; x < meshDivisions + 2; x++) { float worldX = y * squareHeight - meshHeight / 2 + t.Center.X; float worldZ = -x * squareWidth + meshWidth / 2 + t.Center.Z; vertices[i++] = new VertexPositionNormalTexture(new Vector3(worldX, 0,worldZ), Vector3.Up, new Vector2(worldX / meshWidth, worldZ / meshHeight)); } } // save the vertices to the vertex buffer vBuffer = new VertexBuffer(sim.GraphicsDevice, vertices.Length * VertexPositionNormalTexture.SizeInBytes, BufferUsage.WriteOnly); vBuffer.SetData(vertices); }
/// <summary> /// Starts building every world component /// </summary> public void Build() { sim.World.Paused = true; task = "Building Terrain"; Terrain newTerrain = new Terrain(data, sim); newTerrain.LoadContent(sim.Content, data.Climate); newTerrain.Build(); sim.World.LoadTerrain(newTerrain); progress = 0.3f; task = "Building Water"; Water newWater = new Water(sim); newWater.LoadContent(sim.Content, newTerrain); sim.World.Water = newWater; progress = 0.35f; task = "Building Vegetation"; Vegetation newVegetation = new Vegetation(sim, data); newVegetation.LoadContent(sim.Content, newTerrain, data.Climate); sim.World.Vegetation = newVegetation; progress = 0.6f; task = "Building Pathfinding Nodes"; LinkedList<Vector3> nodes = new LinkedList<Vector3>(); task = "Building Weather"; sim.World.Weather.BuildWeather(newTerrain, data.Climate); sim.World.Paused = false; progress = 1.0f; }
public void LoadContent(ContentManager cm, Terrain terrain, Climate climate) { switch (climate) { case Climate.Tropical: texGrass = cm.Load<Texture2D>(@"textures/vegetation/grassShrub"); texTree = cm.Load<Texture2D>(@"textures/vegetation/tree_palm2"); break; case Climate.Polar: texGrass = cm.Load<Texture2D>(@"textures/vegetation/grassTundra"); texTree = cm.Load<Texture2D>(@"textures/vegetation/pine-snowy"); break; case Climate.Dry: texGrass = cm.Load<Texture2D>(@"textures/vegetation/grassShrub"); texTree = cm.Load<Texture2D>(@"textures/vegetation/tree_palm2"); break; default: break; }; effect = cm.Load<Effect>(@"shaders\vegetation"); effect.Parameters["tCloudShadowMap"].SetValue(cm.Load<Texture2D>(@"textures\sky\clouds")); pTexture = effect.Parameters["t0"]; Build(terrain, climate); declaration = new VertexDeclaration(sim.GraphicsDevice, VertexBillboard.VertexElements); }
public void BuildWeather(Terrain t, Climate climate) { Type weatherType = climate == Climate.Polar ? typeof(Snow) : typeof(Rain); if (regions[0].WeatherEffect == null || regions[0].WeatherEffect.GetType() != weatherType) { double cubicArea = Math.Pow(drawDist, 3); // visible weather area int numParticles = (int)(cubicArea * 0.02f / drawDist * 150); bool wasActive = Active; Active = false; foreach (WeatherRegion r in regions) r.BuildEffect(weatherType, numParticles, drawDist, sim); Active = wasActive; } }
public void LoadContent(ContentManager cm, Terrain t) { effect = cm.Load<Effect>(@"shaders\water"); //effect.Parameters["tEnvMap"].SetValue(cm.Load<TextureCube>(@"textures\cubemaps\sea")); effect.Parameters["tNormalMap"].SetValue(cm.Load<Texture2D>(@"textures\terrain\wavebumps")); effect.Parameters["vTextureScale"].SetValue(35); effect.Parameters["tHeightmap"].SetValue(t.Heightmap); effect.Parameters["fTerrainWidth"].SetValue(t.Width); effect.Parameters["fTerrainLength"].SetValue(t.Length); effect.Parameters["matWorld"].SetValue(Matrix.CreateTranslation(0, sealevel, 0)); effect.Parameters["fShoreBlendCoefficient"].SetValue(10 * t.Scale.Y); effect.Parameters["tCloudShadowMap"].SetValue(cm.Load<Texture2D>(@"textures\sky\clouds")); pTime = effect.Parameters["fTime"]; BuildVertices(t); BuildIndices(); declaration = new VertexDeclaration(sim.GraphicsDevice, VertexPositionNormalTexture.VertexElements); }
public void LoadTerrain(Terrain t) { this.terrain = t; }
private void Build(Terrain t, Climate climate) { float min = 5; float low = t.LowMark * 2; float max = t.HighMark / 2; float high = (max + low) / 2.0f; List <VertexBillboard> treeVertices = new List <VertexBillboard>(); List <int> treeIndices = new List <int>(); List <VertexBillboard> grassVertices = new List <VertexBillboard>(); List <int> grassIndices = new List <int>(); Random rand = new Random(); for (int i = 0; i < t.Vertices.Length; i++) { VertexTerrain v = t.Vertices[i]; // current vertex in the terrain mesh float height = v.Position.Y; float baseProbability = 0; // initially, there is no chance of adding a plant if (height > min && height < max) // check if the vertex is within allowed height range { if (v.TextureWeights.W >= 0.5f) { baseProbability = 0; } else if (climate == Climate.Dry) { if (height < low) { baseProbability = 0.6f - (height - max / 2) / (max / 2 - min); } else if (height > high) { baseProbability = 0.2f - (height - high) / (max - high); } else { baseProbability = 0.2f; } } else { if (height < low) { baseProbability = (height - min) / (low - min); // fewer plants toward shore } else if (height > high) { baseProbability = v.TextureWeights.Z - (height - high) / (max - high); // fewer plants near mountain peaks } else { baseProbability = v.TextureWeights.Z; // otherwise, the vertex is perfectly good for a plant } } } baseProbability *= density; // scale probability by density float treeProbability = baseProbability * treeDensity; double roll = rand.NextDouble(); // get a random number to check if the plant should be added if (baseProbability > roll) { if (treeProbability > roll) { //trees.Add(new TreeModel(treeModel, v.Position, rand.Next(175,250)/100.0f)); AddTree(v, treeVertices, treeIndices, rand, climate); } else { AddGrass(v, grassVertices, grassIndices, rand, climate); } } } // buffer the vegetation if (grassVertices.Count > 0) { vBufferGrass = new VertexBuffer(sim.GraphicsDevice, grassVertices.Count * VertexBillboard.SizeInBytes, BufferUsage.WriteOnly); vBufferGrass.SetData(grassVertices.ToArray()); iBufferGrass = new IndexBuffer(sim.GraphicsDevice, typeof(int), grassIndices.Count, BufferUsage.WriteOnly); iBufferGrass.SetData(grassIndices.ToArray()); } if (treeVertices.Count > 0) { vBufferTrees = new VertexBuffer(sim.GraphicsDevice, treeVertices.Count * VertexBillboard.SizeInBytes, BufferUsage.WriteOnly); vBufferTrees.SetData(treeVertices.ToArray()); iBufferTrees = new IndexBuffer(sim.GraphicsDevice, typeof(int), treeIndices.Count, BufferUsage.WriteOnly); iBufferTrees.SetData(treeIndices.ToArray()); } }
private void Build(Terrain t, Climate climate) { float min = 5; float low = t.LowMark * 2; float max = t.HighMark / 2; float high = (max + low) / 2.0f; List<VertexBillboard> treeVertices = new List<VertexBillboard>(); List<int> treeIndices = new List<int>(); List<VertexBillboard> grassVertices = new List<VertexBillboard>(); List<int> grassIndices = new List<int>(); Random rand = new Random(); for (int i = 0; i < t.Vertices.Length; i++) { VertexTerrain v = t.Vertices[i]; // current vertex in the terrain mesh float height = v.Position.Y; float baseProbability = 0; // initially, there is no chance of adding a plant if (height > min && height < max) // check if the vertex is within allowed height range { if (v.TextureWeights.W >= 0.5f) baseProbability = 0; else if (climate == Climate.Dry) { if (height < low) baseProbability = 0.6f - (height - max/2) / (max/2 - min); else if (height > high) baseProbability = 0.2f - (height - high) / (max - high); else baseProbability = 0.2f; } else { if (height < low) baseProbability = (height - min) / (low - min); // fewer plants toward shore else if (height > high) baseProbability = v.TextureWeights.Z - (height - high) / (max - high); // fewer plants near mountain peaks else baseProbability = v.TextureWeights.Z; // otherwise, the vertex is perfectly good for a plant } } baseProbability *= density; // scale probability by density float treeProbability = baseProbability * treeDensity; double roll = rand.NextDouble(); // get a random number to check if the plant should be added if (baseProbability > roll) { if (treeProbability > roll) //trees.Add(new TreeModel(treeModel, v.Position, rand.Next(175,250)/100.0f)); AddTree(v, treeVertices, treeIndices, rand, climate); else AddGrass(v, grassVertices, grassIndices, rand, climate); } } // buffer the vegetation if (grassVertices.Count > 0) { vBufferGrass = new VertexBuffer(sim.GraphicsDevice, grassVertices.Count * VertexBillboard.SizeInBytes, BufferUsage.WriteOnly); vBufferGrass.SetData(grassVertices.ToArray()); iBufferGrass = new IndexBuffer(sim.GraphicsDevice, typeof(int), grassIndices.Count, BufferUsage.WriteOnly); iBufferGrass.SetData(grassIndices.ToArray()); } if (treeVertices.Count > 0) { vBufferTrees = new VertexBuffer(sim.GraphicsDevice, treeVertices.Count * VertexBillboard.SizeInBytes, BufferUsage.WriteOnly); vBufferTrees.SetData(treeVertices.ToArray()); iBufferTrees = new IndexBuffer(sim.GraphicsDevice, typeof(int), treeIndices.Count, BufferUsage.WriteOnly); iBufferTrees.SetData(treeIndices.ToArray()); } }