CTI URP Leaves ForwardLit Pass.hlsl 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // Please note: CTI shaders do not support light mapping.
  2. #if (defined(_NORMALMAP) || (defined(_PARALLAXMAP) && !defined(REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR))) || defined(_DETAIL)
  3. #define REQUIRES_WORLD_SPACE_TANGENT_INTERPOLATOR
  4. #endif
  5. // Structs
  6. struct Attributes
  7. {
  8. float4 positionOS : POSITION;
  9. float3 normalOS : NORMAL;
  10. float4 tangentOS : TANGENT;
  11. float2 texcoord : TEXCOORD0;
  12. float2 texcoord1 : TEXCOORD1; // Bending
  13. half4 color : COLOR; // Bending
  14. float3 texcoord2 : TEXCOORD2; // Leaves only: Pivot
  15. //float2 staticLightmapUV : TEXCOORD1;
  16. //float2 dynamicLightmapUV : TEXCOORD2;
  17. UNITY_VERTEX_INPUT_INSTANCE_ID
  18. };
  19. struct Varyings
  20. {
  21. float2 uv : TEXCOORD0;
  22. #if defined(REQUIRES_WORLD_SPACE_POS_INTERPOLATOR)
  23. float3 positionWS : TEXCOORD1;
  24. #endif
  25. half3 normalWS : TEXCOORD2;
  26. #if defined(REQUIRES_WORLD_SPACE_TANGENT_INTERPOLATOR)
  27. half4 tangentWS : TEXCOORD3;
  28. #endif
  29. //float3 viewDirWS : TEXCOORD4;
  30. #ifdef _ADDITIONAL_LIGHTS_VERTEX
  31. half4 fogFactorAndVertexLight : TEXCOORD5;
  32. #else
  33. half fogFactor : TEXCOORD5;
  34. #endif
  35. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  36. float4 shadowCoord : TEXCOORD6;
  37. #endif
  38. //#if defined(REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR)
  39. // half3 viewDirTS : TEXCOORD7;
  40. //#endif
  41. DECLARE_LIGHTMAP_OR_SH(staticLightmapUV, vertexSH, 8);
  42. //#ifdef DYNAMICLIGHTMAP_ON
  43. // float2 dynamicLightmapUV : TEXCOORD9; // Dynamic lightmap UVs
  44. //#endif
  45. float4 positionCS : SV_POSITION;
  46. // CTI specific
  47. half2 occlusionVariation : TEXCOORD7;
  48. //UNITY_VERTEX_INPUT_INSTANCE_ID
  49. UNITY_VERTEX_OUTPUT_STEREO
  50. };
  51. //--------------------------------------
  52. // Include the surface function
  53. #include "Includes/CTI URP Leaves SurfaceData.hlsl"
  54. //--------------------------------------
  55. // Vertex shader
  56. #include "Includes/CTI URP Bending.hlsl"
  57. Varyings LitPassVertex(Attributes input)
  58. {
  59. Varyings output = (Varyings)0;
  60. UNITY_SETUP_INSTANCE_ID(input);
  61. //UNITY_TRANSFER_INSTANCE_ID(input, output);
  62. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  63. CTI_AnimateVertex(
  64. input,
  65. #if defined (_BENDINGCOLRSONLY)
  66. float4(input.color.rg, input.color.ab), // animParams,
  67. #else
  68. float4(input.color.rg, input.texcoord1.xy), // animParams,
  69. #endif
  70. _BaseWindMultipliers
  71. );
  72. // CTI special
  73. output.occlusionVariation = input.color.ar;
  74. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  75. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
  76. half3 vertexLight = VertexLighting(vertexInput.positionWS, normalInput.normalWS);
  77. half fogFactor = 0;
  78. #if !defined(_FOG_FRAGMENT)
  79. fogFactor = ComputeFogFactor(vertexInput.positionCS.z);
  80. #endif
  81. output.uv = input.texcoord;
  82. // Already normalized from normal transform to WS.
  83. output.normalWS = normalInput.normalWS;
  84. #if defined(_NORMALMAP)
  85. float sign = input.tangentOS.w * GetOddNegativeScale();
  86. output.tangentWS = float4(normalInput.tangentWS.xyz, sign);
  87. #endif
  88. //OUTPUT_LIGHTMAP_UV(input.staticLightmapUV, unity_LightmapST, output.staticLightmapUV);
  89. //#ifdef DYNAMICLIGHTMAP_ON
  90. // output.dynamicLightmapUV = input.dynamicLightmapUV.xy * unity_DynamicLightmapST.xy + unity_DynamicLightmapST.zw;
  91. //#endif
  92. OUTPUT_SH(output.normalWS.xyz, output.vertexSH);
  93. #ifdef _ADDITIONAL_LIGHTS_VERTEX
  94. output.fogFactorAndVertexLight = half4(fogFactor, vertexLight);
  95. #else
  96. output.fogFactor = fogFactor;
  97. #endif
  98. #if defined(REQUIRES_WORLD_SPACE_POS_INTERPOLATOR)
  99. output.positionWS = vertexInput.positionWS;
  100. #endif
  101. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  102. output.shadowCoord = GetShadowCoord(vertexInput);
  103. #endif
  104. output.positionCS = vertexInput.positionCS;
  105. return output;
  106. }
  107. //--------------------------------------
  108. // Fragment shader and functions
  109. // InitializeSurfaceData
  110. // InitializeInputData
  111. void InitializeInputData(Varyings input, half3 normalTS, out InputData inputData)
  112. {
  113. inputData = (InputData)0;
  114. #if defined(REQUIRES_WORLD_SPACE_POS_INTERPOLATOR)
  115. inputData.positionWS = input.positionWS;
  116. #endif
  117. half3 viewDirWS = GetWorldSpaceNormalizeViewDir(input.positionWS);
  118. #if defined(_NORMALMAP)
  119. float sgn = input.tangentWS.w; // should be either +1 or -1
  120. float3 bitangent = sgn * cross(input.normalWS.xyz, input.tangentWS.xyz);
  121. inputData.normalWS = TransformTangentToWorld(normalTS, half3x3(input.tangentWS.xyz, bitangent, input.normalWS.xyz));
  122. #else
  123. inputData.normalWS = input.normalWS;
  124. #endif
  125. inputData.normalWS = NormalizeNormalPerPixel(inputData.normalWS);
  126. inputData.viewDirectionWS = viewDirWS;
  127. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  128. inputData.shadowCoord = input.shadowCoord;
  129. #elif defined(MAIN_LIGHT_CALCULATE_SHADOWS)
  130. inputData.shadowCoord = TransformWorldToShadowCoord(inputData.positionWS);
  131. #else
  132. inputData.shadowCoord = float4(0, 0, 0, 0);
  133. #endif
  134. #ifdef _ADDITIONAL_LIGHTS_VERTEX
  135. inputData.fogCoord = InitializeInputDataFog(float4(input.positionWS, 1.0), input.fogFactorAndVertexLight.x);
  136. inputData.vertexLighting = input.fogFactorAndVertexLight.yzw;
  137. #else
  138. inputData.fogCoord = InitializeInputDataFog(float4(input.positionWS, 1.0), input.fogFactor);
  139. #endif
  140. //#if defined(DYNAMICLIGHTMAP_ON)
  141. // inputData.bakedGI = SAMPLE_GI(input.staticLightmapUV, input.dynamicLightmapUV, input.vertexSH, inputData.normalWS);
  142. //#else
  143. // inputData.bakedGI = SAMPLE_GI(input.staticLightmapUV, input.vertexSH, inputData.normalWS);
  144. inputData.bakedGI = SAMPLE_GI(input.texcoord1, input.vertexSH, inputData.normalWS);
  145. //#endif
  146. inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.positionCS);
  147. inputData.shadowMask = SAMPLE_SHADOWMASK(input.lightmapUV);
  148. #if defined(DEBUG_DISPLAY)
  149. #if defined(DYNAMICLIGHTMAP_ON)
  150. //inputData.dynamicLightmapUV = input.dynamicLightmapUV;
  151. #endif
  152. #if defined(LIGHTMAP_ON)
  153. //inputData.staticLightmapUV = input.staticLightmapUV;
  154. #else
  155. inputData.vertexSH = input.vertexSH;
  156. #endif
  157. #endif
  158. }
  159. // Fragment shader
  160. half4 LitPassFragment(Varyings input, half facing : VFACE) : SV_Target
  161. {
  162. //UNITY_SETUP_INSTANCE_ID(input);
  163. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  164. #if defined(LOD_FADE_CROSSFADE) && !defined(SHADER_API_GLES)
  165. LODDitheringTransition(input.positionCS.xyz, unity_LODFade.x);
  166. #endif
  167. SurfaceData surfaceData;
  168. AdditionalSurfaceData additionalSurfaceData;
  169. // Get the surface description / defined in "Includes/CTI LWRP Inputs.hlsl"
  170. InitializeLeavesLitSurfaceData(input.occlusionVariation.y, input.uv.xy, surfaceData, additionalSurfaceData);
  171. // Add ambient occlusion from vertex input
  172. surfaceData.occlusion = input.occlusionVariation.x;
  173. #if defined(_NORMALMAP)
  174. surfaceData.normalTS.z *= facing; // mirror
  175. #else
  176. input.normalWS *= facing;
  177. #endif
  178. surfaceData.smoothness *= (facing < 0) ? _BackfaceSmoothness : 1;
  179. InputData inputData;
  180. InitializeInputData(input, surfaceData.normalTS, inputData);
  181. #ifdef _DBUFFER
  182. #if defined(_RECEIVEDECALS)
  183. ApplyDecalToSurfaceData(input.positionCS, surfaceData, inputData);
  184. #endif
  185. #endif
  186. // Apply lighting
  187. half4 color = CTIURPFragmentPBR(
  188. inputData,
  189. surfaceData,
  190. _Translucency * half4(additionalSurfaceData.translucency, 1, 1, 1),
  191. _AmbientReflection,
  192. _Wrap);
  193. // Add fog
  194. color.rgb = MixFog(color.rgb, inputData.fogCoord);
  195. return color;
  196. }