Advertisement
Guest User

Untitled

a guest
Jun 24th, 2014
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
  2. {
  3. base.Draw(gameTime, spriteBatch);
  4.  
  5. GraphicsDevice.SetRenderTarget(renderTargetA);
  6. GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1.0f, 0);
  7.  
  8. Material material = new Material();
  9. material.DiffuseColor = Color.Red;
  10. material.AmbientColor = Color.Red;
  11. material.AmbientIntensity = 0.2f;
  12. material.SpecularColor = Color.White;
  13. material.SpecularIntensity = 2.0f;
  14. material.SpecularPower = 25.0f;
  15.  
  16. #region Billboard
  17. GraphicsDevice.DepthStencilState = DepthStencilState.None;
  18.  
  19. Matrix billboardMatrix = Matrix.CreateBillboard(lightPositions[0], camera.Eye, camera.Up, camera.Focus - camera.Eye);
  20.  
  21. this.camera.SetEffectParameters(billboard);
  22. billboard.Parameters["World"].SetValue(world * billboardMatrix);
  23. billboard.Parameters["Tex"].SetValue(lightTexture);
  24. billboard.CurrentTechnique = billboard.Techniques["Technique1"];
  25.  
  26. foreach (EffectPass pass in billboard.CurrentTechnique.Passes)
  27. {
  28. pass.Apply();
  29. this.GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionNormalTexture>(PrimitiveType.TriangleList, lightQuad.QuadVertices, 0, lightQuad.QuadVertices.Length, lightQuad.QuadIndices, 0, lightQuad.QuadIndices.Length / 3);
  30. }
  31.  
  32. GraphicsDevice.DepthStencilState = DepthStencilState.Default;
  33. #endregion
  34.  
  35. #region OcclusionMap
  36. Effect tempEffect;
  37. foreach (ModelData m in models)
  38. {
  39. tempEffect = m.Model.Meshes[0].MeshParts[0].Effect;
  40. m.Model.Meshes[0].MeshParts[0].Effect = occlusion;
  41.  
  42. ModelMesh mesh = m.Model.Meshes[0];
  43.  
  44. occlusion.CurrentTechnique = occlusion.Techniques["Technique1"];
  45. this.camera.SetEffectParameters(occlusion);
  46. occlusion.Parameters["World"].SetValue(world * m.Transform);
  47.  
  48. mesh.Draw();
  49.  
  50. m.Model.Meshes[0].MeshParts[0].Effect = tempEffect;
  51. }
  52. #endregion
  53.  
  54. #region PostProcessing
  55. GraphicsDevice.SetRenderTarget(null);
  56. GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1.0f, 0);
  57.  
  58. Matrix projection = Matrix.CreateOrthographicOffCenter(0,
  59. GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, 0, 0, 1);
  60. Matrix halfPixelOffset = Matrix.CreateTranslation(-0.5f, -0.5f, 0);
  61. postProcessing.Parameters["MatrixTransform"].SetValue(halfPixelOffset * projection);
  62.  
  63. postProcessing.CurrentTechnique = postProcessing.Techniques["Godrays"];
  64. Vector3 lightPosition = Vector3.Transform(lightPositions[0], world * camera.ViewMatrix * camera.ProjectionMatrix);
  65. postProcessing.Parameters["lightPosition"].SetValue(new Vector2(lightPosition.X, lightPosition.Y));
  66. postProcessing.Parameters["Tex"].SetValue(renderTargetA);
  67. postProcessing.Parameters["exposure"].SetValue(0.2f);
  68. postProcessing.Parameters["decay"].SetValue(0.999f);
  69. postProcessing.Parameters["weight"].SetValue(0.2f);
  70. postProcessing.Parameters["density"].SetValue(0.9f);
  71.  
  72. spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Additive, SamplerState.PointWrap, DepthStencilState.Default, RasterizerState.CullNone, postProcessing);
  73. spriteBatch.Draw(renderTargetA, new Rectangle(spriteBatch.GraphicsDevice.Viewport.X, spriteBatch.GraphicsDevice.Viewport.Y, spriteBatch.GraphicsDevice.Viewport.Width, spriteBatch.GraphicsDevice.Viewport.Height), Color.White);
  74. spriteBatch.End();
  75.  
  76. GraphicsDevice.BlendState = BlendState.Opaque;
  77. GraphicsDevice.DepthStencilState = DepthStencilState.Default;
  78. GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;
  79. #endregion
  80.  
  81. #region BlinnPhong
  82. GraphicsDevice.SetRenderTarget(renderTargetB);
  83. GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1.0f, 0);
  84.  
  85. foreach (ModelData m in models)
  86. {
  87. ModelMesh mesh = m.Model.Meshes[0];
  88. Effect e = mesh.Effects[0];
  89.  
  90. e.CurrentTechnique = e.Techniques[m.Technique];
  91. material.SetEffectParameters(e);
  92. this.camera.SetEffectParameters(e);
  93. e.Parameters["World"].SetValue(world * m.Transform);
  94. e.Parameters["WorldInverseTransposed"].SetValue(Matrix.Transpose(Matrix.Invert(world * m.Transform)));
  95. e.Parameters["CameraEye"].SetValue(new Vector4(this.camera.Eye, 0));
  96. // TODO: LightSource Color + Intensity
  97. e.Parameters["LightSources"].SetValue(lightPositions);
  98.  
  99. mesh.Draw();
  100. }
  101. #endregion
  102.  
  103. #region Drawing
  104. GraphicsDevice.SetRenderTarget(null);
  105. GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1.0f, 0);
  106.  
  107. spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Additive);
  108. spriteBatch.Draw(renderTargetB, new Rectangle(spriteBatch.GraphicsDevice.Viewport.X, spriteBatch.GraphicsDevice.Viewport.Y, spriteBatch.GraphicsDevice.Viewport.Width, spriteBatch.GraphicsDevice.Viewport.Height), Color.White);
  109. spriteBatch.End();
  110.  
  111. GraphicsDevice.BlendState = BlendState.Opaque;
  112. GraphicsDevice.DepthStencilState = DepthStencilState.Default;
  113. GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;
  114. #endregion
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement