【问题标题】:Issue with progress bar shader in XNAXNA 中的进度条着色器问题
【发布时间】:2014-08-05 04:09:51
【问题描述】:

我正在创建一个简单的着色器,用于在 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。 BackgroundSampler 只是白色的。 我发现正确只显示了最后在着色器中声明的纹理。

请帮我理解为什么会这样?

【问题讨论】:

  • 帖子的答案不应包含在帖子中。请在下面发布您的解决方案作为答案。 :)
  • 对不起,我是新手。

标签: c# xna progress-bar hlsl effect


【解决方案1】:

我明白了!

shader 都是正确的。

听错了:

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 方法的索引相同。

也许它对某人有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-27
    • 1970-01-01
    • 2010-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-10
    • 1970-01-01
    相关资源
    最近更新 更多