我想将像素着色器应用到我的背景精灵上,以创建某种照明。 所以我画了一个渲染目标,并且想通过像素着色器将它合并到背景上。 这是必要的代码:XNA像素着色器夹紧错误
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
lighting.Parameters["lightMask"].SetValue(lightingMask);
lighting.CurrentTechnique.Passes[0].Apply();
spriteBatch.Draw(hexBack, new Vector2(0, 0), Color.White);
spriteBatch.End();
在这种情况下,hexBack是在它绘制的简单sprite的渲染目标和lightingMask是与光的纹理在它的渲染目标。 两者都是Backbuffer的宽度和高度。
所以当我尝试运行该程序时,它崩溃了: XNA Framework Reach配置文件要求TextureAddressMode在使用不是2的幂的纹理大小时为Clamp。
所以我试图设置夹紧,但我无法找到一种方法来得到它的工作。
着色器代码:
texture lightMask;
sampler mainSampler : register(s0);
sampler lightSampler = sampler_state{Texture = lightMask;};
struct PixelShaderInput
{
float4 TextureCoords: TEXCOORD0;
};
float4 PixelShaderFunction(PixelShaderInput input) : COLOR0
{
float2 texCoord = input.TextureCoords;
float4 mainColor = tex2D(mainSampler, texCoord);
float4 lightColor = tex2D(lightSampler, texCoord);
return mainColor * lightColor;
}
technique Technique1
{
pass Pass1
{
PixelShader = compile ps_2_0 PixelShaderFunction();
}
}
感谢您的帮助!
pcnx
@Elideb:你有没有参考文献来说明这一点?我非常确定,使用非幂次幂纹理只会造成内存损失(不是性能),因为XNA必须将它们填充到2的幂次方(因此包装将不起作用)。除非你实际使用包装,否则外观不应该受到影响。 –
另外,默认情况下@pcnx,'SpriteBatch.Begin'使用'LinearClamp'。所以你必须将它设置为'LinearWrap'或类似的代码中的其他地方? –
@AndrewRussell:我从过去的廉价卡片的经验中吸取教训。评论已删除。 – Elideb