XNA中进度条着色器的问题

本文关键字:问题 XNA | 更新日期: 2023-09-27 18:08:34

我创建了一个简单的着色器在XNA绘制进度条。

想法很简单:有两个纹理和值,如果X纹理值小于那么值使用前景纹理像素,否则使用背景纹理。

/* Variables */
texture BackgroundTexture;
sampler2D BackgroundSampler = sampler_state
{
    Texture = (BackgroundTexture);
    MagFilter = Point;
    MinFilter = Point;
    AddressU = Clamp;
    AddressV = Clamp;
};
texture ForegroundTexture;
sampler2D ForegroundSampler = sampler_state
{
    Texture = (ForegroundTexture);
    MagFilter = Point;
    MinFilter = Point;
    AddressU = Clamp;
    AddressV = Clamp;
};
float Value;
/* Pixel shaders */
float4 PixelShader1(float4 pTexCoord : texcoord0) : color0
{
    float4 texColor =
        pTexCoord.x <= Value ?
        tex2D(ForegroundSampler, pTexCoord) :
        tex2D(BackgroundSampler, pTexCoord);
    return texColor;
}
/* Techniques */
technique Technique1
{
    pass Pass1
    {
        PixelShader = compile ps_2_0 PixelShader1();
    }
}

但只正确ForegroundTexture。背景采样器是简单的白色。我发现正确的只显示纹理,在最后的着色器中声明。

请告诉我为什么会这样?

XNA中进度条着色器的问题

我知道了!

这些都是正确的着色器。

error was heare:

this.progressBarEffect.Parameters["Value"].SetValue(progressBarValue);
            this.progressBarEffect.Parameters["ForegroundTexture"].SetValue(this.progressBarForegroundTexture);
            this.progressBarEffect.Parameters["BackgroundTexture"].SetValue(this.progressBarBackgroundTexture);
            this.spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Opaque, SamplerState.PointWrap, null, null, this.progressBarEffect);
            this.spriteBatch.Draw(this.pixelTexture, new Rectangle(5, 200, 180, 30), Color.White);
            this.spriteBatch.End();

正确的变体是:

this.progressBarEffect.Parameters["Value"].SetValue(progressBarValue);
            //this.progressBarEffect.Parameters["ForegroundTexture"].SetValue(this.progressBarForegroundTexture);
            this.progressBarEffect.Parameters["BackgroundTexture"].SetValue(this.progressBarBackgroundTexture);
            this.spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Opaque, SamplerState.PointWrap, null, null, this.progressBarEffect);
            this.spriteBatch.Draw(this.progressBarForegroundTexture, new Rectangle(5, 200, 180, 30), Color.White);
            this.spriteBatch.End();

我忘记了第一个声明的纹理使用相同的索引,纹理传递给Draw方法。

也许它会对某人有用。