How to use shaders in MonoGame?

删除回忆录丶 提交于 2019-12-07 19:27:18

问题


Can someone tell me, how to use shaders in monogame?

I have this error: https://gamedev.stackexchange.com/questions/46994/has-anyone-got-the-krypton-lighting-engine-working-in-monogame-for-windows-8

I tried to use 2MGFX, but the tool reports: The effect must contain at least one technique and pass. From what I can see from myshader.fx the file, it does.

Here is my shader code:

sampler TextureSampler : register(s0);
float _valueAlpha = 1;
float _valueRGB = 1;

float4 main(float4 color : COLOR0, float2 texCoord : TEXCOORD0) : COLOR0
{
    // Look up the texture color.
    float4 tex = tex2D(TextureSampler, texCoord);

    // Convert it to greyscale. The constants 0.3, 0.59, and 0.11 are because
    // the human eye is more sensitive to green light, and less to blue.
    float greyscale = dot(tex.rgb, float3(0.3, 0.59, 0.11));

    // The input color alpha controls saturation level.
    tex.rgb = lerp(greyscale, tex.rgb *_valueRGB, color.a *_valueAlpha);

    return tex;
}
technique Technique1
{
    pass Pass1
    {
        PixelShader = compile ps_3_0 main();
    }
}

I tried changing the technique to Technique, and the pass to Pass, but it still throws "The effect must contain at least one technique and pass"


回答1:


I got it to work! :)

First of all I used 2MGFX tool. Then I loaded the effect like this:

BinaryReader Reader = new BinaryReader(File.Open(@"Content\\myShader.mgfxo", FileMode.Open)); 
myShader = new Effect(GraphicsDevice, Reader.ReadBytes((int)Reader.BaseStream.Length)); 

Hope it will help someone else too! :)

Thanks!



来源:https://stackoverflow.com/questions/16634752/how-to-use-shaders-in-monogame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!