XNA中的进度条着色器出现问题 [英] Issue with progress bar shader in XNA
问题描述
我正在为XNA中的绘制进度条创建一个简单的着色器.
I'm create a simple shader for drawing progress bar in XNA.
想法很简单:有两个纹理和值,如果X纹理坐标小于或等于值,则使用前景纹理中的像素,否则使用背景纹理.
Idea is simple: there are two textures and value and if X texture coord less then value use pixel from foreground texture, else use background texture.
/* 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只是白色. 我发现正确的仅显示了最后在着色器中声明的纹理.
But correct only ForegroundTexture. BackgroundSampler is simply white. I found that correct shown only texture that was declared in shader in last.
请帮助我理解为什么?
推荐答案
我知道了!
着色器都正确.
Mistake受伤了:
Mistake 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方法的纹理相同的索引.
I've forgot that first declared texture use same index that texture passed to Draw method.
也许对某人有用.
这篇关于XNA中的进度条着色器出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!