Built-in转URP Chapter8(1)-AlphaTest 透明度测试

这篇博客对比了两种在Unity中实现AlphaTest的技术:传统的Built-CG写法和使用Unity Universal Render Pipeline (URP) 加 HLSL的写法。内容详细展示了两种方法的Shader代码,并分析了它们在处理透明度剪裁时的差异。
摘要由CSDN通过智能技术生成

话不多说,直接上代码,具体区别我后边会补充上,

暂时委屈各位,先锻炼下自己的“找不同”的能力,哈哈:

Built-CG写法:

Shader "Unity Shaders Book/Chapter 8/Alpha Test" {
	Properties {
		_Color("Color Tint", Color) = (1,1,1,1)
		_MainTex("Main Tex", 2D) = "white" {}
		_Cutoff("Alpha Cutoff", Range(0, 1)) = 0
	}

	SubShader {
		Tags {"Queue" = "AlphaTest" "IgnoreProjector" = "True" "RenderType" = "TransparentCutout"}

		Pass {
			Tags {"LightMode" = "ForwardBase"}

			CGPROGRAM
			#include "Lighting.cginc"

			#pragma vertex vert
			#pragma fragment frag

			fixed4 _Color;
			sampler2D _MainTex;
			float4 _MainTex_ST;
			fixed _Cutoff;

			struct a2v {
				float4 vertex : POSITION;
				float3 normal : NORMAL;
				float4 texcoord : TEXCOORD0;
			};

			struct v2f {
				float4 pos : SV_POSITION;
				float3 worldNormal : TEXCOORD0;
				float3 worldPos : TEXCOORD1;
				float2 uv : TEXCOORD2;
			};

			v2f vert(a2v i) {
				v2f o;
				o.pos = UnityObjectToClipPos(i.vertex);
				o.worldNormal = UnityObjectToWorldNormal(i.normal);
				o.worldPos = mul(unity_ObjectToWorld, i.vertex);
				o.uv = i.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
				return o;
			}

			fixed4 frag(v2f i) : SV_Target {
				fixed3 worldLightDir = UnityWorldSpaceLightDir(i.worldPos);
				fixed3 worldNormal = normalize(i.worldNormal);

				fixed4 texColor = tex2D(_MainTex, i.uv);

				clip(texColor.a - _Cutoff);

				fixed3 albedo = texColor.rgb * _Color.rgb;
				fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;
				fixed3 diffuse = _LightColor0.rgb * saturate(dot(worldLightDir, worldNormal)) * albedo;
				return fixed4(ambient + diffuse, 1.0);
			}

			ENDCG
		}
	}

	FallBack "Transparent/Cutout/VertexLit"
}

URP+HLSL写法:

Shader "Unity Shaders Book/Chapter 8/Alpha Test" {
	Properties {
		_Color("Color Tint", Color) = (1,1,1,1)
		_MainTex("Main Tex", 2D) = "white" {}
		_Cutoff("Alpha Cutoff", Range(0, 1)) = 0
		_LineWidth("Burn Line Width", Range(0.0, 0.2)) = 0.1
		[HDR] _BurnColor("Burn Color", Color) = (2.5,1,1,1)
	}

	SubShader {
		Tags {
				"RenderPipeline"="UniversalPipeline"	
				"Queue" = "AlphaTest" "IgnoreProjector" = "True" "RenderType" = "TransparentCutout"
			}

		Pass {
			Tags {"LightMode" = "UniversalForward"}

			HLSLPROGRAM
			#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
			#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"

			#pragma vertex vert
			#pragma fragment frag

CBUFFER_START(UnityPerMaterial)
			half4 _Color;
			float4 _MainTex_ST;
			half _Cutoff;
			half _LineWidth;
			real4 _BurnColor;
CBUFFER_END

			TEXTURE2D(_MainTex);
			SAMPLER(sampler_MainTex);

			struct a2v {
				float4 vertex : POSITION;
				half3 normal : NORMAL;
				float4 texcoord : TEXCOORD0;
			};

			struct v2f {
				float4 pos : SV_POSITION;
				half3 worldNormal : TEXCOORD0;
				float3 worldPos : TEXCOORD1;
				float2 uv : TEXCOORD2;
			};

			v2f vert(a2v i) {
				v2f o;
				o.pos = TransformObjectToHClip(i.vertex.xyz);
				o.worldNormal = TransformObjectToWorldNormal(i.normal);
				o.worldPos = mul(UNITY_MATRIX_M, i.vertex).xyz;
				o.uv = i.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
				return o;
			}

			half4 frag(v2f i) : SV_Target {
				half3 worldLightDir = normalize(_MainLightPosition.xyz);
				half3 worldNormal = normalize(i.worldNormal);

				half4 texColor = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);

				clip(texColor.a - _Cutoff);

				half3 albedo = texColor.rgb * _Color.rgb;
				half3 ambient = half3(unity_SHAr.w, unity_SHAg.w, unity_SHAb.w) * albedo;
				half3 diffuse = _MainLightColor.rgb * saturate(dot(worldLightDir, worldNormal)) * albedo;
				// half t = 1 - smoothstep(0.0, _LineWidth, texColor.a - _Cutoff);
				// half3 finalColor = lerp(ambient + diffuse, _BurnColor, t * step(0.001, _Cutoff));
				// return half4(finalColor, 1);
				return half4(ambient + diffuse, 1.0);
			}

			ENDHLSL
		}
	}

	FallBack "Universal Render Pipeline/Simple Lit"
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值