【问题标题】:Why doesnt my xna pixel shader turn this texture blue?为什么我的 xna 像素着色器不将此纹理变为蓝色?
【发布时间】:2011-11-15 10:37:16
【问题描述】:

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

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) 我希望结果是一个绿色方块,但它却渲染了一个红色方块,就好像像素着色器没有接触它一样。

我错过了什么?

【问题讨论】:

    标签: xna-4.0 hlsl


    【解决方案1】:

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

    此外,现在有一种更清洁的方法。您可以将效果作为参数传递给 SpriteBatch 开始调用 as detailed here

    【讨论】:

    • 无论是 Effect 还是 Pass 对象都没有 Begin() 方法。现在看起来像在 Pass 对象上调用 .Apply() 而不是调用 Begin()
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-03
      • 1970-01-01
      • 2011-05-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多