【问题标题】:Simple vertex shader not working correctly in Monogame简单的顶点着色器在 Monogame 中无法正常工作
【发布时间】:2015-05-20 08:25:38
【问题描述】:

我对着色器和 Monogame 都很陌生,所以如果我遗漏了一些明显的东西,请原谅我。

我非常松散地遵循本教程:http://www.david-gouveia.com/portfolio/scrolling-textures-with-zoom-and-rotation/

我希望在我自己的游戏中获得与教程中完全相同的效果:基于相机旋转/缩放/位置绘制的纹理。

这是我的着色器代码:

sampler TextureSampler : register(s0);

struct VertexShaderInput
{
    float4 Color: COLOR0;
    float2 TexCoord : TEXCOORD0;
    float4 Position : SV_Position0;
};

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

float2   ViewportSize;
float4x4 ScrollMatrix;

VertexShaderOutput SpriteVertexShader(VertexShaderInput input)
{
    VertexShaderOutput output;
    float4 position = input.Position;

    // Half pixel offset for correct texel centering.
    position.xy -= 0.5;

    // Viewport adjustment.
    position.xy = position.xy / ViewportSize;
    position.xy *= float2(2, -2);
    position.xy -= float2(1, -1);

    // Transform our texture coordinates to account for camera
    output.TexCoord = mul(float4(input.TexCoord.xy, 0, 1), ScrollMatrix).xy;
    output.Position = position;
    output.Color = input.Color;

    return output;
}

technique SpriteBatch
{
    pass
    {
        VertexShader = compile vs_4_0_level_9_1  SpriteVertexShader();
    }
}

我把它加载到monogame中:

Effect effect = content.Load<Effect>("SpriteEffects\\infinite");
effect.Parameters["ViewportSize"].SetValue(new Vector2(viewport.Width,viewport.Height));

绘图时获取效果的函数:

public Effect getScrollEffect()
        {
            //This should use a matrix based on the camera, but I'm using an Identity matrix for testing
            effect.Parameters["ScrollMatrix"].SetValue(Matrix.Identity);
            return effect;
        }

然后画出来:

spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null, getScrollEffect());
spriteBatch.Draw(texture, viewport.Bounds, viewport.Bounds, Color.White);
spriteBatch.End();

纹理如下所示:

我的结果如下所示:

发生了什么事?我的着色器甚至没有触及颜色。即使我从输入和输出中删除“颜色”变量,结果也是一样的。看起来它以某种方式将一个变量的坐标与颜色值混淆了。我是不是用错了shader?

感谢您的帮助。

【问题讨论】:

    标签: c# xna shader monogame vertex-shader


    【解决方案1】:

    这是基于我使用 XNA 的经验,但我无法想象有什么可以让您在单声道中跳过整个像素着色器阶段(并将其替换为默认值)。几何着色器是可选的,像素着色器不是。

    简单地说:您需要像素着色器使用从顶点着色器传递的 UVcoords 将纹理映射到几何体上。

    float4 PixelShader(VertexShaderOutput input) : COLOR0
    {
       float4 Diffuse =  tex2D(DiffuseSampler, input.TexCoord);
       return Diffuse * input.Color;
    } 
    

    input.Color 是顶点颜色,spritebatch 将其设置为顶点,在您的情况下为 Color.White,用作色调。

    别忘了

    PixelShader = compile ps_4_0_level_9_1  PixelShader();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-02
      • 1970-01-01
      • 2015-12-29
      • 2013-02-20
      相关资源
      最近更新 更多