简体   繁体   English

金属中的片段着色器和顶点着色器

[英]Fragment shader and Vertex Shader in Metal

Fragment Shader 片段着色器

vertex VertexOutBezier bezier_vertex(constant BezierParameters *allParams[[buffer(0)]],
                                    //  constant GlobalParameters& globalParams[[buffer(1)]],
                                     uint vertexId [[vertex_id]],
                                     uint instanceId [[instance_id]])
{

    float t = (float) vertexId / 300;
    rint(t);
    BezierParameters params = allParams[instanceId];

    float lineWidth = (1 - (((float) (vertexId % 2)) * 2.0)) * params.lineThickness;

    float2 a = params.a;
    float2 b = params.b;
    float nt = 1.0f - t;
    float nt_2 = nt * nt;
    float nt_3 = nt_2 * nt;
    float t_2 = t * t;
    float t_3 = t_2 * t;

    float2 point = a * nt_3 + params.p1 * nt_2 * t + params.p2 * nt * t_2 + b * t_3;

    float2 tangent = -3.0 * a * nt_2 + params.p1 * (1.0 - 4.0 * t + 3.0 * t_2) + params.p2 * (2.0 * t - 3.0 * t_2) + 3 * b * t_2;

    tangent = normalize(float2(-tangent.y, tangent.x));
    VertexOutBezier vo;   
    vo.pos.xy = point   + (tangent * (lineWidth / 3.0f));
    vo.pos.zw = float2(0, 1);
    vo.color = params.color ;
    return vo;
}

My Fragment shader is 我的片段着色器是

fragment float4 bezier_fragment(VertexOutBezier params[[stage_in]],
                                texture2d<float> texture [[texture(0)]]
                                )
{
    constexpr sampler defaultSampler;
   float4 canvasColor = texture.sample(defaultSampler, params.pos.xy);
    return canvasColor;   
}

Here i expect to get the pixel color of the texture. 在这里,我希望得到纹理的像素颜色。 But here it is only getting single color. 但是在这里它只会变单色。 It is not getting the color of the texture according to its position. 它没有根据其位置获得纹理的颜色。

Even when I do this in fragment I am getting the single color it is not varying with coordinates 即使我在片段中执行此操作,我也会得到单色,并且它不会随坐标变化

fragment float4 bezier_fragment(VertexOutBezier params[[stage_in]],
                                texture2d<float> texture [[texture(0)]]
                                )
{
    constexpr sampler defaultSampler;
   float4 canvasColor = params.color * params.pos.x;
    return canvasColor;
}

If I do this in Vertex Shader I got color varying according position of x 如果我在“顶点着色器”中执行此操作,则颜色会根据x的位置而变化

vo.pos.xy = point   + (tangent * (lineWidth / 3.0f));
vo.pos.zw = float2(0, 1);
vo.color = params.color * vo.pos.x;

What is the Issue in fragment Shader. 片段着色器中的问题是什么。 I cannot get the coordinates from Vertex Shader 我无法从顶点着色器获得坐标

请确保VertexOutBezier.pos.xy值是归一化(0〜1.0),由于defaultSampler仅接收归一化位置值,如果始终返回单个,则该位置可能超出范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM