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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
| Shader "Custom/CustomLitShader" { Properties { [MainTexture] _BaseMap("Base Map", 2D) = "white" {} [MainColor] _BaseColor("Base Color", Color) = (1, 1, 1, 1) _BumpMap("Normal Map", 2D) = "bump" {} _BumpScale("Normal Scale", Range(0, 2)) = 1.0 _Metallic("Metallic", Range(0, 1)) = 0.0 _Smoothness("Smoothness", Range(0, 1)) = 0.5 _SmoothnessTextureChannel("Smoothness Map Channel", Float) = 0 _SpecColor("Specular Color", Color) = (0.2, 0.2, 0.2, 1) _SpecularHighlights("Specular Highlights", Float) = 1.0 [HDR] _EmissionColor("Emission Color", Color) = (0, 0, 0, 1) _EmissionMap("Emission Map", 2D) = "white" {} _OcclusionMap("Occlusion Map", 2D) = "white" {} _OcclusionStrength("Occlusion Strength", Range(0, 1)) = 1.0 [ToggleOff] _SpecularOcclusion("Specular Occlusion", Float) = 1.0 // Advanced options [Enum(UV0, 0, UV1, 1)] _UVSec("UV Set for secondary textures", Float) = 0 [Enum(Off, 0, On, 1)] _EnvironmentReflections("Environment Reflections", Int) = 1 [Enum(Off, 0, On, 1)] _SpecularHighlights("Specular Highlights", Int) = 1 [Enum(Off, 0, On, 1)] _ReceiveShadows("Receive Shadows", Int) = 1 // Blending options [Enum(UnityEngine.Rendering.BlendMode)] _SrcBlend("Src Blend", Int) = 1 [Enum(UnityEngine.Rendering.BlendMode)] _DstBlend("Dst Blend", Int) = 0 [Enum(Off, 0, On, 1)] _ZWrite("Z Write", Int) = 1 }
SubShader { Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" "UniversalMaterialType" = "Lit" "IgnoreProjector" = "True" "ShaderModel" = "4.5" } Pass { Name "ForwardLit" Tags { "LightMode" = "UniversalForward" }
Blend [_SrcBlend] [_DstBlend] ZWrite [_ZWrite] ZTest LEqual Cull Back
HLSLPROGRAM #pragma target 4.5 // ------------------------------------- // Material Keywords #pragma shader_feature_local _NORMALMAP #pragma shader_feature_local _SPECULARHIGHLIGHTS_OFF #pragma shader_feature_local _ENVIRONMENTREFLECTIONS_OFF #pragma shader_feature_local _SPECULAR_SETUP #pragma shader_feature_local_fragment _RECEIVE_SHADOWS_OFF
// ------------------------------------- // Universal Render Pipeline keywords #pragma multi_compile _ _MAIN_LIGHT_SHADOWS #pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE #pragma multi_compile _ _ADDITIONAL_LIGHTS_VERTEX _ADDITIONAL_LIGHTS #pragma multi_compile_fragment _ _ADDITIONAL_LIGHT_SHADOWS #pragma multi_compile_fragment _ _SHADOWS_SOFT #pragma multi_compile_fragment _ _SCREEN_SPACE_OCCLUSION // ------------------------------------- // Unity defined keywords #pragma multi_compile_fog #pragma multi_compile_instancing #pragma instancing_options renderinglayer #pragma vertex LitPassVertex #pragma fragment LitPassFragment
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl" #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/UnityGBuffer.hlsl" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderGraphFunctions.hlsl"
// To prevent SRP Batcher to break, we need to define the CBUFFER CBUFFER_START(UnityPerMaterial) float4 _BaseMap_ST; half4 _BaseColor; half _BumpScale; half _Metallic; half _Smoothness; half _SmoothnessTextureChannel; half _OcclusionStrength; half _SpecularOcclusion; half4 _SpecColor; half4 _EmissionColor; CBUFFER_END
// Textures TEXTURE2D(_BaseMap); SAMPLER(sampler_BaseMap); TEXTURE2D(_BumpMap); SAMPLER(sampler_BumpMap); TEXTURE2D(_EmissionMap); SAMPLER(sampler_EmissionMap); TEXTURE2D(_OcclusionMap); SAMPLER(sampler_OcclusionMap);
// Input structure struct Attributes { float4 positionOS : POSITION; float3 normalOS : NORMAL; float4 tangentOS : TANGENT; float2 uv : TEXCOORD0; float2 uv1 : TEXCOORD1; float2 uv2 : TEXCOORD2; UNITY_VERTEX_INPUT_INSTANCE_ID };
// Varyings structure struct Varyings { float4 positionCS : SV_POSITION; float3 positionWS : TEXCOORD0; float3 normalWS : TEXCOORD1; float4 tangentWS : TEXCOORD2; float2 uv : TEXCOORD3; float2 lightmapUV : TEXCOORD4; half4 fogFactorAndVertexLight : TEXCOORD5; #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR) float4 shadowCoord : TEXCOORD6; #endif #if defined(MAIN_LIGHT_CALCULATE_SHADOWS) float4 shadowCoord : TEXCOORD7; #endif UNITY_VERTEX_INPUT_INSTANCE_ID UNITY_VERTEX_OUTPUT_STEREO };
// Surface data structure struct SurfaceData { half3 albedo; half3 specular; half metallic; half3 emission; half smoothness; half occlusion; half alpha; half3 normalTS; half3 viewDirWS; };
// Vertex function Varyings LitPassVertex(Attributes input) { Varyings output; UNITY_SETUP_INSTANCE_ID(input); UNITY_TRANSFER_INSTANCE_ID(input, output); UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz); VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS); output.positionCS = vertexInput.positionCS; output.positionWS = vertexInput.positionWS; output.normalWS = normalInput.normalWS; output.tangentWS = half4(normalInput.tangentWS, normalInput.sign); output.uv = TRANSFORM_TEX(input.uv, _BaseMap); output.lightmapUV = input.uv1.xy * unity_LightmapST.xy + unity_LightmapST.zw; OUTPUT_LIGHTMAP_UV(input.uv1.xy, unity_LightmapST, output.lightmapUV); OUTPUT_SH(output.normalWS.xyz);
output.fogFactorAndVertexLight = half4(0, 0, 0, 0); #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR) output.shadowCoord = TransformWorldToShadowCoord(output.positionWS); #endif
return output; }
// Get surface data from input SurfaceData InitializeSurfaceData(Varyings input) { SurfaceData surfaceData; // Sample base map half4 baseMap = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, input.uv); surfaceData.albedo = baseMap.rgb * _BaseColor.rgb; surfaceData.alpha = baseMap.a * _BaseColor.a; // Sample normal map #ifdef _NORMALMAP half4 normalMap = SAMPLE_TEXTURE2D(_BumpMap, sampler_BumpMap, input.uv); surfaceData.normalTS = UnpackNormalScale(normalMap, _BumpScale); #else surfaceData.normalTS = half3(0.0, 0.0, 1.0); #endif // Metallic and smoothness surfaceData.metallic = _Metallic; surfaceData.smoothness = _Smoothness; // Specular color surfaceData.specular = _SpecColor.rgb; // Emission half4 emissionMap = SAMPLE_TEXTURE2D(_EmissionMap, sampler_EmissionMap, input.uv); surfaceData.emission = emissionMap.rgb * _EmissionColor.rgb; // Occlusion half4 occlusionMap = SAMPLE_TEXTURE2D(_OcclusionMap, sampler_OcclusionMap, input.uv); surfaceData.occlusion = LerpOneTo(occlusionMap.g, _OcclusionStrength); // View direction surfaceData.viewDirWS = GetWorldSpaceNormalizeViewDir(input.positionWS); return surfaceData; }
// Fragment function half4 LitPassFragment(Varyings input) : SV_Target { UNITY_SETUP_INSTANCE_ID(input); UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
// Initialize surface data SurfaceData surfaceData = InitializeSurfaceData(input); // Transform normal from tangent space to world space half3 normalWS = TransformTangentToWorld(surfaceData.normalTS, half3x3(input.tangentWS.xyz, cross(input.normalWS, input.tangentWS.xyz) * input.tangentWS.w, input.normalWS)); normalWS = NormalizeNormalPerPixel(normalWS); // Get main light Light mainLight = GetMainLight(); // Calculate lighting half3 diffuse = surfaceData.albedo * mainLight.color; half3 specular = surfaceData.specular * mainLight.color; // Apply shadows #if defined(_MAIN_LIGHT_SHADOWS) && !defined(_RECEIVE_SHADOWS_OFF) float4 shadowCoord = TransformWorldToShadowCoord(input.positionWS); half shadowAtten = MainLightShadow(shadowCoord); diffuse *= shadowAtten; specular *= shadowAtten; #endif // Calculate final color half3 finalColor = diffuse + surfaceData.emission; // Apply fog #ifdef LOREPLACE_fog finalColor = MixFog(finalColor, input.fogFactorAndVertexLight.w); #endif return half4(finalColor, surfaceData.alpha); } ENDHLSL }
// Shadow Caster Pass Pass { Name "ShadowCaster" Tags { "LightMode" = "ShadowCaster" }
ZWrite On ZTest LEqual ColorMask 0
HLSLPROGRAM #pragma target 4.5 #pragma multi_compile_instancing #pragma multi_compile_vertex _ _CASTING_PUNCTUAL_LIGHT_SHADOW
#pragma vertex ShadowPassVertex #pragma fragment ShadowPassFragment
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShadowCaster.hlsl"
float3 _LightDirection;
struct Attributes { float4 positionOS : POSITION; float3 normalOS : NORMAL; float2 uv : TEXCOORD0; UNITY_VERTEX_INPUT_INSTANCE_ID };
struct Varyings { float2 uv : TEXCOORD0; float4 positionCS : SV_POSITION; UNITY_VERTEX_INPUT_INSTANCE_ID UNITY_VERTEX_OUTPUT_STEREO };
Varyings ShadowPassVertex(Attributes input) { Varyings output; UNITY_SETUP_INSTANCE_ID(input); UNITY_TRANSFER_INSTANCE_ID(input, output); UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
output.uv = TRANSFORM_TEX(input.uv, _BaseMap); output.positionCS = TransformObjectToHClip(input.positionOS.xyz); float3 positionWS = TransformObjectToWorld(input.positionOS.xyz); float3 normalWS = TransformObjectToWorldNormal(input.normalOS);
output.positionCS = TransformWorldToHClip(ApplyShadowBias(positionWS, normalWS, _LightDirection));
#if UNITY_REVERSED_Z output.positionCS.z = min(output.positionCS.z, output.positionCS.w * UNITY_NEAR_CLIP_VALUE); #else output.positionCS.z = max(output.positionCS.z, output.positionCS.w * UNITY_NEAR_CLIP_VALUE); #endif
return output; }
half4 ShadowPassFragment(Varyings input) : SV_Target { UNITY_SETUP_INSTANCE_ID(input); UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input); half4 baseMap = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, input.uv); clip(baseMap.a * _BaseColor.a - 0.001); return 0; } ENDHLSL }
// Depth Only Pass Pass { Name "DepthOnly" Tags { "LightMode" = "DepthOnly" }
ZWrite On ColorMask 0
HLSLPROGRAM #pragma target 4.5 #pragma multi_compile_instancing
#pragma vertex DepthOnlyVertex #pragma fragment DepthOnlyFragment
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
struct Attributes { float4 positionOS : POSITION; float2 uv : TEXCOORD0; UNITY_VERTEX_INPUT_INSTANCE_ID };
struct Varyings { float2 uv : TEXCOORD0; float4 positionCS : SV_POSITION; UNITY_VERTEX_INPUT_INSTANCE_ID UNITY_VERTEX_OUTPUT_STEREO };
Varyings DepthOnlyVertex(Attributes input) { Varyings output; UNITY_SETUP_INSTANCE_ID(input); UNITY_TRANSFER_INSTANCE_ID(input, output); UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
output.positionCS = TransformObjectToHClip(input.positionOS.xyz); output.uv = TRANSFORM_TEX(input.uv, _BaseMap); return output; }
half4 DepthOnlyFragment(Varyings input) : SV_Target { UNITY_SETUP_INSTANCE_ID(input); UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input); half4 baseMap = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, input.uv); clip(baseMap.a * _BaseColor.a - 0.001); return 0; } ENDHLSL } } Fallback "Universal Render Pipeline/Lit" CustomEditor "UnityEditor.Rendering.Universal.ShaderGUI.LitShader" }
|