话不多说,直接上代码,具体区别我后边会补充上,
暂时委屈各位,先锻炼下自己的“找不同”的能力,哈哈:
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"
}