Lux URP Lit Extended Uber.shader 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. Shader "Lux URP/Lit Extended Uber"
  2. {
  3. Properties
  4. {
  5. [Header(Surface Options)]
  6. [Space(8)]
  7. [Enum(UnityEngine.Rendering.CompareFunction)]
  8. _ZTest ("ZTest", Int) = 4
  9. [Enum(UnityEngine.Rendering.CullMode)]
  10. _Cull ("Culling", Float) = 2
  11. [Toggle(_ALPHATEST_ON)]
  12. _AlphaClip ("Alpha Clipping", Float) = 0.0
  13. _Cutoff (" Threshold", Range(0.0, 1.0)) = 0.5
  14. [Toggle(_FADING_ON)]
  15. _CameraFadingEnabled (" Enable Camera Fading", Float) = 0.0
  16. _CameraFadeDist (" Fade Distance", Float) = 1.0
  17. [Toggle(_FADING_SHADOWS_ON)]
  18. _CameraFadeShadows (" Fade Shadows", Float) = 0.0
  19. _CameraShadowFadeDist (" Shadow Fade Distance", Float) = 1.0
  20. [ToggleOff(_RECEIVE_SHADOWS_OFF)]
  21. _ReceiveShadows ("Receive Shadows", Float) = 1.0
  22. [Header(Surface Inputs)]
  23. [Space(8)]
  24. [MainTexture] _BaseMap ("Albedo", 2D) = "white" {}
  25. [MainColor] _BaseColor ("Color", Color) = (1,1,1,1)
  26. [Space(5)]
  27. [Toggle(_NORMALMAP)]
  28. _EnableNormal ("Enable Normal Map", Float) = 0
  29. [NoScaleOffset] _BumpMap (" Normal Map", 2D) = "bump" {}
  30. _BumpScale (" Normal Scale", Float) = 1.0
  31. [Toggle(_BENTNORMAL)]
  32. _EnableBentNormal ("Enable Bent Normal Map", Float) = 0
  33. _BentNormalMap ("Bent Normal Map", 2D) = "bump" {}
  34. [Space(5)]
  35. [Toggle(_PARALLAX)]
  36. _EnableParallax ("Enable Height Map", Float) = 0
  37. [NoScaleOffset] _HeightMap (" Height Map (G)", 2D) = "black" {}
  38. _Parallax (" Extrusion", Range (0.0, 0.1)) = 0.02
  39. [Toggle(_PARALLAXSHADOWS)]
  40. _EnableParallaxShadows ("Enable Parallax Shadows", Float) = 0
  41. [Space(5)]
  42. [Toggle(_SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A)]
  43. _SmoothnessTextureChannel ("Sample Smoothness from Albedo Alpha", Float) = 0
  44. _Smoothness ("Smoothness", Range(0.0, 1.0)) = 0.5
  45. [Header(Work Flow)]
  46. [Space(5)]
  47. [NoScaleOffset] _SpecGlossMap ("Specular Map", 2D) = "white" {}
  48. _SpecColor ("Specular Color", Color) = (0.2, 0.2, 0.2) // Order of props important for editor!?
  49. [ToggleOff(_SPECULAR_SETUP)]
  50. _WorkflowMode ("Metal Workflow", Float) = 1.0
  51. [Space(5)]
  52. [NoScaleOffset] _MetallicGlossMap (" Metallic Map", 2D) = "white" {} // Order of props important for editor!?
  53. [Gamma] _Metallic (" Metallic", Range(0.0, 1.0)) = 0.0
  54. [Space(5)]
  55. [Toggle(_METALLICSPECGLOSSMAP)]
  56. _EnableMetalSpec ("Enable Spec/Metal Map", Float) = 0.0
  57. [Header(Additional Maps)]
  58. [Space(8)]
  59. [Toggle(_OCCLUSIONMAP)]
  60. _EnableOcclusion ("Enable Occlusion", Float) = 0.0
  61. [NoScaleOffset] _OcclusionMap (" Occlusion Map", 2D) = "white" {}
  62. _OcclusionStrength (" Occlusion Strength", Range(0.0, 1.0)) = 1.0
  63. [Space(5)]
  64. [Toggle(_EMISSION)]
  65. _Emission ("Enable Emission", Float) = 0.0
  66. _EmissionColor (" Color", Color) = (0,0,0)
  67. [NoScaleOffset] _EmissionMap (" Emission", 2D) = "white" {}
  68. [Header(Detail Maps)]
  69. [Space(8)]
  70. _DetailMask("Detail Mask", 2D) = "white" {}
  71. _DetailAlbedoMapScale("Scale", Range(0.0, 2.0)) = 1.0
  72. _DetailAlbedoMap("Detail Albedo x2", 2D) = "linearGrey" {}
  73. _DetailNormalMapScale("Scale", Range(0.0, 2.0)) = 1.0
  74. [Normal] _DetailNormalMap("Normal Map", 2D) = "bump" {}
  75. [Header(Rim Lighting)]
  76. [Space(8)]
  77. [HideInInspector] _Dummy("Dummy", Float) = 0.0 // needed by custum inspector
  78. [Toggle(_RIMLIGHTING)]
  79. _Rim ("Enable Rim Lighting", Float) = 0
  80. [HDR] _RimColor ("Rim Color", Color) = (0.5,0.5,0.5,1)
  81. _RimPower ("Rim Power", Float) = 2
  82. _RimFrequency ("Rim Frequency", Float) = 0
  83. _RimMinPower (" Rim Min Power", Float) = 1
  84. _RimPerPositionFrequency (" Rim Per Position Frequency", Range(0.0, 1.0)) = 1
  85. //[Header(Stencil)]
  86. //[Space(5)]
  87. [IntRange] _Stencil ("Stencil Reference", Range (0, 255)) = 0
  88. [IntRange] _ReadMask (" Read Mask", Range (0, 255)) = 255
  89. [IntRange] _WriteMask (" Write Mask", Range (0, 255)) = 255
  90. [Enum(UnityEngine.Rendering.CompareFunction)]
  91. _StencilComp ("Stencil Comparison", Int) = 8 // always – terrain should be the first thing being rendered anyway
  92. [Enum(UnityEngine.Rendering.StencilOp)]
  93. _StencilOp ("Stencil Operation", Int) = 0 // 0 = keep, 2 = replace
  94. [Enum(UnityEngine.Rendering.StencilOp)]
  95. _StencilFail ("Stencil Fail Op", Int) = 0 // 0 = keep
  96. [Enum(UnityEngine.Rendering.StencilOp)]
  97. _StencilZFail ("Stencil ZFail Op", Int) = 0 // 0 = keep
  98. [Header(Advanced)]
  99. [Space(8)]
  100. [Toggle(_ENABLE_GEOMETRIC_SPECULAR_AA)]
  101. _GeometricSpecularAA ("Geometric Specular AA", Float) = 0.0
  102. _ScreenSpaceVariance (" Screen Space Variance", Range(0.0, 1.0)) = 0.1
  103. _SAAThreshold (" Threshold", Range(0.0, 1.0)) = 0.2
  104. [Space(5)]
  105. [Toggle(_ENABLE_AO_FROM_GI)]
  106. _AOfromGI ("Get ambient specular Occlusion from GI", Float) = 0.0
  107. _GItoAO (" GI to AO Factor", Float) = 10
  108. _GItoAOBias (" GI to AO Bias", Range(0,1)) = 0.0
  109. _HorizonOcclusion ("Horizon Occlusion", Range(0,1)) = 0.5
  110. [Space(5)]
  111. [ToggleOff]
  112. _SpecularHighlights ("Specular Highlights", Float) = 1.0
  113. [ToggleOff]
  114. _EnvironmentReflections ("Environment Reflections", Float) = 1.0
  115. // Blending state
  116. [HideInInspector] _Surface("__surface", Float) = 0.0
  117. [HideInInspector] _Blend("__blend", Float) = 0.0
  118. //[HideInInspector] _AlphaClip("__clip", Float) = 0.0
  119. [HideInInspector] _SrcBlend("__src", Float) = 1.0
  120. [HideInInspector] _DstBlend("__dst", Float) = 0.0
  121. [HideInInspector] _ZWrite("__zw", Float) = 1.0
  122. //[HideInInspector] _Cull("__cull", Float) = 2.0
  123. // _ReceiveShadows("Receive Shadows", Float) = 1.0
  124. // Editmode props
  125. [HideInInspector] _QueueOffset("Queue offset", Float) = 0.0
  126. // ObsoleteProperties
  127. [HideInInspector] _MainTex("BaseMap", 2D) = "white" {}
  128. [HideInInspector] _Color("Base Color", Color) = (1, 1, 1, 1)
  129. [HideInInspector] _GlossMapScale("Smoothness", Float) = 0.0
  130. [HideInInspector] _Glossiness("Smoothness", Float) = 0.0
  131. [HideInInspector] _GlossyReflections("EnvironmentReflections", Float) = 0.0
  132. // GUI
  133. [HideInInspector] _FoldSurfaceOptions("Surface Options", Float) = 0.0
  134. [HideInInspector] _FoldSurfaceInputs("Surface Inputs", Float) = 1.0
  135. [HideInInspector] _FoldSurfaceDetailInputs("Detail Surface Inputs", Float) = 1.0
  136. [HideInInspector] _FoldAdvancedSurfaceInputs("Advanced Surface Inputs", Float) = 1.0
  137. [HideInInspector] _FoldRimLightingInputs("Rim Lighting Options", Float) = 0.0
  138. [HideInInspector] _FoldStencilOptions("Stencil Options", Float) = 0.0
  139. [HideInInspector] _FoldAdvanced("Advanced", Float) = 0.0
  140. }
  141. SubShader
  142. {
  143. // Lightweight Pipeline tag is required. If Lightweight render pipeline is not set in the graphics settings
  144. // this Subshader will fail. One can add a subshader below or fallback to Standard built-in to make this
  145. // material work with both Lightweight Render Pipeline and Builtin Unity Pipeline
  146. Tags{"RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" "IgnoreProjector" = "True"}
  147. LOD 300
  148. // ------------------------------------------------------------------
  149. // Forward pass. Shades all light in a single pass. GI + emission + Fog
  150. Pass
  151. {
  152. // Lightmode matches the ShaderPassName set in LightweightRenderPipeline.cs. SRPDefaultUnlit and passes with
  153. // no LightMode tag are also rendered by Lightweight Render Pipeline
  154. Name "ForwardLit"
  155. Tags{"LightMode" = "UniversalForward"}
  156. Stencil {
  157. Ref [_Stencil]
  158. ReadMask [_ReadMask]
  159. WriteMask [_WriteMask]
  160. Comp [_StencilComp]
  161. Pass [_StencilOp]
  162. Fail [_StencilFail]
  163. ZFail [_StencilZFail]
  164. //replace
  165. }
  166. Blend[_SrcBlend][_DstBlend]
  167. ZTest [_ZTest]
  168. ZWrite[_ZWrite]
  169. Cull[_Cull]
  170. HLSLPROGRAM
  171. // Required to compile gles 2.0 with standard SRP library
  172. #pragma prefer_hlslcc gles
  173. #pragma exclude_renderers d3d11_9x
  174. #pragma target 2.0
  175. // -------------------------------------
  176. // Material Keywords
  177. #define _UBER
  178. #pragma shader_feature_local _NORMALMAP
  179. #pragma shader_feature_local_fragment _SAMPLENORMAL
  180. #pragma shader_feature_local_fragment _PARALLAX
  181. #pragma shader_feature_local_fragment _BENTNORMAL
  182. #pragma shader_feature_local_fragment _ENABLE_GEOMETRIC_SPECULAR_AA
  183. #pragma shader_feature_local_fragment _ENABLE_AO_FROM_GI
  184. #pragma shader_feature_local_fragment _RIMLIGHTING
  185. #pragma shader_feature_local_fragment _FADING_ON
  186. #pragma shader_feature_local_fragment _ALPHATEST_ON
  187. #pragma shader_feature_local_fragment _ALPHAPREMULTIPLY_ON
  188. #pragma shader_feature_local_fragment _EMISSION
  189. #pragma shader_feature_local_fragment _METALLICSPECGLOSSMAP
  190. #pragma shader_feature_local_fragment _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
  191. #pragma shader_feature_local_fragment _OCCLUSIONMAP
  192. #pragma shader_feature_local _DETAIL
  193. #pragma shader_feature_local_fragment _SPECULARHIGHLIGHTS_OFF
  194. #pragma shader_feature_local_fragment _ENVIRONMENTREFLECTIONS_OFF
  195. #pragma shader_feature_local_fragment _SPECULAR_SETUP
  196. #pragma shader_feature_local _RECEIVE_SHADOWS_OFF
  197. // -------------------------------------
  198. // Universal Pipeline keywords
  199. #pragma multi_compile _ _MAIN_LIGHT_SHADOWS
  200. #pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
  201. #pragma multi_compile _ _ADDITIONAL_LIGHTS_VERTEX _ADDITIONAL_LIGHTS
  202. #pragma multi_compile_fragment _ _ADDITIONAL_LIGHT_SHADOWS
  203. #pragma multi_compile_fragment _ _SHADOWS_SOFT
  204. #pragma multi_compile_fragment _ _SCREEN_SPACE_OCCLUSION
  205. #pragma multi_compile _ LIGHTMAP_SHADOW_MIXING
  206. #pragma multi_compile _ SHADOWS_SHADOWMASK
  207. // -------------------------------------
  208. // Unity defined keywords
  209. #pragma multi_compile _ DIRLIGHTMAP_COMBINED
  210. #pragma multi_compile _ LIGHTMAP_ON
  211. #pragma multi_compile_fog
  212. //--------------------------------------
  213. // GPU Instancing
  214. #pragma multi_compile_instancing
  215. // #pragma multi_compile _ DOTS_INSTANCING_ON // needs shader target 4.5
  216. #pragma multi_compile __ LOD_FADE_CROSSFADE
  217. #pragma vertex LitPassVertexUber
  218. #pragma fragment LitPassFragmentUber
  219. #include "Includes/Lux URP Lit Extended Inputs.hlsl"
  220. #include "Includes/Lux URP Uber Lit Pass.hlsl"
  221. #include "Includes/Lux URP Lit Extended Lighting.hlsl"
  222. half4 LitPassFragmentUber(Varyings input, half facing : VFACE) : SV_Target
  223. {
  224. UNITY_SETUP_INSTANCE_ID(input);
  225. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  226. // LOD crossfading
  227. #if defined(LOD_FADE_CROSSFADE) && !defined(SHADER_API_GLES)
  228. //LODDitheringTransition(input.positionCS.xy, unity_LODFade.x);
  229. clip (unity_LODFade.x - Dither32(input.positionCS.xy, 1));
  230. #endif
  231. // Camera Fading
  232. #if defined(_ALPHATEST_ON) && defined(_FADING_ON)
  233. clip ( input.positionCS.w - _CameraFadeDist - Dither32(input.positionCS.xy, 1));
  234. #endif
  235. // Calculate bitangent upfront
  236. #if defined (_NORMALMAP) || defined(_PARALLAX) || defined (_BENTNORMAL)
  237. float sgn = input.tangentWS.w; // should be either +1 or -1
  238. float3 bitangentWS = sgn * cross(input.normalWS.xyz, input.tangentWS.xyz);
  239. #else
  240. float3 bitangentWS = 0;
  241. #endif
  242. half3 viewDirWS = SafeNormalize(input.viewDirWS);
  243. #if defined(_PARALLAX)
  244. // NOTE: Take possible back faces into account.
  245. input.normalWS.xyz *= facing;
  246. half3x3 tangentSpaceRotation = half3x3(input.tangentWS.xyz, bitangentWS, input.normalWS.xyz);
  247. half3 viewDirTS = SafeNormalize( mul(tangentSpaceRotation, viewDirWS) );
  248. #else
  249. half3 viewDirTS = 0;
  250. #endif
  251. SurfaceData surfaceData;
  252. InitializeStandardLitSurfaceDataUber(input.uv, viewDirTS, surfaceData);
  253. InputData inputData;
  254. // Custom function! which uses bitangentWS and viewDirWS as additional inputs here.
  255. InitializeInputData(input, bitangentWS, viewDirWS, surfaceData.normalTS, inputData);
  256. #if defined(_BENTNORMAL)
  257. half3 bentNormal = SampleNormalExtended(input.uv, TEXTURE2D_ARGS(_BentNormalMap, sampler_BentNormalMap), 1);
  258. #if defined(_SAMPLENORMAL)
  259. bentNormal = normalize(half3(bentNormal.xy + surfaceData.normalTS.xy, bentNormal.z*surfaceData.normalTS.z));
  260. #endif
  261. bentNormal = TransformTangentToWorld(bentNormal, half3x3(input.tangentWS.xyz, bitangentWS.xyz, input.normalWS.xyz));
  262. //bentNormal = mul(GetObjectToWorldMatrix(), float4(bentNormal, 0) );
  263. bentNormal = NormalizeNormalPerPixel(bentNormal);
  264. #if !defined(LIGHTMAP_ON)
  265. inputData.bakedGI = SAMPLE_GI(input.lightmapUV, input.vertexSH, bentNormal);
  266. #endif
  267. #endif
  268. #if defined(_ENABLE_GEOMETRIC_SPECULAR_AA)
  269. half3 worldNormalFace = input.normalWS.xyz;
  270. half roughness = 1.0h - surfaceData.smoothness;
  271. //roughness *= roughness; // as in Core?
  272. half3 deltaU = ddx( worldNormalFace );
  273. half3 deltaV = ddy( worldNormalFace );
  274. half variance = _ScreenSpaceVariance * ( dot(deltaU, deltaU) + dot(deltaV, deltaV) );
  275. half kernelSquaredRoughness = min( 2.0h * variance , _SAAThreshold );
  276. half squaredRoughness = saturate( roughness * roughness + kernelSquaredRoughness );
  277. surfaceData.smoothness = 1.0h - sqrt(squaredRoughness);
  278. #endif
  279. #if defined(_RIMLIGHTING)
  280. half rim = saturate(1.0h - saturate( dot(inputData.normalWS, inputData.viewDirectionWS ) ) );
  281. half power = _RimPower;
  282. UNITY_BRANCH if(_RimFrequency > 0 ) {
  283. half perPosition = lerp(0.0h, 1.0h, dot(1.0h, frac(UNITY_MATRIX_M._m03_m13_m23) * 2.0h - 1.0h ) * _RimPerPositionFrequency ) * 3.1416h;
  284. power = lerp(power, _RimMinPower, (1.0h + sin(_Time.y * _RimFrequency + perPosition) ) * 0.5h );
  285. }
  286. surfaceData.emission += pow(rim, power) * _RimColor.rgb * _RimColor.a;
  287. #endif
  288. half4 color = LuxExtended_UniversalFragmentPBR(inputData, surfaceData.albedo, surfaceData.metallic, surfaceData.specular, surfaceData.smoothness, surfaceData.occlusion, surfaceData.emission, surfaceData.alpha,
  289. #if defined(_ENABLE_AO_FROM_GI) && defined(LIGHTMAP_ON)
  290. _GItoAO, _GItoAOBias,
  291. #else
  292. 1, 0,
  293. #endif
  294. #if defined(_BENTNORMAL)
  295. bentNormal,
  296. #else
  297. half3(0,0,0),
  298. #endif
  299. input.normalWS.xyz,
  300. _HorizonOcclusion
  301. );
  302. color.rgb = MixFog(color.rgb, inputData.fogCoord);
  303. // Preview Spec AA
  304. // color.rgb = surfaceData.smoothness;
  305. // Preview Visibility from Bent Normal
  306. // color.rgb = dot(inputData.normalWS, bentNormal); // * 0.5 + 0.5;
  307. return color;
  308. }
  309. ENDHLSL
  310. }
  311. Pass
  312. {
  313. Name "ShadowCaster"
  314. Tags{"LightMode" = "ShadowCaster"}
  315. ZWrite On
  316. ZTest [_ZTest]
  317. ColorMask 0
  318. Cull[_Cull]
  319. HLSLPROGRAM
  320. // Required to compile gles 2.0 with standard SRP library
  321. #pragma prefer_hlslcc gles
  322. #pragma exclude_renderers d3d11_9x
  323. #pragma target 2.0
  324. // -------------------------------------
  325. // Material Keywords
  326. #define _UBER
  327. #pragma shader_feature_local _ALPHATEST_ON
  328. #pragma shader_feature_local_fragment _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
  329. //#pragma shader_feature _NORMALMAP
  330. #pragma shader_feature_local _PARALLAXSHADOWS
  331. #pragma shader_feature_local _FADING_SHADOWS_ON
  332. #if defined (_PARALLAXSHADOWS) && !defined(_NORMALMAP)
  333. #define _NORMALMAP
  334. #endif
  335. //--------------------------------------
  336. // GPU Instancing
  337. #pragma multi_compile_instancing
  338. // #pragma multi_compile _ DOTS_INSTANCING_ON // needs shader target 4.5
  339. #pragma multi_compile __ LOD_FADE_CROSSFADE
  340. #pragma vertex ShadowPassVertex
  341. #pragma fragment ShadowPassFragment
  342. #include "Includes/Lux URP Lit Extended Inputs.hlsl"
  343. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl"
  344. //#include "Packages/com.unity.render-pipelines.universal/Shaders/ShadowCasterPass.hlsl"
  345. float3 _LightDirection;
  346. VertexOutput ShadowPassVertex(VertexInput input)
  347. {
  348. VertexOutput output = (VertexOutput)0;
  349. UNITY_SETUP_INSTANCE_ID(input);
  350. UNITY_TRANSFER_INSTANCE_ID(input, output);
  351. float3 positionWS = TransformObjectToWorld(input.positionOS.xyz);
  352. //float3 normalWS = TransformObjectToWorldDir(input.normalOS);
  353. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
  354. #if defined(_ALPHATEST_ON)
  355. output.uv = TRANSFORM_TEX(input.texcoord, _BaseMap);
  356. #if defined(_PARALLAXSHADOWS)
  357. //half3x3 tangentSpaceRotation = half3x3(normalInput.tangentWS, normalInput.bitangentWS, normalInput.normalWS);
  358. half3 viewDirWS = GetCameraPositionWS() - positionWS;
  359. //output.viewDirTS = SafeNormalize( mul(tangentSpaceRotation, viewDirWS) );
  360. output.normalWS = half4(normalInput.normalWS, viewDirWS.x);
  361. output.tangentWS = half4(normalInput.tangentWS, viewDirWS.y);
  362. output.bitangentWS = half4(normalInput.bitangentWS, viewDirWS.z);
  363. #endif
  364. #endif
  365. // When rendering backfaces normal extrusion is in the wrong direction...
  366. float facingNormal = dot(normalInput.normalWS, _LightDirection);
  367. output.positionCS = TransformWorldToHClip(ApplyShadowBias(positionWS, sign(facingNormal) * normalInput.normalWS, _LightDirection));
  368. #if UNITY_REVERSED_Z
  369. output.positionCS.z = min(output.positionCS.z, output.positionCS.w * UNITY_NEAR_CLIP_VALUE);
  370. #else
  371. output.positionCS.z = max(output.positionCS.z, output.positionCS.w * UNITY_NEAR_CLIP_VALUE);
  372. #endif
  373. #if defined(_ALPHATEST_ON) && defined(_FADING_SHADOWS_ON)
  374. //output.screenPos = ComputeScreenPos(output.positionCS);
  375. //output.screenPos.z = distance(positionWS, GetCameraPositionWS() );
  376. output.screenPos = distance(positionWS, GetCameraPositionWS() );
  377. #endif
  378. return output;
  379. }
  380. half4 ShadowPassFragment(VertexOutput input, half facing : VFACE) : SV_TARGET
  381. {
  382. UNITY_SETUP_INSTANCE_ID(input);
  383. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  384. // LOD crossfading
  385. #if defined(LOD_FADE_CROSSFADE) && !defined(SHADER_API_GLES)
  386. //LODDitheringTransition(input.positionCS.xy, unity_LODFade.x);
  387. clip (unity_LODFade.x - Dither32(input.positionCS.xy, 1));
  388. #endif
  389. #if defined(_ALPHATEST_ON)
  390. // Camera Fade
  391. #if defined(_FADING_SHADOWS_ON)
  392. //float4 screenPos = input.screenPos;
  393. //clip ( screenPos.z - _CameraShadowFadeDist - Dither32(screenPos.xy / screenPos.w * _ScreenParams.xy, 1));
  394. clip ( input.screenPos - _CameraShadowFadeDist - Dither32(input.positionCS.xy, 1));
  395. #endif
  396. float2 uv = input.uv;
  397. // Parallax
  398. #if defined(_PARALLAXSHADOWS)
  399. // When it comes to shadows we can calculate the proper viewdirWS only for directional lights
  400. // So all other lights will simply skip parallax extrusion
  401. float isDirectinalLight = UNITY_MATRIX_VP._m33;
  402. // UNITY_BRANCH
  403. // if(isDirectinalLight == 1) {
  404. input.normalWS.xyz *= facing;
  405. half3x3 tangentSpaceRotation = half3x3(input.tangentWS.xyz, input.bitangentWS.xyz, input.normalWS.xyz);
  406. //half3 viewDirWS = half3(input.normalWS.w, input.tangentWS.w, input.bitangentWS.w);
  407. // viewDirWS in case of the directional light equals cam forward
  408. half3 viewDirWS = UNITY_MATRIX_V[2].xyz;
  409. half3 viewDirTS = SafeNormalize( mul(tangentSpaceRotation, viewDirWS) );
  410. float3 v = SafeNormalize(viewDirTS); //input.viewDirTS);
  411. v.z += 0.42;
  412. v.xy /= v.z;
  413. float halfParallax = _Parallax * 0.5f;
  414. float parallax = SAMPLE_TEXTURE2D(_HeightMap, sampler_HeightMap, uv).g * _Parallax - halfParallax;
  415. float2 offset1 = parallax * v.xy;
  416. // Calculate 2nd height
  417. parallax = SAMPLE_TEXTURE2D(_HeightMap, sampler_HeightMap, uv + offset1).g * _Parallax - halfParallax;
  418. float2 offset2 = parallax * v.xy;
  419. // Final UVs
  420. uv += (offset1 + offset2) * 0.5f;
  421. //}
  422. #endif
  423. half alpha = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, uv).a * _BaseColor.a;
  424. clip (alpha - _Cutoff);
  425. #endif
  426. return 0;
  427. }
  428. ENDHLSL
  429. }
  430. Pass
  431. {
  432. Name "DepthOnly"
  433. Tags{"LightMode" = "DepthOnly"}
  434. ZWrite On
  435. ZTest [_ZTest]
  436. ColorMask 0
  437. Cull[_Cull]
  438. HLSLPROGRAM
  439. // Required to compile gles 2.0 with standard SRP library
  440. #pragma prefer_hlslcc gles
  441. #pragma exclude_renderers d3d11_9x
  442. #pragma target 2.0
  443. #pragma vertex DepthOnlyVertex
  444. #pragma fragment DepthOnlyFragment
  445. // -------------------------------------
  446. // Material Keywords
  447. #define _UBER
  448. #pragma shader_feature_local _ALPHATEST_ON
  449. #pragma shader_feature_local_fragment _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
  450. //#pragma shader_feature _NORMALMAP
  451. #pragma shader_feature_local _PARALLAX
  452. #pragma shader_feature_local_fragment _FADING_ON
  453. #if defined (_PARALLAX) && !defined(_NORMALMAP)
  454. #define _NORMALMAP
  455. #endif
  456. //--------------------------------------
  457. // GPU Instancing
  458. #pragma multi_compile_instancing
  459. // #pragma multi_compile _ DOTS_INSTANCING_ON // needs shader target 4.5
  460. #pragma multi_compile __ LOD_FADE_CROSSFADE
  461. #include "Includes/Lux URP Lit Extended Inputs.hlsl"
  462. //#include "Packages/com.unity.render-pipelines.universal/Shaders/DepthOnlyPass.hlsl"
  463. VertexOutput DepthOnlyVertex(VertexInput input)
  464. {
  465. VertexOutput output = (VertexOutput)0;
  466. UNITY_SETUP_INSTANCE_ID(input);
  467. UNITY_TRANSFER_INSTANCE_ID(input, output);
  468. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  469. float3 positionWS = TransformObjectToWorld(input.positionOS.xyz);
  470. output.positionCS = TransformWorldToHClip(positionWS);
  471. #if defined(_ALPHATEST_ON)
  472. //#if defined(_FADING_ON)
  473. // output.screenPos = ComputeScreenPos(output.positionCS);
  474. //#endif
  475. output.uv = TRANSFORM_TEX(input.texcoord, _BaseMap);
  476. #if defined(_PARALLAX)
  477. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
  478. //half3x3 tangentSpaceRotation = half3x3(normalInput.tangentWS, normalInput.bitangentWS, normalInput.normalWS);
  479. // was half3 - but output is float?! lets add normalize here - not: it breaks the regular pass...
  480. output.viewDirWS = GetCameraPositionWS() - positionWS;
  481. //output.viewDirTS = SafeNormalize( mul(tangentSpaceRotation, viewDirWS) );
  482. output.normalWS = normalInput.normalWS;
  483. //output.tangentWS = normalInput.tangentWS;
  484. float sign = input.tangentOS.w * GetOddNegativeScale();
  485. output.tangentWS = float4(normalInput.tangentWS.xyz, sign);
  486. //output.bitangentWS = half4(normalInput.bitangentWS, viewDirWS.z);
  487. #endif
  488. #endif
  489. return output;
  490. }
  491. half4 DepthOnlyFragment(VertexOutput input) : SV_TARGET
  492. {
  493. UNITY_SETUP_INSTANCE_ID(input);
  494. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  495. // LOD crossfading
  496. #if defined(LOD_FADE_CROSSFADE) && !defined(SHADER_API_GLES)
  497. //LODDitheringTransition(input.positionCS.xy, unity_LODFade.x);
  498. clip (unity_LODFade.x - Dither32(input.positionCS.xy, 1));
  499. #endif
  500. #if defined(_ALPHATEST_ON)
  501. #if defined(_FADING_ON)
  502. //float4 screenPos = input.screenPos;
  503. clip ( input.positionCS.w - _CameraFadeDist - Dither32(input.positionCS.xy, 1));
  504. //clip ( input.positionCS.w - _CameraFadeDist - Dither32(screenPos.xy / screenPos.w * _ScreenParams.xy, 1));
  505. #endif
  506. float2 uv = input.uv;
  507. #if defined(_PARALLAX)
  508. // Parallax
  509. half sgn = input.tangentWS.w; // should be either +1 or -1
  510. half3 bitangentWS = sgn * cross(input.normalWS.xyz, input.tangentWS.xyz);
  511. half3x3 tangentSpaceRotation = half3x3(input.tangentWS.xyz, bitangentWS.xyz, input.normalWS.xyz);
  512. //half3 viewDirWS = half3(input.normalWS.w, input.tangentWS.w, input.bitangentWS.w);
  513. half3 viewDirWS = input.viewDirWS;
  514. half3 viewDirTS = SafeNormalize( mul(tangentSpaceRotation, viewDirWS) );
  515. float3 v = SafeNormalize(viewDirTS);
  516. v.z += 0.42;
  517. v.xy /= v.z;
  518. float halfParallax = _Parallax * 0.5f;
  519. float parallax = SAMPLE_TEXTURE2D(_HeightMap, sampler_HeightMap, uv).g * _Parallax - halfParallax;
  520. float2 offset1 = parallax * v.xy;
  521. // Calculate 2nd height
  522. parallax = SAMPLE_TEXTURE2D(_HeightMap, sampler_HeightMap, uv + offset1).g * _Parallax - halfParallax;
  523. float2 offset2 = parallax * v.xy;
  524. // Final UVs
  525. uv += (offset1 + offset2) * 0.5f;
  526. #endif
  527. half alpha = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, uv).a * _BaseColor.a;
  528. clip (alpha - _Cutoff);
  529. #endif
  530. return 0;
  531. }
  532. ENDHLSL
  533. }
  534. // Depth Normal --------------------------------------------------------
  535. // This pass is used when drawing to a _CameraNormalsTexture texture
  536. Pass
  537. {
  538. Name "DepthNormals"
  539. Tags{"LightMode" = "DepthNormals"}
  540. ZWrite On
  541. Cull[_Cull]
  542. HLSLPROGRAM
  543. // Required to compile gles 2.0 with standard SRP library
  544. #pragma prefer_hlslcc gles
  545. #pragma exclude_renderers d3d11_9x
  546. #pragma target 2.0
  547. #pragma vertex DepthNormalVertex
  548. #pragma fragment DepthNormalFragment
  549. // -------------------------------------
  550. // Material Keywords
  551. #define _UBER
  552. #pragma shader_feature_local _ALPHATEST_ON
  553. #pragma shader_feature_local_fragment _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
  554. //#pragma shader_feature _NORMALMAP
  555. #pragma shader_feature_local _PARALLAX
  556. #pragma shader_feature_local _FADING_ON
  557. #if defined (_PARALLAX) && !defined(_NORMALMAP)
  558. #define _NORMALMAP
  559. #endif
  560. //--------------------------------------
  561. // GPU Instancing
  562. #pragma multi_compile_instancing
  563. // #pragma multi_compile _ DOTS_INSTANCING_ON // needs shader target 4.5
  564. #pragma multi_compile __ LOD_FADE_CROSSFADE
  565. #include "Includes/Lux URP Lit Extended Inputs.hlsl"
  566. //#include "Packages/com.unity.render-pipelines.universal/Shaders/DepthOnlyPass.hlsl"
  567. VertexOutput DepthNormalVertex(VertexInput input)
  568. {
  569. VertexOutput output = (VertexOutput)0;
  570. UNITY_SETUP_INSTANCE_ID(input);
  571. UNITY_TRANSFER_INSTANCE_ID(input, output);
  572. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  573. float3 positionWS = TransformObjectToWorld(input.positionOS.xyz);
  574. output.positionCS = TransformWorldToHClip(positionWS);
  575. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
  576. output.normalWS = normalInput.normalWS;
  577. #if defined(_ALPHATEST_ON)
  578. //#if defined(_FADING_ON)
  579. // output.screenPos = ComputeScreenPos(output.positionCS);
  580. //#endif
  581. output.uv = TRANSFORM_TEX(input.texcoord, _BaseMap);
  582. #if defined(_PARALLAX)
  583. //half3x3 tangentSpaceRotation = half3x3(normalInput.tangentWS, normalInput.bitangentWS, normalInput.normalWS);
  584. // was half3 - but output is float?! lets add normalize here - not: it breaks the regular pass...
  585. output.viewDirWS = GetCameraPositionWS() - positionWS;
  586. //output.viewDirTS = SafeNormalize( mul(tangentSpaceRotation, viewDirWS) );
  587. //output.tangentWS = normalInput.tangentWS;
  588. float sign = input.tangentOS.w * GetOddNegativeScale();
  589. output.tangentWS = float4(normalInput.tangentWS.xyz, sign);
  590. //output.bitangentWS = half4(normalInput.bitangentWS, viewDirWS.z);
  591. #endif
  592. #endif
  593. return output;
  594. }
  595. half4 DepthNormalFragment(VertexOutput input) : SV_TARGET
  596. {
  597. UNITY_SETUP_INSTANCE_ID(input);
  598. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  599. // LOD crossfading
  600. #if defined(LOD_FADE_CROSSFADE) && !defined(SHADER_API_GLES)
  601. //LODDitheringTransition(input.positionCS.xy, unity_LODFade.x);
  602. clip (unity_LODFade.x - Dither32(input.positionCS.xy, 1));
  603. #endif
  604. #if defined(_ALPHATEST_ON)
  605. #if defined(_FADING_ON)
  606. //float4 screenPos = input.screenPos;
  607. clip ( input.positionCS.w - _CameraFadeDist - Dither32(input.positionCS.xy, 1));
  608. //clip ( input.positionCS.w - _CameraFadeDist - Dither32(screenPos.xy / screenPos.w * _ScreenParams.xy, 1));
  609. #endif
  610. float2 uv = input.uv;
  611. #if defined(_PARALLAX)
  612. // Parallax
  613. half sgn = input.tangentWS.w; // should be either +1 or -1
  614. half3 bitangentWS = sgn * cross(input.normalWS.xyz, input.tangentWS.xyz);
  615. half3x3 tangentSpaceRotation = half3x3(input.tangentWS.xyz, bitangentWS.xyz, input.normalWS.xyz);
  616. //half3 viewDirWS = half3(input.normalWS.w, input.tangentWS.w, input.bitangentWS.w);
  617. half3 viewDirWS = input.viewDirWS;
  618. half3 viewDirTS = SafeNormalize( mul(tangentSpaceRotation, viewDirWS) );
  619. float3 v = SafeNormalize(viewDirTS);
  620. v.z += 0.42;
  621. v.xy /= v.z;
  622. float halfParallax = _Parallax * 0.5f;
  623. float parallax = SAMPLE_TEXTURE2D(_HeightMap, sampler_HeightMap, uv).g * _Parallax - halfParallax;
  624. float2 offset1 = parallax * v.xy;
  625. // Calculate 2nd height
  626. parallax = SAMPLE_TEXTURE2D(_HeightMap, sampler_HeightMap, uv + offset1).g * _Parallax - halfParallax;
  627. float2 offset2 = parallax * v.xy;
  628. // Final UVs
  629. uv += (offset1 + offset2) * 0.5f;
  630. #endif
  631. half alpha = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, uv).a * _BaseColor.a;
  632. clip (alpha - _Cutoff);
  633. #endif
  634. float3 normal = input.normalWS;
  635. // Handle backface rendering
  636. normal = TransformWorldToViewDir(normal, true);
  637. normal.z = abs(normal.z);
  638. return float4(PackNormalOctRectEncode(normal), 0.0, 0.0);
  639. }
  640. ENDHLSL
  641. }
  642. // Meta -------------------------------------------------------------
  643. // This pass it not used during regular rendering, only for lightmap baking.
  644. Pass
  645. {
  646. Name "Meta"
  647. Tags{"LightMode" = "Meta"}
  648. Cull Off
  649. HLSLPROGRAM
  650. // Required to compile gles 2.0 with standard srp library
  651. #pragma prefer_hlslcc gles
  652. #pragma exclude_renderers d3d11_9x
  653. #pragma vertex UniversalVertexMeta
  654. #pragma fragment UniversalFragmentMeta
  655. #define _UBER
  656. #define _PARALLAX
  657. #pragma shader_feature_local_fragment _SPECULAR_SETUP
  658. #pragma shader_feature_local_fragment _EMISSION
  659. #pragma shader_feature_local_fragment _METALLICSPECGLOSSMAP
  660. #pragma shader_feature_local _ALPHATEST_ON
  661. #pragma shader_feature_local_fragment _ _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
  662. #pragma shader_feature_local_fragment _SPECGLOSSMAP
  663. #include "Includes/Lux URP Lit Extended Inputs.hlsl"
  664. #include "Packages/com.unity.render-pipelines.universal/Shaders/LitMetaPass.hlsl"
  665. ENDHLSL
  666. }
  667. }
  668. FallBack "Hidden/InternalErrorShader"
  669. CustomEditor "LuxUberShaderGUI"
  670. }