1

私はManagedDirectX 2.0とC#を使用しており、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);

これは、RenderTextureと呼ばれるTextureオブジェクトにアタッチされているSurfaceRenderSurfaceにレンダリングされます。

次に、次のコードを呼び出してサーフェスを画面にレンダリングし、レンダリングされたテクスチャに2番目のシェーダー「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();

ただし、私が見るのは、テクスチャにレンダリングされた元のレンダリングされたシーンだけですが、2番目のシェーダーによって変更されていません。

重要な場合に備えて、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 に答える