/// <summary> /// Prepares the light map to be drawn (pre-render) /// </summary> public void LightMapPrepare() { // Prepare the matrix with optional settings and assign it to an effect parameter Matrix lightMapMatrix = LightmapMatrixGet(); _effect.Parameters["Matrix"].SetValue(lightMapMatrix); // Obtain the original rendering states var originalRenderTargets = GraphicsDevice.GetRenderTargets(); // Set and clear the target GraphicsDevice.SetRenderTarget(_map); GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.Stencil, AmbientColor, 0, 1); // Make sure we're culling the right way! GraphicsDevice.RasterizerState = KryptonEngine.RasterizerStateGetFromCullMode(CullMode); // put the render target's size into a more friendly format var targetSize = new Vector2(_map.Width, _map.Height); // Render Light Maps foreach (var light in Lights) { // Loop through each light within the view frustum if (!light.Bounds.Intersects(_bounds)) { continue; } // Clear the stencil and set the scissor rect (because we're stretching geometry past the light's reach) GraphicsDevice.Clear( options: ClearOptions.Stencil, color: Color.Black, depth: 0, stencil: 0); GraphicsDevice.ScissorRectangle = ScissorRectCreateForLight( light: light, matrix: lightMapMatrix, targetSize: targetSize); // Draw the light! light.Draw(RenderHelper, Hulls); } if (_bluriness > 0) { // Blur the shadow map horizontally to the blur target GraphicsDevice.SetRenderTarget(_mapBlur); RenderHelper.BlurTextureToTarget( _map, LightMapSize.Full, BlurTechnique.Horizontal, _bluriness); // Blur the shadow map vertically back to the final map GraphicsDevice.SetRenderTarget(_map); RenderHelper.BlurTextureToTarget( _mapBlur, LightMapSize.Full, BlurTechnique.Vertical, _bluriness); } // Reset to the original rendering states GraphicsDevice.SetRenderTargets(originalRenderTargets); }