Unity Procedural Circle Mask Shader – Fixed
(2019/02/15)

This is the same shader that is on http://wiki.unity3d.com/index.php/Procedural_Circle_Mask, however it has a tiny fix. I didn’t feel like submitting a lot of IRL credentials just to be approved to make such a small correction.

Purpose

Renders a circle in UV space and uses it as an alpha mask. Because the shader uses a gradient to define the circle it’s very easy to get well defined circles with controlled levels of aliasing.

Parameters

  • Main Texture: Diffuse texture
  • Color: Color multiplied into diffuse & alpha
  • Distort: X/Y affect the circle’s center, W/Z affect the circle’s (inverse) scale horizontally and vertically respectively.
  • Outer Radius: Main radius of the circle
  • Inner Radius: Inner radius of the circle (used to create a ring)
  • Hardness: Flattens the gradient towards the radius distances

Code

Shader "Custom/Unlit Flat Circle v3" {
	Properties {
		_MainTex ("Main Texture", 2D) = "white" {}
		_Color ("Color", Color) = (1,1,1,1)
		_Distort("Distort", vector) = (0.5, 0.5, 1.0, 1.0)
		_OuterRadius ("Outer Radius", float) = 0.5
		_InnerRadius ("Inner Radius", float) = -0.5
		_Hardness("Hardness", float) = 1.0
	}

	SubShader {
		Tags { "RenderType"="Transparent" "Queue"="Transparent" "AllowProjectors"="False" }
		
		blend SrcAlpha OneMinusSrcAlpha
		
		CGPROGRAM
		#pragma surface surf NoLighting alpha:blend
		
		fixed4 LightingNoLighting(SurfaceOutput s, fixed3 lightDir, fixed atten)
		{
			return fixed4(s.Albedo, s.Alpha);
		}

		sampler2D _MainTex;

		struct Input
		{
			float2 uv_MainTex;
		};

		float4 _Color, _Distort;
		float _OuterRadius, _InnerRadius, _Hardness;
		void surf (Input IN, inout SurfaceOutput o)
		{
			half4 c = tex2D (_MainTex, IN.uv_MainTex);
						
			float x = length((_Distort.xy - IN.uv_MainTex.xy) * _Distort.zw);

			float rc = (_OuterRadius + _InnerRadius) * 0.5f; // "central" radius
			float rd = _OuterRadius - rc; // distance from "central" radius to edge radii

			float circleTest = saturate(abs(x - rc) / rd);

			o.Albedo = _Color.rgb * c.rgb;
			o.Alpha = (1.0f - pow(circleTest, _Hardness)) * _Color.a * c.a;
		}
		ENDCG
	} 
	FallBack "Diffuse"
}

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.