CTI URP Bark ForwardLit Pass.hlsl 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. #if defined(CTIBARKARRAY)
  48. half3 occlusionVariation : TEXCOORD7;
  49. #else
  50. half2 occlusionVariation : TEXCOORD7;
  51. #endif
  52. //UNITY_VERTEX_INPUT_INSTANCE_ID
  53. UNITY_VERTEX_OUTPUT_STEREO
  54. };
  55. //--------------------------------------
  56. // Include the surface function
  57. #include "Includes/CTI URP Bark SurfaceData.hlsl"
  58. //--------------------------------------
  59. // Vertex shader
  60. #include "Includes/CTI URP Bending.hlsl"
  61. Varyings LitPassVertex(Attributes input)
  62. {
  63. Varyings output = (Varyings)0;
  64. UNITY_SETUP_INSTANCE_ID(input);
  65. //UNITY_TRANSFER_INSTANCE_ID(input, output);
  66. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  67. CTI_AnimateVertex(
  68. input,
  69. #if defined (_BENDINGCOLRSONLY)
  70. float4(input.color.rg, input.color.ab), // animParams,
  71. #else
  72. float4(input.color.rg, input.texcoord1.xy), // animParams,
  73. #endif
  74. _BaseWindMultipliers
  75. );
  76. // CTI special
  77. #if defined(CTIBARKARRAY)
  78. output.occlusionVariation = input.color.arb;
  79. #else
  80. output.occlusionVariation = input.color.ar;
  81. #endif
  82. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  83. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
  84. half3 vertexLight = VertexLighting(vertexInput.positionWS, normalInput.normalWS);
  85. half fogFactor = 0;
  86. #if !defined(_FOG_FRAGMENT)
  87. fogFactor = ComputeFogFactor(vertexInput.positionCS.z);
  88. #endif
  89. output.uv = input.texcoord;
  90. // Already normalized from normal transform to WS.
  91. output.normalWS = normalInput.normalWS;
  92. #if defined(_NORMALMAP)
  93. float sign = input.tangentOS.w * GetOddNegativeScale();
  94. output.tangentWS = float4(normalInput.tangentWS.xyz, sign);
  95. #endif
  96. //OUTPUT_LIGHTMAP_UV(input.staticLightmapUV, unity_LightmapST, output.staticLightmapUV);
  97. //#ifdef DYNAMICLIGHTMAP_ON
  98. // output.dynamicLightmapUV = input.dynamicLightmapUV.xy * unity_DynamicLightmapST.xy + unity_DynamicLightmapST.zw;
  99. //#endif
  100. OUTPUT_SH(output.normalWS.xyz, output.vertexSH);
  101. #ifdef _ADDITIONAL_LIGHTS_VERTEX
  102. output.fogFactorAndVertexLight = half4(fogFactor, vertexLight);
  103. #else
  104. output.fogFactor = fogFactor;
  105. #endif
  106. #if defined(REQUIRES_WORLD_SPACE_POS_INTERPOLATOR)
  107. output.positionWS = vertexInput.positionWS;
  108. #endif
  109. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  110. output.shadowCoord = GetShadowCoord(vertexInput);
  111. #endif
  112. output.positionCS = vertexInput.positionCS;
  113. return output;
  114. }
  115. //--------------------------------------
  116. // Fragment shader and functions
  117. // InitializeSurfaceData
  118. // InitializeInputData
  119. void InitializeInputData(Varyings input, half3 normalTS, out InputData inputData)
  120. {
  121. inputData = (InputData)0;
  122. #if defined(REQUIRES_WORLD_SPACE_POS_INTERPOLATOR)
  123. inputData.positionWS = input.positionWS;
  124. #endif
  125. half3 viewDirWS = GetWorldSpaceNormalizeViewDir(input.positionWS);
  126. #if defined(_NORMALMAP)
  127. float sgn = input.tangentWS.w; // should be either +1 or -1
  128. float3 bitangent = sgn * cross(input.normalWS.xyz, input.tangentWS.xyz);
  129. inputData.normalWS = TransformTangentToWorld(normalTS, half3x3(input.tangentWS.xyz, bitangent, input.normalWS.xyz));
  130. #else
  131. inputData.normalWS = input.normalWS;
  132. #endif
  133. inputData.normalWS = NormalizeNormalPerPixel(inputData.normalWS);
  134. inputData.viewDirectionWS = viewDirWS;
  135. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  136. inputData.shadowCoord = input.shadowCoord;
  137. #elif defined(MAIN_LIGHT_CALCULATE_SHADOWS)
  138. inputData.shadowCoord = TransformWorldToShadowCoord(inputData.positionWS);
  139. #else
  140. inputData.shadowCoord = float4(0, 0, 0, 0);
  141. #endif
  142. #ifdef _ADDITIONAL_LIGHTS_VERTEX
  143. inputData.fogCoord = InitializeInputDataFog(float4(input.positionWS, 1.0), input.fogFactorAndVertexLight.x);
  144. inputData.vertexLighting = input.fogFactorAndVertexLight.yzw;
  145. #else
  146. inputData.fogCoord = InitializeInputDataFog(float4(input.positionWS, 1.0), input.fogFactor);
  147. #endif
  148. //#if defined(DYNAMICLIGHTMAP_ON)
  149. // inputData.bakedGI = SAMPLE_GI(input.staticLightmapUV, input.dynamicLightmapUV, input.vertexSH, inputData.normalWS);
  150. //#else
  151. // inputData.bakedGI = SAMPLE_GI(input.staticLightmapUV, input.vertexSH, inputData.normalWS);
  152. inputData.bakedGI = SAMPLE_GI(input.texcoord1, input.vertexSH, inputData.normalWS);
  153. //#endif
  154. inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.positionCS);
  155. inputData.shadowMask = SAMPLE_SHADOWMASK(input.lightmapUV);
  156. #if defined(DEBUG_DISPLAY)
  157. #if defined(DYNAMICLIGHTMAP_ON)
  158. //inputData.dynamicLightmapUV = input.dynamicLightmapUV;
  159. #endif
  160. #if defined(LIGHTMAP_ON)
  161. //inputData.staticLightmapUV = input.staticLightmapUV;
  162. #else
  163. inputData.vertexSH = input.vertexSH;
  164. #endif
  165. #endif
  166. }
  167. // Fragment shader
  168. half4 LitPassFragment(Varyings input, half facing : VFACE) : SV_Target
  169. {
  170. //UNITY_SETUP_INSTANCE_ID(input);
  171. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  172. #if defined(LOD_FADE_CROSSFADE) && !defined(SHADER_API_GLES)
  173. LODDitheringTransition(input.positionCS.xyz, unity_LODFade.x);
  174. #endif
  175. SurfaceData surfaceData;
  176. // Get the surface description
  177. #if defined(CTIBARKARRAY)
  178. InitializeStandardLitSurfaceData(input.occlusionVariation.yz, input.uv.xy, surfaceData);
  179. #else
  180. InitializeStandardLitSurfaceData(input.occlusionVariation.y, input.uv.xy, surfaceData);
  181. #endif
  182. // Add ambient occlusion from vertex input
  183. surfaceData.occlusion = input.occlusionVariation.x;
  184. InputData inputData;
  185. InitializeInputData(input, surfaceData.normalTS, inputData);
  186. #ifdef _DBUFFER
  187. #if defined(_RECEIVEDECALS)
  188. ApplyDecalToSurfaceData(input.positionCS, surfaceData, inputData);
  189. #endif
  190. #endif
  191. // Apply lighting
  192. half4 color = UniversalFragmentPBR(inputData, surfaceData);
  193. // Add fog
  194. color.rgb = MixFog(color.rgb, inputData.fogCoord);
  195. return color;
  196. }