Esempio n. 1
0
        /// <summary>
        ///  见接口
        /// </summary>
        /// <param name="renderer"></param>
        /// <param name="screenTarget"></param>
        public void RenderFullScene(ISceneRenderer renderer, RenderTarget screenTarget, RenderMode mode)
        {
            Microsoft.Xna.Framework.Input.KeyboardState ks = Microsoft.Xna.Framework.Input.Keyboard.GetState();
            if (ks.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.C) &&
                lastState.IsKeyUp(Microsoft.Xna.Framework.Input.Keys.C))
            {
                testMode++;
                if (testMode > TestMode.Blurred)
                {
                    testMode = TestMode.Final;
                }
            }
            lastState = ks;

            if (testMode == TestMode.Original)
            {
                renderer.RenderScene(screenTarget, RenderMode.Final);
                return;
            }
            if (testMode == TestMode.Normal)
            {
                renderer.RenderScene(screenTarget, RenderMode.DeferredNormal);
                return;
            }

            renderer.RenderScene(nrmDepthBuffer, RenderMode.DeferredNormal);

            renderer.RenderScene(colorBuffer, RenderMode.Final);


            Viewport vp = renderSys.Viewport;

            ShaderSamplerState sampler1;

            sampler1.AddressU      = TextureAddressMode.Clamp;
            sampler1.AddressV      = TextureAddressMode.Clamp;
            sampler1.AddressW      = TextureAddressMode.Clamp;
            sampler1.BorderColor   = ColorValue.Transparent;
            sampler1.MagFilter     = TextureFilter.Point;
            sampler1.MaxAnisotropy = 0;
            sampler1.MaxMipLevel   = 0;
            sampler1.MinFilter     = TextureFilter.Point;
            sampler1.MipFilter     = TextureFilter.None;
            sampler1.MipMapLODBias = 0;


            ShaderSamplerState sampler2 = sampler1;

            sampler2.MagFilter = TextureFilter.Linear;
            sampler2.MinFilter = TextureFilter.Linear;

            #region 边缘合成
            renderSys.SetRenderTarget(0, testMode == TestMode.Edge ? screenTarget : edgeResultBuffer);
            edgeEff.Begin();

            edgeEff.SetSamplerStateDirect(0, ref sampler1);
            edgeEff.SetSamplerStateDirect(1, ref sampler1);

            edgeEff.SetTextureDirect(0, nrmDepthBuffer.GetColorBufferTexture());
            edgeEff.SetTextureDirect(1, testMode == TestMode.Edge ? whitePixel : colorBuffer.GetColorBufferTexture());

            Vector2 nrmBufSize = new Vector2(vp.Width, vp.Height);
            edgeEff.SetValue("normalBufferSize", ref nrmBufSize);
            DrawBigQuad();
            edgeEff.End();
            #endregion

            if (testMode == TestMode.Edge)
            {
                return;
            }
            if (testMode == TestMode.Depth)
            {
                renderSys.SetRenderTarget(0, screenTarget);
                depthViewEff.Begin();
                depthViewEff.SetSamplerStateDirect(0, ref sampler1);
                depthViewEff.SetTextureDirect(0, nrmDepthBuffer.GetColorBufferTexture());
                DrawBigQuad();
                depthViewEff.End();
                return;
            }
            #region 高斯X
            renderSys.SetRenderTarget(0, blurredRt1);

            gaussBlur.Begin();

            gaussBlur.SetSamplerStateDirect(0, ref sampler1);
            gaussBlur.SetTextureDirect(0, edgeResultBuffer.GetColorBufferTexture());

            for (int i = 0; i < SampleCount; i++)
            {
                gaussBlur.SetValueDirect(i, ref guassFilter.SampleOffsetsX[i]);
                gaussBlur.SetValueDirect(i + 15, guassFilter.SampleWeights[i]);
            }

            DrawSmallQuad();

            gaussBlur.End();
            #endregion

            #region 高斯Y

            renderSys.SetRenderTarget(0, testMode == TestMode.Blurred ? screenTarget : blurredRt2);
            gaussBlur.Begin();

            gaussBlur.SetSamplerStateDirect(0, ref sampler1);
            gaussBlur.SetTextureDirect(0, blurredRt1.GetColorBufferTexture());

            for (int i = 0; i < SampleCount; i++)
            {
                gaussBlur.SetValueDirect(i, ref guassFilter.SampleOffsetsY[i]);
                gaussBlur.SetValueDirect(i + 15, guassFilter.SampleWeights[i]);
            }
            if (testMode == TestMode.Blurred)
            {
                DrawBigQuad();
            }
            else
            {
                DrawSmallQuad();
            }

            gaussBlur.End();


            #endregion

            if (testMode == TestMode.Blurred)
            {
                return;
            }

            #region DOF合成

            renderSys.SetRenderTarget(0, screenTarget);

            compEff.Begin();

            compEff.SetSamplerStateDirect(0, ref sampler1);
            compEff.SetSamplerStateDirect(1, ref sampler2);
            compEff.SetSamplerStateDirect(2, ref sampler2);

            compEff.SetTextureDirect(0, edgeResultBuffer.GetColorBufferTexture());
            compEff.SetTextureDirect(1, blurredRt2.GetColorBufferTexture());
            compEff.SetTextureDirect(2, nrmDepthBuffer.GetColorBufferTexture());

            //if (camera != null)
            //{
            //    float focNear = (camera.Position.Length() - PlanetEarth.PlanetRadius) / camera.FarPlane;
            //    compEff.SetValue("FocusNear", focNear);
            //}
            //else
            //{
            //    compEff.SetValue("FocusNear", 0.3f);
            //}

            renderSys.RenderStates.AlphaBlendEnable = true;
            renderSys.RenderStates.SourceBlend      = Blend.SourceAlpha;
            renderSys.RenderStates.DestinationBlend = Blend.InverseSourceAlpha;
            renderSys.RenderStates.BlendOperation   = BlendFunction.Add;

            DrawBigQuad();

            compEff.End();
            #endregion
        }