Beispiel #1
0
        /// <summary>
        /// Show shadows with help of our blur map shader
        /// </summary>
        public void ShowShadows()
        {
            // Only apply post screen blur if texture is valid and effect are valid
            if (blurMapTexture == null ||
                Valid == false ||
                // If the shadow scene map is not yet filled, there is no point
                // continuing here ...
                blurMapTexture.XnaTexture == null)
            {
                return;
            }

            // Don't use or write to the z buffer
            BaseGame.Device.DepthStencilState = DepthStencilState.None;

            // Make sure we clamp everything to 0-1
            BaseGame.Device.SamplerStates[0] = SamplerState.LinearClamp;

            // Restore back buffer as render target
            //not required: BaseGame.ResetRenderTarget(false);

            if (blurMap != null)
            {
                blurMap.SetValue(blurMapTexture.XnaTexture);
            }

            effect.CurrentTechnique = effect.Techniques["ScreenAdvancedBlur20"];

            // We must have exactly 2 passes!
            if (effect.CurrentTechnique.Passes.Count != 2)
            {
                throw new InvalidOperationException(
                          "This shader should have exactly 2 passes!");
            }

            // Render second pass

            // Use ZeroSourceBlend alpha mode for the final result
            BaseGame.Device.BlendState = ZeroSourceBlend;

            /*BaseGame.Device.RenderState.AlphaBlendEnable = true;
             * BaseGame.Device.RenderState.AlphaBlendOperation = BlendFunction.Add;
             * BaseGame.Device.RenderState.SourceBlend = Blend.Zero;
             * BaseGame.Device.RenderState.DestinationBlend = Blend.SourceColor;*/

            EffectPass effectPass = effect.CurrentTechnique.Passes[1];

            effectPass.Apply();
            VBScreenHelper.Render();

            // Restore z buffer state
            BaseGame.Device.DepthStencilState = DepthStencilState.Default;
            // Set u/v addressing back to wrap
            BaseGame.Device.SamplerStates[0] = SamplerState.LinearWrap;
            // Restore normal alpha blending
            //BaseGame.Device.RenderState.BlendFunction = BlendFunction.Add;
            BaseGame.SetCurrentAlphaMode(BaseGame.AlphaMode.Default);
        }
Beispiel #2
0
        /// <summary>
        /// Show shadows with help of our blur map shader
        /// </summary>
        public void RenderShadows()
        {
            // Only apply post screen blur if texture is valid and effect are valid
            if (sceneMapTexture == null ||
                Valid == false ||
                // If the shadow scene map is not yet filled, there is no point
                // continuing here ...
                sceneMapTexture.XnaTexture == null)
            {
                return;
            }

            // Don't use or write to the z buffer
            BaseGame.Device.DepthStencilState = DepthStencilState.None;
            // Disable alpha for the first pass
            BaseGame.Device.BlendState = BlendState.Opaque;

            if (windowSize != null)
            {
                windowSize.SetValue(
                    new float[] { sceneMapTexture.Width, sceneMapTexture.Height });
            }
            if (sceneMap != null)
            {
                sceneMap.SetValue(sceneMapTexture.XnaTexture);
            }

            effect.CurrentTechnique = effect.Techniques["ScreenAdvancedBlur20"];

            // We must have exactly 2 passes!
            if (effect.CurrentTechnique.Passes.Count != 2)
            {
                throw new InvalidOperationException(
                          "This shader should have exactly 2 passes!");
            }

            // Just start pass 0

            blurMapTexture.SetRenderTarget();

            EffectPass effectPass = effect.CurrentTechnique.Passes[0];

            effectPass.Apply();
            VBScreenHelper.Render();


            blurMapTexture.Resolve();
            BaseGame.ResetRenderTarget(false);

            // Restore z buffer state
            BaseGame.Device.DepthStencilState = DepthStencilState.Default;
            // Set u/v addressing back to wrap
            BaseGame.Device.SamplerStates[0] = SamplerState.LinearWrap;
            // Restore normal alpha blending
            //BaseGame.Device.RenderState.BlendFunction = BlendFunction.Add;
            BaseGame.SetCurrentAlphaMode(BaseGame.AlphaMode.Default);
        }
        /// <summary>
        /// Execute shaders and show result on screen, Start(..) must have been
        /// called before and the scene should be rendered to sceneMapTexture.
        /// </summary>
        public virtual 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();

            // 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;

            if (windowSize != null)
            {
                windowSize.SetValue(
                    new float[] { sceneMapTexture.Width, sceneMapTexture.Height });
            }
            if (sceneMap != null)
            {
                sceneMap.SetValue(sceneMapTexture.XnaTexture);
            }

            if (timer != null)
            {
                // Add a little offset to prevent first effect.
                timer.SetValue(BaseGame.TotalTime + 0.75f);
            }

            effect.CurrentTechnique = effect.Techniques["ScreenGlow20"];

            // We must have exactly 4 passes!
            if (effect.CurrentTechnique.Passes.Count != 4)
            {
                throw new InvalidOperationException(
                          "This shader should have exactly 4 passes!");
            }

            try
            {
                effect.Begin();
                for (int pass = 0; pass < effect.CurrentTechnique.Passes.Count; pass++)
                {
                    if (pass == 0)
                    {
                        downsampleMapTexture.SetRenderTarget();
                    }
                    else if (pass == 1)
                    {
                        blurMap1Texture.SetRenderTarget();
                    }
                    else if (pass == 2)
                    {
                        blurMap2Texture.SetRenderTarget();
                    }
                    else
                    {
                        // Do a full reset back to the back buffer
                        BaseGame.ResetRenderTarget(true);
                    }

                    EffectPass effectPass = effect.CurrentTechnique.Passes[pass];
                    effectPass.Begin();
                    VBScreenHelper.Render();
                    effectPass.End();

                    if (pass == 0)
                    {
                        downsampleMapTexture.Resolve();
                        if (downsampleMap != null)
                        {
                            downsampleMap.SetValue(downsampleMapTexture.XnaTexture);
                        }
                        effect.CommitChanges();
                    }
                    else if (pass == 1)
                    {
                        blurMap1Texture.Resolve();
                        if (blurMap1 != null)
                        {
                            blurMap1.SetValue(blurMap1Texture.XnaTexture);
                        }
                        effect.CommitChanges();
                    }
                    else if (pass == 2)
                    {
                        blurMap2Texture.Resolve();
                        if (blurMap2 != null)
                        {
                            blurMap2.SetValue(blurMap2Texture.XnaTexture);
                        }
                        effect.CommitChanges();
                    }
                }
            }
            finally
            {
                effect.End();

                // Restore z buffer state
                BaseGame.Device.RenderState.DepthBufferEnable      = true;
                BaseGame.Device.RenderState.DepthBufferWriteEnable = true;
            }
        }
