话不多说,直接上代码,具体区别我后边会补充上,
暂时委屈各位,先锻炼下自己的“找不同”的能力:
Built-CG写法:
Shader "Unity Shaders Book/Chapter 6/Diffuse Pixel-Level" {
Properties {
_Diffuse("Diffuse", Color) = (1, 1, 1, 1)
}
SubShader {
Pass {
Tags {
"LightMode"="ForwardBase"
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
fixed4 _Diffuse;
struct v2f {
float4 pos : SV_POSITION;
float3 worldNor : TEXCOORD0;
};
v2f vert(appdata_full data) {
v2f v;
v.pos = UnityObjectToClipPos(data.vertex);
v.worldNor = mul(data.normal, (float3x3)unity_WorldToObject);
return v;
}
fixed4 frag(v2f v) : SV_Target {
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
fixed3 worldVetNormal = normalize(v.worldNor);
fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);
fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldVetNormal, worldLightDir));
fixed3 color = diffuse + ambient;
return fixed4(color, 1);
}
ENDCG
}
}
FallBack "Diffuse"
}
URP+HLSL写法:
Shader "Unity Shaders Book/Chapter 6/Diffuse Pixel-Level" {
Properties {
_Diffuse("Diffuse", Color) = (1, 1, 1, 1)
}
SubShader {
Tags {
"RenderPipeline"="UniversalPipeline"
}
Pass {
Tags {
"LightMode"="UniversalForward"
}
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
CBUFFER_START(UnityPerMaterial)
half4 _Diffuse;
CBUFFER_END
struct a2v {
float4 vertex : POSITION;
half3 normal : NORMAL;
};
struct v2f {
float4 pos : SV_POSITION;
half3 worldNormal : TEXCOORD0;
};
v2f vert(a2v data) {
v2f v;
v.pos = TransformObjectToHClip(data.vertex.xyz);
v.worldNormal = mul(data.normal, (float3x3)UNITY_MATRIX_I_M);
return v;
}
half4 frag(v2f v) : SV_Target {
half3 ambient = half3(unity_SHAr.w, unity_SHAg.w, unity_SHAb.w);
half3 worldNormal = normalize(v.worldNormal);
half3 worldLightDir = normalize(_MainLightPosition.xyz);
half3 diffuse = _MainLightColor.rgb * _Diffuse.rgb * saturate(dot(worldNormal, worldLightDir));
half3 color = diffuse + ambient;
return half4(color, 1);
}
ENDHLSL
}
}
FallBack "Universal Render Pipeline/Simple Lit"
}