Skip to content

Instantly share code, notes, and snippets.

@dvdfu
Last active July 24, 2020 19:10
Show Gist options
  • Save dvdfu/9c303bb18a10f5e19d4097fc65eb84d1 to your computer and use it in GitHub Desktop.
Save dvdfu/9c303bb18a10f5e19d4097fc65eb84d1 to your computer and use it in GitHub Desktop.
Shader "Custom/Ring" {
Properties {
_MainTex ("Albedo (RGB)", 2D) = "white" {}
}
SubShader {
Tags {
"RenderType" = "Transparent"
"Queue" = "Transparent"
}
Blend SrcAlpha OneMinusSrcAlpha
ZTest Always
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
static const float PI = 3.14159265f;
uniform float _Thickness;
uniform float _LowerAngle;
uniform float _ArcAngle;
struct VertOutput {
float4 color: COLOR;
float4 pos: SV_POSITION;
float2 uv: TEXCOORD0;
};
sampler2D _MainTex;
VertOutput vert(appdata_full input) {
VertOutput output;
output.color = input.color;
output.pos = UnityObjectToClipPos(input.vertex);
output.uv = input.texcoord;
return output;
}
float mod(float x, float y) {
return (x % y + y) % y;
}
fixed2 polarToCart(float angle, float radius) {
return fixed2(cos(angle), sin(angle)) * radius;
}
fixed4 frag(VertOutput output): SV_Target {
if (_ArcAngle == 0) {
return output.color * fixed4(1, 1, 1, 0);
}
// Basic coord data
fixed2 uv = output.uv * 2 - 1;
float angle = atan2(uv.y, uv.x);
float len = length(uv);
float d = fwidth(len);
// Color!
float anglePast = mod(_LowerAngle - angle + PI * 2, PI * 2);
float upperAngle = _LowerAngle - _ArcAngle;
float innerRing = 1 - _Thickness;
float ringMid = (1 + innerRing) / 2;
float ringRad = (1 - innerRing) / 2;
float value = 1;
value *= smoothstep(innerRing - d, innerRing + d, len);
value *= 1 - smoothstep(1 - d, 1 + d, len);
value *= 1 - smoothstep(_ArcAngle - d, _ArcAngle + d, anglePast);
value += 1 - smoothstep(ringRad - d, ringRad + d, length(uv - polarToCart(_LowerAngle, ringMid)));
value += 1 - smoothstep(ringRad - d, ringRad + d, length(uv - polarToCart(upperAngle, ringMid)));
return output.color * fixed4(1, 1, 1, clamp(value, 0, 1));
}
ENDCG
}
}
FallBack "Diffuse"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment