Lux URP Projection Top Down.shader 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. // Shader might write to emission, so it needs a custom inspector
  2. Shader "Lux URP/Projection/Top Down"
  3. {
  4. Properties
  5. {
  6. [HeaderHelpLuxURP_URL(80kxmwjj8akf)]
  7. [Header(Surface Options)]
  8. [Space(8)]
  9. [ToggleOff(_RECEIVE_SHADOWS_OFF)]
  10. _ReceiveShadows ("Receive Shadows", Float) = 1.0
  11. [Header(Surface Inputs)]
  12. [Space(8)]
  13. _BaseMap ("Albedo (RGB) Smoothness (A)", 2D) = "white" {}
  14. [MainColor] _BaseColor ("Base Color", Color) = (1,1,1,1)
  15. [Toggle(_DYNSCALE)]
  16. _ApplyDynScale ("Enable dynamic tiling", Float) = 0.0
  17. [Space(5)]
  18. _GlossMapScale ("Smoothness Scale", Range(0.0, 1.0)) = 1.0
  19. _SpecColor ("Specular", Color) = (0.2, 0.2, 0.2)
  20. [Space(5)]
  21. [Toggle(_NORMALMAP)]
  22. _ApplyNormal ("Enable Normal Map", Float) = 1.0
  23. [NoScaleOffset] _BumpMap (" Normal Map", 2D) = "bump" {}
  24. _BumpScale (" Normal Scale", Float) = 1.0
  25. [Header(Mask Map)]
  26. [Space(8)]
  27. [Toggle(_COMBINEDTEXTURE)]
  28. _CombinedTexture ("Enable Mask Map", Float) = 0.0
  29. [NoScaleOffset] _MaskMap (" Metallness (R) Occlusion (G) Height (B) Emission (A) ", 2D) = "bump" {}
  30. [HDR] _EmissionColor (" Emission Color", Color) = (0,0,0)
  31. [Toggle(_EMISSION)]
  32. _Emission (" Bake Emission", Float) = 0.0
  33. _Occlusion (" Occlusion", Range(0.0, 1.0)) = 1.0
  34. [Header(Top Down Projection)]
  35. [Space(8)]
  36. [Toggle(_TOPDOWNPROJECTION)]
  37. _ApplyTopDownProjection ("Enable top down Projection", Float) = 1.0
  38. [NoScaleOffset]_TopDownBaseMap (" Albedo (RGB) Smoothness (A)", 2D) = "white" {}
  39. _GlossMapScaleDyn (" Smoothness Scale", Range(0.0, 1.0)) = 1.0
  40. [Space(5)]
  41. [Toggle(_MASKFROMNORMAL)]
  42. _MaskFromNormal (" Get Mask from Normal", Float) = 0.0
  43. [NoScaleOffset]_TopDownNormalMap(" Normal (RGB) or Normal (AG) Mask (B)", 2D) = "bump" {}
  44. _BumpScaleDyn (" Normal Scale", Float) = 1.0
  45. [Space(5)]
  46. _TopDownTiling (" Tiling", Float) = 1.0
  47. [LuxURPVectorThreeDrawer]
  48. _TerrainPosition (" Terrain Position (XYZ)", Vector) = (0,0,0,0)
  49. [Header(Blending)]
  50. [Space(8)]
  51. _NormalLimit ("Angle Limit", Range(0.05,1)) = 0.5
  52. _NormalFactor ("Strength", Range(0.0,2)) = 1
  53. [Space(5)]
  54. _LowerNormalInfluence ("Base Normal Influence", Range(0,1)) = 1
  55. _LowerNormalMinStrength ("Base Normal Strength", Range(0,1)) = 0.2
  56. [Space(5)]
  57. _HeightBlendSharpness ("Height Influence", Range(0.0, 1.0)) = 1.0
  58. [Header(Fuzz Lighting)]
  59. [Space(8)]
  60. [Toggle(_SIMPLEFUZZ)]
  61. _EnableFuzzyLighting ("Enable Fuzzy Lighting", Float) = 0
  62. _FuzzWrap (" Diffuse Wrap", Range(0, 1)) = 0.5
  63. _FuzzStrength (" Fuzz Strength", Range(0, 8)) = 1
  64. _FuzzPower (" Fuzz Power", Range(1, 16)) = 4
  65. _FuzzBias (" Fuzz Bias", Range(0, 1)) = 0
  66. _FuzzAmbient (" Ambient Strength", Range(0, 1)) = 1
  67. [Header(Advanced)]
  68. [Space(8)]
  69. [ToggleOff]
  70. _SpecularHighlights ("Enable Specular Highlights", Float) = 1.0
  71. [ToggleOff]
  72. _EnvironmentReflections ("Environment Reflections", Float) = 1.0
  73. // DepthNormal compatibility
  74. [HideInInspector] _Cutoff("Alpha Cutoff", Range(0.0, 1.0)) = 0
  75. }
  76. SubShader
  77. {
  78. Tags
  79. {
  80. "RenderPipeline" = "UniversalPipeline"
  81. "RenderType" = "Opaque"
  82. "Queue"="Geometry"
  83. "DisableBatching" = "LODFading"
  84. }
  85. // Base -----------------------------------------------------
  86. Pass
  87. {
  88. Tags{"LightMode" = "UniversalForward"}
  89. ZWrite On
  90. Cull Back
  91. ZTest LEqual
  92. ZWrite On
  93. HLSLPROGRAM
  94. // Required to compile gles 2.0 with standard SRP library
  95. #pragma prefer_hlslcc gles
  96. #pragma exclude_renderers d3d11_9x
  97. #pragma target 2.0
  98. // -------------------------------------
  99. // Material Keywords
  100. #pragma shader_feature_local _NORMALMAP
  101. #define _SPECULAR_SETUP 1
  102. #pragma shader_feature_local _TOPDOWNPROJECTION
  103. #pragma shader_feature_local _DYNSCALE
  104. #pragma shader_feature_local_fragment _COMBINEDTEXTURE
  105. #pragma shader_feature_local_fragment _MASKFROMNORMAL
  106. #pragma shader_feature_local_fragment _SIMPLEFUZZ
  107. #pragma shader_feature_local_fragment _SPECULARHIGHLIGHTS_OFF
  108. #pragma shader_feature_local_fragment _ENVIRONMENTREFLECTIONS_OFF
  109. #pragma shader_feature_local _RECEIVE_SHADOWS_OFF
  110. // -------------------------------------
  111. // Universal Pipeline keywords
  112. #pragma multi_compile _ _MAIN_LIGHT_SHADOWS
  113. #pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
  114. #pragma multi_compile _ _ADDITIONAL_LIGHTS_VERTEX _ADDITIONAL_LIGHTS
  115. #pragma multi_compile_fragment _ _ADDITIONAL_LIGHT_SHADOWS
  116. #pragma multi_compile_fragment _ _SHADOWS_SOFT
  117. #pragma multi_compile_fragment _ _SCREEN_SPACE_OCCLUSION
  118. #pragma multi_compile _ LIGHTMAP_SHADOW_MIXING
  119. #pragma multi_compile _ SHADOWS_SHADOWMASK
  120. // -------------------------------------
  121. // Unity defined keywords
  122. #pragma multi_compile _ DIRLIGHTMAP_COMBINED
  123. #pragma multi_compile _ LIGHTMAP_ON
  124. #pragma multi_compile_fog
  125. //--------------------------------------
  126. // GPU Instancing
  127. #pragma multi_compile_instancing
  128. // #pragma multi_compile _ DOTS_INSTANCING_ON // needs shader target 4.5
  129. #pragma multi_compile _ LOD_FADE_CROSSFADE
  130. #include "Includes/Top Down URP Inputs.hlsl"
  131. #if defined(_COMBINEDTEXTURE)
  132. TEXTURE2D(_MaskMap); SAMPLER(sampler_MaskMap);
  133. #endif
  134. TEXTURE2D(_TopDownBaseMap); SAMPLER(sampler_TopDownBaseMap);
  135. TEXTURE2D(_TopDownNormalMap); SAMPLER(sampler_TopDownNormalMap);
  136. #pragma vertex LitPassVertex
  137. #pragma fragment LitPassFragment
  138. void InitializeInputData(VertexOutput input, half3 normalTS, out InputData inputData)
  139. {
  140. inputData = (InputData)0;
  141. inputData.positionWS = input.positionWS;
  142. half3 viewDirWS = SafeNormalize(input.viewDirWS);
  143. #ifdef _NORMALMAP
  144. // Here normalTS is already normalWS
  145. // inputData.normalWS = TransformTangentToWorld(normalTS, half3x3(input.tangentWS.xyz, input.bitangentWS.xyz, input.normalWS.xyz));
  146. inputData.normalWS = normalTS;
  147. #else
  148. inputData.normalWS = input.normalWS;
  149. #endif
  150. inputData.normalWS = NormalizeNormalPerPixel(inputData.normalWS);
  151. inputData.viewDirectionWS = viewDirWS;
  152. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  153. inputData.shadowCoord = input.shadowCoord;
  154. #elif defined(MAIN_LIGHT_CALCULATE_SHADOWS)
  155. inputData.shadowCoord = TransformWorldToShadowCoord(inputData.positionWS);
  156. #else
  157. inputData.shadowCoord = float4(0, 0, 0, 0);
  158. #endif
  159. inputData.fogCoord = input.fogFactorAndVertexLight.x;
  160. inputData.vertexLighting = input.fogFactorAndVertexLight.yzw;
  161. inputData.bakedGI = SAMPLE_GI(input.lightmapUV, input.vertexSH, inputData.normalWS);
  162. inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.positionCS);
  163. inputData.shadowMask = SAMPLE_SHADOWMASK(input.lightmapUV);
  164. }
  165. VertexOutput LitPassVertex(VertexInput input)
  166. {
  167. VertexOutput output = (VertexOutput)0;
  168. UNITY_SETUP_INSTANCE_ID(input);
  169. UNITY_TRANSFER_INSTANCE_ID(input, output);
  170. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  171. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  172. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
  173. float3 viewDirWS = GetCameraPositionWS() - vertexInput.positionWS;
  174. half3 vertexLight = VertexLighting(vertexInput.positionWS, normalInput.normalWS);
  175. half fogFactor = ComputeFogFactor(vertexInput.positionCS.z);
  176. output.uv.xy = TRANSFORM_TEX(input.texcoord, _BaseMap);
  177. output.uv.zw = vertexInput.positionWS.xz * _TopDownTiling + _TerrainPosition.xz;
  178. #if defined (_DYNSCALE)
  179. float scale = length( TransformObjectToWorld( float3(1,0,0) ) - UNITY_MATRIX_M._m03_m13_m23 );
  180. output.uv.xy *= scale;
  181. #endif
  182. // Already normalized from normal transform to WS!
  183. output.normalWS = normalInput.normalWS;
  184. #ifdef _NORMALMAP
  185. float sign = input.tangentOS.w * GetOddNegativeScale();
  186. output.tangentWS = float4(normalInput.tangentWS.xyz, sign);
  187. #endif
  188. output.viewDirWS = viewDirWS;
  189. OUTPUT_LIGHTMAP_UV(input.lightmapUV, unity_LightmapST, output.lightmapUV);
  190. OUTPUT_SH(output.normalWS.xyz, output.vertexSH);
  191. output.fogFactorAndVertexLight = half4(fogFactor, vertexLight);
  192. // #ifdef _ADDITIONAL_LIGHTS
  193. output.positionWS = vertexInput.positionWS;
  194. // #endif
  195. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  196. output.shadowCoord = GetShadowCoord(vertexInput);
  197. #endif
  198. output.positionCS = vertexInput.positionCS;
  199. return output;
  200. }
  201. #define oneMinusDielectricSpecConst half(1.0 - 0.04)
  202. // derived from #define kDieletricSpec half4(0.04, 0.04, 0.04, 1.0 - 0.04) // standard dielectric reflectivity coef at incident angle (= 4%)
  203. // Surface function which has full access to all vertex interpolators
  204. inline void InitializeStandardLitSurfaceData(VertexOutput input, out SurfaceDescription outSurfaceData)
  205. {
  206. half4 albedoAlpha = SampleAlbedoAlpha(input.uv.xy, TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap)) * _BaseColor;
  207. albedoAlpha.a *= _GlossMapScale;
  208. outSurfaceData.alpha = 1;
  209. outSurfaceData.albedo = albedoAlpha.rgb;
  210. outSurfaceData.metallic = 0;
  211. outSurfaceData.emission = 0;
  212. outSurfaceData.occlusion = 1;
  213. outSurfaceData.fuzzMask = 0;
  214. #if defined(_SPECULAR_SETUP)
  215. outSurfaceData.specular = _SpecColor.rgb;
  216. #else
  217. outSurfaceData.specular = 0;
  218. #endif
  219. #if defined(_COMBINEDTEXTURE)
  220. half4 combinedTextureSample = SAMPLE_TEXTURE2D(_MaskMap, sampler_MaskMap, input.uv.xy);
  221. outSurfaceData.specular = lerp(_SpecColor, albedoAlpha.rgb, combinedTextureSample.rrr);
  222. // Remap albedo
  223. albedoAlpha.rgb *= oneMinusDielectricSpecConst - combinedTextureSample.rrr * oneMinusDielectricSpecConst;
  224. outSurfaceData.emission = _EmissionColor.rgb * combinedTextureSample.a;
  225. outSurfaceData.occlusion = lerp(1.0h, combinedTextureSample.g, _Occlusion);
  226. #endif
  227. outSurfaceData.smoothness = albedoAlpha.a * _GlossMapScale;
  228. outSurfaceData.normalTS = SampleNormal(input.uv.xy, TEXTURE2D_ARGS(_BumpMap, sampler_BumpMap), _BumpScale);
  229. #ifdef _NORMALMAP
  230. #if defined (_MASKFROMNORMAL)
  231. half4 packedNormal = SAMPLE_TEXTURE2D(_TopDownNormalMap, sampler_TopDownNormalMap, input.uv.zw);
  232. #if BUMP_SCALE_NOT_SUPPORTED
  233. half3 topDownNormal = UnpackNormalmapRGorAG(packedNormal, 1.0);
  234. #else
  235. half3 topDownNormal = UnpackNormalmapRGorAG(packedNormal, _BumpScaleDyn);
  236. #endif
  237. #else
  238. half3 topDownNormal = SampleNormal(input.uv.zw, TEXTURE2D_ARGS(_TopDownNormalMap, sampler_TopDownNormalMap), _BumpScaleDyn);
  239. #endif
  240. #endif
  241. // Please note: outSurfaceData.normalTS will actually contain a normal in world space!
  242. #if defined(_TOPDOWNPROJECTION)
  243. float blendFactor = 0;
  244. #ifdef _NORMALMAP
  245. // Get per pixel worldspace normal (needed by blending)
  246. float sgn = input.tangentWS.w; // should be either +1 or -1
  247. float3 bitangent = sgn * cross(input.normalWS.xyz, input.tangentWS.xyz);
  248. float3 normalWS = TransformTangentToWorld(outSurfaceData.normalTS, half3x3(input.tangentWS.xyz, bitangent, input.normalWS.xyz));
  249. blendFactor = lerp(input.normalWS.y, normalWS.y, _LowerNormalInfluence);
  250. #else
  251. blendFactor = input.normalWS.y;
  252. #endif
  253. // Prevent projected texture from gettings stretched by masking out steep faces
  254. //blendFactor = saturate( blendFactor - (1 - saturate ( (blendFactor - _NormalLimit) * 4 ) ) );
  255. blendFactor = lerp(-_NormalLimit, 1, saturate(blendFactor));
  256. // Widen blendfactor
  257. blendFactor = blendFactor * _NormalFactor;
  258. #if defined(_COMBINEDTEXTURE) || defined (_NORMALMAP) && defined (_MASKFROMNORMAL)
  259. #if defined (_NORMALMAP) && defined (_MASKFROMNORMAL)
  260. float mask = saturate(packedNormal.b * _HeightBlendSharpness);
  261. #else
  262. // Mask is height and we want less on high levels. So it is some kind of inverted.
  263. float mask = saturate(combinedTextureSample.b * _HeightBlendSharpness);
  264. #endif
  265. blendFactor = smoothstep(mask, 1, blendFactor);
  266. #else
  267. // Somehow compensate missing height sample, smoothstep is not compensated? Nope. Just saturate.
  268. blendFactor = saturate(blendFactor); // * (1 + _HeightBlendSharpness));
  269. #endif
  270. float normalBlendFactor = blendFactor;
  271. blendFactor *= blendFactor * blendFactor * blendFactor;
  272. outSurfaceData.fuzzMask = blendFactor;
  273. // Get top down projected Texture(s)
  274. //float2 topDownUV = input.positionWS.xz * _TopDownTiling + _TerrainPosition.xz;
  275. half4 topDownSample = SAMPLE_TEXTURE2D(_TopDownBaseMap, sampler_TopDownBaseMap, input.uv.zw);
  276. topDownSample.a *= _GlossMapScaleDyn;
  277. albedoAlpha = lerp(albedoAlpha, topDownSample, blendFactor.xxxx);
  278. outSurfaceData.emission = lerp(outSurfaceData.emission, half3(0.0h, 0.0h, 0.0h), blendFactor.xxx);
  279. outSurfaceData.occlusion = lerp(outSurfaceData.occlusion, 1, blendFactor);
  280. #ifdef _NORMALMAP
  281. // 1. Normal is not sampled in tangent space
  282. outSurfaceData.normalTS = normalWS;
  283. // 2. So we use Reoriented Normal Mapping to bring the top down normal into world space
  284. // See e.g.: https://medium.com/@bgolus/normal-mapping-for-a-triplanar-shader-10bf39dca05a
  285. // We must apply some crazy swizzling here: Swizzle world space to tangent space
  286. half3 n1 = input.normalWS.xzy;
  287. half3 n2 = topDownNormal.xyz;
  288. n1.z += 1.0h;
  289. n2.xy *= -1.0h;
  290. topDownNormal = n1 * dot(n1, n2) / n1.z - n2;
  291. // Swizzle tangent space to world space
  292. topDownNormal = topDownNormal.xzy;
  293. // 3. Finally we blend both normals in world space
  294. outSurfaceData.normalTS = lerp(outSurfaceData.normalTS, topDownNormal, saturate(normalBlendFactor.xxx - _LowerNormalMinStrength) );
  295. #endif
  296. #else
  297. #ifdef _NORMALMAP
  298. float sgn = input.tangentWS.w; // should be either +1 or -1
  299. float3 bitangent = sgn * cross(input.normalWS.xyz, input.tangentWS.xyz);
  300. outSurfaceData.normalTS = TransformTangentToWorld(outSurfaceData.normalTS, half3x3(input.tangentWS.xyz, bitangent, input.normalWS.xyz));
  301. #else
  302. outSurfaceData.normalTS = input.normalWS.xyz;
  303. #endif
  304. #endif
  305. outSurfaceData.albedo = albedoAlpha.rgb;
  306. outSurfaceData.smoothness = albedoAlpha.a;
  307. }
  308. half4 LitPassFragment(VertexOutput input) : SV_Target
  309. {
  310. UNITY_SETUP_INSTANCE_ID(input);
  311. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  312. #if defined(LOD_FADE_CROSSFADE) && !defined(SHADER_API_GLES)
  313. LODDitheringTransition(input.positionCS.xyz, unity_LODFade.x);
  314. #endif
  315. SurfaceDescription surfaceData;
  316. // Get the surface description
  317. InitializeStandardLitSurfaceData(input, surfaceData);
  318. // Transfer all to world space
  319. // Please note: surfaceData.normalTS already contains the world space normal!
  320. InputData inputData;
  321. InitializeInputData(input, surfaceData.normalTS, inputData);
  322. // Apply lighting
  323. //half4 color = LightweightFragmentPBR(inputData, surfaceData.albedo, surfaceData.metallic, surfaceData.specular, surfaceData.smoothness, surfaceData.occlusion, surfaceData.emission, surfaceData.alpha);
  324. // Apply lighting
  325. half4 color = LuxURPSimpleFuzzFragmentPBR(
  326. inputData,
  327. surfaceData.albedo,
  328. surfaceData.metallic,
  329. surfaceData.specular,
  330. surfaceData.smoothness,
  331. surfaceData.occlusion,
  332. surfaceData.emission,
  333. surfaceData.alpha,
  334. #if defined(_SCATTERING)
  335. half4(surfaceData.translucency * _TranslucencyStrength, _TranslucencyPower, _ShadowStrength, _Distortion),
  336. #else
  337. half4(0,0,0,0),
  338. #endif
  339. surfaceData.fuzzMask, // Fuzzmask
  340. _FuzzPower,
  341. _FuzzBias,
  342. _FuzzWrap,
  343. _FuzzStrength * PI,
  344. _FuzzAmbient
  345. );
  346. // Add fog
  347. color.rgb = MixFog(color.rgb, inputData.fogCoord);
  348. return color;
  349. }
  350. ENDHLSL
  351. }
  352. // Shadows -----------------------------------------------------
  353. Pass
  354. {
  355. Name "ShadowCaster"
  356. Tags{"LightMode" = "ShadowCaster"}
  357. ZWrite On
  358. ZTest LEqual
  359. ColorMask 0
  360. Cull Back
  361. HLSLPROGRAM
  362. // Required to compile gles 2.0 with standard srp library
  363. #pragma prefer_hlslcc gles
  364. #pragma exclude_renderers d3d11_9x
  365. #pragma target 2.0
  366. // -------------------------------------
  367. // Material Keywords
  368. #pragma multi_compile _ LOD_FADE_CROSSFADE
  369. //--------------------------------------
  370. // GPU Instancing
  371. #pragma multi_compile_instancing
  372. // #pragma multi_compile _ DOTS_INSTANCING_ON // needs shader target 4.5
  373. #pragma vertex ShadowPassVertex
  374. #pragma fragment ShadowPassFragment
  375. // Needed functions usually included in LitInput.hlsl
  376. half LerpWhiteTo(half b, half t)
  377. {
  378. half oneMinusT = 1.0 - t;
  379. return oneMinusT + b * t;
  380. }
  381. half3 LerpWhiteTo(half3 b, half t)
  382. {
  383. half oneMinusT = 1.0 - t;
  384. return half3(oneMinusT, oneMinusT, oneMinusT) + b * t;
  385. }
  386. // #include "Packages/com.unity.render-pipelines.universal/Shaders/LitInput.hlsl"
  387. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  388. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl"
  389. #include "Includes/Top Down URP Inputs.hlsl"
  390. float3 _LightDirection;
  391. VertexOutput ShadowPassVertex(VertexInput input)
  392. {
  393. VertexOutput output = (VertexOutput)0;
  394. UNITY_SETUP_INSTANCE_ID(input);
  395. UNITY_TRANSFER_INSTANCE_ID(input, output);
  396. float3 positionWS = TransformObjectToWorld(input.positionOS.xyz);
  397. float3 normalWS = TransformObjectToWorldDir(input.normalOS);
  398. output.positionCS = TransformWorldToHClip(ApplyShadowBias(positionWS, normalWS, _LightDirection));
  399. #if UNITY_REVERSED_Z
  400. output.positionCS.z = min(output.positionCS.z, output.positionCS.w * UNITY_NEAR_CLIP_VALUE);
  401. #else
  402. output.positionCS.z = max(output.positionCS.z, output.positionCS.w * UNITY_NEAR_CLIP_VALUE);
  403. #endif
  404. return output;
  405. }
  406. half4 ShadowPassFragment(VertexOutput input) : SV_TARGET
  407. {
  408. #if defined(LOD_FADE_CROSSFADE) && !defined(SHADER_API_GLES)
  409. LODDitheringTransition(input.positionCS.xyz, unity_LODFade.x);
  410. #endif
  411. return 0;
  412. }
  413. ENDHLSL
  414. }
  415. // Depth -----------------------------------------------------
  416. Pass
  417. {
  418. Tags{"LightMode" = "DepthOnly"}
  419. ZWrite On
  420. ColorMask 0
  421. Cull Back
  422. HLSLPROGRAM
  423. // Required to compile gles 2.0 with standard srp library
  424. #pragma prefer_hlslcc gles
  425. #pragma exclude_renderers d3d11_9x
  426. #pragma target 2.0
  427. #pragma vertex DepthOnlyVertex
  428. #pragma fragment DepthOnlyFragment
  429. // -------------------------------------
  430. // Material Keywords
  431. #pragma multi_compile _ LOD_FADE_CROSSFADE
  432. //--------------------------------------
  433. // GPU Instancing
  434. #pragma multi_compile_instancing
  435. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  436. #define DEPTHONLYPASS
  437. #include "Includes/Top Down URP Inputs.hlsl"
  438. VertexOutput DepthOnlyVertex(VertexInput input)
  439. {
  440. VertexOutput output = (VertexOutput)0;
  441. UNITY_SETUP_INSTANCE_ID(input);
  442. UNITY_TRANSFER_INSTANCE_ID(input, output);
  443. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  444. output.positionCS = TransformObjectToHClip(input.positionOS.xyz);
  445. return output;
  446. }
  447. half4 DepthOnlyFragment(VertexOutput input) : SV_TARGET
  448. {
  449. UNITY_SETUP_INSTANCE_ID(input);
  450. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  451. #if defined(LOD_FADE_CROSSFADE) && !defined(SHADER_API_GLES) // enable dithering LOD transition if user select CrossFade transition in LOD group
  452. LODDitheringTransition(input.positionCS.xyz, unity_LODFade.x);
  453. #endif
  454. return 0;
  455. }
  456. ENDHLSL
  457. }
  458. // Depth Normal ---------------------------------------------
  459. // This pass is used when drawing to a _CameraNormalsTexture texture
  460. Pass
  461. {
  462. Name "DepthNormals"
  463. Tags{"LightMode" = "DepthNormals"}
  464. ZWrite On
  465. Cull[_Cull]
  466. HLSLPROGRAM
  467. // Required to compile gles 2.0 with standard srp library
  468. #pragma prefer_hlslcc gles
  469. #pragma exclude_renderers d3d11_9x
  470. #pragma target 2.0
  471. #pragma vertex DepthNormalsVertex
  472. #pragma fragment DepthNormalsFragment
  473. // -------------------------------------
  474. // Material Keywords
  475. #pragma shader_feature_local _NORMALMAP
  476. //#pragma shader_feature_local_fragment _ALPHATEST_ON
  477. //#pragma shader_feature_local_fragment _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
  478. //--------------------------------------
  479. // GPU Instancing
  480. #pragma multi_compile_instancing
  481. // #pragma multi_compile _ DOTS_INSTANCING_ON // needs shader target 4.5
  482. //#include "Packages/com.unity.render-pipelines.universal/Shaders/LitInput.hlsl"
  483. #define DEPTHNORMALONLYPASS
  484. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  485. #include "Includes/Top Down URP Inputs.hlsl"
  486. #include "Packages/com.unity.render-pipelines.universal/Shaders/DepthNormalsPass.hlsl"
  487. ENDHLSL
  488. }
  489. // Meta -----------------------------------------------------
  490. Pass
  491. {
  492. Tags{"LightMode" = "Meta"}
  493. Cull Back
  494. HLSLPROGRAM
  495. // Required to compile gles 2.0 with standard srp library
  496. #pragma prefer_hlslcc gles
  497. #pragma exclude_renderers d3d11_9x
  498. #pragma vertex UniversalVertexMetaCustom
  499. //UniversalVertexMeta
  500. #pragma fragment UniversalFragmentMetaCustom
  501. // -------------------------------------
  502. // Material Keywords
  503. #pragma shader_feature_local _NORMALMAP
  504. #define _SPECULAR_SETUP 1
  505. #pragma shader_feature _SPECGLOSSMAP
  506. // #pragma shader_feature _EMISSION // Not needed as we do our own emission
  507. #pragma shader_feature_local _TOPDOWNPROJECTION
  508. #pragma shader_feature_local _DYNSCALE
  509. #pragma shader_feature_local _COMBINEDTEXTURE
  510. #pragma shader_feature_local _MASKFROMNORMAL
  511. #define CUSTOMMETAPASS
  512. // This breaks all
  513. // #include "Packages/com.unity.render-pipelines.universal/Shaders/LitInput.hlsl"
  514. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  515. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/CommonMaterial.hlsl"
  516. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/SurfaceInput.hlsl"
  517. #include "Includes/Top Down URP Inputs.hlsl"
  518. #if defined(_COMBINEDTEXTURE)
  519. TEXTURE2D(_MaskMap); SAMPLER(sampler_MaskMap);
  520. #endif
  521. #if defined (_NORMALMAP) || defined (_MASKFROMNORMAL)
  522. TEXTURE2D(_TopDownNormalMap); SAMPLER(sampler_TopDownNormalMap);
  523. #endif
  524. TEXTURE2D(_TopDownBaseMap); SAMPLER(sampler_TopDownBaseMap);
  525. #define oneMinusDielectricSpecConst half(1.0 - 0.04)
  526. // derived from #define kDieletricSpec half4(0.04, 0.04, 0.04, 1.0 - 0.04) // standard dielectric reflectivity coef at incident angle (= 4%)
  527. inline void InitializeStandardLitSurfaceData(float4 uv, float3 positionWS, float3 normalWS, out SurfaceData outSurfaceData)
  528. {
  529. half4 albedoAlpha = SampleAlbedoAlpha(uv.xy, TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap));
  530. albedoAlpha.a *= _GlossMapScale;
  531. outSurfaceData.alpha = 1;
  532. #if _SPECULAR_SETUP
  533. outSurfaceData.metallic = 1.0h;
  534. outSurfaceData.specular = _SpecColor.rgb;
  535. #else
  536. outSurfaceData.metallic = specGloss.r;
  537. outSurfaceData.specular = half3(0.0h, 0.0h, 0.0h);
  538. #endif
  539. #if defined(_COMBINEDTEXTURE)
  540. half4 combinedTextureSample = SAMPLE_TEXTURE2D(_MaskMap, sampler_MaskMap, uv.xy);
  541. outSurfaceData.specular = lerp(_SpecColor, albedoAlpha.rgb, combinedTextureSample.rrr);
  542. // Remap albedo
  543. albedoAlpha.rgb *= oneMinusDielectricSpecConst - combinedTextureSample.rrr * oneMinusDielectricSpecConst;
  544. outSurfaceData.emission = _EmissionColor * combinedTextureSample.a;
  545. outSurfaceData.occlusion = lerp(1.0h, combinedTextureSample.g, _Occlusion);
  546. #else
  547. outSurfaceData.emission = 0;
  548. outSurfaceData.occlusion = 1;
  549. #endif
  550. #ifdef _NORMALMAP
  551. #if defined (_MASKFROMNORMAL)
  552. half4 packedNormal = SAMPLE_TEXTURE2D(_TopDownNormalMap, sampler_TopDownNormalMap, uv.zw);
  553. #if BUMP_SCALE_NOT_SUPPORTED
  554. half3 topDownNormal = UnpackNormalmapRGorAG(packedNormal, 1.0);
  555. #else
  556. half3 topDownNormal = UnpackNormalmapRGorAG(packedNormal, _BumpScaleDyn);
  557. #endif
  558. #else
  559. half3 topDownNormal = SampleNormal(uv.zw, TEXTURE2D_ARGS(_TopDownNormalMap, sampler_TopDownNormalMap), _BumpScaleDyn);
  560. #endif
  561. #endif
  562. #if defined(_TOPDOWNPROJECTION)
  563. float blendFactor = normalWS.y;
  564. // Prevent projected texture from gettings stretched by masking out steep faces
  565. //blendFactor = saturate( blendFactor - (1 - saturate ( (blendFactor - _NormalLimit) * 4 ) ) );
  566. blendFactor = lerp(-_NormalLimit, 1, saturate(blendFactor));
  567. // Widen blendfactor
  568. blendFactor = blendFactor * _NormalFactor;
  569. #if defined(_COMBINEDTEXTURE) || defined (_NORMALMAP) && defined (_MASKFROMNORMAL)
  570. #if defined (_NORMALMAP) && defined (_MASKFROMNORMAL)
  571. float mask = saturate(packedNormal.b * _HeightBlendSharpness);
  572. #else
  573. // Mask is height and we want less on high levels. So it is some kind of inverted.
  574. float mask = saturate(combinedTextureSample.b * _HeightBlendSharpness);
  575. #endif
  576. blendFactor = smoothstep(mask, 1, blendFactor);
  577. #else
  578. // Somehow compensate missing height sample, smoothstep is not compensated
  579. blendFactor = saturate(blendFactor); // * (1 + _HeightBlendSharpness));
  580. #endif
  581. blendFactor *= blendFactor * blendFactor * blendFactor;
  582. // float2 topDownUV = positionWS.xz * _TopDownTiling + _TerrainPosition.xz;
  583. half4 topDownSample = SAMPLE_TEXTURE2D(_TopDownBaseMap, sampler_TopDownBaseMap, uv.zw);
  584. topDownSample.a *= _GlossMapScaleDyn;
  585. albedoAlpha = lerp(albedoAlpha, topDownSample, blendFactor.xxxx);
  586. outSurfaceData.emission = lerp(outSurfaceData.emission, 0, blendFactor);
  587. #endif
  588. outSurfaceData.albedo = albedoAlpha.rgb;
  589. outSurfaceData.smoothness = albedoAlpha.a;
  590. outSurfaceData.normalTS = half3(0,0,1);
  591. outSurfaceData.clearCoatMask = 0;
  592. outSurfaceData.clearCoatSmoothness = 0;
  593. }
  594. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/MetaInput.hlsl"
  595. // Needed by URP
  596. struct Attributes
  597. {
  598. float4 positionOS : POSITION;
  599. float3 normalOS : NORMAL;
  600. float2 uv0 : TEXCOORD0;
  601. float2 uv1 : TEXCOORD1;
  602. float2 uv2 : TEXCOORD2;
  603. #ifdef _TANGENT_TO_WORLD
  604. float4 tangentOS : TANGENT;
  605. #endif
  606. };
  607. struct VertexOutputMeta {
  608. float4 positionCS : SV_POSITION;
  609. float4 uv : TEXCOORD0;
  610. float3 positionWS : TEXCOORD1;
  611. half3 normalWS : TEXCOORD2;
  612. };
  613. VertexOutputMeta UniversalVertexMetaCustom(Attributes input)
  614. {
  615. VertexOutputMeta output;
  616. output.positionCS = MetaVertexPosition(input.positionOS, input.uv1, input.uv2,
  617. unity_LightmapST, unity_DynamicLightmapST);
  618. output.uv.xy = TRANSFORM_TEX(input.uv0, _BaseMap);
  619. #if defined (_DYNSCALE)
  620. float scale = length( TransformObjectToWorld( float3(1,0,0) ) - UNITY_MATRIX_M._m03_m13_m23 );
  621. output.uv.xy *= scale;
  622. #endif
  623. output.positionWS = mul(UNITY_MATRIX_M, input.positionOS).xyz;
  624. output.normalWS = TransformObjectToWorldNormal(input.normalOS);
  625. output.uv.zw = output.normalWS.xz * _TopDownTiling + _TerrainPosition.xz;
  626. return output;
  627. }
  628. half4 UniversalFragmentMetaCustom(VertexOutputMeta input) : SV_Target
  629. {
  630. SurfaceData surfaceData;
  631. InitializeStandardLitSurfaceData(input.uv, input.positionWS, input.normalWS, surfaceData);
  632. BRDFData brdfData;
  633. InitializeBRDFData(surfaceData.albedo, surfaceData.metallic, surfaceData.specular, surfaceData.smoothness, surfaceData.alpha, brdfData);
  634. MetaInput metaInput;
  635. metaInput.Albedo = brdfData.diffuse + brdfData.specular * brdfData.roughness * 0.5;
  636. metaInput.SpecularColor = surfaceData.specular;
  637. metaInput.Emission = surfaceData.emission;
  638. return MetaFragment(metaInput);
  639. }
  640. ENDHLSL
  641. }
  642. }
  643. FallBack "Hidden/InternalErrorShader"
  644. CustomEditor "LuxURPCustomShaderGUI"
  645. }