的问题是,内置SpriteBatch
着色器是2.0。如果仅指定像素着色器,则SpriteBatch
仍使用其内置顶点着色器。因此版本不匹配。
的解决方案的话,同时指定一个顶点着色器自己。幸运的是,微软提供了source to XNA's built-in shaders。它涉及的只是一个矩阵变换。下面的代码,修改,以便您可以直接使用它:
float4x4 MatrixTransform;
void SpriteVertexShader(inout float4 color : COLOR0,
inout float2 texCoord : TEXCOORD0,
inout float4 position : SV_Position)
{
position = mul(position, MatrixTransform);
}
然后 - 因为SpriteBatch
不会将它适合你 - 你的设置效果的MatrixTransform
正确。这是“客户”空间的简单投影(源自this blog post)。代码如下:
Matrix projection = Matrix.CreateOrthographicOffCenter(0,
GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, 0, 0, 1);
Matrix halfPixelOffset = Matrix.CreateTranslation(-0.5f, -0.5f, 0);
effect.Parameters["MatrixTransform"].SetValue(halfPixelOffset * projection);
不要忘记“VertexShader = compile vs_3_0 SpriteVertexShader();” 'vs'vs'ps'滑倒了我的视线片刻! (很好的回答,http://gamedev.sleptlate.org/blog/165-implementing-a-pass-through-3vertex-shader-for-games-without-vertices/帮助你或其他方式?) – Lodewijk 2013-10-26 21:31:00
@Lodewijk独立 - 我以前没见过这篇博文(虽然他的发布日期早几个月)。他们几乎相同并不令人惊讶 - 看起来我们都使用了相同的源材料(请参阅我的链接)。 – 2014-05-06 04:56:14
感谢您的回答。看到一些代码集有这样的生活,总是在博客中传播很有趣。非常奇特的同时。 – Lodewijk 2014-05-06 22:33:14