我被要求将以下问题拆分为多个问题:
这是在问第一个问题,我是否可以在HLSL 3中运行像素着色器而没有顶点着色器。在HLSL 2中,我注意到可以,但是在3中似乎找不到方法?
着色器将编译成功,但是当调用SpriteBatch Draw()时,我会收到来自Visual Studio的以下错误。
“不能混合着色器模型3.0和早期的着色器模型。如果顶点着色器或像素着色器被编译为3.0,则它们必须都是。”
我不认为我已经定义了任何使用早期版本的内容。所以我有点困惑。任何帮助都将不胜感激。
我被要求将以下问题拆分为多个问题:
这是在问第一个问题,我是否可以在HLSL 3中运行像素着色器而没有顶点着色器。在HLSL 2中,我注意到可以,但是在3中似乎找不到方法?
着色器将编译成功,但是当调用SpriteBatch Draw()时,我会收到来自Visual Studio的以下错误。
“不能混合着色器模型3.0和早期的着色器模型。如果顶点着色器或像素着色器被编译为3.0,则它们必须都是。”
我不认为我已经定义了任何使用早期版本的内容。所以我有点困惑。任何帮助都将不胜感激。
SpriteBatch
着色器是2.0版本。如果只指定像素着色器,SpriteBatch
仍然使用其内置的顶点着色器。因此版本不匹配。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);
您可以在这里尝试简单的示例。灰度着色器是一个非常好的例子,可以帮助您了解最小像素着色器的工作原理。
基本上,您可以在内容项目下创建一个效果,就像这个:
sampler s0;
float4 PixelShaderFunction(float2 coords: TEXCOORD0) : COLOR0
{
// B/N
//float4 color = tex2D(s0, coords);
//color.gb = color.r;
// Transparent
float4 color = tex2D(s0, coords);
return color;
}
technique Technique1
{
pass Pass1
{
PixelShader = compile ps_2_0 PixelShaderFunction();
}
}
你还需要:
创建一个Effect对象并加载它的内容。
ambienceEffect = Content.Load("Effects/Ambient");
调用SpriteBatch.Begin()方法,并传入要使用的Effect对象。
spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend, null, null, null, ambienceEffect, camera2d.GetTransformation());
在SpriteBatch.Begin() - SpriteBatch.End()块中,必须调用Effect内的Technique
ambienceEffect.CurrentTechnique.Passes[0].Apply();