Skip to content

Instantly share code, notes, and snippets.

@tblack-studio
Created April 11, 2018 03:37
Show Gist options
  • Save tblack-studio/b67c2bea37b5f49ab90ef9b6f65a7656 to your computer and use it in GitHub Desktop.
Save tblack-studio/b67c2bea37b5f49ab90ef9b6f65a7656 to your computer and use it in GitHub Desktop.
Unity surface shader with round corners
Shader "Custom/Round Rect" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_Pos("Position", Vector) = (0,0,0,0)
_Width("Width", float) = 0.0
_Height ("Height", float) = 0.0
_Radius ("Radius", float) = 0.5
}
SubShader {
Tags { "RenderType"="Transparent" "Queue"="Transparent" "AllowProjectors"="False" }
blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma surface surf NoLighting alpha
fixed4 LightingNoLighting(SurfaceOutput s, fixed3 lightDir, fixed atten)
{
return fixed4(0,0,0, s.Alpha);
}
sampler2D _MainTex;
struct Input
{
float4 color : COLOR;
float3 worldPos;
};
float4 _Color;
float4 _Pos;
float _Radius;
float _Width;
float _Height;
void surf (Input IN, inout SurfaceOutput o) {
float4 rectCenter = fixed4(_Pos.x - _Width/2.0 + _Radius, _Pos.y + _Height/2.0 - _Radius,IN.worldPos.z,0);
if(IN.worldPos.x < rectCenter.x && IN.worldPos.y > rectCenter.y && distance(IN.worldPos, rectCenter) > _Radius){
o.Alpha = 0;
} else {
rectCenter = fixed4(_Pos.x + _Width/2.0 - _Radius, _Pos.y + _Height/2.0 - _Radius,IN.worldPos.z,0);
if(IN.worldPos.x > rectCenter.x && IN.worldPos.y > rectCenter.y && distance(IN.worldPos, rectCenter) > _Radius){
o.Alpha = 0;
} else {
rectCenter = fixed4(_Pos.x + _Width/2.0 - _Radius, _Pos.y - _Height/2.0 + _Radius,IN.worldPos.z,0);
if(IN.worldPos.x > rectCenter.x && IN.worldPos.y < rectCenter.y && distance(IN.worldPos, rectCenter) > _Radius){
o.Alpha = 0;
} else {
rectCenter = fixed4(_Pos.x - _Width/2.0 + _Radius, _Pos.y - _Height/2.0 + _Radius,IN.worldPos.z,0);
if(IN.worldPos.x < rectCenter.x && IN.worldPos.y < rectCenter.y && distance(IN.worldPos, rectCenter) > _Radius){
o.Alpha = 0;
} else {
o.Alpha = 1;
}
}
}
}
o.Emission = _Color.rgb;
}
ENDCG
}
FallBack "Diffuse"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment