分类存档: unity shader

自己学习写的第一个shader,画个圆


学习到如果需要显示一个圆型的图片,需要自己来写shader,这里是我学习写的第一个shader。

以后会边学习边将写的shader记录下来,也可以不断补充自己学习能力。

Shader "Custom/Circle2" {
	Properties{
		_Color("Color", Color) = (1,0,0,0)
	}
	SubShader{
 
		Tags{ "Queue" = "Transparent" "RenderType" = "Transparent" "IgnoreProjector" = "True" }
		Pass{
			ZWrite Off
			Blend SrcAlpha OneMinusSrcAlpha

			CGPROGRAM

			#pragma vertex vert
			#pragma fragment frag
			#include "UnityCG.cginc"

			fixed4 _Color; // low precision type is usually enough for colors

			struct fragmentInput {
				float4 pos : SV_POSITION;
				float2 uv : TEXCOORD0;
			};

			fragmentInput vert(appdata_base v)
			{
				fragmentInput o;

				o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
				o.uv = v.texcoord.xy - fixed2(0.5,0.5);
 
				return o;
			}

			fixed4 frag(fragmentInput i) : SV_Target{
				float distance = sqrt(pow(i.uv.x, 2) + pow(i.uv.y,2));
		
				if (distance > 0.5f) {
					if (distance < 0.51f) {
						return fixed4(_Color.r* distance, _Color.g* distance, _Color.b* distance, 0.6f);
					}
					else {
						return fixed4(0, 0, 0, 0);
					}
				}
				else {
					return fixed4(_Color.r * distance, _Color.g * distance, _Color.b * distance,1);
				}
			}
			ENDCG
		}
	}
}