SHOW:
|
|
- or go back to the newest paste.
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["Tex"].SetValue(renderTarget); | |
55 | postProcessing.Parameters["exposure"].SetValue(0.5f); | |
56 | postProcessing.Parameters["decay"].SetValue(0.5f); | |
57 | postProcessing.Parameters["weight"].SetValue(0.5f); | |
58 | postProcessing.Parameters["density"].SetValue(0.5f); | |
59 | ||
60 | //Draw the renderTarget to the screen at the size of the viewport so it fits the screen | |
61 | spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Additive, SamplerState.PointWrap, DepthStencilState.Default, RasterizerState.CullNone, postProcessing); | |
62 | spriteBatch.Draw(renderTarget, new Rectangle(spriteBatch.GraphicsDevice.Viewport.X, spriteBatch.GraphicsDevice.Viewport.Y, spriteBatch.GraphicsDevice.Viewport.Width, spriteBatch.GraphicsDevice.Viewport.Height), Color.White); | |
63 | spriteBatch.End(); | |
64 | ||
65 | //Restore some of the changes spriteBatch.Begin() made to the graphics device so the 3D render won't break | |
66 | GraphicsDevice.BlendState = BlendState.Opaque; | |
67 | GraphicsDevice.DepthStencilState = DepthStencilState.Default; | |
68 | GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap; | |
69 | } |