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
}
效果图: