Advertisement
Temeez

LightRendererXNA_whichdoesntworkwithMonoGame

Jun 11th, 2015
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.09 KB | None | 0 0
  1.     public class LightRenderer
  2.     {
  3.         public List<SpotLight> spotLights;
  4.         public List<PointLight> pointLights;
  5.  
  6.         public RenderTarget2D backBufferCache;
  7.         public RenderTarget2D midGroundTarget;
  8.         public RenderTarget2D lightMap;
  9.         public RenderTarget2D unwrapTarget;
  10.         public RenderTarget2D occlusionMap;
  11.         public RenderTarget2D postProcessTarget;
  12.         public RenderTarget2D horizontalBlurTarget;
  13.         public RenderTarget2D verticalBlurTarget;
  14.  
  15.         public Effect unwrapSpotlight;
  16.         public Effect unwrap;
  17.         public Effect spotLight;
  18.         public Effect pointLight;
  19.         public Effect horizontalBlur;
  20.         public Effect verticalBlur;
  21.         public Effect lightBlend;
  22.  
  23.         Rectangle fullScreen;
  24.         SpriteBatch spriteBatch;
  25.  
  26.         Vector2 screenDims;
  27.  
  28.         public BlendState collapseBlendState;
  29.  
  30.         public GraphicsDeviceManager graphics;
  31.  
  32.         public float minLight = -1f;
  33.         public float lightBias = -1f;
  34.  
  35.         public float minBlur = 0.0f;
  36.         public float maxBlur = 5.0f;
  37.        
  38.  
  39.         public LightRenderer(GraphicsDeviceManager _graphics)
  40.         {
  41.             graphics = _graphics;
  42.         }
  43.  
  44.         public void Initialize()
  45.         {
  46.             spotLights = new List<SpotLight>();
  47.             pointLights = new List<PointLight>();
  48.  
  49.             backBufferCache = new RenderTarget2D(graphics.GraphicsDevice, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height);
  50.             midGroundTarget = new RenderTarget2D(graphics.GraphicsDevice, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height);
  51.             lightMap = new RenderTarget2D(graphics.GraphicsDevice, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height, false, SurfaceFormat.Color, DepthFormat.None, 1, RenderTargetUsage.PreserveContents);
  52.             unwrapTarget = new RenderTarget2D(graphics.GraphicsDevice, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Width, false, SurfaceFormat.HdrBlendable, DepthFormat.None);
  53.             occlusionMap = new RenderTarget2D(graphics.GraphicsDevice, graphics.GraphicsDevice.Viewport.Width, 1, false, SurfaceFormat.HdrBlendable, DepthFormat.None);
  54.             postProcessTarget = new RenderTarget2D(graphics.GraphicsDevice, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height);
  55.             horizontalBlurTarget = new RenderTarget2D(graphics.GraphicsDevice, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height);
  56.             verticalBlurTarget = new RenderTarget2D(graphics.GraphicsDevice, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height);
  57.  
  58.             fullScreen = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height);
  59.  
  60.             collapseBlendState = new BlendState();
  61.             collapseBlendState.ColorBlendFunction = BlendFunction.Min;
  62.             collapseBlendState.AlphaBlendFunction = BlendFunction.Min;
  63.             collapseBlendState.ColorSourceBlend = Blend.One;
  64.             collapseBlendState.ColorDestinationBlend = Blend.One;
  65.             collapseBlendState.AlphaSourceBlend = Blend.One;
  66.             collapseBlendState.AlphaDestinationBlend = Blend.One;
  67.  
  68.             screenDims = new Vector2(graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height);
  69.         }
  70.  
  71.         public void LoadContent(ContentManager Content)
  72.         {
  73.             spriteBatch = new SpriteBatch(graphics.GraphicsDevice);
  74.  
  75.             unwrap = Content.Load<Effect>(@"Effects\Unwrap");
  76.             unwrapSpotlight = Content.Load<Effect>(@"Effects\UnwrapSpotlight");
  77.             spotLight = Content.Load<Effect>(@"Effects\SpotLight");
  78.             pointLight = Content.Load<Effect>(@"Effects\PointLight");
  79.             verticalBlur = Content.Load<Effect>(@"Effects\VerticalBlur");
  80.             horizontalBlur = Content.Load<Effect>(@"Effects\HorizontalBlur");
  81.             lightBlend = Content.Load<Effect>(@"Effects\LightBlend");
  82.         }
  83.  
  84.         public void BeginDrawBackground()
  85.         {
  86.             graphics.GraphicsDevice.SetRenderTarget(backBufferCache);
  87.         }
  88.  
  89.         public void EndDrawBackground()
  90.         {
  91.         }
  92.  
  93.         public void BeginDrawShadowCasters()
  94.         {
  95.             graphics.GraphicsDevice.SetRenderTarget(midGroundTarget);
  96.             graphics.GraphicsDevice.Clear(Color.Transparent);
  97.         }
  98.  
  99.         public void EndDrawShadowCasters()
  100.         {
  101.         }
  102.  
  103.         public void DrawLitScene()
  104.         {
  105.             //Error checking
  106.             //.....
  107.             //
  108.  
  109.             PrepareResources();
  110.  
  111.             for (int i = 0; i < spotLights.Count; i++)
  112.             {
  113.                 float lightDirAngle = spotLights[i].GetBiasedAngle();
  114.  
  115.                 float angleBias = spotLights[i].GetAngleBias();
  116.  
  117.                 UnwrapShadowCasters(spotLights[i], lightDirAngle, angleBias);
  118.  
  119.                 CreateOcclusionMap();
  120.  
  121.                 CreateLightMap(spotLights[i], lightDirAngle, angleBias);
  122.  
  123.                 BlurLightMaps(spotLights[i]);
  124.  
  125.                 AccumulateLightMaps(spotLights[i]);
  126.             }
  127.  
  128.             for (int i = 0; i < pointLights.Count; i++)
  129.             {
  130.                 UnwrapShadowCasters(pointLights[i]);
  131.  
  132.                 CreateOcclusionMap();
  133.  
  134.                 CreateLightMap(pointLights[i]);
  135.  
  136.                 BlurLightMaps(pointLights[i]);
  137.  
  138.                 AccumulateLightMaps(pointLights[i]);
  139.             }
  140.  
  141.             RenderFinalScene();
  142.         }
  143.  
  144.         private void PrepareResources()
  145.         {
  146.             unwrap.Parameters["TextureWidth"].SetValue(screenDims.X);
  147.             unwrap.Parameters["TextureHeight"].SetValue(screenDims.Y);
  148.             unwrap.Parameters["DiagonalLength"].SetValue(screenDims.Length());
  149.  
  150.             spotLight.Parameters["ScreenDimensions"].SetValue(screenDims);
  151.             spotLight.Parameters["DiagonalLength"].SetValue(screenDims.Length());
  152.             spotLight.Parameters["Bias"].SetValue(lightBias);
  153.  
  154.             unwrapSpotlight.Parameters["TextureWidth"].SetValue(screenDims.X);
  155.             unwrapSpotlight.Parameters["TextureHeight"].SetValue(screenDims.Y);
  156.             unwrapSpotlight.Parameters["DiagonalLength"].SetValue(screenDims.Length());
  157.  
  158.             verticalBlur.Parameters["ScreenDims"].SetValue(screenDims);
  159.             verticalBlur.Parameters["minBlur"].SetValue(minBlur);
  160.             verticalBlur.Parameters["maxBlur"].SetValue(maxBlur);
  161.  
  162.             horizontalBlur.Parameters["ScreenDims"].SetValue(screenDims);
  163.             horizontalBlur.Parameters["minBlur"].SetValue(minBlur);
  164.             horizontalBlur.Parameters["maxBlur"].SetValue(maxBlur);
  165.  
  166.             pointLight.Parameters["ScreenDimensions"].SetValue(screenDims);
  167.             pointLight.Parameters["DiagonalLength"].SetValue(screenDims.Length());
  168.             pointLight.Parameters["Bias"].SetValue(lightBias);
  169.  
  170.             graphics.GraphicsDevice.SetRenderTarget(lightMap);
  171.             graphics.GraphicsDevice.Clear(Color.Black);
  172.         }
  173.  
  174.         private void UnwrapShadowCasters(SpotLight sLight, float lightDirAngle, float angleBias)
  175.         {
  176.             graphics.GraphicsDevice.SetRenderTarget(unwrapTarget);
  177.             graphics.GraphicsDevice.Clear(Color.Transparent);
  178.  
  179.             unwrapSpotlight.Parameters["LightPos"].SetValue(sLight.Position);
  180.             unwrapSpotlight.Parameters["MinAngle"].SetValue(lightDirAngle - (sLight.outerAngle / 2f));
  181.             unwrapSpotlight.Parameters["MaxAngle"].SetValue(lightDirAngle + (sLight.outerAngle / 2f));
  182.  
  183.             spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Opaque, SamplerState.PointClamp, null, null, unwrapSpotlight);
  184.             spriteBatch.Draw(midGroundTarget, new Rectangle(0, 0, fullScreen.Width, fullScreen.Width), Color.White);
  185.             spriteBatch.End();
  186.         }
  187.  
  188.         private void UnwrapShadowCasters(PointLight pLight)
  189.         {
  190.             graphics.GraphicsDevice.SetRenderTarget(unwrapTarget);
  191.             graphics.GraphicsDevice.Clear(Color.Transparent);
  192.  
  193.             unwrap.Parameters["LightPos"].SetValue(pLight.Position);
  194.  
  195.             spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Opaque, SamplerState.PointClamp, null, null, unwrap);
  196.             spriteBatch.Draw(midGroundTarget, new Rectangle(0, 0, fullScreen.Width, fullScreen.Width), Color.White);
  197.             spriteBatch.End();
  198.         }
  199.  
  200.         private void CreateOcclusionMap()
  201.         {
  202.             graphics.GraphicsDevice.SetRenderTarget(occlusionMap);
  203.             graphics.GraphicsDevice.Clear(Color.White);
  204.  
  205.             spriteBatch.Begin(SpriteSortMode.Deferred, collapseBlendState, SamplerState.PointClamp, null, null);
  206.             for (int i = 0; i < fullScreen.Width; i++)
  207.             {
  208.                 spriteBatch.Draw(unwrapTarget, new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, 1), new Rectangle(0, i, graphics.GraphicsDevice.Viewport.Width, 1), Color.White);
  209.             }
  210.             spriteBatch.End();
  211.         }
  212.  
  213.         private void CreateLightMap(SpotLight sLight, float lightDirAngle, float angleBias)
  214.         {
  215.             graphics.GraphicsDevice.SetRenderTarget(postProcessTarget);
  216.             graphics.GraphicsDevice.Clear(Color.Black);
  217.  
  218.             spotLight.Parameters["LightPos"].SetValue(sLight.Position);
  219.             spotLight.Parameters["LightPow"].SetValue(sLight.Power);
  220.             spotLight.Parameters["Radius"].SetValue(sLight.radius);
  221.             spotLight.Parameters["OuterMinAngle"].SetValue(lightDirAngle - (sLight.outerAngle / 2f));
  222.             spotLight.Parameters["OuterMaxAngle"].SetValue(lightDirAngle + (sLight.outerAngle / 2f));
  223.             spotLight.Parameters["CenterAngle"].SetValue(lightDirAngle);
  224.             spotLight.Parameters["HalfInnerArc"].SetValue(sLight.innerAngle / 2f);
  225.             spotLight.Parameters["HalfOuterArc"].SetValue(sLight.outerAngle / 2f);
  226.             spotLight.Parameters["AngleBias"].SetValue(angleBias);
  227.  
  228.             spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Opaque, SamplerState.PointClamp, null, null, spotLight);
  229.             spriteBatch.Draw(occlusionMap, fullScreen, Color.White);
  230.             spriteBatch.End();
  231.         }
  232.  
  233.         private void CreateLightMap(PointLight pLight)
  234.         {
  235.             graphics.GraphicsDevice.SetRenderTarget(postProcessTarget);
  236.             graphics.GraphicsDevice.Clear(Color.Black);
  237.  
  238.             //Set params
  239.             pointLight.Parameters["LightPos"].SetValue(pLight.Position);
  240.             pointLight.Parameters["LightPow"].SetValue(pLight.Power);
  241.             pointLight.Parameters["Radius"].SetValue(pLight.radius);
  242.  
  243.             spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Opaque, SamplerState.PointClamp, null, null, pointLight);
  244.             spriteBatch.Draw(occlusionMap, fullScreen, Color.White);
  245.             spriteBatch.End();
  246.         }
  247.  
  248.         private void BlurLightMaps(Light light)
  249.         {
  250.             graphics.GraphicsDevice.SetRenderTarget(horizontalBlurTarget);
  251.             graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
  252.  
  253.             //Set some params
  254.             horizontalBlur.Parameters["LightPos"].SetValue(light.Position);
  255.             horizontalBlur.Parameters["Radius"].SetValue(light.radius);
  256.  
  257.             spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Opaque, null, null, null, horizontalBlur);
  258.             spriteBatch.Draw(postProcessTarget, fullScreen, Color.White);
  259.             spriteBatch.End();
  260.  
  261.             graphics.GraphicsDevice.SetRenderTarget(verticalBlurTarget);
  262.             graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
  263.  
  264.             //Set some more params
  265.             verticalBlur.Parameters["LightPos"].SetValue(light.Position);
  266.             verticalBlur.Parameters["Radius"].SetValue(light.radius);
  267.  
  268.             spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Opaque, null, null, null, verticalBlur);
  269.             spriteBatch.Draw(horizontalBlurTarget, fullScreen, Color.White);
  270.             spriteBatch.End();
  271.         }
  272.  
  273.         private void AccumulateLightMaps(Light light)
  274.         {
  275.             graphics.GraphicsDevice.SetRenderTarget(lightMap);
  276.  
  277.             spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Additive, null, null, null);
  278.             spriteBatch.Draw(verticalBlurTarget, fullScreen, light.color);
  279.             spriteBatch.End();
  280.         }
  281.  
  282.         private void RenderFinalScene()
  283.         {
  284.             graphics.GraphicsDevice.SetRenderTarget(null);
  285.             graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
  286.  
  287.             graphics.GraphicsDevice.Textures[1] = lightMap;
  288.             lightBlend.Parameters["MinLight"].SetValue(minLight);
  289.  
  290.             spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, null, null, lightBlend);
  291.             spriteBatch.Draw(backBufferCache, fullScreen, Color.White);
  292.             spriteBatch.End();
  293.             graphics.GraphicsDevice.Textures[1] = null;
  294.  
  295.             spriteBatch.Begin();
  296.             spriteBatch.Draw(midGroundTarget, fullScreen, Color.White);
  297.             spriteBatch.End();
  298.         }
  299.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement