2013-02-08 116 views
3

我一直在问到下面分裂的问题分成多个问题:HLSL 3可以单独声明像素着色器吗?

HLSL and Pix number of questions

这是问的第一个问题,我可以在HLSL 3运行像素着色器没有顶点着色器。在HLSL 2中,我注意到你可以,但我似乎无法在3中找到方法?

着色器将编译罚款,那么我就要但是从Visual Studio中调用SpriteBatch绘制时出现此错误()。

“不能在早期着色模式的混合Shader Model 3.0的。如果两个顶点着色器和像素着色器编译为3.0,它们必须是。”

我不相信我在着色器中定义的任何使用任何较早然后3,所以我离开了有点糊涂了。任何帮助,将不胜感激。

回答

7

的问题是,内置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); 
+1

不要忘记“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

+0

@Lodewijk独立 - 我以前没见过这篇博文(虽然他的发布日期早几个月)。他们几乎相同并不令人惊讶 - 看起来我们都使用了相同的源材料(请参阅我的链接)。 – 2014-05-06 04:56:14

+0

感谢您的回答。看到一些代码集有这样的生活,总是在博客中传播很有趣。非常奇特的同时。 – Lodewijk 2014-05-06 22:33:14

-2

你可以试试简单的例子here。灰度着色器是了解最小像素着色器如何工作的一个很好的示例。

基本上,你创建你的内容项目下的一个效果像这样的:

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(); 
    } 
} 

您还需要:

  1. 创建效果对象,并加载其内容。

    ambienceEffect = Content.Load( “效果/环境”);

  2. 打电话给你SpriteBatch.Begin()方法,传递你想使用

    spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend, 空, 空, 空, ambienceEffect效果对象, camera2d.GetTransformation());

  3. 里面的SpriteBatch.Begin() - SpriteBatch.End()块,你必须调用影响内部的技术

    ambienceEffect.CurrentTechnique.Passes [0]。应用();

相关问题