Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class LightRenderer
- {
- public List<SpotLight> spotLights;
- public List<PointLight> pointLights;
- public RenderTarget2D backBufferCache;
- public RenderTarget2D midGroundTarget;
- public RenderTarget2D lightMap;
- public RenderTarget2D unwrapTarget;
- public RenderTarget2D occlusionMap;
- public RenderTarget2D postProcessTarget;
- public RenderTarget2D horizontalBlurTarget;
- public RenderTarget2D verticalBlurTarget;
- public Effect unwrapSpotlight;
- public Effect unwrap;
- public Effect spotLight;
- public Effect pointLight;
- public Effect horizontalBlur;
- public Effect verticalBlur;
- public Effect lightBlend;
- Rectangle fullScreen;
- SpriteBatch spriteBatch;
- Vector2 screenDims;
- public BlendState collapseBlendState;
- public GraphicsDeviceManager graphics;
- public float minLight = -1f;
- public float lightBias = -1f;
- public float minBlur = 0.0f;
- public float maxBlur = 5.0f;
- public LightRenderer(GraphicsDeviceManager _graphics)
- {
- graphics = _graphics;
- }
- public void Initialize()
- {
- spotLights = new List<SpotLight>();
- pointLights = new List<PointLight>();
- backBufferCache = new RenderTarget2D(graphics.GraphicsDevice, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height);
- midGroundTarget = new RenderTarget2D(graphics.GraphicsDevice, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height);
- lightMap = new RenderTarget2D(graphics.GraphicsDevice, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height, false, SurfaceFormat.Color, DepthFormat.None, 1, RenderTargetUsage.PreserveContents);
- unwrapTarget = new RenderTarget2D(graphics.GraphicsDevice, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Width, false, SurfaceFormat.HdrBlendable, DepthFormat.None);
- occlusionMap = new RenderTarget2D(graphics.GraphicsDevice, graphics.GraphicsDevice.Viewport.Width, 1, false, SurfaceFormat.HdrBlendable, DepthFormat.None);
- postProcessTarget = new RenderTarget2D(graphics.GraphicsDevice, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height);
- horizontalBlurTarget = new RenderTarget2D(graphics.GraphicsDevice, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height);
- verticalBlurTarget = new RenderTarget2D(graphics.GraphicsDevice, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height);
- fullScreen = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height);
- collapseBlendState = new BlendState();
- collapseBlendState.ColorBlendFunction = BlendFunction.Min;
- collapseBlendState.AlphaBlendFunction = BlendFunction.Min;
- collapseBlendState.ColorSourceBlend = Blend.One;
- collapseBlendState.ColorDestinationBlend = Blend.One;
- collapseBlendState.AlphaSourceBlend = Blend.One;
- collapseBlendState.AlphaDestinationBlend = Blend.One;
- screenDims = new Vector2(graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height);
- }
- public void LoadContent(ContentManager Content)
- {
- spriteBatch = new SpriteBatch(graphics.GraphicsDevice);
- unwrap = Content.Load<Effect>(@"Effects\Unwrap");
- unwrapSpotlight = Content.Load<Effect>(@"Effects\UnwrapSpotlight");
- spotLight = Content.Load<Effect>(@"Effects\SpotLight");
- pointLight = Content.Load<Effect>(@"Effects\PointLight");
- verticalBlur = Content.Load<Effect>(@"Effects\VerticalBlur");
- horizontalBlur = Content.Load<Effect>(@"Effects\HorizontalBlur");
- lightBlend = Content.Load<Effect>(@"Effects\LightBlend");
- }
- public void BeginDrawBackground()
- {
- graphics.GraphicsDevice.SetRenderTarget(backBufferCache);
- }
- public void EndDrawBackground()
- {
- }
- public void BeginDrawShadowCasters()
- {
- graphics.GraphicsDevice.SetRenderTarget(midGroundTarget);
- graphics.GraphicsDevice.Clear(Color.Transparent);
- }
- public void EndDrawShadowCasters()
- {
- }
- public void DrawLitScene()
- {
- //Error checking
- //.....
- //
- PrepareResources();
- for (int i = 0; i < spotLights.Count; i++)
- {
- float lightDirAngle = spotLights[i].GetBiasedAngle();
- float angleBias = spotLights[i].GetAngleBias();
- UnwrapShadowCasters(spotLights[i], lightDirAngle, angleBias);
- CreateOcclusionMap();
- CreateLightMap(spotLights[i], lightDirAngle, angleBias);
- BlurLightMaps(spotLights[i]);
- AccumulateLightMaps(spotLights[i]);
- }
- for (int i = 0; i < pointLights.Count; i++)
- {
- UnwrapShadowCasters(pointLights[i]);
- CreateOcclusionMap();
- CreateLightMap(pointLights[i]);
- BlurLightMaps(pointLights[i]);
- AccumulateLightMaps(pointLights[i]);
- }
- RenderFinalScene();
- }
- private void PrepareResources()
- {
- unwrap.Parameters["TextureWidth"].SetValue(screenDims.X);
- unwrap.Parameters["TextureHeight"].SetValue(screenDims.Y);
- unwrap.Parameters["DiagonalLength"].SetValue(screenDims.Length());
- spotLight.Parameters["ScreenDimensions"].SetValue(screenDims);
- spotLight.Parameters["DiagonalLength"].SetValue(screenDims.Length());
- spotLight.Parameters["Bias"].SetValue(lightBias);
- unwrapSpotlight.Parameters["TextureWidth"].SetValue(screenDims.X);
- unwrapSpotlight.Parameters["TextureHeight"].SetValue(screenDims.Y);
- unwrapSpotlight.Parameters["DiagonalLength"].SetValue(screenDims.Length());
- verticalBlur.Parameters["ScreenDims"].SetValue(screenDims);
- verticalBlur.Parameters["minBlur"].SetValue(minBlur);
- verticalBlur.Parameters["maxBlur"].SetValue(maxBlur);
- horizontalBlur.Parameters["ScreenDims"].SetValue(screenDims);
- horizontalBlur.Parameters["minBlur"].SetValue(minBlur);
- horizontalBlur.Parameters["maxBlur"].SetValue(maxBlur);
- pointLight.Parameters["ScreenDimensions"].SetValue(screenDims);
- pointLight.Parameters["DiagonalLength"].SetValue(screenDims.Length());
- pointLight.Parameters["Bias"].SetValue(lightBias);
- graphics.GraphicsDevice.SetRenderTarget(lightMap);
- graphics.GraphicsDevice.Clear(Color.Black);
- }
- private void UnwrapShadowCasters(SpotLight sLight, float lightDirAngle, float angleBias)
- {
- graphics.GraphicsDevice.SetRenderTarget(unwrapTarget);
- graphics.GraphicsDevice.Clear(Color.Transparent);
- unwrapSpotlight.Parameters["LightPos"].SetValue(sLight.Position);
- unwrapSpotlight.Parameters["MinAngle"].SetValue(lightDirAngle - (sLight.outerAngle / 2f));
- unwrapSpotlight.Parameters["MaxAngle"].SetValue(lightDirAngle + (sLight.outerAngle / 2f));
- spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Opaque, SamplerState.PointClamp, null, null, unwrapSpotlight);
- spriteBatch.Draw(midGroundTarget, new Rectangle(0, 0, fullScreen.Width, fullScreen.Width), Color.White);
- spriteBatch.End();
- }
- private void UnwrapShadowCasters(PointLight pLight)
- {
- graphics.GraphicsDevice.SetRenderTarget(unwrapTarget);
- graphics.GraphicsDevice.Clear(Color.Transparent);
- unwrap.Parameters["LightPos"].SetValue(pLight.Position);
- spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Opaque, SamplerState.PointClamp, null, null, unwrap);
- spriteBatch.Draw(midGroundTarget, new Rectangle(0, 0, fullScreen.Width, fullScreen.Width), Color.White);
- spriteBatch.End();
- }
- private void CreateOcclusionMap()
- {
- graphics.GraphicsDevice.SetRenderTarget(occlusionMap);
- graphics.GraphicsDevice.Clear(Color.White);
- spriteBatch.Begin(SpriteSortMode.Deferred, collapseBlendState, SamplerState.PointClamp, null, null);
- for (int i = 0; i < fullScreen.Width; i++)
- {
- spriteBatch.Draw(unwrapTarget, new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, 1), new Rectangle(0, i, graphics.GraphicsDevice.Viewport.Width, 1), Color.White);
- }
- spriteBatch.End();
- }
- private void CreateLightMap(SpotLight sLight, float lightDirAngle, float angleBias)
- {
- graphics.GraphicsDevice.SetRenderTarget(postProcessTarget);
- graphics.GraphicsDevice.Clear(Color.Black);
- spotLight.Parameters["LightPos"].SetValue(sLight.Position);
- spotLight.Parameters["LightPow"].SetValue(sLight.Power);
- spotLight.Parameters["Radius"].SetValue(sLight.radius);
- spotLight.Parameters["OuterMinAngle"].SetValue(lightDirAngle - (sLight.outerAngle / 2f));
- spotLight.Parameters["OuterMaxAngle"].SetValue(lightDirAngle + (sLight.outerAngle / 2f));
- spotLight.Parameters["CenterAngle"].SetValue(lightDirAngle);
- spotLight.Parameters["HalfInnerArc"].SetValue(sLight.innerAngle / 2f);
- spotLight.Parameters["HalfOuterArc"].SetValue(sLight.outerAngle / 2f);
- spotLight.Parameters["AngleBias"].SetValue(angleBias);
- spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Opaque, SamplerState.PointClamp, null, null, spotLight);
- spriteBatch.Draw(occlusionMap, fullScreen, Color.White);
- spriteBatch.End();
- }
- private void CreateLightMap(PointLight pLight)
- {
- graphics.GraphicsDevice.SetRenderTarget(postProcessTarget);
- graphics.GraphicsDevice.Clear(Color.Black);
- //Set params
- pointLight.Parameters["LightPos"].SetValue(pLight.Position);
- pointLight.Parameters["LightPow"].SetValue(pLight.Power);
- pointLight.Parameters["Radius"].SetValue(pLight.radius);
- spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Opaque, SamplerState.PointClamp, null, null, pointLight);
- spriteBatch.Draw(occlusionMap, fullScreen, Color.White);
- spriteBatch.End();
- }
- private void BlurLightMaps(Light light)
- {
- graphics.GraphicsDevice.SetRenderTarget(horizontalBlurTarget);
- graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
- //Set some params
- horizontalBlur.Parameters["LightPos"].SetValue(light.Position);
- horizontalBlur.Parameters["Radius"].SetValue(light.radius);
- spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Opaque, null, null, null, horizontalBlur);
- spriteBatch.Draw(postProcessTarget, fullScreen, Color.White);
- spriteBatch.End();
- graphics.GraphicsDevice.SetRenderTarget(verticalBlurTarget);
- graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
- //Set some more params
- verticalBlur.Parameters["LightPos"].SetValue(light.Position);
- verticalBlur.Parameters["Radius"].SetValue(light.radius);
- spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Opaque, null, null, null, verticalBlur);
- spriteBatch.Draw(horizontalBlurTarget, fullScreen, Color.White);
- spriteBatch.End();
- }
- private void AccumulateLightMaps(Light light)
- {
- graphics.GraphicsDevice.SetRenderTarget(lightMap);
- spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Additive, null, null, null);
- spriteBatch.Draw(verticalBlurTarget, fullScreen, light.color);
- spriteBatch.End();
- }
- private void RenderFinalScene()
- {
- graphics.GraphicsDevice.SetRenderTarget(null);
- graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
- graphics.GraphicsDevice.Textures[1] = lightMap;
- lightBlend.Parameters["MinLight"].SetValue(minLight);
- spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, null, null, lightBlend);
- spriteBatch.Draw(backBufferCache, fullScreen, Color.White);
- spriteBatch.End();
- graphics.GraphicsDevice.Textures[1] = null;
- spriteBatch.Begin();
- spriteBatch.Draw(midGroundTarget, fullScreen, Color.White);
- spriteBatch.End();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement