创建圆角矩形着色器的问题

问题描述 投票:0回答:0

我正在处理一个圆角矩形着色器,但我正在努力解决一个问题。

这就是我现在拥有的:

fixed4 frag (v2f i) : SV_Target 
{            
    float2 uv = i.uv;
    float2 size = _MainTex_TexelSize.xy;

    float2 texelSize      = _MainTex_TexelSize.zw;
    float  aspectRatio    = max(texelSize.x, texelSize.y) / min(texelSize.x, texelSize.y);
    float2 aspectRatioVec = texelSize.x > texelSize.y ? float2(aspectRatio, 1.0) : float2(1.0, aspectRatio);

    float2 position = (uv - 0.5) * 2.0 * aspectRatioVec;

    float4 r = _Roundness;

    r.xy = position.x > 0.0 ? r.xy : r.zw;
    r.x  = position.y > 0.0 ? r.x : r.y;

    float4 scaledRoundness = r * float4(aspectRatioVec, aspectRatioVec);

    float  m = scaledRoundness.x;
    float2 q = abs(position) - aspectRatioVec + m;

    float dist = min(max(q.x, q.y), 0.0) + length(max(q, 0.0)) - m;

    float edgeSoftness = length(float2(ddx(dist), ddy(dist)));
    float alpha = 1.0 - smoothstep(-edgeSoftness, edgeSoftness, dist);

    _Color.rgb *= alpha;

    return float4(_Color.rgb, alpha);
}

我可以改变每个单独的矩形角,当输出的宽度和高度相同或高度大于宽度时,此着色器工作良好,但如果宽度大于高度,则会出现问题。

有人可以帮我解决这个问题吗?我一直在摸不着头脑。不幸的是,我的数学能力不是很好......

非常感谢您的帮助!

unity3d shader hlsl
© www.soinside.com 2019 - 2024. All rights reserved.