2017-05-29 27 views
0

我想在Monogame中实现PixelShader。MonoGame 2D PixelShader无法正常工作

着色器(现在)应该只需要一个纹理并将其恢复为未处理状态。

sampler MyTex : register(s0); 

float4 PixelShaderFunction(float2 coords: TEXCOORD0) : COLOR0 
{ 
    float4 Color = tex2D(MyTex, coords); 
    return Color; 
} 

technique tech 
{ 
    pass Pass1 
    { 
     PixelShader = compile ps_4_0_level_9_1 PixelShaderFunction(); 
    } 
} 

我Monogame实现看起来是这样的:在

LoadContent

spriteBatch = new SpriteBatch(GraphicsDevice); 
texture = Content.Load<Texture2D>("surge"); 
GraphicsDevice.Textures[0] = texture; 
effect = Content.Load<Effect>("sha"); 

,并在绘制方法:

GraphicsDevice.Clear(Color.Aquamarine); 

spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend,null, null, null, effect, null); 
spriteBatch.Draw(texture, new Vector2(150, 150), Color.White); 
spriteBatch.End(); 

但是这说明不了什么。将BlendState更改为“不透明”会给我一个纹理应该是的黑色Rectangle。我也尝试改变其他Sortmodes和BlendStates没有成功。我似乎无法找到问题。 任何有助于解决问题的答案或正在减少我因为这个而产生的自我仇恨是非常感谢!

编辑:资源绑定(注册)在整个着色器模型版本中更改的问题?! 有人请帮忙!

回答

0

嗨斯蒂芬我已经尝试此代码为灰度效果

texture Texture; 
sampler TextureSampler = sampler_state 
{ 
    Texture = <Texture>; 
}; 

//该数据来自精灵批顶点着色器

struct VertexShaderOutput 
{ 
    float4 Position : TEXCOORD0; 
    float4 Color : COLOR0; 
    float2 TextureCordinate : TEXCOORD0; 
}; 

//我们的像素着色器

float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0 
{ 
    float4 color = tex2D(TextureSampler, input.TextureCordinate); 

    float value = 0.299*color.r + 0.587*color.g + 0.114*color.b; 
    color.r = value; 
    color.g = value; 
    color.b = value; 
    color.a = 1.0f; 

    return color; 
} 

//编译我们的着色器

technique Technique1 
{ 
    pass Pass1 
    { 
    PixelShader = compile ps_2_0 PixelShaderFunction(); 
    } 
} 

或者,你可以尝试这个linkthis

+0

谢谢您的回答,但与shaderfile我甚至不能编译。 MSBuild \ MonoGame \ v3.0 \ MonoGame.Content.Builder.targets(90,5):错误MSB3073:[..]以代码1结束 – Steffen