为什么我的 xna 像素着色器不将此纹理变成蓝色?

发布于 2024-12-15 16:29:12 字数 1343 浏览 1 评论 0原文

我有一个非常简单的像素着色器:

float4 PixelShaderFunction(float2 uv : TEXCOORD0) : COLOR0
{
    return float4(0, 1, 0, 1);
}

technique Technique1
{
    pass Pass1
    {
        PixelShader = compile ps_3_0 PixelShaderFunction();
    }
}

我有一个纹理:

        Vector4[] textureData = new Vector4[width * height];
        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                textureData[y * width + x] = new Vector4(1, 0, 0, 1);
            }
        }
        myTexture = new Texture2D(GraphicsDevice, width, height, false, SurfaceFormat.Vector4);
        myTexture.SetData(textureData);

我用以下代码绘制它:

        spriteBatch.Begin(SpriteSortMode.Texture,
                          BlendState.Additive,
                          SamplerState.PointWrap,
                          DepthStencilState.DepthRead,
                          RasterizerState.CullNone);

        myEffect.CurrentTechnique.Passes[0].Apply();
        spriteBatch.Draw(myTexture, new Rectangle(0, 0, width, height), Color.White);

        spriteBatch.End();

我会想到通过在像素着色器上调用 .Apply() ,后续的 spriteBatch.Draw() 调用将通过发送 myTexure我的像素着色器。由于像素着色器函数始终返回 float4(0, 1, 0, 1),我预计结果是一个绿色方块,但它却渲染了一个红色方块,就好像像素着色器没有接触它一样。

我缺少什么?

I have a really simple pixel shader:

float4 PixelShaderFunction(float2 uv : TEXCOORD0) : COLOR0
{
    return float4(0, 1, 0, 1);
}

technique Technique1
{
    pass Pass1
    {
        PixelShader = compile ps_3_0 PixelShaderFunction();
    }
}

I have a texture:

        Vector4[] textureData = new Vector4[width * height];
        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                textureData[y * width + x] = new Vector4(1, 0, 0, 1);
            }
        }
        myTexture = new Texture2D(GraphicsDevice, width, height, false, SurfaceFormat.Vector4);
        myTexture.SetData(textureData);

and I draw it with this code:

        spriteBatch.Begin(SpriteSortMode.Texture,
                          BlendState.Additive,
                          SamplerState.PointWrap,
                          DepthStencilState.DepthRead,
                          RasterizerState.CullNone);

        myEffect.CurrentTechnique.Passes[0].Apply();
        spriteBatch.Draw(myTexture, new Rectangle(0, 0, width, height), Color.White);

        spriteBatch.End();

I would have figured that by calling .Apply() on the pixel shader that the subsequent spriteBatch.Draw() call would have sent myTexure through my pixel shader. Since the pixel shader function always returns float4(0, 1, 0, 1) I expected the result to have been a green square, but instead it renders a red one, as if the pixel shader is not touching it.

What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

允世 2024-12-22 16:29:12

您实际上从未在着色器上调用 Begin(),因此它仍将使用默认着色器。

此外,现在有一种更干净的方法。您可以将效果作为参数传递给 SpriteBatch 开始调用,详见此处

You're never actually calling Begin() on your shader, so it will still be using the default shader.

Also, there is a cleaner way of doing it now. You can pass your effect as a parameter to the SpriteBatch begin call, as detailed here.

杀お生予夺 2024-12-22 16:29:12

看起来我只需要将 SpriteSortMode 更改为 Immediate 并将像素着色器版本从 3 更改为 2

Looks like I just needed to change SpriteSortMode to Immediate and change the pixel shader version from 3 to 2

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文