public override void Initialize() { _treeProfile = Content.Load<TreeProfile>("Trees/Graywood"); _tree = new SunlitSimpleTree(_treeProfile.GenerateSimpleTree()); _tree.Lighting = _sky; // Scale tree down to a desired height const int desiredHeight = 6; float treeModelHeight = _tree.TrunkMesh.BoundingSphere.Radius*2; _scale = Matrix.CreateScale(desiredHeight/treeModelHeight); Position = _position; _wind = new WindStrengthSin(); _animator = new TreeWindAnimator(_wind); base.Initialize(); }
/// <summary> /// Load your graphics content. /// </summary> protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); // Calculate the projection matrix. Viewport viewport = GraphicsDevice.Viewport; float aspectRatio = (float)viewport.Width / (float)viewport.Height; projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, aspectRatio, 1, 10000); terrain = Content.Load<Model>("terrain"); BasicDirectionalLight terrainDirectionalLight = null; directionToSun = Vector3.Normalize(new Vector3(2f, 0.2f, -1.0f)); foreach (ModelMesh mesh in terrain.Meshes) { foreach (BasicEffect effect in mesh.Effects) { effect.Projection = projection; effect.PreferPerPixelLighting = true; // Set the specular lighting to match the sky color. effect.SpecularColor = new Vector3(0f); // Set the fog to match the distant mountains // that are drawn into the sky texture. effect.FogEnabled = false; effect.FogColor = new Vector3(0.15f); effect.FogStart = 100; effect.FogEnd = 320; effect.EnableDefaultLighting(); effect.AmbientLightColor = new Vector3(0.5f, 0.5f, 0.5f); effect.DirectionalLight1.Enabled = false; effect.DirectionalLight2.Enabled = false; effect.DirectionalLight0.Direction = -directionToSun; effect.DiffuseColor = new Vector3(1f, 1f, 1f); terrainDirectionalLight = effect.DirectionalLight0; } } heightmap = Content.Load<Texture2D>("Textures/terrain"); CalculateHeightValues(); sky = Content.Load<Sky>("skybox"); sunTexture = Content.Load<Texture2D>("Textures/Sun"); backbufferWidth = graphics.PreferredBackBufferWidth; backbufferHeight = graphics.PreferredBackBufferHeight; lightScatterPostProcess = Content.Load<Effect>("Effects/LightScatterPostProcess"); //Setup post-process parameters lightScatterPostProcess.Parameters["Density"].SetValue(0.95f); lightScatterPostProcess.Parameters["Weight"].SetValue(1f / 120f * 1.2f); lightScatterPostProcess.Parameters["Decay"].SetValue(0.991f); lightScatterPostProcess.Parameters["Exposure"].SetValue(0.5f); //Trees Random r = new Random(Environment.TickCount); treeProfile = Content.Load<TreeProfile>("Trees/Willow"); wind = new WindStrengthSin(0.1f); windAnimator = new TreeWindAnimator(wind); trees = new LinkedList<Tree>(); for (int i = 0; i < 300; i++) { SimpleTree simpleTree = treeProfile.GenerateSimpleTree(); simpleTree.LeafEffect.CurrentTechnique = simpleTree.LeafEffect.Techniques["SetNoRenderStates"]; simpleTree.TrunkEffect.Parameters["DirLight0DiffuseColor"].SetValue(new Vector4(terrainDirectionalLight.DiffuseColor, 1f)); simpleTree.TrunkEffect.Parameters["DirLight0Direction"].SetValue(terrainDirectionalLight.Direction); simpleTree.TrunkEffect.Parameters["DirLight1Enabled"].SetValue(false); simpleTree.LeafEffect.Parameters["DirLight0DiffuseColor"].SetValue(new Vector4(terrainDirectionalLight.DiffuseColor, 1f)); simpleTree.LeafEffect.Parameters["DirLight0Direction"].SetValue(terrainDirectionalLight.Direction); simpleTree.LeafEffect.Parameters["DirLight1Enabled"].SetValue(false); int x = r.Next((int)(terrainWidth / terrainScale)); int z = r.Next((int)(terrainHeight / terrainScale)); float y = heightValues[x, z]; x *= (int)terrainScale; z *= (int)terrainScale; Vector3 position = new Vector3(x, y, z) - new Vector3(terrainWidth * 0.5f, 0f, terrainHeight * 0.5f); Matrix scale = Matrix.CreateScale(Vector3.Lerp(new Vector3(0.005f), new Vector3(0.015f), (float)r.NextDouble())); trees.AddLast(new Tree(simpleTree, scale * Matrix.CreateTranslation(position))); } //Render targets sceneRenderTarget = new RenderTarget2D(GraphicsDevice, backbufferWidth, backbufferHeight, 1, SurfaceFormat.Color, GraphicsDevice.PresentationParameters.MultiSampleType, GraphicsDevice.PresentationParameters.MultiSampleQuality); lightScatterRenderTarget = new RenderTarget2D(GraphicsDevice, backbufferWidth, backbufferHeight, 1, SurfaceFormat.Color, GraphicsDevice.PresentationParameters.MultiSampleType, GraphicsDevice.PresentationParameters.MultiSampleQuality); //FPS fpsFont = Content.Load<SpriteFont>("Fonts/Verdana"); }