Beispiel #4
0
        /// <summary>
        /// Execute shaders and show result on screen, Start(..) must have been
        /// called before and the scene should be rendered to sceneMapTexture.
        /// </summary>
        public override void Show()
        {
            // Only apply post screen glow if texture is valid and effect is valid
            if (sceneMapTexture == null ||
                effect == null ||
                started == false)
            {
                return;
            }

            started = false;

            // Resolve sceneMapTexture render target for Xbox360 support
            sceneMapTexture.Resolve();

            // 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.
            //Update: allow writing to alpha!
            BaseGame.Device.RenderState.AlphaBlendEnable = false;

            if (windowSize != null)
            {
                windowSize.SetValue(
                    new float[] { sceneMapTexture.Width, sceneMapTexture.Height });
            }
            if (sceneMap != null)
            {
                sceneMap.SetValue(sceneMapTexture.XnaTexture);
            }

            RadialBlurScaleFactor =
                // 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.0025f + RacingGameManager.Player.Speed * 0.005f /
                  Player.DefaultMaxSpeed);

            effect.CurrentTechnique = effect.Techniques["ScreenGlow20"];

            // We must have exactly 5 passes!
            if (effect.CurrentTechnique.Passes.Count != 5)
            {
                throw new InvalidOperationException(
                          "This shader should have exactly 5 passes!");
            }

            try
            {
                effect.Begin();
                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
                        BaseGame.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();
                        if (radialSceneMap != null)
                        {
                            radialSceneMap.SetValue(radialSceneMapTexture.XnaTexture);
                        }
                        effect.CommitChanges();
                    }
                    else if (pass == 1)
                    {
                        downsampleMapTexture.Resolve();
                        if (downsampleMap != null)
                        {
                            downsampleMap.SetValue(downsampleMapTexture.XnaTexture);
                        }
                        effect.CommitChanges();
                    }
                    else if (pass == 2)
                    {
                        blurMap1Texture.Resolve();
                        if (blurMap1 != null)
                        {
                            blurMap1.SetValue(blurMap1Texture.XnaTexture);
                        }
                        effect.CommitChanges();
                    }
                    else if (pass == 3)
                    {
                        blurMap2Texture.Resolve();
                        if (blurMap2 != null)
                        {
                            blurMap2.SetValue(blurMap2Texture.XnaTexture);
                        }
                        effect.CommitChanges();
                    }
                }
            }
            finally
            {
                effect.End();

                // Restore z buffer state
                BaseGame.Device.RenderState.DepthBufferEnable      = true;
                BaseGame.Device.RenderState.DepthBufferWriteEnable = true;
            }
        }