CTI URP Billboard ForwardLit Pass.hlsl 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. float4 positionOS : POSITION;
  8. float3 normalOS : NORMAL;
  9. float4 tangentOS : TANGENT;
  10. float2 texcoord : TEXCOORD0;
  11. float3 texcoord1 : TEXCOORD1;
  12. UNITY_VERTEX_INPUT_INSTANCE_ID
  13. };
  14. struct Varyings {
  15. float4 positionCS : SV_POSITION;
  16. float2 uv : TEXCOORD0;
  17. //DECLARE_LIGHTMAP_OR_SH(lightmapUV, vertexSH, 1);
  18. half3 vertexSH : TEXCOORD1;
  19. //#ifdef _ADDITIONAL_LIGHTS
  20. float3 positionWS : TEXCOORD2;
  21. //#endif
  22. half3 normalWS : TEXCOORD3;
  23. #if defined(_NORMALMAP) || defined(DEPTHNORMALPASS)
  24. half4 tangentWS : TEXCOORD4;
  25. #endif
  26. #ifdef _ADDITIONAL_LIGHTS_VERTEX
  27. half4 fogFactorAndVertexLight : TEXCOORD5;
  28. #else
  29. half fogFactor : TEXCOORD5;
  30. #endif
  31. // Due to the order of our includes we have to check for both defines
  32. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR) || !defined(_MAIN_LIGHT_SHADOWS_CASCADE)
  33. float4 shadowCoord : TEXCOORD7;
  34. #endif
  35. half colorVariation : TEXCOORD8;
  36. //UNITY_VERTEX_INPUT_INSTANCE_ID
  37. UNITY_VERTEX_OUTPUT_STEREO
  38. };
  39. //--------------------------------------
  40. // Include the surface function
  41. #include "Includes/CTI URP Billboard SurfaceData.hlsl"
  42. //--------------------------------------
  43. // Vertex shader
  44. #include "Includes/CTI URP Billboard.hlsl"
  45. Varyings LitPassVertex(Attributes input)
  46. {
  47. Varyings output = (Varyings)0;
  48. UNITY_SETUP_INSTANCE_ID(input);
  49. //UNITY_TRANSFER_INSTANCE_ID(input, output);
  50. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  51. half4 color;
  52. CTIBillboardVert(input, 0, color);
  53. // Set color variation
  54. output.colorVariation = color.r;
  55. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  56. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
  57. half3 vertexLight = VertexLighting(vertexInput.positionWS, normalInput.normalWS);
  58. half fogFactor = 0;
  59. #if !defined(_FOG_FRAGMENT)
  60. fogFactor = ComputeFogFactor(vertexInput.positionCS.z);
  61. #endif
  62. output.uv = input.texcoord;
  63. // Already normalized from normal transform to WS.
  64. output.normalWS = normalInput.normalWS;
  65. #if defined(_NORMALMAP)
  66. float sign = input.tangentOS.w * GetOddNegativeScale();
  67. output.tangentWS = float4(normalInput.tangentWS.xyz, sign);
  68. #endif
  69. //OUTPUT_LIGHTMAP_UV(input.staticLightmapUV, unity_LightmapST, output.staticLightmapUV);
  70. //#ifdef DYNAMICLIGHTMAP_ON
  71. // output.dynamicLightmapUV = input.dynamicLightmapUV.xy * unity_DynamicLightmapST.xy + unity_DynamicLightmapST.zw;
  72. //#endif
  73. OUTPUT_SH(output.normalWS.xyz, output.vertexSH);
  74. #ifdef _ADDITIONAL_LIGHTS_VERTEX
  75. output.fogFactorAndVertexLight = half4(fogFactor, vertexLight);
  76. #else
  77. output.fogFactor = fogFactor;
  78. #endif
  79. #if defined(REQUIRES_WORLD_SPACE_POS_INTERPOLATOR)
  80. output.positionWS = vertexInput.positionWS;
  81. #endif
  82. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  83. output.shadowCoord = GetShadowCoord(vertexInput);
  84. #endif
  85. output.positionCS = vertexInput.positionCS;
  86. return output;
  87. }
  88. //--------------------------------------
  89. // Fragment shader and functions
  90. // InitializeSurfaceData
  91. // InitializeInputData
  92. void InitializeInputData(Varyings input, half3 normalTS, out InputData inputData)
  93. {
  94. inputData = (InputData)0;
  95. #if defined(REQUIRES_WORLD_SPACE_POS_INTERPOLATOR)
  96. inputData.positionWS = input.positionWS;
  97. #endif
  98. half3 viewDirWS = GetWorldSpaceNormalizeViewDir(input.positionWS);
  99. #if defined(_NORMALMAP)
  100. float sgn = input.tangentWS.w; // should be either +1 or -1
  101. float3 bitangent = sgn * cross(input.normalWS.xyz, input.tangentWS.xyz);
  102. inputData.normalWS = TransformTangentToWorld(normalTS, half3x3(input.tangentWS.xyz, bitangent, input.normalWS.xyz));
  103. #else
  104. inputData.normalWS = input.normalWS;
  105. #endif
  106. inputData.normalWS = NormalizeNormalPerPixel(inputData.normalWS);
  107. inputData.viewDirectionWS = viewDirWS;
  108. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  109. inputData.shadowCoord = input.shadowCoord;
  110. #elif defined(MAIN_LIGHT_CALCULATE_SHADOWS)
  111. inputData.shadowCoord = TransformWorldToShadowCoord(inputData.positionWS);
  112. #else
  113. inputData.shadowCoord = float4(0, 0, 0, 0);
  114. #endif
  115. #ifdef _ADDITIONAL_LIGHTS_VERTEX
  116. inputData.fogCoord = InitializeInputDataFog(float4(input.positionWS, 1.0), input.fogFactorAndVertexLight.x);
  117. inputData.vertexLighting = input.fogFactorAndVertexLight.yzw;
  118. #else
  119. inputData.fogCoord = InitializeInputDataFog(float4(input.positionWS, 1.0), input.fogFactor);
  120. #endif
  121. //#if defined(DYNAMICLIGHTMAP_ON)
  122. // inputData.bakedGI = SAMPLE_GI(input.staticLightmapUV, input.dynamicLightmapUV, input.vertexSH, inputData.normalWS);
  123. //#else
  124. // inputData.bakedGI = SAMPLE_GI(input.staticLightmapUV, input.vertexSH, inputData.normalWS);
  125. inputData.bakedGI = SAMPLE_GI(input.texcoord1, input.vertexSH, inputData.normalWS);
  126. //#endif
  127. inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.positionCS);
  128. inputData.shadowMask = SAMPLE_SHADOWMASK(input.lightmapUV);
  129. #if defined(DEBUG_DISPLAY)
  130. #if defined(DYNAMICLIGHTMAP_ON)
  131. //inputData.dynamicLightmapUV = input.dynamicLightmapUV;
  132. #endif
  133. #if defined(LIGHTMAP_ON)
  134. //inputData.staticLightmapUV = input.staticLightmapUV;
  135. #else
  136. inputData.vertexSH = input.vertexSH;
  137. #endif
  138. #endif
  139. }
  140. // Fragment shader
  141. half4 LitPassFragment(Varyings input) : SV_Target
  142. {
  143. //UNITY_SETUP_INSTANCE_ID(input);
  144. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  145. #if defined(LOD_FADE_CROSSFADE) && !defined(SHADER_API_GLES)
  146. LODDitheringTransition(input.positionCS.xyz, unity_LODFade.x);
  147. #endif
  148. SurfaceData surfaceData;
  149. AdditionalSurfaceData additionalSurfaceData;
  150. // Get the surface description
  151. InitializeBillboardLitSurfaceData(input.colorVariation.x, input.uv, surfaceData, additionalSurfaceData);
  152. // Add ambient occlusion from alpha
  153. surfaceData.occlusion = (surfaceData.occlusion <= _AlphaLeak) ? 1 : surfaceData.occlusion; // Eliminate alpha leaking into ao
  154. surfaceData.occlusion = lerp(1, surfaceData.occlusion * 2 - 1, _OcclusionStrength);
  155. InputData inputData;
  156. InitializeInputData(input, surfaceData.normalTS, inputData);
  157. // Apply lighting
  158. half4 color = CTIURPFragmentPBR(
  159. inputData,
  160. surfaceData,
  161. _Translucency * half4(additionalSurfaceData.translucency, 1, 1, 1),
  162. _AmbientReflection,
  163. _Wrap);
  164. // Add fog
  165. color.rgb = MixFog(color.rgb, inputData.fogCoord);
  166. return color;
  167. }