话不多说,直接上代码,具体区别我后边会补充上,
暂时委屈各位,先锻炼下自己的“找不同”的能力,哈哈:
Built-CG写法:
Shader "Unity Shaders Book/Chapter 7/Mask Texture" {
Properties {
_Color("Color", Color) = (1, 1, 1, 1)
_MainTex("MainTex", 2D) = "white"{}
_BumpMap("BumpTex", 2D) = "white"{}
_BumpScale("BumpScale", Range(-2, 2)) = 1.0
_SpecularMask("SpecularMask", 2D) = "white"{}
_SpecularScale("SpecularScale", Range(-2, 2)) = 1.0
_Specular("Specular", Color) = (1, 1, 1, 1)
_Gloss("Gloss", Range(10, 256)) = 20
}
SubShader {
Pass {
Tags {
"LightMode"="ForwardBase"
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
fixed4 _Color;
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _BumpMap;
float _BumpScale;
sampler2D _SpecularMask;
float _SpecularScale;
fixed4 _Specular;
float _Gloss;
struct a2v {
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 tangent : TANGENT;
float4 texcoord : TEXCOORD0;
};
struct v2f {
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float3 lightDir : TEXCOORD1;
float3 viewDir : TEXCOORD2;
};
v2f vert(a2v v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv.xy = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
// float3 binormal = cross(v.normal, v.tangent.xyz) * v.tangent.w;
// float3x3 rotation = float3x3(v.tangent.xyz, binormal, v.normal);
TANGENT_SPACE_ROTATION;
o.lightDir = mul(rotation, ObjSpaceLightDir(v.vertex)).xyz;
o.viewDir = mul(rotation, ObjSpaceViewDir(v.vertex)).xyz;
return o;
// v2f o;
// o.pos = UnityObjectToClipPos(v.vertex);
// o.uv.xy = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
// TANGENT_SPACE_ROTATION;
// o.lightDir = mul(rotation, ObjSpaceLightDir(v.vertex)).xyz;
// o.viewDir = mul(rotation, ObjSpaceViewDir(v.vertex)).xyz;
// return o;
}
fixed4 frag(v2f v) : SV_Target {
fixed3 tangentLightDir = normalize(v.lightDir);
fixed3 tangentViewDir = normalize(v.viewDir);
fixed3 tangentNormal = UnpackNormal(tex2D(_BumpMap, v.uv));
tangentNormal.xy *= _BumpScale;
tangentNormal.z = sqrt(1.0 - max(0, dot(tangentNormal.xy, tangentNormal.xy)));
fixed3 albedo = tex2D(_MainTex, v.uv).rgb * _Color.rgb;
// 环境光
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;
// 漫反射
fixed3 diffuse = _LightColor0.rgb * albedo * max(0, dot(tangentNormal, tangentViewDir));
// 高光
fixed3 halfDir = normalize(tangentViewDir + tangentLightDir);
fixed specularMask = tex2D(_SpecularMask, v.uv).r * _SpecularScale;
fixed3 specular = _LightColor0.rgb * _Specular.rbg * pow(max(0, dot(halfDir, tangentNormal)), _Gloss) * specularMask;
return fixed4(ambient + diffuse + specular, 1.0);
}
ENDCG
}
}
}
URP+HLSL写法:
Shader "Unity Shaders Book/Chapter 7/Mask Texture" {
Properties {
_Color("Color", Color) = (1, 1, 1, 1)
_MainTex("MainTex", 2D) = "white"{}
_BumpMap("BumpTex", 2D) = "white"{}
_BumpScale("BumpScale", Range(-2, 2)) = 1.0
_SpecularMask("SpecularMask", 2D) = "white"{}
_SpecularScale("SpecularScale", Range(-2, 2)) = 1.0
_Specular("Specular", Color) = (1, 1, 1, 1)
_Gloss("Gloss", Range(10, 256)) = 20
}
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 _Color;
float4 _MainTex_ST;
float _BumpScale;
float _SpecularScale;
half4 _Specular;
float _Gloss;
CBUFFER_END
TEXTURE2D(_MainTex);
SAMPLER(sampler_MainTex);
TEXTURE2D(_BumpMap);
SAMPLER(sampler_BumpMap);
TEXTURE2D(_SpecularMask);
SAMPLER(sampler_SpecularMask);
struct a2v {
float4 vertex : POSITION;
half3 normal : NORMAL;
float4 tangent : TANGENT;
float4 texcoord : TEXCOORD0;
};
struct v2f {
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float3 lightDir : TEXCOORD1;
float3 viewDir : TEXCOORD2;
};
v2f vert(a2v v) {
v2f o;
o.pos = TransformObjectToHClip(v.vertex.xyz);
o.uv.xy = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
float3 binormal = cross(v.normal, v.tangent.xyz) * v.tangent.w;
float3x3 rotation = float3x3(v.tangent.xyz, binormal, v.normal);
// TANGENT_SPACE_ROTATION;
half3 objectSpaceLightDir = TransformWorldToObjectDir(_MainLightPosition.xyz);
o.lightDir = mul(rotation, objectSpaceLightDir).xyz;
half3 objectSpaceViewDir = TransformWorldToObject(_WorldSpaceCameraPos.xyz - v.vertex.xyz);
o.viewDir = mul(rotation, objectSpaceViewDir).xyz;
return o;
// v2f o;
// o.pos = UnityObjectToClipPos(v.vertex);
// o.uv.xy = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
// TANGENT_SPACE_ROTATION;
// o.lightDir = mul(rotation, ObjSpaceLightDir(v.vertex)).xyz;
// o.viewDir = mul(rotation, ObjSpaceViewDir(v.vertex)).xyz;
// return o;
}
half4 frag(v2f v) : SV_Target {
half3 tangentLightDir = normalize(v.lightDir);
half3 tangentViewDir = normalize(v.viewDir);
half3 tangentNormal = UnpackNormal(SAMPLE_TEXTURE2D(_BumpMap, sampler_BumpMap, v.uv));
tangentNormal.xy *= _BumpScale;
tangentNormal.z = sqrt(1.0 - max(0, dot(tangentNormal.xy, tangentNormal.xy)));
half3 albedo = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, v.uv).rgb * _Color.rgb;
// 环境光
half3 ambient = half3(unity_SHAr.w, unity_SHAg.w, unity_SHAb.w) * albedo;
// 漫反射
half3 diffuse = _MainLightColor.rgb * albedo * max(0, dot(tangentNormal, tangentViewDir));
// 高光
half3 halfDir = normalize(tangentViewDir + tangentLightDir);
half specularMask = SAMPLE_TEXTURE2D(_SpecularMask, sampler_SpecularMask, v.uv).r * _SpecularScale;
half3 specular = _MainLightColor.rgb * _Specular.rbg * pow(max(0, dot(halfDir, tangentNormal)), _Gloss) * specularMask;
return half4(ambient + diffuse + specular, 1.0);
}
ENDHLSL
}
}
}