Lux URP Tree Creator Leaves.shader 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // Shader uses custom editor to set double sided GI
  2. // Needs _Culling to be set properly
  3. Shader "Lux URP/Nature/Tree Creator Leaves"
  4. {
  5. Properties
  6. {
  7. [Header(Surface Inputs)]
  8. [Space(5)]
  9. _Color ("Main Color", Color) = (1,1,1,1)
  10. [PowerSlider(5.0)] _Shininess ("Shininess", Range (0.01, 1)) = 0.078125
  11. _MainTex ("Base (RGB) Alpha (A)", 2D) = "white" {}
  12. _Cutoff ("Alpha cutoff", Range(0.0, 1.0)) = 0.5
  13. [Space(5)]
  14. [NoScaleOffset]
  15. _BumpMap ("Normalmap", 2D) = "bump" {}
  16. [NoScaleOffset]
  17. _GlossMap ("Gloss (A)", 2D) = "black" {}
  18. [NoScaleOffset]
  19. _TranslucencyMap ("Translucency (A)", 2D) = "white" {}
  20. [HideInInspector] _TreeInstanceColor ("TreeInstanceColor", Vector) = (1,1,1,1)
  21. [HideInInspector] _TreeInstanceScale ("TreeInstanceScale", Vector) = (1,1,1,1)
  22. [HideInInspector] _SquashAmount ("Squash", Float) = 1
  23. // Lightmapper and outline selection shader need _MainTex, _Color and _Cutoff
  24. }
  25. SubShader
  26. {
  27. Tags
  28. {
  29. "RenderPipeline" = "UniversalPipeline"
  30. "RenderType" = "TransparentCutout"
  31. "IgnoreProjector" = "True"
  32. "Queue"="AlphaTest"
  33. }
  34. LOD 100
  35. Pass
  36. {
  37. Name "ForwardLit"
  38. Tags{"LightMode" = "UniversalForward"}
  39. ZWrite On
  40. Cull Back
  41. HLSLPROGRAM
  42. // Required to compile gles 2.0 with standard SRP library
  43. #pragma prefer_hlslcc gles
  44. #pragma exclude_renderers d3d11_9x
  45. // Shader target needs to be 3.0 due to tex2Dlod in the vertex shader and VFACE
  46. #pragma target 2.0
  47. // -------------------------------------
  48. // Material Keywords
  49. // #define _SPECULAR_SETUP 1
  50. #define DUMMYSHADER
  51. #define _ALPHATEST_ON
  52. #define _NORMALMAP
  53. // -------------------------------------
  54. // Lightweight Pipeline keywords
  55. #pragma multi_compile _ _MAIN_LIGHT_SHADOWS
  56. #pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
  57. #pragma multi_compile _ _ADDITIONAL_LIGHTS_VERTEX _ADDITIONAL_LIGHTS
  58. #pragma multi_compile _ _ADDITIONAL_LIGHT_SHADOWS
  59. #pragma multi_compile _ _SHADOWS_SOFT
  60. #pragma multi_compile _ _MIXED_LIGHTING_SUBTRACTIVE
  61. // -------------------------------------
  62. // Unity defined keywords
  63. // Trees do not support lightmapping
  64. // #pragma multi_compile _ DIRLIGHTMAP_COMBINED
  65. // #pragma multi_compile _ LIGHTMAP_ON
  66. // #pragma multi_compile_fog
  67. // Include base inputs and all other needed "base" includes
  68. #include "Includes/Lux URP Tree Creator Inputs.hlsl"
  69. #include "Includes/Lux URP Tree Creator Library.hlsl"
  70. #pragma vertex LitPassVertex
  71. #pragma fragment LitPassFragment
  72. //--------------------------------------
  73. // Vertex shader
  74. VertexOutput LitPassVertex(VertexInput input)
  75. {
  76. VertexOutput output = (VertexOutput)0;
  77. UNITY_SETUP_INSTANCE_ID(input);
  78. UNITY_TRANSFER_INSTANCE_ID(input, output);
  79. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  80. TreeVertLeaf(input);
  81. VertexPositionInputs vertexInput; //
  82. vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  83. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
  84. half3 viewDirWS = GetCameraPositionWS() - vertexInput.positionWS;
  85. half3 vertexLight = VertexLighting(vertexInput.positionWS, normalInput.normalWS);
  86. half fogFactor = ComputeFogFactor(vertexInput.positionCS.z);
  87. output.uv = TRANSFORM_TEX(input.texcoord, _MainTex);
  88. output.normalWS = half4(normalInput.normalWS, viewDirWS.x);
  89. output.tangentWS = half4(normalInput.tangentWS, viewDirWS.y);
  90. output.bitangentWS = half4(normalInput.bitangentWS, viewDirWS.z);
  91. OUTPUT_LIGHTMAP_UV(input.lightmapUV, unity_LightmapST, output.lightmapUV);
  92. OUTPUT_SH(output.normalWS.xyz, output.vertexSH);
  93. output.fogFactorAndVertexLight = half4(fogFactor, vertexLight);
  94. #if defined(REQUIRES_WORLD_SPACE_POS_INTERPOLATOR)
  95. output.positionWS = vertexInput.positionWS;
  96. #endif
  97. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  98. output.shadowCoord = GetShadowCoord(vertexInput);
  99. #endif
  100. output.positionCS = vertexInput.positionCS;
  101. output.color = input.color;
  102. #if defined(BILLBOARD_FACE_CAMERA_POS) && defined(_ENABLEDITHERING)
  103. output.screenPos = ComputeScreenPos(output.positionCS);
  104. #endif
  105. return output;
  106. }
  107. //--------------------------------------
  108. // Fragment shader and functions
  109. inline void InitializeSurfaceData(
  110. float2 uv,
  111. out SurfaceDescription outSurfaceData)
  112. {
  113. half4 albedoAlpha = SampleAlbedoAlpha(uv.xy, TEXTURE2D_ARGS(_MainTex, sampler_MainTex));
  114. outSurfaceData.alpha = Alpha(albedoAlpha.a, half4(1,1,1,1), _Cutoff);
  115. outSurfaceData.albedo = albedoAlpha.rgb; // * _Color.rgb;
  116. // Normal
  117. outSurfaceData.normalTS = SampleNormal(uv, TEXTURE2D_ARGS(_BumpMap, sampler_BumpMap), 5);
  118. outSurfaceData.specular = _Shininess;
  119. outSurfaceData.occlusion = 1;
  120. // Transmission
  121. half4 maskSample = SAMPLE_TEXTURE2D(_TranslucencyMap, sampler_TranslucencyMap, uv);
  122. outSurfaceData.translucency = maskSample.b;
  123. // Gloss
  124. outSurfaceData.gloss = SAMPLE_TEXTURE2D(_GlossMap, sampler_GlossMap, uv).a;
  125. }
  126. void InitializeInputData(VertexOutput input, half3 normalTS, out InputData inputData)
  127. {
  128. inputData = (InputData)0;
  129. #if defined(REQUIRES_WORLD_SPACE_POS_INTERPOLATOR)
  130. inputData.positionWS = input.positionWS;
  131. #endif
  132. half3 viewDirWS = half3(input.normalWS.w, input.tangentWS.w, input.bitangentWS.w);
  133. inputData.normalWS = TransformTangentToWorld(normalTS, half3x3(input.tangentWS.xyz, input.bitangentWS.xyz, input.normalWS.xyz));
  134. inputData.normalWS = NormalizeNormalPerPixel(inputData.normalWS);
  135. viewDirWS = SafeNormalize(viewDirWS);
  136. inputData.viewDirectionWS = viewDirWS;
  137. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  138. inputData.shadowCoord = input.shadowCoord;
  139. #elif defined(MAIN_LIGHT_CALCULATE_SHADOWS)
  140. inputData.shadowCoord = TransformWorldToShadowCoord(inputData.positionWS);
  141. #else
  142. inputData.shadowCoord = float4(0, 0, 0, 0);
  143. #endif
  144. inputData.fogCoord = input.fogFactorAndVertexLight.x;
  145. inputData.vertexLighting = input.fogFactorAndVertexLight.yzw;
  146. inputData.bakedGI = SAMPLE_GI(input.lightmapUV, input.vertexSH, inputData.normalWS);
  147. }
  148. half4 LitPassFragment(VertexOutput input) : SV_Target
  149. {
  150. UNITY_SETUP_INSTANCE_ID(input);
  151. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  152. // Get the surface description
  153. SurfaceDescription surfaceData;
  154. InitializeSurfaceData(input.uv, surfaceData);
  155. // Apply tree color and occlusion
  156. surfaceData.albedo *= input.color.rgb;
  157. surfaceData.occlusion = input.color.a;
  158. // Prepare surface data (like bring normal into world space and get missing inputs like gi)
  159. InputData inputData;
  160. InitializeInputData(input, surfaceData.normalTS, inputData);
  161. // Apply lighting
  162. half4 color = LuxURPTreeLeafFragmentPBR (
  163. inputData,
  164. surfaceData.albedo,
  165. surfaceData.specular,
  166. surfaceData.gloss,
  167. surfaceData.occlusion,
  168. surfaceData.alpha,
  169. half2(surfaceData.translucency, _TranslucencyViewDependency),
  170. _TranslucencyColor.rgb,
  171. UNITY_ACCESS_INSTANCED_PROP(Props, _SquashAmount),
  172. 1.0h // ShadowStrength
  173. );
  174. // Add fog
  175. color.rgb = MixFog(color.rgb, inputData.fogCoord);
  176. return color;
  177. }
  178. ENDHLSL
  179. }
  180. // End Passes -----------------------------------------------------
  181. }
  182. FallBack "Hidden/InternalErrorShader"
  183. Dependency "OptimizedShader" = "Lux URP/Nature/Tree Creator Leaves Optimized"
  184. }