1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
| Shader "Custom/URP/ToonShading" { Properties { _BaseColor ("Base Color", Color) = (1,1,1,1) _BaseMap ("Base Texture", 2D) = "white" {} // 色阶控制(卡通光照分级) _ShadowColor ("Shadow Color", Color) = (0.4, 0.5, 0.7, 1) _ShadowThreshold ("Shadow Threshold", Range(0,1)) = 0.5 _ShadowSmooth ("Shadow Smooth", Range(0.001, 0.3)) = 0.05 _HighlightColor ("Highlight Color", Color) = (1,1,1,1) _HighlightThreshold ("Highlight Threshold", Range(0,1)) = 0.9 _HighlightSmooth ("Highlight Smooth", Range(0.001, 0.1)) = 0.02 // Rim Light(边缘光) _RimColor ("Rim Color", Color) = (0.5, 0.7, 1.0, 1) _RimPower ("Rim Power", Range(1, 8)) = 3.0 _RimStrength ("Rim Strength", Range(0, 1)) = 0.4 // 描边(通过法线扩展实现) _OutlineColor ("Outline Color", Color) = (0.1, 0.1, 0.15, 1) _OutlineWidth ("Outline Width", Range(0, 0.05)) = 0.01 // 法线贴图 _BumpMap ("Normal Map", 2D) = "bump" {} _BumpScale ("Normal Scale", Range(0,2)) = 1.0 }
SubShader { Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" "Queue" = "Geometry" }
// ===== Pass 1:描边 Pass(法线外扩)===== Pass { Name "Outline" // 只渲染背面(法线外扩后背面可见) Cull Front
HLSLPROGRAM #pragma vertex vertOutline #pragma fragment fragOutline
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
CBUFFER_START(UnityPerMaterial) float4 _BaseMap_ST; float4 _BaseColor; float4 _ShadowColor; float4 _HighlightColor; float4 _RimColor; float4 _OutlineColor; float _ShadowThreshold; float _ShadowSmooth; float _HighlightThreshold; float _HighlightSmooth; float _RimPower; float _RimStrength; float _OutlineWidth; float _BumpScale; CBUFFER_END
struct OutlineAttributes { float4 positionOS : POSITION; float3 normalOS : NORMAL; UNITY_VERTEX_INPUT_INSTANCE_ID };
struct OutlineVaryings { float4 positionHCS : SV_POSITION; UNITY_VERTEX_OUTPUT_STEREO };
OutlineVaryings vertOutline(OutlineAttributes IN) { OutlineVaryings OUT; UNITY_SETUP_INSTANCE_ID(IN); UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
// 在对象空间沿法线方向外扩顶点 float3 expandedPos = IN.positionOS.xyz + normalize(IN.normalOS) * _OutlineWidth; OUT.positionHCS = TransformObjectToHClip(expandedPos); return OUT; }
half4 fragOutline(OutlineVaryings IN) : SV_Target { return _OutlineColor; } ENDHLSL }
// ===== Pass 2:卡通光照 Pass ===== Pass { Name "ToonForward" Tags { "LightMode" = "UniversalForward" } Cull Back
HLSLPROGRAM #pragma vertex vert #pragma fragment frag #pragma multi_compile _ _MAIN_LIGHT_SHADOWS _MAIN_LIGHT_SHADOWS_CASCADE #pragma multi_compile _ _ADDITIONAL_LIGHTS #pragma multi_compile_fog
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
TEXTURE2D(_BaseMap); SAMPLER(sampler_BaseMap); TEXTURE2D(_BumpMap); SAMPLER(sampler_BumpMap);
CBUFFER_START(UnityPerMaterial) float4 _BaseMap_ST; float4 _BaseColor; float4 _ShadowColor; float4 _HighlightColor; float4 _RimColor; float4 _OutlineColor; float _ShadowThreshold; float _ShadowSmooth; float _HighlightThreshold; float _HighlightSmooth; float _RimPower; float _RimStrength; float _OutlineWidth; float _BumpScale; CBUFFER_END
struct Attributes { float4 positionOS : POSITION; float3 normalOS : NORMAL; float4 tangentOS : TANGENT; float2 uv : TEXCOORD0; UNITY_VERTEX_INPUT_INSTANCE_ID };
struct Varyings { float4 positionHCS : SV_POSITION; float2 uv : TEXCOORD0; float3 positionWS : TEXCOORD1; float3 normalWS : TEXCOORD2; float3 tangentWS : TEXCOORD3; float3 bitangentWS : TEXCOORD4; float4 shadowCoord : TEXCOORD5; float fogFactor : TEXCOORD6; UNITY_VERTEX_OUTPUT_STEREO };
// ======== 卡通色阶光照核心函数 ========
// 将连续的 NdotL 值分级为离散的色阶 float toonDiffuse(float NdotL, float threshold, float smooth) { // smoothstep 产生软边缘,模拟软阴影效果 // 硬边缘:step(threshold, NdotL) return smoothstep(threshold - smooth, threshold + smooth, NdotL); }
// 卡通高光(Toon Specular) float toonSpecular(float NdotH, float threshold, float smooth) { return smoothstep(threshold - smooth, threshold + smooth, NdotH); }
Varyings vert(Attributes IN) { Varyings OUT; UNITY_SETUP_INSTANCE_ID(IN); UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
VertexPositionInputs posInputs = GetVertexPositionInputs(IN.positionOS.xyz); VertexNormalInputs normalInputs = GetVertexNormalInputs(IN.normalOS, IN.tangentOS);
OUT.positionHCS = posInputs.positionCS; OUT.positionWS = posInputs.positionWS; OUT.normalWS = normalInputs.normalWS; OUT.tangentWS = normalInputs.tangentWS; OUT.bitangentWS = normalInputs.bitangentWS; OUT.uv = TRANSFORM_TEX(IN.uv, _BaseMap); OUT.shadowCoord = GetShadowCoord(posInputs); OUT.fogFactor = ComputeFogFactor(posInputs.positionCS.z); return OUT; }
half4 frag(Varyings IN) : SV_Target { // === 法线 === float4 normalPacked = SAMPLE_TEXTURE2D(_BumpMap, sampler_BumpMap, IN.uv); float3 normalTS = UnpackNormalScale(normalPacked, _BumpScale); float3x3 TBN = float3x3(normalize(IN.tangentWS), normalize(IN.bitangentWS), normalize(IN.normalWS)); float3 normalWS = normalize(TransformTangentToWorld(normalTS, TBN));
// === 基础颜色 === half4 baseColor = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, IN.uv) * _BaseColor;
// === 主光源(带阴影) === Light mainLight = GetMainLight(IN.shadowCoord); float3 lightDir = mainLight.direction; float3 lightColor = mainLight.color; float shadowAttenuation = mainLight.shadowAttenuation;
float3 viewDirWS = normalize(GetCameraPositionWS() - IN.positionWS);
// NdotL(乘以阴影衰减,让阴影影响色阶分级) float NdotL = saturate(dot(normalWS, lightDir)) * shadowAttenuation; float3 halfDir = normalize(viewDirWS + lightDir); float NdotH = saturate(dot(normalWS, halfDir));
// === 卡通漫反射(三色阶:高光/中间/阴影)=== float toonDiff = toonDiffuse(NdotL, _ShadowThreshold, _ShadowSmooth); float toonSpec = toonSpecular(NdotH, _HighlightThreshold, _HighlightSmooth);
// 颜色混合:阴影色 → 基础色 → 高光色 half3 diffuseColor = lerp(_ShadowColor.rgb, baseColor.rgb, toonDiff); diffuseColor = lerp(diffuseColor, _HighlightColor.rgb, toonSpec);
// 乘以光源颜色 half3 litColor = diffuseColor * lightColor;
// === Rim Light(边缘光)=== float NdotV = saturate(dot(normalWS, viewDirWS)); float rimFactor = pow(1.0 - NdotV, _RimPower); // Rim 只在亮面出现(背光面不加边缘光) rimFactor *= saturate(NdotL * 2.0); half3 rimLight = _RimColor.rgb * rimFactor * _RimStrength; litColor += rimLight;
// === 额外光源(简化为 Lambert,保持卡通风格) === #ifdef _ADDITIONAL_LIGHTS uint additionalLightCount = GetAdditionalLightsCount(); for (uint i = 0; i < additionalLightCount; i++) { Light addLight = GetAdditionalLight(i, IN.positionWS); float addNdotL = saturate(dot(normalWS, addLight.direction)); float addToon = toonDiffuse(addNdotL * addLight.distanceAttenuation, _ShadowThreshold, _ShadowSmooth); litColor += baseColor.rgb * addLight.color * addToon * 0.5; } #endif
// === 环境光(球谐函数)=== half3 ambient = SampleSH(normalWS) * baseColor.rgb * 0.3; half3 finalColor = litColor + ambient;
// 雾效 finalColor = MixFog(finalColor, IN.fogFactor);
return half4(finalColor, baseColor.a); } ENDHLSL }
UsePass "Universal Render Pipeline/Lit/ShadowCaster" UsePass "Universal Render Pipeline/Lit/DepthOnly" } }
|