Lux URP Tree Creator Leaves Optimized.shader 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. // Shader uses custom editor to set double sided GI
  2. // Needs _Culling to be set properly
  3. // Please note: This shader will never be batched as unity uses Materialpropertyblocks to write per instance properties.
  4. Shader "Lux URP/Nature/Tree Creator Leaves Optimized"
  5. {
  6. Properties
  7. {
  8. [Header(Surface Options)]
  9. [Space(5)]
  10. [ToggleOff(_RECEIVE_SHADOWS_OFF)]
  11. _ReceiveShadows ("Receive Shadows", Float) = 1.0
  12. [Toggle(_ENABLEDITHERING)]
  13. _EnableDither ("Enable Dithering for VR", Float) = 0.0
  14. [Header(Surface Inputs)]
  15. [Space(5)]
  16. [MainColor]
  17. _Color ("Main Color", Color) = (1,1,1,1)
  18. [MainTexture]
  19. _MainTex ("Base (RGB) Alpha (A)", 2D) = "white" {}
  20. _Cutoff ("Alpha cutoff", Range(0.0, 1.0)) = 0.5
  21. [NoScaleOffset]
  22. _BumpSpecMap ("Normalmap (GA) Spec (R)", 2D) = "bump" {}
  23. [NoScaleOffset]
  24. _TranslucencyMap ("Trans (B) Gloss(A)", 2D) = "white" {}
  25. [Space(5)]
  26. _SpecColor ("Specular Color", Color) = (0.2, 0.2, 0.2)
  27. [Header(Transmission)]
  28. [Space(5)]
  29. _TranslucencyColor ("Translucency Color", Color) = (0.73,0.85,0.41,1) // (187,219,106,255)
  30. _TranslucencyViewDependency ("View dependency", Range(0,1)) = 0.7
  31. _ShadowStrength ("Shadow Strength", Range(0,1)) = 0.8
  32. [HideInInspector] _ShadowOffsetScale ("Shadow Offset Scale", Float) = 1
  33. [HideInInspector] _TreeInstanceColor ("TreeInstanceColor", Vector) = (1,1,1,1)
  34. [HideInInspector] _TreeInstanceScale ("TreeInstanceScale", Vector) = (1,1,1,1)
  35. [HideInInspector] _SquashAmount ("Squash", Float) = 1
  36. // Lightmapper and outline selection shader need _MainTex, _Color and _Cutoff
  37. }
  38. SubShader
  39. {
  40. Tags
  41. {
  42. "RenderPipeline" = "UniversalPipeline"
  43. "RenderType" = "TransparentCutout"
  44. "IgnoreProjector" = "True"
  45. "Queue"="AlphaTest"
  46. }
  47. LOD 100
  48. Pass
  49. {
  50. Name "ForwardLit"
  51. Tags{"LightMode" = "UniversalForward"}
  52. ZWrite On
  53. Cull Back
  54. HLSLPROGRAM
  55. // Required to compile gles 2.0 with standard srp library
  56. #pragma prefer_hlslcc gles
  57. #pragma exclude_renderers d3d11_9x
  58. #pragma target 2.0
  59. // -------------------------------------
  60. // Material Keywords
  61. // #define _SPECULAR_SETUP 1
  62. #define _ALPHATEST_ON
  63. #pragma shader_feature _ENABLEDITHERING
  64. #pragma multi_compile __ BILLBOARD_FACE_CAMERA_POS
  65. // We always have a combined normal map
  66. // #pragma shader_feature _NORMALMAP
  67. // We do not use PBR
  68. // #pragma shader_feature _SPECULARHIGHLIGHTS_OFF
  69. //#pragma shader_feature _ENVIRONMENTREFLECTIONS_OFF
  70. #pragma shader_feature _RECEIVE_SHADOWS_OFF
  71. // -------------------------------------
  72. // Universal Pipeline keywords
  73. #pragma multi_compile _ _MAIN_LIGHT_SHADOWS
  74. #pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
  75. #pragma multi_compile _ _ADDITIONAL_LIGHTS_VERTEX _ADDITIONAL_LIGHTS
  76. #pragma multi_compile_fragment _ _ADDITIONAL_LIGHT_SHADOWS
  77. #pragma multi_compile_fragment _ _SHADOWS_SOFT
  78. #pragma multi_compile_fragment _ _SCREEN_SPACE_OCCLUSION
  79. #pragma multi_compile _ LIGHTMAP_SHADOW_MIXING
  80. #pragma multi_compile _ SHADOWS_SHADOWMASK
  81. // -------------------------------------
  82. // Unity defined keywords
  83. // Trees do not support lightmapping
  84. // #pragma multi_compile _ DIRLIGHTMAP_COMBINED
  85. // #pragma multi_compile _ LIGHTMAP_ON
  86. #pragma multi_compile_fog
  87. //--------------------------------------
  88. // GPU Instancing
  89. #pragma multi_compile_instancing
  90. // #pragma multi_compile _ DOTS_INSTANCING_ON // needs shader target 4.5
  91. // Include base inputs and all other needed "base" includes
  92. #include "Includes/Lux URP Tree Creator Inputs.hlsl"
  93. #include "Includes/Lux URP Tree Creator Library.hlsl"
  94. #pragma vertex LitPassVertex
  95. #pragma fragment LitPassFragment
  96. //--------------------------------------
  97. // Vertex shader
  98. VertexOutput LitPassVertex(VertexInput input)
  99. {
  100. VertexOutput output = (VertexOutput)0;
  101. UNITY_SETUP_INSTANCE_ID(input);
  102. UNITY_TRANSFER_INSTANCE_ID(input, output);
  103. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  104. TreeVertLeaf(input);
  105. VertexPositionInputs vertexInput; //
  106. vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  107. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
  108. half3 viewDirWS = GetCameraPositionWS() - vertexInput.positionWS;
  109. half3 vertexLight = VertexLighting(vertexInput.positionWS, normalInput.normalWS);
  110. half fogFactor = ComputeFogFactor(vertexInput.positionCS.z);
  111. output.uv = TRANSFORM_TEX(input.texcoord, _MainTex);
  112. output.normalWS = half4(normalInput.normalWS, viewDirWS.x);
  113. output.tangentWS = half4(normalInput.tangentWS, viewDirWS.y);
  114. output.bitangentWS = half4(normalInput.bitangentWS, viewDirWS.z);
  115. OUTPUT_LIGHTMAP_UV(input.lightmapUV, unity_LightmapST, output.lightmapUV);
  116. OUTPUT_SH(output.normalWS.xyz, output.vertexSH);
  117. output.fogFactorAndVertexLight = half4(fogFactor, vertexLight);
  118. #if defined(REQUIRES_WORLD_SPACE_POS_INTERPOLATOR)
  119. output.positionWS = vertexInput.positionWS;
  120. #endif
  121. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  122. output.shadowCoord = GetShadowCoord(vertexInput);
  123. #endif
  124. output.positionCS = vertexInput.positionCS;
  125. output.color = input.color;
  126. #if defined(BILLBOARD_FACE_CAMERA_POS) && defined(_ENABLEDITHERING)
  127. output.screenPos = ComputeScreenPos(output.positionCS);
  128. #endif
  129. return output;
  130. }
  131. //--------------------------------------
  132. // Fragment shader and functions
  133. inline void InitializeSurfaceData(
  134. float2 uv,
  135. #if defined(BILLBOARD_FACE_CAMERA_POS) && defined(_ENABLEDITHERING)
  136. float4 screenPos,
  137. half dither,
  138. #endif
  139. out SurfaceDescription outSurfaceData)
  140. {
  141. half4 albedoAlpha = SampleAlbedoAlpha(uv.xy, TEXTURE2D_ARGS(_MainTex, sampler_MainTex));
  142. // Dither
  143. #if defined(BILLBOARD_FACE_CAMERA_POS) && defined(_ENABLEDITHERING)
  144. half coverage = 1.0h;
  145. [branch]
  146. if (dither < 1.0h) {
  147. coverage = ComputeAlphaCoverage(screenPos, dither);
  148. }
  149. albedoAlpha.a *= coverage;
  150. #endif
  151. outSurfaceData.alpha = Alpha(albedoAlpha.a, half4(1,1,1,1), _Cutoff);
  152. outSurfaceData.albedo = albedoAlpha.rgb; // * _Color.rgb;
  153. // Normal
  154. half4 normalSample = SAMPLE_TEXTURE2D(_BumpSpecMap, sampler_BumpSpecMap, uv);
  155. half3 normal;
  156. normal.xy = normalSample.ag * 2 - 1;
  157. normal.xy *= UNITY_ACCESS_INSTANCED_PROP(Props, _SquashAmount);
  158. normal.z = sqrt(1.0 - saturate(dot(normal.xy, normal.xy)));
  159. outSurfaceData.normalTS = normal;
  160. outSurfaceData.specular = normalSample.rrr;
  161. outSurfaceData.occlusion = 1;
  162. // Transmission
  163. half4 maskSample = SAMPLE_TEXTURE2D(_TranslucencyMap, sampler_TranslucencyMap, uv);
  164. outSurfaceData.translucency = maskSample.b;
  165. outSurfaceData.gloss = maskSample.a * _Color.a;
  166. }
  167. void InitializeInputData(VertexOutput input, half3 normalTS, out InputData inputData)
  168. {
  169. inputData = (InputData)0;
  170. #if defined(REQUIRES_WORLD_SPACE_POS_INTERPOLATOR)
  171. inputData.positionWS = input.positionWS;
  172. #endif
  173. half3 viewDirWS = half3(input.normalWS.w, input.tangentWS.w, input.bitangentWS.w);
  174. inputData.normalWS = TransformTangentToWorld(normalTS, half3x3(input.tangentWS.xyz, input.bitangentWS.xyz, input.normalWS.xyz));
  175. inputData.normalWS = NormalizeNormalPerPixel(inputData.normalWS);
  176. viewDirWS = SafeNormalize(viewDirWS);
  177. inputData.viewDirectionWS = viewDirWS;
  178. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  179. inputData.shadowCoord = input.shadowCoord;
  180. #elif defined(MAIN_LIGHT_CALCULATE_SHADOWS)
  181. inputData.shadowCoord = TransformWorldToShadowCoord(inputData.positionWS);
  182. #else
  183. inputData.shadowCoord = float4(0, 0, 0, 0);
  184. #endif
  185. inputData.fogCoord = input.fogFactorAndVertexLight.x;
  186. inputData.vertexLighting = input.fogFactorAndVertexLight.yzw;
  187. inputData.bakedGI = SAMPLE_GI(input.lightmapUV, input.vertexSH, inputData.normalWS);
  188. inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.positionCS);
  189. inputData.shadowMask = SAMPLE_SHADOWMASK(input.lightmapUV);
  190. }
  191. half4 LitPassFragment(VertexOutput input) : SV_Target
  192. {
  193. UNITY_SETUP_INSTANCE_ID(input);
  194. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  195. // Get the surface description
  196. SurfaceDescription surfaceData;
  197. InitializeSurfaceData(input.uv,
  198. #if defined(BILLBOARD_FACE_CAMERA_POS) && defined(_ENABLEDITHERING)
  199. input.screenPos, UNITY_ACCESS_INSTANCED_PROP(Props, _TreeInstanceColor).a,
  200. #endif
  201. surfaceData);
  202. // Apply tree color and occlusion
  203. surfaceData.albedo *= input.color.rgb;
  204. surfaceData.occlusion = input.color.a;
  205. // Prepare surface data (like bring normal into world space and get missing inputs like gi)
  206. InputData inputData;
  207. InitializeInputData(input, surfaceData.normalTS, inputData);
  208. // Apply lighting
  209. half4 color = LuxURPTreeLeafFragmentPBR (
  210. inputData,
  211. surfaceData.albedo,
  212. surfaceData.specular,
  213. surfaceData.gloss,
  214. surfaceData.occlusion,
  215. surfaceData.alpha,
  216. half2(surfaceData.translucency, _TranslucencyViewDependency),
  217. _TranslucencyColor.rgb,
  218. UNITY_ACCESS_INSTANCED_PROP(Props, _SquashAmount),
  219. _ShadowStrength
  220. );
  221. // Add fog
  222. color.rgb = MixFog(color.rgb, inputData.fogCoord);
  223. return color;
  224. }
  225. ENDHLSL
  226. }
  227. // Shadows -----------------------------------------------------
  228. Pass
  229. {
  230. Name "ShadowCaster"
  231. Tags{"LightMode" = "ShadowCaster"}
  232. ZWrite On
  233. ZTest LEqual
  234. ColorMask 0
  235. Cull Back
  236. HLSLPROGRAM
  237. // Required to compile gles 2.0 with standard srp library
  238. #pragma prefer_hlslcc gles
  239. #pragma exclude_renderers d3d11_9x
  240. #pragma target 2.0
  241. // -------------------------------------
  242. // Material Keywords
  243. #define _ALPHATEST_ON
  244. // Usually no shadows during the transition...
  245. #pragma shader_feature _ENABLEDITHERING
  246. #pragma multi_compile __ BILLBOARD_FACE_CAMERA_POS
  247. //--------------------------------------
  248. // GPU Instancing
  249. #pragma multi_compile_instancing
  250. // #pragma multi_compile _ DOTS_INSTANCING_ON // needs shader target 4.5
  251. #pragma vertex ShadowPassVertex
  252. #pragma fragment ShadowPassFragment
  253. // Include base inputs and all other needed "base" includes
  254. #include "Includes/Lux URP Tree Creator Inputs.hlsl"
  255. #include "Includes/Lux URP Tree Creator Library.hlsl"
  256. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl"
  257. // Shadow caster specific input
  258. float3 _LightDirection;
  259. VertexOutput ShadowPassVertex(VertexInput input)
  260. {
  261. VertexOutput output = (VertexOutput)0;
  262. UNITY_SETUP_INSTANCE_ID(input);
  263. UNITY_TRANSFER_INSTANCE_ID(input, output);
  264. TreeVertLeaf(input);
  265. float3 positionWS = TransformObjectToWorld(input.positionOS.xyz);
  266. float3 normalWS = TransformObjectToWorldDir(input.normalOS);
  267. #if defined(_ALPHATEST_ON)
  268. output.uv = TRANSFORM_TEX(input.texcoord, _MainTex);
  269. #endif
  270. output.positionCS = TransformWorldToHClip(ApplyShadowBias(positionWS, normalWS, _LightDirection));
  271. // Dither coords - not perfect for shadows but ok.
  272. #if defined(BILLBOARD_FACE_CAMERA_POS) && defined(_ENABLEDITHERING)
  273. output.screenPos = ComputeScreenPos(output.positionCS);
  274. #endif
  275. #if UNITY_REVERSED_Z
  276. output.positionCS.z = min(output.positionCS.z, output.positionCS.w * UNITY_NEAR_CLIP_VALUE);
  277. #else
  278. output.positionCS.z = max(output.positionCS.z, output.positionCS.w * UNITY_NEAR_CLIP_VALUE);
  279. #endif
  280. return output;
  281. }
  282. half4 ShadowPassFragment(VertexOutput input) : SV_TARGET
  283. {
  284. UNITY_SETUP_INSTANCE_ID(input);
  285. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  286. #if defined(_ALPHATEST_ON)
  287. half mask = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, input.uv).a;
  288. // Dither
  289. #if defined(BILLBOARD_FACE_CAMERA_POS) && defined(_ENABLEDITHERING)
  290. half coverage = 1.0h;
  291. half dither = UNITY_ACCESS_INSTANCED_PROP(Props, _TreeInstanceColor).a;
  292. [branch]
  293. if ( dither < 1.0h) {
  294. coverage = ComputeAlphaCoverage(input.screenPos, dither );
  295. }
  296. mask *= coverage;
  297. #endif
  298. clip (mask - _Cutoff);
  299. #endif
  300. return 0;
  301. }
  302. ENDHLSL
  303. }
  304. // Depth -----------------------------------------------------
  305. Pass
  306. {
  307. Tags{"LightMode" = "DepthOnly"}
  308. ZWrite On
  309. ColorMask 0
  310. Cull Back
  311. HLSLPROGRAM
  312. // Required to compile gles 2.0 with standard srp library
  313. #pragma prefer_hlslcc gles
  314. #pragma exclude_renderers d3d11_9x
  315. #pragma target 2.0
  316. #pragma vertex DepthOnlyVertex
  317. #pragma fragment DepthOnlyFragment
  318. // -------------------------------------
  319. // Material Keywords
  320. #define _ALPHATEST_ON
  321. #pragma shader_feature _ENABLEDITHERING
  322. #pragma multi_compile __ BILLBOARD_FACE_CAMERA_POS
  323. //--------------------------------------
  324. // GPU Instancing
  325. #pragma multi_compile_instancing
  326. // #pragma multi_compile _ DOTS_INSTANCING_ON // needs shader target 4.5
  327. #define DEPTHONLYPASS
  328. #include "Includes/Lux URP Tree Creator Inputs.hlsl"
  329. #include "Includes/Lux URP Tree Creator Library.hlsl"
  330. VertexOutput DepthOnlyVertex(VertexInput input)
  331. {
  332. VertexOutput output = (VertexOutput)0;
  333. UNITY_SETUP_INSTANCE_ID(input);
  334. UNITY_TRANSFER_INSTANCE_ID(input, output);
  335. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  336. TreeVertLeaf(input);
  337. #if defined(_ALPHATEST_ON)
  338. output.uv = TRANSFORM_TEX(input.texcoord, _MainTex);
  339. #endif
  340. output.positionCS = TransformObjectToHClip(input.positionOS.xyz);
  341. #if defined(BILLBOARD_FACE_CAMERA_POS) && defined(_ENABLEDITHERING)
  342. output.screenPos = ComputeScreenPos(output.positionCS);
  343. #endif
  344. return output;
  345. }
  346. half4 DepthOnlyFragment(VertexOutput input) : SV_TARGET
  347. {
  348. UNITY_SETUP_INSTANCE_ID(input);
  349. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  350. #if defined(_ALPHATEST_ON)
  351. half mask = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, input.uv).a;
  352. // Dither
  353. #if defined(BILLBOARD_FACE_CAMERA_POS) && defined(_ENABLEDITHERING)
  354. half coverage = 1.0h;
  355. half dither = UNITY_ACCESS_INSTANCED_PROP(Props, _TreeInstanceColor).a;
  356. [branch]
  357. if ( dither < 1.0h) {
  358. coverage = ComputeAlphaCoverage(input.screenPos, dither );
  359. }
  360. mask *= coverage;
  361. #endif
  362. clip (mask - _Cutoff);
  363. #endif
  364. return 0;
  365. }
  366. ENDHLSL
  367. }
  368. // DepthNormal -----------------------------------------------------
  369. Pass
  370. {
  371. Name "DepthNormals"
  372. Tags{"LightMode" = "DepthNormals"}
  373. ZWrite On
  374. Cull[_Cull]
  375. HLSLPROGRAM
  376. // Required to compile gles 2.0 with standard srp library
  377. #pragma prefer_hlslcc gles
  378. #pragma exclude_renderers d3d11_9x
  379. #pragma target 2.0
  380. #pragma vertex DepthNormalsVertex
  381. #pragma fragment DepthNormalsFragment
  382. // -------------------------------------
  383. // Material Keywords
  384. #define _ALPHATEST_ON
  385. #pragma shader_feature _ENABLEDITHERING
  386. #pragma multi_compile __ BILLBOARD_FACE_CAMERA_POS
  387. //--------------------------------------
  388. // GPU Instancing
  389. #pragma multi_compile_instancing
  390. // #pragma multi_compile _ DOTS_INSTANCING_ON // needs shader target 4.5
  391. #define DEPTHNORMALONLYPASS
  392. #include "Includes/Lux URP Tree Creator Inputs.hlsl"
  393. #include "Includes/Lux URP Tree Creator Library.hlsl"
  394. VertexOutput DepthNormalsVertex(VertexInput input)
  395. {
  396. VertexOutput output = (VertexOutput)0;
  397. UNITY_SETUP_INSTANCE_ID(input);
  398. UNITY_TRANSFER_INSTANCE_ID(input, output);
  399. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  400. TreeVertLeaf(input);
  401. #if defined(_ALPHATEST_ON)
  402. output.uv = TRANSFORM_TEX(input.texcoord, _MainTex);
  403. #endif
  404. output.positionCS = TransformObjectToHClip(input.positionOS.xyz);
  405. #if defined(BILLBOARD_FACE_CAMERA_POS) && defined(_ENABLEDITHERING)
  406. output.screenPos = ComputeScreenPos(output.positionCS);
  407. #endif
  408. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, float4(1,1,1,1)); //input.tangentOS);
  409. output.normalWS.xyz = NormalizeNormalPerVertex(normalInput.normalWS).xyz;
  410. return output;
  411. }
  412. half4 DepthNormalsFragment(VertexOutput input) : SV_TARGET
  413. {
  414. UNITY_SETUP_INSTANCE_ID(input);
  415. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  416. #if defined(_ALPHATEST_ON)
  417. half mask = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, input.uv).a;
  418. // Dither
  419. #if defined(BILLBOARD_FACE_CAMERA_POS) && defined(_ENABLEDITHERING)
  420. half coverage = 1.0h;
  421. half dither = UNITY_ACCESS_INSTANCED_PROP(Props, _TreeInstanceColor).a;
  422. [branch]
  423. if ( dither < 1.0h) {
  424. coverage = ComputeAlphaCoverage(input.screenPos, dither );
  425. }
  426. mask *= coverage;
  427. #endif
  428. clip (mask - _Cutoff);
  429. #endif
  430. float3 normal = input.normalWS.xyz;
  431. return float4(PackNormalOctRectEncode(TransformWorldToViewDir(normal, true)), 0.0, 0.0);
  432. }
  433. ENDHLSL
  434. }
  435. // End Passes -----------------------------------------------------
  436. }
  437. FallBack "Hidden/InternalErrorShader"
  438. Dependency "BillboardShader" = "Hidden/Nature/Lux Tree Creator Leaves Rendertex"
  439. }