Guest User

Lambert/Diffuse Lit Shader Example for Universal RP

a guest
Jul 27th, 2020
680
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Example Shader for Universal RP
  2. // Written by @Cyanilux
  3. // https://cyangamedev.wordpress.com/urp-shader-code/
  4. Shader "Custom/LambertShaderExample" {
  5. Properties {
  6. _BaseMap ("Example Texture", 2D) = "white" {}
  7. _BaseColor ("Example Colour", Color) = (0, 0.66, 0.73, 1)
  8. _Cutoff ("Alpha Cutoff", Float) = 0.5
  9. }
  10. SubShader {
  11. Tags { "RenderType"="Opaque" "RenderPipeline"="UniversalPipeline" }
  12.  
  13. HLSLINCLUDE
  14. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  15.  
  16. CBUFFER_START(UnityPerMaterial)
  17. float4 _BaseMap_ST;
  18. float4 _BaseColor;
  19. float _Cutoff;
  20. CBUFFER_END
  21. ENDHLSL
  22.  
  23. Pass {
  24. Name "Example"
  25. Tags { "LightMode"="UniversalForward" }
  26.  
  27. HLSLPROGRAM
  28. #pragma vertex vert
  29. #pragma fragment frag
  30.  
  31. #pragma multi_compile _ _MAIN_LIGHT_SHADOWS
  32. #pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
  33. #pragma multi_compile _ _SHADOWS_SOFT
  34.  
  35. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
  36.  
  37. struct Attributes {
  38. float4 positionOS : POSITION;
  39. float2 uv : TEXCOORD0;
  40. float4 color : COLOR;
  41.  
  42. float4 normalOS : NORMAL;
  43. };
  44.  
  45. struct Varyings {
  46. float4 positionCS : SV_POSITION;
  47. float2 uv : TEXCOORD0;
  48. float4 color : COLOR;
  49.  
  50. float3 normalWS : NORMAL;
  51. float3 positionWS : TEXCOORD2;
  52. };
  53.  
  54. TEXTURE2D(_BaseMap);
  55. SAMPLER(sampler_BaseMap);
  56.  
  57. Varyings vert(Attributes IN) {
  58. Varyings OUT;
  59.  
  60. VertexPositionInputs positionInputs = GetVertexPositionInputs(IN.positionOS.xyz);
  61. OUT.positionCS = positionInputs.positionCS;
  62. // Or this :
  63. //OUT.positionCS = TransformObjectToHClip(IN.positionOS.xyz);
  64.  
  65. OUT.uv = TRANSFORM_TEX(IN.uv, _BaseMap);
  66. OUT.color = IN.color;
  67.  
  68. OUT.positionWS = positionInputs.positionWS;
  69.  
  70. VertexNormalInputs normalInputs = GetVertexNormalInputs(IN.normalOS.xyz);
  71. OUT.normalWS = normalInputs.normalWS;
  72.  
  73. return OUT;
  74. }
  75.  
  76. half4 frag(Varyings IN) : SV_Target {
  77. half4 baseMap = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, IN.uv);
  78. half4 color = baseMap * _BaseColor * IN.color;
  79.  
  80. float4 shadowCoord = TransformWorldToShadowCoord(IN.positionWS.xyz);
  81. Light light = GetMainLight(shadowCoord);
  82. half3 shading = LightingLambert(light.color, light.direction, IN.normalWS);
  83.  
  84. return half4(color.rgb * shading * light.shadowAttenuation, color.a);
  85. }
  86. ENDHLSL
  87. }
  88.  
  89. Pass {
  90. Name "ShadowCaster"
  91. Tags { "LightMode"="ShadowCaster" }
  92.  
  93. ZWrite On
  94. ZTest LEqual
  95.  
  96. HLSLPROGRAM
  97. // Required to compile gles 2.0 with standard srp library
  98. #pragma prefer_hlslcc gles
  99. #pragma exclude_renderers d3d11_9x gles
  100. //#pragma target 4.5
  101.  
  102. // Material Keywords
  103. #pragma shader_feature _ALPHATEST_ON
  104. #pragma shader_feature _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
  105.  
  106. // GPU Instancing
  107. #pragma multi_compile_instancing
  108. #pragma multi_compile _ DOTS_INSTANCING_ON
  109.  
  110. #pragma vertex ShadowPassVertex
  111. #pragma fragment ShadowPassFragment
  112.  
  113. //#include "Packages/com.unity.render-pipelines.universal/Shaders/LitInput.hlsl"
  114. // Note, the Lit shader that URP provides uses this, but it also handles the cbuffer which we already have.
  115. // We could change the shader to use their cbuffer, but we can also just do this :
  116. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/CommonMaterial.hlsl"
  117. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/SurfaceInput.hlsl"
  118. #include "Packages/com.unity.render-pipelines.universal/Shaders/ShadowCasterPass.hlsl"
  119.  
  120. ENDHLSL
  121. }
  122. }
  123. }
RAW Paste Data