UnityShader从头学

Posted avi9111

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UnityShader从头学相关的知识,希望对你有一定的参考价值。

111

 

z之后再补充吧,估计又完成不了

Shader "UI/Default-UI"
{
	Properties
	{
		[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
		_Color ("Tint", Color) = (1,1,1,1)
		[Toggle] Font("Font", Float) = 0
		_MaskRect ("MaskRect", Vector) = (0,0,1,1)
		_ClipSize ("ClipSize", Vector) = (0,0,1,1)
	}

	SubShader
	{
		Tags
		{ 
			"Queue"="Transparent" 
			"IgnoreProjector"="True" 
			"RenderType"="Transparent" 
			"PreviewType"="Plane"
			"CanUseSpriteAtlas"="True"
		}

		Cull Off
		Lighting Off
		ZWrite Off
		ZTest [unity_GUIZTestMode]
		Fog { Mode Off }
		Blend SrcAlpha OneMinusSrcAlpha

		Pass
		{
		CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#pragma target 2.0
			#pragma multi_compile FONT_OFF FONT_ON
			#pragma multi_compile USEMASK_OFF USEMASK_ON
			#include "UnityCG.cginc"

			struct appdata_t
			{
				float4 vertex   : POSITION;
				float4 color    : COLOR;
				float2 texcoord : TEXCOORD0;
			};

			struct v2f
			{
				float4 vertex   : SV_POSITION;
				fixed4 color    : COLOR;
				half2 texcoord  : TEXCOORD0;
#ifdef USEMASK_ON
				half2 screenUV  : TEXCOORD1;
#endif
			};
			
			fixed4 _Color;
			fixed4 _TextureSampleAdd;
#ifdef USEMASK_ON
			float4 _MaskRect;
			float4 _ClipSize;
#endif

			v2f vert(appdata_t IN)
			{
				v2f OUT;
				OUT.vertex = UnityObjectToClipPos(IN.vertex);
				OUT.texcoord = IN.texcoord;
#ifdef USEMASK_ON
				OUT.screenUV = (OUT.vertex.xy / OUT.vertex.w) * 0.5f + 0.5f;
				if (_ProjectionParams.x > 0)
					OUT.screenUV.y = 1.0f - OUT.screenUV.y; 
#endif
#ifdef UNITY_HALF_TEXEL_OFFSET
				OUT.vertex.xy += (_ScreenParams.zw-1.0)*float2(-1,1);
#endif
				OUT.color = IN.color * _Color;

				return OUT;
			}

			sampler2D _MainTex;

			fixed4 frag(v2f IN) : SV_Target
			{
#ifdef FONT_ON
				half4 color = IN.color;
				color.a *= tex2D(_MainTex, IN.texcoord).a;
#else
				half4 color = (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd) * IN.color;
#endif
#ifdef USEMASK_ON
				half2 uvMask = abs((IN.screenUV - _MaskRect.xy) * _MaskRect.zw);
				half2 mask = saturate((1.0f - uvMask) * _ClipSize);
				color.a *= mask.x * mask.y;
#endif
				return color;
			}
		ENDCG
		}
	}
}

以上是关于UnityShader从头学的主要内容,如果未能解决你的问题,请参考以下文章

学unityshader之前学unity吗

从头开始查看寻呼机图像(片段)滑块

从头开始学 RecyclerView 封装简化

从头开始学数据结构02

从头开始学数据结构02

从头开始学 RecyclerView LayoutManager