Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.51 KB | None | 0 0
  1. public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
  2. {
  3. base.Draw(gameTime, spriteBatch);
  4.  
  5. //Set the rendertarget to draw the Blinn-Phong shaded scene to
  6. GraphicsDevice.SetRenderTarget(renderTarget);
  7. GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1.0f, 0);
  8.  
  9. Material material = new Material();
  10. material.DiffuseColor = Color.Red;
  11. material.AmbientColor = Color.Red;
  12. material.AmbientIntensity = 0.2f;
  13. material.SpecularColor = Color.White;
  14. material.SpecularIntensity = 2.0f;
  15. material.SpecularPower = 25.0f;
  16.  
  17. //Draw all the models
  18. foreach (ModelData m in models)
  19. {
  20. ModelMesh mesh = m.Model.Meshes[0];
  21. Effect e = mesh.Effects[0];
  22.  
  23. e.CurrentTechnique = e.Techniques[m.Technique];
  24. material.SetEffectParameters(e);
  25. this.camera.SetEffectParameters(e);
  26. e.Parameters["World"].SetValue(world * m.Transform);
  27. e.Parameters["WorldInverseTransposed"].SetValue(Matrix.Transpose(Matrix.Invert(world * m.Transform)));
  28. e.Parameters["CameraEye"].SetValue(new Vector4(this.camera.Eye, 0));
  29. // TODO: LightSource Color + Intensity
  30. e.Parameters["LightSources"].SetValue(lightPositions);
  31.  
  32. mesh.Draw();
  33. }
  34.  
  35. //Restore the rendertarget to the backbuffer and clear it.
  36. GraphicsDevice.SetRenderTarget(null);
  37. GraphicsDevice.Clear(Color.Black);
  38.  
  39. //Pass the standard variable to the spriteBatch vertex shader.
  40. //The vertex shader isn't used (for as far as I can tell at least) but because the standard
  41. //vertex shader for spriteBatch is compiled in 2_0 and the God Ray shader in 3_0 I had to implement it
  42. //manually in order to compile it in 3_0.
  43. Matrix projection = Matrix.CreateOrthographicOffCenter(0,
  44. GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, 0, 0, 1);
  45. Matrix halfPixelOffset = Matrix.CreateTranslation(-0.5f, -0.5f, 0);
  46. postProcessing.Parameters["MatrixTransform"].SetValue(halfPixelOffset * projection);
  47.  
  48. //Setup all the required data for the shader
  49. postProcessing.CurrentTechnique = postProcessing.Techniques["Technique1"];
  50. Vector3 lightPosition = Vector3.Transform(lightPositions[0], world * camera.ViewMatrix * camera.ProjectionMatrix);
  51. postProcessing.Parameters["lightPosition"].SetValue(new Vector2(lightPosition.X, lightPosition.Y));
  52. postProcessing.Parameters["Tex"].SetValue(renderTarget);
  53. postProcessing.Parameters["exposure"].SetValue(0.5f);
  54. postProcessing.Parameters["decay"].SetValue(0.5f);
  55. postProcessing.Parameters["weight"].SetValue(0.5f);
  56. postProcessing.Parameters["density"].SetValue(0.5f);
  57.  
  58. //Draw the renderTarget to the screen at the size of the viewport so it fits the screen
  59. spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Additive, SamplerState.PointWrap, DepthStencilState.Default, RasterizerState.CullNone, postProcessing);
  60. spriteBatch.Draw(renderTarget, new Rectangle(spriteBatch.GraphicsDevice.Viewport.X, spriteBatch.GraphicsDevice.Viewport.Y, spriteBatch.GraphicsDevice.Viewport.Width, spriteBatch.GraphicsDevice.Viewport.Height), Color.White);
  61. spriteBatch.End();
  62.  
  63. //Restore some of the changes spriteBatch.Begin() made to the graphics device so the 3D render won't break
  64. GraphicsDevice.BlendState = BlendState.Opaque;
  65. GraphicsDevice.DepthStencilState = DepthStencilState.Default;
  66. GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;
  67. }
  68.  
  69. sampler2D renderTarget;
  70.  
  71. #define SAMPLE_AMOUNT 64
  72.  
  73. Texture2D Tex;
  74. float2 lightPosition;
  75. float exposure;
  76. float decay;
  77. float weight;
  78. float density;
  79.  
  80. SamplerState State = sampler_state
  81. {
  82. Texture = <Tex>;
  83. MipFilter = Point;
  84. MinFilter = Linear;
  85. MagFilter = Linear;
  86. AddressU = Wrap;
  87. AddressV = Wrap;
  88. };
  89.  
  90. float4x4 MatrixTransform;
  91.  
  92. //The default vertex shader of the spriteBatch, implemented manually so it can be compiled in 3_0
  93. void SpriteVertexShader(inout float4 color : COLOR0,
  94. inout float2 texCoord : TEXCOORD0,
  95. inout float4 position : SV_Position)
  96. {
  97. position = mul(position, MatrixTransform);
  98. }
  99.  
  100. //Copypasted from the tutorial, only changed some variable names
  101. float4 main(float2 texCoord : TEXCOORD0) : COLOR0
  102. {
  103. // Calculate vector from pixel to light source in screen space.
  104. half2 deltaTexCoord = (texCoord - lightPosition.xy);
  105. // Divide by number of samples and scale by control factor.
  106. deltaTexCoord *= 1.0f / SAMPLE_AMOUNT * density;
  107. // Store initial sample.
  108. half3 color = tex2D(State, texCoord);
  109. // Set up illumination decay factor.
  110. half illuminationDecay = 1.0f;
  111. // Evaluate summation from Equation 3 NUM_SAMPLES iterations.
  112. for (int i = 0; i < SAMPLE_AMOUNT; i++)
  113. {
  114. // Step sample location along ray.
  115. texCoord -= deltaTexCoord;
  116. // Retrieve sample at new location.
  117. half3 sample = tex2D(State, texCoord);
  118. // Apply sample attenuation scale/decay factors.
  119. sample *= illuminationDecay * weight;
  120. // Accumulate combined color.
  121. color += sample;
  122. // Update exponential decay factor.
  123. illuminationDecay *= decay;
  124. }
  125. // Output final color with a further scale control factor.
  126. return float4( color * exposure, 1);
  127. }
  128.  
  129. technique Technique1
  130. {
  131. pass Pass1
  132. {
  133. VertexShader = compile vs_3_0 SpriteVertexShader();
  134. PixelShader = compile ps_3_0 main();
  135. }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement