Lux URP Toon & Outline.shader 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. Shader "Lux URP/Toon & Outline HLSL"
  2. {
  3. Properties
  4. {
  5. [HeaderHelpLuxURPToon_URL(6zvjnfuiskj1)]
  6. [Header(Surface Options)]
  7. [Space(8)]
  8. [Enum(UnityEngine.Rendering.CompareFunction)]
  9. _ZTest ("ZTest", Int) = 4
  10. [Enum(UnityEngine.Rendering.CullMode)]
  11. _Cull ("Culling", Float) = 2
  12. [Toggle(_ALPHATEST_ON)]
  13. _AlphaClip ("Alpha Clipping", Float) = 0.0
  14. [LuxURPHelpDrawer]
  15. _Help ("Enabling Alpha Clipping needs you to enable and assign the Albedo (RGB) Alpha (A) Map as well.", Float) = 0.0
  16. _Cutoff (" Threshold", Range(0.0, 1.0)) = 0.5
  17. [Enum(Off,0,On,1)]_Coverage (" Alpha To Coverage", Float) = 0
  18. [Enum(All,15,Depth,0)]
  19. _ColorMask ("Color Mask", Float) = 15
  20. [Space(5)]
  21. [Toggle(_SSAO_ENABLED)]
  22. _ReceiveSSAO ("Receive SSAO", Float) = 1.0
  23. [Header(Toon Lighting)]
  24. [Space(8)]
  25. [IntRange] _Steps ("Steps", Range(1, 8)) = 1
  26. _DiffuseFallOff ("Diffuse Falloff", Range(0, 1)) = 0.01
  27. _DiffuseStep ("Diffuse Step", Range(-1, 1)) = 0
  28. [Space(5)]
  29. [KeywordEnum(Off, SmoothSampling, PointSampling)] _Ramp ("Ramp Mode", Float) = 0
  30. [NoScaleOffset]
  31. _GradientMap (" Ramp", 2D) = "white" {}
  32. _OcclusionStrength ("Occlusion", Range(0.0, 1.0)) = 1
  33. [Header(Advanced Toon Lighting)]
  34. [Space(8)]
  35. //[Toggle(_COLORIZEMAIN)]
  36. _ColorizedShadowsMain ("Colorize Main Light", Range(0, 1)) = 0.0
  37. //[Toggle(_COLORIZEADD)]
  38. _ColorizedShadowsAdd ("Colorize Add Lights", Range(0, 1)) = 0.0
  39. _LightColorContribution ("Light Color Contribution", Range(0, 1)) = 1
  40. _AddLightFallOff ("Light Falloff", Range(0.0001, 1)) = 1
  41. [Header(Specular Toon Lighting)]
  42. [Space(8)]
  43. [ToggleOff]
  44. _SpecularHighlights ("Enable Specular Highlights", Float) = 1.0
  45. [Toggle(_ANISOTROPIC)]
  46. _Anisotropic (" Anisotropic Specular", Float) = 0.0
  47. _Anisotropy (" Anisotropy", Range(-1.0, 1.0)) = 0.0
  48. [Space(5)]
  49. [Toggle]
  50. _EnergyConservation (" EnergyConservation", Float) = 1
  51. [HDR] _SpecColor (" Specular", Color) = (0.2, 0.2, 0.2)
  52. [HDR] _SpecColor2nd (" Secondary Specular", Color) = (0.4, 0.4, 0.4)
  53. _Smoothness (" Smoothness", Range(0.0, 1.0)) = 0.5
  54. _SpecularStep (" Specular Step", Range(0.3, 0.5)) = 0.45
  55. _SpecularFallOff (" Specular Falloff", Range(0, 1)) = 0.01
  56. [Header(Toon Rim)]
  57. [Space(8)]
  58. [Toggle(_TOONRIM)]
  59. _EnableToonRim ("Enable Toon Rim", Float) = 0.0
  60. [HDR] _ToonRimColor (" Rim Color", Color) = (1, 1, 1, 1)
  61. _ToonRimPower (" Rim Power", Range(0, 1)) = 0.5
  62. _ToonRimFallOff (" Rim Falloff", Range(0, 1)) = 1
  63. _ToonRimAttenuation (" Rim Attenuation", Range(0, 1)) = 0
  64. [Header(Toon Shadows)]
  65. [Space(8)]
  66. [ToggleOff(_RECEIVE_SHADOWS_OFF)]
  67. _ReceiveShadows ("Receive Shadows", Float) = 1.0
  68. _ShadowOffset (" Shadow Offset", Float) = 1.0
  69. _ShadowFallOff (" Shadow Falloff", Range(0.5, 1)) = 0.75
  70. _ShadoBiasDirectional (" Shadow Bias Directional", Range(0, 1)) = 0
  71. _ShadowBiasAdditional (" Shadow Bias Additional", Range(0, 1)) = 0
  72. [Header(Toon Outline)]
  73. [Space(8)]
  74. _OutlineColor ("Color (RGB) Alpha (A)", Color) = (0,0,0,1)
  75. _Border ("Width", Float) = 10
  76. [Toggle(_COMPENSATESCALE)]
  77. _CompensateScale ("Compensate Object Scale", Float) = 0
  78. [Toggle(_OUTLINEINSCREENSPACE)]
  79. _OutlineInScreenSpace ("Calculate width in Screen Space", Float) = 0
  80. [Enum(UnityEngine.Rendering.CompareFunction)] _ZTestOutline ("ZTest Outline", Int) = 4
  81. [Enum(UnityEngine.Rendering.CullMode)]
  82. _CullOutline ("Culling Outline", Float) = 1
  83. [Header(Surface Inputs)]
  84. [Space(8)]
  85. [MainColor]
  86. _BaseColor ("Color (RGB) Alpha (A)", Color) = (1,1,1,1)
  87. _ShadedBaseColor ("Shaded Color (RGB)", Color) = (0,0,0,1)
  88. [Space(5)]
  89. [KeywordEnum(Off, One, Two)] _TexMode ("TextureMode", Float) = 0
  90. [MainTexture]
  91. _BaseMap ("Albedo (RGB) Alpha (A)", 2D) = "white" {}
  92. [NoScaleOffset]
  93. _ShadedBaseMap ("Shaded Albedo (RGB)", 2D) = "white" {}
  94. [Space(5)]
  95. [Toggle(_MASKMAP)]
  96. _EnableMaskMap ("Enable Mask Map", Float) = 0.0
  97. [NoScaleOffset]
  98. _MaskMap (" Emission (R) Specular (G) Occlusion (B) Smoothness (A)", 2D) = "white" {}
  99. [HDR] _EmissionColor (" Emission Color", Color) = (0,0,0,0)
  100. [Space(5)]
  101. [Toggle(_NORMALMAP)]
  102. _ApplyNormal ("Enable Normal Map", Float) = 0.0
  103. [NoScaleOffset]
  104. _BumpMap (" Normal Map", 2D) = "bump" {}
  105. _BumpScale (" Normal Scale", Float) = 1.0
  106. // Lux URP standards
  107. [Header(Rim Lighting)]
  108. [Space(8)]
  109. [Toggle(_RIMLIGHTING)]
  110. _Rim ("Enable Rim Lighting", Float) = 0
  111. [HDR] _RimColor ("Rim Color", Color) = (0.5,0.5,0.5,1)
  112. _RimPower ("Rim Power", Float) = 2
  113. _RimFrequency ("Rim Frequency", Float) = 0
  114. _RimMinPower (" Rim Min Power", Float) = 1
  115. _RimPerPositionFrequency (" Rim Per Position Frequency", Range(0.0, 1.0)) = 1
  116. [Header(Stencil)]
  117. [Space(8)]
  118. [IntRange] _Stencil ("Stencil Reference", Range (0, 255)) = 0
  119. [IntRange] _ReadMask (" Read Mask", Range (0, 255)) = 255
  120. [IntRange] _WriteMask (" Write Mask", Range (0, 255)) = 255
  121. [Enum(UnityEngine.Rendering.CompareFunction)]
  122. _StencilComp ("Stencil Comparison", Int) = 8 // always – terrain should be the first thing being rendered anyway
  123. [Enum(UnityEngine.Rendering.StencilOp)]
  124. _StencilOp ("Stencil Operation", Int) = 0 // 0 = keep, 2 = replace
  125. [Enum(UnityEngine.Rendering.StencilOp)]
  126. _StencilFail ("Stencil Fail Op", Int) = 0 // 0 = keep
  127. [Enum(UnityEngine.Rendering.StencilOp)]
  128. _StencilZFail ("Stencil ZFail Op", Int) = 0 // 0 = keep
  129. [Header(Advanced)]
  130. [Space(8)]
  131. [ToggleOff]
  132. _EnvironmentReflections ("Environment Reflections", Float) = 0.0
  133. [Header(Render Queue)]
  134. [Space(8)]
  135. [IntRange] _QueueOffset ("Queue Offset", Range(-50, 50)) = 0
  136. // Needed by the inspector
  137. [HideInInspector] _Culling ("Culling", Float) = 0.0
  138. [HideInInspector] _AlphaFromMaskMap ("AlphaFromMaskMap", Float) = 1.0
  139. // Lightmapper and outline selection shader need _MainTex, _Color and _Cutoff
  140. [HideInInspector] _MainTex ("Albedo", 2D) = "white" {}
  141. [HideInInspector] _Color ("Color", Color) = (1,1,1,1)
  142. [HideInInspector]
  143. _SrcBlend ("SrcBlend", Float) = 1.0
  144. [HideInInspector]
  145. _DstBlend ("DstBlend", Float) = 0.0
  146. [HideInInspector]
  147. _ZWrite ("ZWrite", Float) = 1.0
  148. }
  149. SubShader
  150. {
  151. Tags
  152. {
  153. "RenderPipeline" = "UniversalPipeline"
  154. "RenderType" = "Opaque"
  155. "Queue" = "Geometry"
  156. }
  157. LOD 100
  158. // /////////////////////////////////////////////////////
  159. // Outline Pass
  160. Pass
  161. {
  162. Name "Outline"
  163. // Here we have to fool URP < 8.0: We want the outline to render AFTER the regular shaded pass.
  164. // This worked fine in URP 7.4.1 but 8.x and above would draw the outine first...
  165. // So we tag the outline pass as "LightMode" = "UniversalForward" which makes unity draw it after our "regular" pass.
  166. Tags{"LightMode" = "UniversalForward"}
  167. //Tags{"LightMode" = "Outline"} // Needed to disable pass but then it does not get rendered...
  168. Blend SrcAlpha OneMinusSrcAlpha
  169. Cull [_CullOutline]
  170. ZWrite On
  171. ZTest [_ZTestOutline]
  172. AlphaToMask [_Coverage]
  173. HLSLPROGRAM
  174. // Required to compile gles 2.0 with standard SRP library
  175. #pragma prefer_hlslcc gles
  176. #pragma exclude_renderers d3d11_9x
  177. #pragma target 2.0
  178. #pragma shader_feature_local _ALPHATEST_ON
  179. #pragma shader_feature_local _COMPENSATESCALE
  180. #pragma shader_feature_local _OUTLINEINSCREENSPACE
  181. #pragma multi_compile_fog
  182. #pragma multi_compile_instancing
  183. #pragma vertex OutlineVertex
  184. #pragma fragment OutlineFrag
  185. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  186. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/CommonMaterial.hlsl"
  187. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/SurfaceInput.hlsl"
  188. // Include base inputs and all other needed "base" includes
  189. #include "Includes/Lux URP Toon Inputs.hlsl"
  190. //#if defined(_ALPHATEST_ON)
  191. //TEXTURE2D(_Basemap); SAMPLER(sampler_Basemap);
  192. //#endif
  193. struct VertexInputOutline
  194. {
  195. float4 vertex : POSITION;
  196. #if defined(_ALPHATEST_ON)
  197. float2 texcoord : TEXCOORD0;
  198. float2 uv : TEXCOORD1;
  199. #else
  200. float3 normal : NORMAL;
  201. #endif
  202. UNITY_VERTEX_INPUT_INSTANCE_ID
  203. };
  204. struct VertexOutputOutline
  205. {
  206. float4 positionCS : POSITION;
  207. half fogCoord : TEXCOORD0;
  208. #if defined(_ALPHATEST_ON)
  209. float2 uv : TEXCOORD1;
  210. #endif
  211. UNITY_VERTEX_INPUT_INSTANCE_ID
  212. UNITY_VERTEX_OUTPUT_STEREO
  213. };
  214. VertexOutputOutline OutlineVertex(VertexInputOutline input)
  215. {
  216. VertexOutputOutline output = (VertexOutputOutline)0;
  217. UNITY_SETUP_INSTANCE_ID(input);
  218. UNITY_TRANSFER_INSTANCE_ID(input, output);
  219. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  220. #if !defined(_ALPHATEST_ON)
  221. // Extrude
  222. #if !defined(_OUTLINEINSCREENSPACE)
  223. #if defined(_COMPENSATESCALE)
  224. float3 scale;
  225. scale.x = length(float3(UNITY_MATRIX_M[0].x, UNITY_MATRIX_M[1].x, UNITY_MATRIX_M[2].x));
  226. scale.y = length(float3(UNITY_MATRIX_M[0].y, UNITY_MATRIX_M[1].y, UNITY_MATRIX_M[2].y));
  227. scale.z = length(float3(UNITY_MATRIX_M[0].z, UNITY_MATRIX_M[1].z, UNITY_MATRIX_M[2].z));
  228. #endif
  229. input.vertex.xyz += input.normal * 0.001 * _Border
  230. #if defined(_COMPENSATESCALE)
  231. / scale
  232. #endif
  233. ;
  234. #endif
  235. output.positionCS = TransformObjectToHClip(input.vertex.xyz);
  236. // Extrude
  237. #if defined(_OUTLINEINSCREENSPACE)
  238. if (_Border > 0.0h) {
  239. //float3 normal = mul(UNITY_MATRIX_MVP, float4(input.normal, 0)).xyz; // to clip space
  240. float3 normal = mul(GetWorldToHClipMatrix(), mul(GetObjectToWorldMatrix(), float4(v.normal, 0.0))).xyz;
  241. float2 offset = normalize(normal.xy);
  242. float2 ndc = _ScreenParams.xy * 0.5;
  243. output.positionCS.xy += ((offset * _Border) / ndc * output.positionCS.w);
  244. }
  245. #endif
  246. #else
  247. output.uv = TRANSFORM_TEX(input.texcoord, _BaseMap);
  248. output.positionCS = TransformObjectToHClip(input.vertex.xyz);
  249. #endif
  250. output.fogCoord = ComputeFogFactor(output.positionCS.z);
  251. return output;
  252. }
  253. // Helper
  254. inline float2 shufflefast (float2 offset, float2 shift) {
  255. return offset * shift;
  256. }
  257. half4 OutlineFrag (VertexOutputOutline input ) : SV_Target
  258. {
  259. UNITY_SETUP_INSTANCE_ID(input);
  260. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  261. #if defined(_ALPHATEST_ON)
  262. float2 uv = input.uv;
  263. float2 offset = float2(1,1);
  264. #if defined(_OUTLINEINSCREENSPACE)
  265. float2 shift = fwidth(uv) * _Border * 0.5f;
  266. #else
  267. float2 shift = _Border.xx * 0.5 * _BaseMap_TexelSize.xy;
  268. #endif
  269. float2 sampleCoord = uv + shufflefast(offset, shift);
  270. half shuffleAlpha = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, sampleCoord).a;
  271. offset = float2(-1,1);
  272. sampleCoord = uv + shufflefast(offset, shift);
  273. shuffleAlpha += SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, sampleCoord).a;
  274. offset = float2(1,-1);
  275. sampleCoord = uv + shufflefast(offset, shift);
  276. shuffleAlpha += SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, sampleCoord).a;
  277. offset = float2(-1,-1);
  278. sampleCoord = uv + shufflefast(offset, shift);
  279. shuffleAlpha += SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, sampleCoord).a;
  280. //shuffleAlpha = saturate(shuffleAlpha); // not needed
  281. //shuffleAlpha *= 0.25; // bad!
  282. // Mask inner parts - does not work properly with different _Cutoff values?!
  283. // So we go with ZTest Less
  284. //shuffleAlpha = shuffleAlpha * ( 1 - step(_Cutoff, innerAlpha) );
  285. //shuffleAlpha = lerp(shuffleAlpha, 0, step(_Cutoff, innerAlpha) );
  286. // Apply clip
  287. clip(shuffleAlpha - _Cutoff);
  288. #endif
  289. half4 color = _OutlineColor;
  290. color.rgb = MixFog(color.rgb, input.fogCoord);
  291. return color;
  292. }
  293. ENDHLSL
  294. }
  295. // /////////////////////////////////////////////////////
  296. // Regular Passes
  297. Pass
  298. {
  299. Name "ForwardLit"
  300. // Outline pass uses "LightMode" = "UniversalForward" to fool URP > 8.x
  301. //Tags{"LightMode" = "UniversalForward"}
  302. Stencil {
  303. Ref [_Stencil]
  304. ReadMask [_ReadMask]
  305. WriteMask [_WriteMask]
  306. Comp [_StencilComp]
  307. Pass [_StencilOp]
  308. Fail [_StencilFail]
  309. ZFail [_StencilZFail]
  310. }
  311. Blend [_SrcBlend][_DstBlend]
  312. ZWrite [_ZWrite]
  313. ZTest [_ZTest]
  314. Cull [_Cull]
  315. AlphaToMask [_Coverage]
  316. // Experimental!
  317. ColorMask [_ColorMask]
  318. HLSLPROGRAM
  319. // Required to compile gles 2.0 with standard SRP library
  320. #pragma prefer_hlslcc gles
  321. #pragma exclude_renderers d3d11_9x
  322. // Shader target needs to be 3.0 due to tex2Dlod in the vertex shader or VFACE
  323. #pragma target 3.0
  324. // -------------------------------------
  325. // Material Keywords
  326. #define _SPECULAR_SETUP 1
  327. #pragma shader_feature_local _ALPHATEST_ON
  328. #pragma shader_feature_local_fragment _COLORIZEMAIN
  329. #pragma shader_feature_local_fragment _COLORIZEADD
  330. #pragma shader_feature_local_fragment _TOONRIM
  331. //#pragma shader_feature_local GRADIENT_ON
  332. #pragma shader_feature_local _ _RAMP_SMOOTHSAMPLING _RAMP_POINTSAMPLING
  333. #pragma shader_feature_local _ _TEXMODE_ONE _TEXMODE_TWO
  334. #pragma shader_feature_local_fragment _SSAO_ENABLED
  335. #pragma shader_feature_local _MASKMAP
  336. #pragma shader_feature_local _NORMALMAP
  337. #pragma shader_feature_local_fragment _RIMLIGHTING
  338. #pragma shader_feature_local_fragment _SPECULARHIGHLIGHTS_OFF
  339. #pragma shader_feature_local_fragment _ENVIRONMENTREFLECTIONS_OFF
  340. #pragma shader_feature_local _RECEIVE_SHADOWS_OFF
  341. #pragma shader_feature_local _ANISOTROPIC // Also affects vertex shader! (vertex input)
  342. // -------------------------------------
  343. // Universal Pipeline keywords
  344. #pragma multi_compile _ _MAIN_LIGHT_SHADOWS
  345. #pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
  346. #pragma multi_compile _ _ADDITIONAL_LIGHTS_VERTEX _ADDITIONAL_LIGHTS
  347. #pragma multi_compile_fragment _ _ADDITIONAL_LIGHT_SHADOWS
  348. #pragma multi_compile_fragment _ _SHADOWS_SOFT
  349. #pragma multi_compile_fragment _ _SCREEN_SPACE_OCCLUSION
  350. #pragma multi_compile _ LIGHTMAP_SHADOW_MIXING
  351. #pragma multi_compile _ SHADOWS_SHADOWMASK
  352. // -------------------------------------
  353. // Unity defined keywords
  354. #pragma multi_compile _ DIRLIGHTMAP_COMBINED
  355. #pragma multi_compile _ LIGHTMAP_ON
  356. #pragma multi_compile_fog
  357. //--------------------------------------
  358. // GPU Instancing
  359. #pragma multi_compile_instancing
  360. // Include base inputs and all other needed "base" includes
  361. #include "Includes/Lux URP Toon Inputs.hlsl"
  362. #pragma vertex LitPassVertex
  363. #pragma fragment LitPassFragment
  364. //--------------------------------------
  365. // Vertex shader
  366. VertexOutput LitPassVertex(VertexInput input)
  367. {
  368. VertexOutput output = (VertexOutput)0;
  369. UNITY_SETUP_INSTANCE_ID(input);
  370. UNITY_TRANSFER_INSTANCE_ID(input, output);
  371. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  372. VertexPositionInputs vertexInput; //
  373. vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  374. #if defined(_NORMALMAP) || (defined(_ANISOTROPIC) && !defined(_SPECULARHIGHLIGHTS_OFF))
  375. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
  376. #else
  377. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, float4(0,0,0,0));
  378. #endif
  379. float3 viewDirWS = GetCameraPositionWS() - vertexInput.positionWS;
  380. half3 vertexLight = VertexLighting(vertexInput.positionWS, normalInput.normalWS);
  381. half fogFactor = ComputeFogFactor(vertexInput.positionCS.z);
  382. #if defined(_TEXMODE_ONE) || defined(_TEXMODE_TWO) || defined(_TEXMODE_TWO) || defined(_NORMALMAP) || defined(_MASKMAP)
  383. output.uv = TRANSFORM_TEX(input.texcoord, _BaseMap);
  384. #endif
  385. output.normalWS = normalInput.normalWS;
  386. output.viewDirWS = viewDirWS;
  387. #if defined (_NORMALMAP) || (defined(_ANISOTROPIC) && !defined(_SPECULARHIGHLIGHTS_OFF))
  388. float sign = input.tangentOS.w * GetOddNegativeScale();
  389. output.tangentWS = float4(normalInput.tangentWS.xyz, sign);
  390. #endif
  391. OUTPUT_LIGHTMAP_UV(input.lightmapUV, unity_LightmapST, output.lightmapUV);
  392. OUTPUT_SH(output.normalWS.xyz, output.vertexSH);
  393. output.fogFactorAndVertexLight = half4(fogFactor, vertexLight);
  394. #if defined(REQUIRES_WORLD_SPACE_POS_INTERPOLATOR)
  395. output.positionWS = vertexInput.positionWS;
  396. #endif
  397. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  398. output.shadowCoord = GetShadowCoord(vertexInput);
  399. #endif
  400. output.positionCS = vertexInput.positionCS;
  401. return output;
  402. }
  403. //--------------------------------------
  404. // Fragment shader and functions
  405. inline void InitializeSurfaceData(
  406. float2 uv,
  407. out SurfaceDescription outSurfaceData)
  408. {
  409. #if defined(_TEXMODE_ONE) || defined(_TEXMODE_TWO)
  410. half4 albedoAlpha = SampleAlbedoAlpha(uv.xy, TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap));
  411. albedoAlpha *= _BaseColor;
  412. #else
  413. half4 albedoAlpha = _BaseColor;
  414. #endif
  415. #if defined(_ALPHATEST_ON) && (defined(_TEXMODE_ONE) || defined(_TEXMODE_TWO))
  416. outSurfaceData.alpha = Alpha(albedoAlpha.a, 1, _Cutoff);
  417. #else
  418. outSurfaceData.alpha = albedoAlpha.a;
  419. #endif
  420. #if defined(_TEXMODE_TWO)
  421. half3 albedoShaded = SampleAlbedoAlpha(uv, TEXTURE2D_ARGS(_ShadedBaseMap, sampler_BaseMap)).rgb;
  422. #else
  423. #if defined(_TEXMODE_ONE)
  424. half3 albedoShaded = albedoAlpha.rgb;
  425. #else
  426. half3 albedoShaded = half3(1,1,1);
  427. #endif
  428. #endif
  429. #if defined(_MASKMAP)
  430. half4 maskSample = SAMPLE_TEXTURE2D(_MaskMap, sampler_MaskMap, uv);
  431. outSurfaceData.occlusion = lerp(1.0h, maskSample.b, _OcclusionStrength);
  432. outSurfaceData.smoothness = maskSample.a * _Smoothness;
  433. outSurfaceData.specular = lerp(_SpecColor2nd.rgb, _SpecColor.rgb, maskSample.g);
  434. outSurfaceData.emission = maskSample.r * _EmissionColor.rgb;
  435. #else
  436. outSurfaceData.occlusion = _OcclusionStrength;
  437. outSurfaceData.smoothness = _Smoothness;
  438. outSurfaceData.specular = _SpecColor.rgb;
  439. outSurfaceData.emission = 0;
  440. #endif
  441. outSurfaceData.albedo = albedoAlpha.rgb;
  442. outSurfaceData.albedoShaded = albedoShaded * _ShadedBaseColor.rgb;
  443. outSurfaceData.metallic = 0;
  444. // Normal Map
  445. #if defined (_NORMALMAP)
  446. outSurfaceData.normalTS = SampleNormal(uv.xy, TEXTURE2D_ARGS(_BumpMap, sampler_BumpMap), _BumpScale);
  447. #else
  448. outSurfaceData.normalTS = half3(0,0,1);
  449. #endif
  450. }
  451. void InitializeInputData(VertexOutput input, half3 normalTS, half facing, out InputData inputData)
  452. {
  453. inputData = (InputData)0;
  454. #if defined(REQUIRES_WORLD_SPACE_POS_INTERPOLATOR)
  455. inputData.positionWS = input.positionWS;
  456. #endif
  457. half3 viewDirWS = SafeNormalize(input.viewDirWS);
  458. #if defined(_NORMALMAP)
  459. normalTS.z *= facing;
  460. float sgn = input.tangentWS.w; // should be either +1 or -1
  461. float3 bitangent = sgn * cross(input.normalWS.xyz, input.tangentWS.xyz);
  462. inputData.normalWS = TransformTangentToWorld(normalTS, half3x3(input.tangentWS.xyz, bitangent.xyz, input.normalWS.xyz));
  463. #else
  464. inputData.normalWS = input.normalWS * facing;
  465. #endif
  466. // Not normalized normals cause uggly specular highlights on mobile. So we always normalize.
  467. #if !defined(_SPECULARHIGHLIGHTS_OFF)
  468. inputData.normalWS = normalize(inputData.normalWS);
  469. #else
  470. inputData.normalWS = NormalizeNormalPerPixel(inputData.normalWS);
  471. #endif
  472. inputData.viewDirectionWS = viewDirWS;
  473. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  474. inputData.shadowCoord = input.shadowCoord;
  475. #elif defined(MAIN_LIGHT_CALCULATE_SHADOWS)
  476. inputData.shadowCoord = TransformWorldToShadowCoord(inputData.positionWS);
  477. #else
  478. inputData.shadowCoord = float4(0, 0, 0, 0);
  479. #endif
  480. inputData.fogCoord = input.fogFactorAndVertexLight.x;
  481. inputData.vertexLighting = input.fogFactorAndVertexLight.yzw;
  482. inputData.bakedGI = SAMPLE_GI(input.lightmapUV, input.vertexSH, inputData.normalWS);
  483. //inputData.normalizedScreenSpaceUV = input.positionCS.xy;
  484. inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.positionCS);
  485. inputData.shadowMask = SAMPLE_SHADOWMASK(input.lightmapUV);
  486. }
  487. half4 LitPassFragment(VertexOutput input, half facing : VFACE) : SV_Target
  488. {
  489. UNITY_SETUP_INSTANCE_ID(input);
  490. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  491. // Get the surface description
  492. SurfaceDescription surfaceData;
  493. #if defined(_TEXMODE_ONE) || defined(_TEXMODE_TWO) || defined(_TEXMODE_TWO) || defined(_NORMALMAP) || defined(_MASKMAP)
  494. InitializeSurfaceData(input.uv, surfaceData);
  495. #else
  496. InitializeSurfaceData(float2(0,0), surfaceData);
  497. #endif
  498. // Prepare surface data (like bring normal into world space and get missing inputs like gi)
  499. InputData inputData;
  500. InitializeInputData(input, surfaceData.normalTS, facing, inputData);
  501. #if defined(_RIMLIGHTING)
  502. half rim = saturate(1.0h - saturate( dot(inputData.normalWS, inputData.viewDirectionWS) ) );
  503. half power = _RimPower;
  504. UNITY_BRANCH if(_RimFrequency > 0 ) {
  505. half perPosition = lerp(0.0h, 1.0h, dot(1.0h, frac(UNITY_MATRIX_M._m03_m13_m23) * 2.0h - 1.0h ) * _RimPerPositionFrequency ) * 3.1416h;
  506. power = lerp(power, _RimMinPower, (1.0h + sin(_Time.y * _RimFrequency + perPosition) ) * 0.5h );
  507. }
  508. surfaceData.emission += pow(rim, power) * _RimColor.rgb * _RimColor.a;
  509. #endif
  510. // Apply lighting
  511. half4 color = LuxURPToonFragmentPBR(
  512. inputData,
  513. #if defined(_ANISOTROPIC) && !defined(_SPECULARHIGHLIGHTS_OFF)
  514. input.tangentWS,
  515. _Anisotropy,
  516. #endif
  517. surfaceData.albedo,
  518. surfaceData.albedoShaded,
  519. surfaceData.metallic,
  520. surfaceData.specular,
  521. _Steps,
  522. _DiffuseStep,
  523. _DiffuseFallOff,
  524. _EnergyConservation,
  525. _SpecularStep,
  526. _SpecularFallOff,
  527. _ColorizedShadowsMain,
  528. _ColorizedShadowsAdd,
  529. _LightColorContribution,
  530. _AddLightFallOff,
  531. _ShadowFallOff,
  532. _ShadoBiasDirectional,
  533. _ShadowBiasAdditional,
  534. _ToonRimColor.rgb,
  535. _ToonRimPower,
  536. _ToonRimFallOff,
  537. _ToonRimAttenuation,
  538. surfaceData.smoothness,
  539. surfaceData.occlusion,
  540. surfaceData.emission,
  541. surfaceData.alpha
  542. );
  543. // Add fog
  544. color.rgb = MixFog(color.rgb, inputData.fogCoord);
  545. return color;
  546. }
  547. ENDHLSL
  548. }
  549. // Shadows -----------------------------------------------------
  550. Pass
  551. {
  552. Name "ShadowCaster"
  553. Tags{"LightMode" = "ShadowCaster"}
  554. ZWrite On
  555. ZTest LEqual
  556. Cull[_Cull]
  557. HLSLPROGRAM
  558. // Required to compile gles 2.0 with standard srp library
  559. #pragma prefer_hlslcc gles
  560. #pragma exclude_renderers d3d11_9x
  561. #pragma target 2.0
  562. // -------------------------------------
  563. // Material Keywords
  564. #pragma shader_feature_local _ALPHATEST_ON
  565. #pragma shader_feature_local _ _TEXMODE_ONE _TEXMODE_TWO
  566. //--------------------------------------
  567. // GPU Instancing
  568. #pragma multi_compile_instancing
  569. #pragma vertex ShadowPassVertex
  570. #pragma fragment ShadowPassFragment
  571. // Include base inputs and all other needed "base" includes
  572. #include "Includes/Lux URP Toon Inputs.hlsl"
  573. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl"
  574. // Shadow caster specific input
  575. float3 _LightDirection;
  576. VertexOutput ShadowPassVertex(VertexInput input)
  577. {
  578. VertexOutput output = (VertexOutput)0;
  579. UNITY_SETUP_INSTANCE_ID(input);
  580. UNITY_TRANSFER_INSTANCE_ID(input, output);
  581. #if defined(_ALPHATEST_ON) && (defined(_TEXMODE_ONE) || defined(_TEXMODE_TWO))
  582. output.uv.xy = TRANSFORM_TEX(input.texcoord, _BaseMap);
  583. #endif
  584. float3 positionWS = TransformObjectToWorld(input.positionOS.xyz);
  585. float3 normalWS = TransformObjectToWorldDir(input.normalOS);
  586. output.positionCS = TransformWorldToHClip(ApplyShadowBias(positionWS, normalWS * _ShadowOffset, _LightDirection));
  587. #if UNITY_REVERSED_Z
  588. output.positionCS.z = min(output.positionCS.z, output.positionCS.w * UNITY_NEAR_CLIP_VALUE);
  589. #else
  590. output.positionCS.z = max(output.positionCS.z, output.positionCS.w * UNITY_NEAR_CLIP_VALUE);
  591. #endif
  592. return output;
  593. }
  594. half4 ShadowPassFragment(VertexOutput input) : SV_TARGET
  595. {
  596. UNITY_SETUP_INSTANCE_ID(input);
  597. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  598. #if defined(_ALPHATEST_ON) && (defined(_TEXMODE_ONE) || defined(_TEXMODE_TWO))
  599. half mask = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, input.uv).a;
  600. clip (mask - _Cutoff);
  601. #endif
  602. return 0;
  603. }
  604. ENDHLSL
  605. }
  606. // Depth -----------------------------------------------------
  607. Pass
  608. {
  609. Tags{"LightMode" = "DepthOnly"}
  610. ZWrite On
  611. ColorMask 0
  612. Cull[_Cull]
  613. HLSLPROGRAM
  614. // Required to compile gles 2.0 with standard srp library
  615. #pragma prefer_hlslcc gles
  616. #pragma exclude_renderers d3d11_9x
  617. #pragma target 2.0
  618. #pragma vertex DepthOnlyVertex
  619. #pragma fragment DepthOnlyFragment
  620. // -------------------------------------
  621. // Material Keywords
  622. #pragma shader_feature_local _ALPHATEST_ON
  623. #pragma shader_feature_local _ _TEXMODE_ONE _TEXMODE_TWO
  624. //--------------------------------------
  625. // GPU Instancing
  626. #pragma multi_compile_instancing
  627. #define DEPTHONLYPASS
  628. #include "Includes/Lux URP Toon Inputs.hlsl"
  629. VertexOutput DepthOnlyVertex(VertexInput input)
  630. {
  631. VertexOutput output = (VertexOutput)0;
  632. UNITY_SETUP_INSTANCE_ID(input);
  633. UNITY_TRANSFER_INSTANCE_ID(input, output);
  634. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  635. #if defined(_ALPHATEST_ON) && (defined(_TEXMODE_ONE) || defined(_TEXMODE_TWO))
  636. output.uv.xy = TRANSFORM_TEX(input.texcoord, _BaseMap);
  637. #endif
  638. output.positionCS = TransformObjectToHClip(input.positionOS.xyz);
  639. return output;
  640. }
  641. half4 DepthOnlyFragment(VertexOutput input) : SV_TARGET
  642. {
  643. UNITY_SETUP_INSTANCE_ID(input);
  644. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  645. #if defined(_ALPHATEST_ON) && (defined(_TEXMODE_ONE) || defined(_TEXMODE_TWO))
  646. half mask = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, input.uv.xy).a;
  647. clip (mask - _Cutoff);
  648. #endif
  649. return 0;
  650. }
  651. ENDHLSL
  652. }
  653. // Depth Normals -----------------------------------------------------
  654. Pass
  655. {
  656. Name "DepthNormals"
  657. Tags{"LightMode" = "DepthNormals"}
  658. ZWrite On
  659. Cull[_Cull]
  660. HLSLPROGRAM
  661. // Required to compile gles 2.0 with standard SRP library
  662. #pragma prefer_hlslcc gles
  663. #pragma exclude_renderers d3d11_9x
  664. #pragma target 2.0
  665. #pragma vertex DepthNormalsVertex
  666. #pragma fragment DepthNormalsFragment
  667. // -------------------------------------
  668. // Material Keywords
  669. #pragma shader_feature_local _NORMALMAP
  670. #pragma shader_feature_local _ALPHATEST_ON
  671. #pragma shader_feature_local _ _TEXMODE_ONE _TEXMODE_TWO
  672. //--------------------------------------
  673. // GPU Instancing
  674. #pragma multi_compile_instancing
  675. // #pragma multi_compile _ DOTS_INSTANCING_ON // needs shader target 4.5
  676. //#include "Packages/com.unity.render-pipelines.universal/Shaders/LitInput.hlsl"
  677. #include "Includes/Lux URP Toon Inputs.hlsl"
  678. #include "Packages/com.unity.render-pipelines.universal/Shaders/DepthNormalsPass.hlsl"
  679. ENDHLSL
  680. }
  681. // Meta -----------------------------------------------------
  682. Pass
  683. {
  684. Tags{"LightMode" = "Meta"}
  685. Cull Off
  686. HLSLPROGRAM
  687. // Required to compile gles 2.0 with standard srp library
  688. #pragma prefer_hlslcc gles
  689. #pragma vertex UniversalVertexMeta
  690. #pragma fragment UniversalFragmentMeta
  691. #define _SPECULAR_SETUP
  692. // First include all our custom stuff
  693. #include "Includes/Lux URP Toon Inputs.hlsl"
  694. //--------------------------------------
  695. // Fragment shader and functions
  696. inline void InitializeStandardLitSurfaceData(float2 uv, out SurfaceData outSurfaceData)
  697. {
  698. half4 albedoAlpha = SampleAlbedoAlpha(uv, TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap));
  699. outSurfaceData.alpha = Alpha(albedoAlpha.a, half4(1.0h, 1.0h, 1.0h, 1.0h), _Cutoff);
  700. outSurfaceData.albedo = albedoAlpha.rgb * _BaseColor.rgb;
  701. outSurfaceData.metallic = 0;
  702. outSurfaceData.specular = _SpecColor.rgb;
  703. outSurfaceData.smoothness = _Smoothness;
  704. outSurfaceData.normalTS = half3(0,0,1);
  705. outSurfaceData.occlusion = 1;
  706. outSurfaceData.emission = 0;
  707. // UPR 10+
  708. //#if VERSION_GREATER_EQUAL(10, 0)
  709. outSurfaceData.clearCoatMask = 0;
  710. outSurfaceData.clearCoatSmoothness = 0;
  711. //#endif
  712. }
  713. // Finally include the meta pass related stuff
  714. #include "Packages/com.unity.render-pipelines.universal/Shaders/LitMetaPass.hlsl"
  715. ENDHLSL
  716. }
  717. // End Passes -----------------------------------------------------
  718. }
  719. FallBack "Hidden/InternalErrorShader"
  720. CustomEditor "LuxURPUniversalCustomShaderGUI"
  721. }