Last active
December 30, 2021 06:06
-
-
Save geniuszxy/5823651e7533406ae3123408879c2990 to your computer and use it in GitHub Desktop.
Rounded corner rectangle shader (support different radius for each corner), idea from https://www.shadertoy.com/view/WtdSDs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Hidden/RoundedRectangle" | |
{ | |
Properties | |
{ | |
_MainTex("Texture", 2D) = "white" {} | |
_Radius("Radius %", Range(0, 0.5)) = 0.2 | |
} | |
SubShader | |
{ | |
Tags | |
{ | |
"RenderType" = "Opaque" | |
} | |
LOD 100 | |
Pass | |
{ | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
struct appdata | |
{ | |
float4 vertex : POSITION; | |
float2 uv : TEXCOORD0; | |
}; | |
struct v2f | |
{ | |
float2 uv : TEXCOORD0; | |
float2 p : TEXCOORD1; | |
float4 vertex : SV_POSITION; | |
}; | |
sampler2D _MainTex; | |
float4 _MainTex_ST; | |
float _Radius; | |
v2f vert(appdata v) | |
{ | |
v2f o; | |
o.vertex = UnityObjectToClipPos(v.vertex); | |
o.uv = TRANSFORM_TEX(v.uv, _MainTex); | |
o.p = o.uv - float2(0.5, 0.5); // origin to center of the image. | |
return o; | |
} | |
fixed4 frag(v2f i) : SV_Target | |
{ | |
float2 p = abs(i.p); // convert point to first quadrant. | |
p -= float2(0.5, 0.5) - _Radius; // reduce the inner rectangle. | |
p = max(p, 0.0); // limit the point in the first quad after translate. | |
clip(_Radius - length(p)); // radius test | |
return tex2D(_MainTex, i.uv); | |
} | |
ENDCG | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Different radius for each corner | |
Shader "Hidden/RoundedRectangle4" | |
{ | |
Properties | |
{ | |
_MainTex("Texture", 2D) = "white" {} | |
_Radius("Radius %", Vector) = (0.1, 0.2, 0.3, 0.4) | |
} | |
SubShader | |
{ | |
Tags | |
{ | |
"RenderType" = "Opaque" | |
} | |
LOD 100 | |
Pass | |
{ | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
struct appdata | |
{ | |
float4 vertex : POSITION; | |
float2 uv : TEXCOORD0; | |
}; | |
struct v2f | |
{ | |
float2 uv : TEXCOORD0; | |
float2 p : TEXCOORD1; | |
float4 vertex : SV_POSITION; | |
}; | |
sampler2D _MainTex; | |
float4 _MainTex_ST; | |
float4 _Radius; | |
v2f vert(appdata v) | |
{ | |
v2f o; | |
o.vertex = UnityObjectToClipPos(v.vertex); | |
o.uv = TRANSFORM_TEX(v.uv, _MainTex); | |
o.p = o.uv - float2(0.5, 0.5); | |
return o; | |
} | |
float rectangularPulse(float x, float min, float max) | |
{ | |
return x < min ? 0 : (x > max ? 0 : 1); | |
} | |
fixed4 frag(v2f i) : SV_Target | |
{ | |
float ang = atan2(i.p.y, i.p.x); | |
// select raidus based on angle | |
float radius = dot(_Radius, float4( | |
rectangularPulse(ang, -UNITY_PI, -UNITY_HALF_PI), | |
rectangularPulse(ang, -UNITY_HALF_PI, 0), | |
rectangularPulse(ang, 0, UNITY_HALF_PI), | |
rectangularPulse(ang, UNITY_HALF_PI, UNITY_PI) | |
)); | |
clip(radius - length(max(abs(i.p) - float2(0.5, 0.5) + radius, 0.0))); | |
return tex2D(_MainTex, i.uv); | |
} | |
ENDCG | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment