问题
I'm create a simple shader for drawing progress bar in XNA.
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();
}
}
But correct only ForegroundTexture. BackgroundSampler is simply white. I found that correct shown only texture that was declared in shader in last.
Please help me understand why so?
回答1:
I've got it!
There are all correct with shader.
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();
And correct variant is:
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();
I've forgot that first declared texture use same index that texture passed to Draw method.
Maybe it will be usefull for somebody.
来源:https://stackoverflow.com/questions/24224977/issue-with-progress-bar-shader-in-xna