【Shader】用 Shader 画点和线

Shader "Custom/SimpleDotShader" {
    Properties {
        _CircleRadius ("Circle Radius", float) = 5
        _CircleColor ("Circle Color", Color) = (1,0,0,1)
        _LineWidth ("Line Width", float) = 5
        _LineColor ("Line Color", Color) = (1,1,1,1)
        _Antialias ("Antialias Factor", float) = 3
        _BackgroundColor ("Background Color", Color) = (0,1,0,1)
    }

    CGINCLUDE

    #include "UnityCG.cginc"
    #include "ShaderToyDefines.cginc"
    #pragma target 3.0

    float _CircleRadius;
    float4 _CircleColor;
    float _LineWidth;
    float4 _LineColor;
    float _Antialias;
    float4 _BackgroundColor;

    struct v2f {
        float4 pos : SV_POSITION;
        float4 scrPos : TEXCOORD0;
    };

    v2f vert(appdata_base v) {
        v2f o;
        o.pos = UnityObjectToClipPos (v.vertex);
        o.scrPos = ComputeScreenPos(o.pos);
        return o;
    }

    vec4 main(vec2 fragCoord);

    fixed4 frag(v2f _iParam) : COLOR0 {
        return main(gl_FragCoord);
    }

    vec4 drawLine(vec2 pos, vec2 point1, vec2 point2, float width, float3 color, float antialias) {
        float k = (point1.y - point2.y) / (point1.x - point2.x);
        float b = point1.y - k * point1.x;
        float d = abs(k * pos.x - pos.y + b) / sqrt( k*k + 1);
        float t = smoothstep(width/2.0, width/2.0 + antialias, d);
        return vec4(color, 1.0 - t);
    }

    vec4 circle(vec2 pos, vec2 center, float radius, float3 color, float antialias) {
        float d = length(pos - center) - radius;
        float t = smoothstep(0, antialias, d);
        return vec4(color, 1.0 - t);
    }

    vec4 main(vec2 fragCoord) {

        vec2 pos = fragCoord;
        vec2 point1 = vec2(0.4, 0.1) * iResolution.xy;
        vec2 point2 = vec2(0.7, 0.8) * iResolution.xy;

        vec4 layer1 = vec4(_BackgroundColor.rgb, 1.0);
        vec4 layer2 = drawLine(pos, point1, point2, _LineWidth, _LineColor.rgb, _Antialias);
        vec4 layer3 = circle(pos, point1, _CircleRadius, _CircleColor.rgb, _Antialias);
        vec4 layer4 = circle(pos, point2, _CircleRadius, _CircleColor.rgb, _Antialias);

        vec4 fragColor = mix(layer1, layer2, layer2.a);
        fragColor = mix(fragColor, layer3, layer3.a);
        fragColor = mix(fragColor, layer4, layer4.a);

        return fragColor;
    }

    ENDCG

    SubShader {
        Pass {

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma fragmentoption ARB_precision_hint_fastest
            ENDCG

        }
    }

    FallBack Off
}

效果图:

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值