} // Show() #endregion #region Unit Testing #if DEBUG /// <summary> /// Test post screen glow /// </summary> public static void TestPostScreenGlow() { Model testModel = null; PostScreenGlow glowShader = null; TestGame.Start("TestPostScreenGlow", delegate { testModel = new Model("Asteroid2"); glowShader = new PostScreenGlow(); }, delegate { //Thread.Sleep(10); glowShader.Start(); BaseGame.skyCube.RenderSky(); testModel.Render(Vector3.Zero); BaseGame.MeshRenderManager.Render(); if (Input.Keyboard.IsKeyDown(Keys.LeftAlt) == false && Input.GamePadAPressed == false) { glowShader.Show(); } else { // Resolve first glowShader.sceneMapTexture.Resolve(false); started = false; // Reset background buffer RenderToTexture.ResetRenderTarget(true); // Just show scene map glowShader.sceneMapTexture.RenderOnScreen(BaseGame.ResolutionRect); } // else TextureFont.WriteText(2, 30, "Press left alt or A to just show the unchanged screen."); TextureFont.WriteText(2, 60, "Press space or B to see all menu post screen render passes."); //*TODO if (Input.Keyboard.IsKeyDown(Keys.Space) || // == false) Input.GamePadBPressed) { glowShader.sceneMapTexture.RenderOnScreen( new Rectangle(10, 10, 256, 256)); glowShader.downsampleMapTexture.RenderOnScreen( new Rectangle(10 + 256 + 10, 10, 256, 256)); glowShader.blurMap1Texture.RenderOnScreen( new Rectangle(10 + 256 + 10 + 256 + 10, 10, 256, 256)); glowShader.blurMap2Texture.RenderOnScreen( new Rectangle(10, 10 + 256 + 10, 256, 256)); } // if (Input.Keyboard.IsKeyDown) }); } // TestPostScreenGlow()
} // Start() #endregion #region Show /// <summary> /// Execute shaders and show result on screen, Start(..) must have been /// called before and the scene should be rendered to sceneMapTexture. /// </summary> public void Show() { // Only apply post screen glow if texture is valid and effect is valid if (sceneMapTexture == null || Valid == false || started == false) { return; } started = false; // Resolve sceneMapTexture render target for Xbox360 support sceneMapTexture.Resolve(true); try { // Don't use or write to the z buffer BaseGame.Device.RenderState.DepthBufferEnable = false; BaseGame.Device.RenderState.DepthBufferWriteEnable = false; // Also don't use any kind of blending. BaseGame.Device.RenderState.AlphaBlendEnable = false; //unused: BaseGame.Device.RenderState.Lighting = false; if (windowSize != null) { windowSize.SetValue(new float[] { sceneMapTexture.Width, sceneMapTexture.Height }); } if (sceneMap != null) { sceneMap.SetValue(sceneMapTexture.XnaTexture); } if (downsampleMap != null) { downsampleMap.SetValue(downsampleMapTexture.XnaTexture); } if (blurMap1 != null) { blurMap1.SetValue(blurMap1Texture.XnaTexture); } if (blurMap2 != null) { blurMap2.SetValue(blurMap2Texture.XnaTexture); } if (radialSceneMap != null) { radialSceneMap.SetValue(radialSceneMapTexture.XnaTexture); } RadialBlurScaleFactor = //-0.0125f; // Warning: To big values will make the motion blur look to // stepy (we see each step and thats not good). -0.02 should be max. -(0.005f + Player.Speed * 0.015f); if (BaseGame.UsePS20) { effect.CurrentTechnique = effect.Techniques["ScreenGlow20"]; } else { effect.CurrentTechnique = effect.Techniques["ScreenGlow"]; } // We must have exactly 5 passes! if (effect.CurrentTechnique.Passes.Count != 5) { throw new Exception("This shader should have exactly 5 passes!"); } effect.Begin(); //SaveStateMode.None); for (int pass = 0; pass < effect.CurrentTechnique.Passes.Count; pass++) { if (pass == 0) { radialSceneMapTexture.SetRenderTarget(); } else if (pass == 1) { downsampleMapTexture.SetRenderTarget(); } else if (pass == 2) { blurMap1Texture.SetRenderTarget(); } else if (pass == 3) { blurMap2Texture.SetRenderTarget(); } else { // Do a full reset back to the back buffer RenderToTexture.ResetRenderTarget(true); } EffectPass effectPass = effect.CurrentTechnique.Passes[pass]; effectPass.Begin(); // For first effect we use radial blur, draw it with a grid // to get cooler results (more blur at borders than in middle). if (pass == 0) { VBScreenHelper.Render10x10Grid(); } else { VBScreenHelper.Render(); } effectPass.End(); if (pass == 0) { radialSceneMapTexture.Resolve(false); if (radialSceneMap != null) { radialSceneMap.SetValue(radialSceneMapTexture.XnaTexture); } effect.CommitChanges(); } // if else if (pass == 1) { downsampleMapTexture.Resolve(false); if (downsampleMap != null) { downsampleMap.SetValue(downsampleMapTexture.XnaTexture); } effect.CommitChanges(); } // if else if (pass == 2) { blurMap1Texture.Resolve(false); if (blurMap1 != null) { blurMap1.SetValue(blurMap1Texture.XnaTexture); } effect.CommitChanges(); } // else if else if (pass == 3) { blurMap2Texture.Resolve(false); if (blurMap2 != null) { blurMap2.SetValue(blurMap2Texture.XnaTexture); } effect.CommitChanges(); } // else if } // for (pass, <, ++) } // try catch (Exception ex) { // Make effect invalid, continue rendering without this // post screen shader. effect = null; RenderToTexture.ResetRenderTarget(true); #if DEBUG throw ex; #else Log.Write("Failed to render post screen shader " + Filename + ": " + ex.ToString()); #endif } // catch finally { if (effect != null) { effect.End(); } // Restore z buffer state BaseGame.Device.RenderState.DepthBufferEnable = true; BaseGame.Device.RenderState.DepthBufferWriteEnable = true; } // finally } // Show()