【发布时间】:2015-12-15 21:39:12
【问题描述】:
好的,所以我正在尝试了解 Monogame 3.4 中的 HLSL。部分问题是我发现的许多现有示例都不能正常工作,所以我无法弄清楚我做错了什么,而且我得到的错误没有帮助!
我正在尝试将纹理和调色板纹理传递给像素着色器,目前我正在使用 SpriteBatch 进行绘图,但最终我会将其应用于多边形(希望如此)。纹理通过 SpriteBatch 绘制方法(我假设为 S0)进入,而我需要通过使用参数传递palleteImage,但是当我尝试执行以下操作时,我在 SetValue 上得到一个 NullException,因为它认为没有一个参数。有任何想法吗?或者更重要的是,有一个工作示例,我可以看到我发现的许多现有像素着色器在 MonoGame 3.4 上不起作用!
HLSL
sampler TextureSampler : register(s0);
texture paletteImage;
sampler2D PaletteSampler = sampler_state {
Texture = <paletteImage>;
};
float4 PixelShaderFunction(float4 position : SV_Position, float4 color : COLOR0, float2 texCoord : TEXCOORD0) : COLOR0
{
float4 tex = tex2D(TextureSampler, texCoord);
float2 coords = float2(0.5f, 0.5f);
float4 palColour = tex2D(PaletteSampler, coords);
return tex * color;
}
technique Technique1
{
pass Pass1
{
PixelShader = compile ps_4_0 PixelShaderFunction();
}
}
绘制代码
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
effect.CurrentTechnique.Passes[0].Apply();
effect.Parameters["paletteImage"].SetValue(paletteImage);
// draw here
spriteBatch.Draw(texture, new Rectangle(10, 10, texture.Width * 3, texture.Height * 3), Color.White)
spriteBatch.End();
异常发生在 SetValue,据我所知 HLSL 很好,但有些不对劲...我注意到,如果它认为没有使用参数,则会引发错误(例如,我尝试传入一种颜色,但直到我在 HLSL 代码中使用该颜色后才被接受...这是同一件事吗?)
【问题讨论】: