1

我正在使用带有 C# 的 Managed Direct X 2.0,并且我正在尝试将片段着色器应用于通过使用 RenderToSurface 帮助器类将屏幕渲染到纹理而构建的纹理。

我用来执行此操作的代码是:

RtsHelper.BeginScene(RenderSurface);
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.White, 1.0f, 0);
//pre-render shader setup
preProc.Begin(FX.None);
    preProc.BeginPass(0);
        //mesh drawing
        mesh.DrawSubset(j);
        preProc.CommitChanges();
    preProc.EndPass();
preProc.End();
RtsHelper.EndScene(Filter.None);

渲染到我的 Surface,RenderSurface,它附加到一个名为 RenderTexture 的纹理对象

然后我调用以下代码将表面渲染到屏幕上,将第二个着色器“PostProc”应用于渲染的纹理。此着色器基于每个像素组合颜色值并将场景转换为灰度。我在这里学习教程:http ://rbwhitaker.wikidot.com/post-processing-effects

device.BeginScene();
{
    using (Sprite sprite = new Sprite(device))
    {
        sprite.Begin(SpriteFlags.DoNotSaveState);
            postProc.Begin(FX.None);
                postProc.BeginPass(0);
                    sprite.Draw(RenderTexture, new Rectangle(0, 0, WINDOWWIDTH, WINDOWHEIGHT), new Vector3(0, 0, 0), new Vector3(0, 0, 0), Color.White);
                    postProc.CommitChanges();
                postProc.EndPass();
            postProc.End();
        sprite.End();
    }

}
device.EndScene();
device.Present();
this.Invalidate();

然而,我看到的只是原始渲染场景,渲染到纹理,但未被第二个着色器修改。

FX文件在下面,以防它很重要。

//------------------------------ TEXTURE PROPERTIES ----------------------------
// This is the texture that Sprite will try to set before drawing
texture ScreenTexture;

// Our sampler for the texture, which is just going to be pretty simple
sampler TextureSampler = sampler_state
    {
        Texture = <ScreenTexture>;
    };

//------------------------ PIXEL SHADER ----------------------------------------
// This pixel shader will simply look up the color of the texture at the
// requested point, and turns it into a shade of gray
float4 PixelShaderFunction(float2 TextureCoordinate : TEXCOORD0) : COLOR0
{
    float4 color = tex2D(TextureSampler, TextureCoordinate);

    float value = (color.r + color.g + color.b) / 3; 
    color.r = value;
    color.g = value;
    color.b = value;

    return color;
}

//-------------------------- TECHNIQUES ----------------------------------------
// This technique is pretty simple - only one pass, and only a pixel shader
technique BlackAndWhite
{
    pass Pass1
    {
        PixelShader = compile ps_1_1 PixelShaderFunction();
    }
}
4

1 回答 1

0

解决它。为后处理器着色器初始化使用了错误的标志

曾是:

sprite.Begin(SpriteFlags.DoNotSaveState);
    postProc.Begin(FX.None);

应该:

sprite.Begin(SpriteFlags.DoNotSaveState);
    postProc.Begin(FX.DoNotSaveState);
于 2011-01-23T14:44:26.210 回答