2011-07-31 35 views
0

我想将像素着色器应用到我的背景精灵上,以创建某种照明。 所以我画了一个渲染目标,并且想通过像素着色器将它合并到背景上。 这是必要的代码: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

+0

@Elideb:你有没有参考文献来说明这一点?我非常确定,使用非幂次幂纹理只会造成内存损失(不是性能),因为XNA必须将它们填充到2的幂次方(因此包装将不起作用)。除非你实际使用包装,否则外观不应该受到影响。 –

+0

另外,默认情况下@pcnx,'SpriteBatch.Begin'使用'LinearClamp'。所以你必须将它设置为'LinearWrap'或类似的代码中的其他地方? –

+0

@AndrewRussell:我从过去的廉价卡片的经验中吸取教训。评论已删除。 – Elideb

回答

0

错误指的是你的纹理寻址模式(即:不纹理环绕的边缘,或者是它在边缘处夹住)。与着色器无关。

用于 SpriteBatch.BeginMSDN)的重载之一,需要一个 SamplerState,并传入 SamplerState.LinearClampMSDN)。

SpriteBatch.Begin默认为SamplerState.LinearClamp,所以你必须设置一个不同的状态:在你的代码在其他地方(如LinearWrap)到图形设备?不要这样做。

(或者:从到达的个人资料在你的项目设置HiDef轮廓变化)

0

如果您无法使用两个纹理的力量,你必须改变你的Spritebath.begin电话和specify a SamplerState。指定的最小值应该是

public void Begin (
     SpriteSortMode sortMode, 
     BlendState blendState, 
     SamplerState samplerState, 
     DepthStencilState depthStencilState, 
     RasterizerState rasterizerState 
)