public void Draw(GraphicsDevice device, Camera camera) { BlendState beforeBlend = device.BlendState; RasterizerState beforeRasterizer = device.RasterizerState; device.RasterizerState = new RasterizerState() { FillMode = beforeRasterizer.FillMode, CullMode = CullMode.None }; device.BlendState = BlendState.Additive; billboardEffect.Parameters["W"].SetValue(Matrix.CreateScale(Scale) * Matrix.CreateWorld(Position, Position - camera.Position, Vector3.Up)); billboardEffect.Parameters["VP"].SetValue(camera.View * camera.Projection); billboardEffect.Parameters["tex"].SetValue(Texture); billboardEffect.Parameters["col"].SetValue(Color.ToVector4()); foreach (EffectPass p in billboardEffect.CurrentTechnique.Passes) { p.Apply(); device.DrawUserIndexedPrimitives<VertexPositionColorTexture>(PrimitiveType.TriangleList, VertexBuffer, 0, 4, IndexBuffer, 0, 2); } device.RasterizerState = beforeRasterizer; device.BlendState = beforeBlend; }
public void Render(GraphicsDevice device, SpriteBatch sBatch, Camera camera, bool debug = false) { BoundingFrustum frustum = camera.Frustum; List<Chunk> toDraw = new List<Chunk>(); foreach (Chunk c in Chunks) if (c.bbox.Intersects(frustum)) toDraw.Add(c); #region effect parameters terrainEffect.Parameters["GrassTimer"].SetValue(totalTime); terrainEffect.Parameters["BlockTexture"].SetValue(Block.BlockTexture); terrainEffect.Parameters["AmbientBrightness"].SetValue(AmbientBrightness); terrainEffect.Parameters["LightDirection"].SetValue(LightDirection * (IsDayTime ? 1 : -1)); terrainEffect.Parameters["CameraPosition"].SetValue(camera.Position); terrainEffect.Parameters["VP"].SetValue(camera.View * camera.Projection); //terrainEffect.Parameters["LightWVP"].SetValue(getLightProjection(camera.Position)); skyEffect.Parameters["W"].SetValue(Matrix.CreateTranslation(camera.Position)); skyEffect.Parameters["VP"].SetValue(camera.View * camera.Projection); skyEffect.Parameters["CameraPosition"].SetValue(camera.Position); skyEffect.Parameters["SkyTexture"].SetValue(NightSkybox); skyEffect.Parameters["SkyAlpha"].SetValue(1f - (AmbientBrightness - .05f) / .95f); if (blurKernel == null || blurXOffsets == null || blurYOffsets == null) { Util.ComputeKernel(7, .5f, out blurKernel); Util.ComputeOffsets2D(7, sceneRenderTarget.Width, sceneRenderTarget.Height, out blurXOffsets, out blurYOffsets); postfxEffect.Parameters["weights"].SetValue(blurKernel); postfxEffect.Parameters["pixel"].SetValue(Vector2.One / new Vector2(sceneRenderTarget.Width, sceneRenderTarget.Height)); } #endregion #region terrain device.BlendState = BlendState.AlphaBlend; device.DepthStencilState = DepthStencilState.Default; /*RasterizerState b4 = device.RasterizerState; device.RasterizerState = RasterizerState.CullClockwise; terrainEffect.Parameters["VP"].SetValue(getLightProjection(camera.Position)); device.SetRenderTarget(depthRenderTarget); device.Clear(Color.Transparent); foreach (Chunk c in toDraw) c.Render(device, frustum, terrainEffect, true, DrawGrass, debug); if (!debug) terrainEffect.Parameters["VP"].SetValue(camera.View * camera.Projection);*/ //device.RasterizerState = b4; device.SetRenderTarget(sceneRenderTarget); device.Clear(Color.Transparent); //terrainEffect.Parameters["DepthTexture"].SetValue(depthRenderTarget); ChunksDrawnLastFrame = 0; foreach (Chunk c in toDraw) ChunksDrawnLastFrame += c.Render(device, frustum, terrainEffect, DrawGrass, debug) ? 1 : 0; device.SetVertexBuffer(null); device.Indices = null; #endregion Debug.DrawBoxes(device, debugEffect); #region sun & moon, sun shafts Sun.Position = camera.Position - LightDirection * 2f; Moon.Position = camera.Position + LightDirection * 2f; device.DepthStencilState = DepthStencilState.None; device.BlendState = BlendState.AlphaBlend; device.SetRenderTarget(sunRenderTarget); device.Clear(Color.Transparent); Sun.Draw(device, camera); postfxEffect.CurrentTechnique = postfxEffect.Techniques["Occuld"]; sBatch.Begin(SpriteSortMode.Deferred, BlendState.Opaque, null, null, null, postfxEffect); sBatch.Draw(sceneRenderTarget, Vector2.Zero, Color.White); sBatch.End(); // sun shafts device.SetRenderTarget(postfxRenderTarget1); device.Clear(Color.Transparent); Vector3 lp = device.Viewport.Project(Sun.Position, camera.Projection, camera.View, Matrix.Identity); postfxEffect.Parameters["lightPosition"].SetValue(new Vector2(lp.X, lp.Y) / new Vector2(sceneRenderTarget.Width, sceneRenderTarget.Height)); postfxEffect.CurrentTechnique = postfxEffect.Techniques["Scatter"]; sBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, null, null, postfxEffect); sBatch.Draw(sunRenderTarget, Vector2.Zero, Color.White); sBatch.End(); // blur x device.SetRenderTarget(postfxRenderTarget2); device.Clear(Color.Transparent); postfxEffect.Parameters["offsets"].SetValue(blurXOffsets); postfxEffect.CurrentTechnique = postfxEffect.Techniques["Blur"]; sBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, null, null, postfxEffect); sBatch.Draw(postfxRenderTarget1, Vector2.Zero, Color.White); sBatch.End(); // blur y device.SetRenderTarget(sunRenderTarget); device.Clear(Color.Transparent); postfxEffect.Parameters["offsets"].SetValue(blurYOffsets); postfxEffect.CurrentTechnique = postfxEffect.Techniques["Blur"]; sBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, null, null, postfxEffect); sBatch.Draw(postfxRenderTarget2, Vector2.Zero, Color.White); sBatch.End(); #endregion #region skybox & moon device.SetRenderTarget(null); device.Clear(Color.LightSkyBlue); device.BlendState = BlendState.AlphaBlend; device.DepthStencilState = DepthStencilState.DepthRead; device.RasterizerState = new RasterizerState() { FillMode = device.RasterizerState.FillMode, CullMode = CullMode.CullClockwiseFace }; skyEffect.CurrentTechnique = skyEffect.Techniques["Skybox"]; foreach (EffectPass p in skyEffect.CurrentTechnique.Passes) { p.Apply(); device.DrawUserIndexedPrimitives<VertexPositionColor>(PrimitiveType.TriangleList, SkyboxVerts, 0, SkyboxVerts.Length, SkyboxInds, 0, SkyboxInds.Length / 3); } Moon.Draw(device, camera); device.RasterizerState = new RasterizerState() { FillMode = device.RasterizerState.FillMode, CullMode = CullMode.CullCounterClockwiseFace }; device.DepthStencilState = DepthStencilState.Default; #endregion sBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend); sBatch.Draw(sceneRenderTarget, Vector2.Zero, Color.White); sBatch.Draw(sunRenderTarget, Vector2.Zero, Color.White); sBatch.End(); }