Advertisement
Guest User

God Rays [bugged]

a guest
Jun 22nd, 2014
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. Notes: -
  2.  
  3. public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
  4. {
  5. base.Draw(gameTime, spriteBatch);
  6.  
  7. //Set the rendertarget to draw the Blinn-Phong shaded scene to
  8. GraphicsDevice.SetRenderTarget(renderTarget);
  9. GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1.0f, 0);
  10.  
  11. Material material = new Material();
  12. material.DiffuseColor = Color.Red;
  13. material.AmbientColor = Color.Red;
  14. material.AmbientIntensity = 0.2f;
  15. material.SpecularColor = Color.White;
  16. material.SpecularIntensity = 2.0f;
  17. material.SpecularPower = 25.0f;
  18.  
  19. //Draw all the models
  20. foreach (ModelData m in models)
  21. {
  22. ModelMesh mesh = m.Model.Meshes[0];
  23. Effect e = mesh.Effects[0];
  24.  
  25. e.CurrentTechnique = e.Techniques[m.Technique];
  26. material.SetEffectParameters(e);
  27. this.camera.SetEffectParameters(e);
  28. e.Parameters["World"].SetValue(world * m.Transform);
  29. e.Parameters["WorldInverseTransposed"].SetValue(Matrix.Transpose(Matrix.Invert(world * m.Transform)));
  30. e.Parameters["CameraEye"].SetValue(new Vector4(this.camera.Eye, 0));
  31. // TODO: LightSource Color + Intensity
  32. e.Parameters["LightSources"].SetValue(lightPositions);
  33.  
  34. mesh.Draw();
  35. }
  36.  
  37. //Restore the rendertarget to the backbuffer and clear it.
  38. GraphicsDevice.SetRenderTarget(null);
  39. GraphicsDevice.Clear(Color.Black);
  40.  
  41. //Pass the standard variable to the spriteBatch vertex shader.
  42. //The vertex shader isn't used (for as far as I can tell at least) but because the standard
  43. //vertex shader for spriteBatch is compiled in 2_0 and the God Ray shader in 3_0 I had to implement it
  44. //manually in order to compile it in 3_0.
  45. Matrix projection = Matrix.CreateOrthographicOffCenter(0,
  46. GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, 0, 0, 1);
  47. Matrix halfPixelOffset = Matrix.CreateTranslation(-0.5f, -0.5f, 0);
  48. postProcessing.Parameters["MatrixTransform"].SetValue(halfPixelOffset * projection);
  49.  
  50. //Setup all the required data for the shader
  51. postProcessing.CurrentTechnique = postProcessing.Techniques["Technique1"];
  52. Vector3 lightPosition = Vector3.Transform(lightPositions[0], world * camera.ViewMatrix * camera.ProjectionMatrix);
  53. postProcessing.Parameters["lightPosition"].SetValue(new Vector2(lightPosition.X, lightPosition.Y));
  54. postProcessing.Parameters["exposure"].SetValue(0.5f);
  55. postProcessing.Parameters["decay"].SetValue(0.5f);
  56. postProcessing.Parameters["weight"].SetValue(0.5f);
  57. postProcessing.Parameters["density"].SetValue(0.5f);
  58.  
  59. //Draw the renderTarget to the screen at the size of the viewport so it fits the screen
  60. spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Additive, SamplerState.PointWrap, DepthStencilState.Default, RasterizerState.CullNone, postProcessing);
  61. spriteBatch.Draw(renderTarget, new Rectangle(spriteBatch.GraphicsDevice.Viewport.X, spriteBatch.GraphicsDevice.Viewport.Y, spriteBatch.GraphicsDevice.Viewport.Width, spriteBatch.GraphicsDevice.Viewport.Height), Color.White);
  62. spriteBatch.End();
  63.  
  64. //Restore some of the changes spriteBatch.Begin() made to the graphics device so the 3D render won't break
  65. GraphicsDevice.BlendState = BlendState.Opaque;
  66. GraphicsDevice.DepthStencilState = DepthStencilState.Default;
  67. GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement