Lux URP Tree Creator Bark Optimized.shader 19 KB

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