LitForwardPass_DR.hlsl 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #ifndef FLATKIT_LIGHT_PASS_DR_INCLUDED
  2. #define FLATKIT_LIGHT_PASS_DR_INCLUDED
  3. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
  4. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
  5. // Check is needed because in Unity 2021 they use two different URP versions - 14 on desktop and 12 on mobile.
  6. #if !VERSION_LOWER(13, 0)
  7. #if defined(LOD_FADE_CROSSFADE)
  8. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/LODCrossFade.hlsl"
  9. #endif
  10. #endif
  11. #include "Lighting_DR.hlsl"
  12. struct Attributes
  13. {
  14. float4 positionOS : POSITION;
  15. float3 normalOS : NORMAL;
  16. float4 tangentOS : TANGENT;
  17. float2 texcoord : TEXCOORD0;
  18. float2 staticLightmapUV : TEXCOORD1;
  19. float2 dynamicLightmapUV : TEXCOORD2;
  20. #if defined(DR_VERTEX_COLORS_ON)
  21. float4 color : COLOR;
  22. #endif
  23. UNITY_VERTEX_INPUT_INSTANCE_ID
  24. };
  25. struct Varyings
  26. {
  27. float2 uv : TEXCOORD0;
  28. float3 positionWS : TEXCOORD1; // xyz: posWS
  29. #ifdef _NORMALMAP
  30. float4 normalWS : TEXCOORD2; // xyz: normal, w: viewDir.x
  31. float4 tangentWS : TEXCOORD3; // xyz: tangent, w: viewDir.y
  32. float4 bitangentWS : TEXCOORD4; // xyz: bitangent, w: viewDir.z
  33. #else
  34. float3 normalWS : TEXCOORD2;
  35. float3 viewDir : TEXCOORD3;
  36. #endif
  37. #ifdef _ADDITIONAL_LIGHTS_VERTEX
  38. half4 fogFactorAndVertexLight : TEXCOORD5; // x: fogFactor, yzw: vertex light
  39. #else
  40. half fogFactor : TEXCOORD5;
  41. #endif
  42. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  43. float4 shadowCoord : TEXCOORD6;
  44. #endif
  45. DECLARE_LIGHTMAP_OR_SH(staticLightmapUV, vertexSH, 7);
  46. #ifdef DYNAMICLIGHTMAP_ON
  47. float2 dynamicLightmapUV : TEXCOORD8; // Dynamic lightmap UVs
  48. #endif
  49. float4 positionCS : SV_POSITION;
  50. #if defined(DR_VERTEX_COLORS_ON)
  51. float4 VertexColor : COLOR;
  52. #endif
  53. UNITY_VERTEX_INPUT_INSTANCE_ID
  54. UNITY_VERTEX_OUTPUT_STEREO
  55. };
  56. /// ---------------------------------------------------------------------------
  57. // Library/PackageCache/com.unity.render-pipelines.universal@16.0.5/Shaders/SimpleLitForwardPass.hlsl
  58. void InitializeInputData_DR(Varyings input, half3 normalTS, out InputData inputData)
  59. {
  60. inputData = (InputData)0;
  61. inputData.positionWS = input.positionWS;
  62. inputData.positionCS = input.positionCS;
  63. #ifdef _NORMALMAP
  64. half3 viewDirWS = half3(input.normalWS.w, input.tangentWS.w, input.bitangentWS.w);
  65. #if VERSION_GREATER_EQUAL(12, 0)
  66. inputData.tangentToWorld = half3x3(input.tangentWS.xyz, input.bitangentWS.xyz, input.normalWS.xyz);
  67. inputData.normalWS = TransformTangentToWorld(normalTS, inputData.tangentToWorld);
  68. #else
  69. float sgn = input.tangentWS.w; // should be either +1 or -1
  70. float3 bitangent = sgn * cross(input.normalWS.xyz, input.tangentWS.xyz);
  71. inputData.normalWS = TransformTangentToWorld(normalTS, half3x3(input.tangentWS.xyz, bitangent.xyz, input.normalWS.xyz));
  72. #endif
  73. #else
  74. half3 viewDirWS = GetWorldSpaceNormalizeViewDir(inputData.positionWS);
  75. inputData.normalWS = input.normalWS;
  76. #endif
  77. inputData.normalWS = NormalizeNormalPerPixel(inputData.normalWS);
  78. viewDirWS = SafeNormalize(viewDirWS);
  79. inputData.viewDirectionWS = viewDirWS;
  80. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  81. inputData.shadowCoord = input.shadowCoord;
  82. #elif defined(MAIN_LIGHT_CALCULATE_SHADOWS)
  83. inputData.shadowCoord = TransformWorldToShadowCoord(inputData.positionWS);
  84. #else
  85. inputData.shadowCoord = float4(0, 0, 0, 0);
  86. #endif
  87. #ifdef _ADDITIONAL_LIGHTS_VERTEX
  88. inputData.fogCoord = InitializeInputDataFog(float4(inputData.positionWS, 1.0), input.fogFactorAndVertexLight.x);
  89. inputData.vertexLighting = input.fogFactorAndVertexLight.yzw;
  90. #else
  91. #if VERSION_GREATER_EQUAL(12, 0)
  92. inputData.fogCoord = InitializeInputDataFog(float4(inputData.positionWS, 1.0), input.fogFactor);
  93. #endif
  94. inputData.vertexLighting = half3(0, 0, 0);
  95. #endif
  96. #if defined(DYNAMICLIGHTMAP_ON)
  97. inputData.bakedGI = SAMPLE_GI(input.staticLightmapUV, input.dynamicLightmapUV, input.vertexSH, inputData.normalWS);
  98. #elif !defined(LIGHTMAP_ON) && (defined(PROBE_VOLUMES_L1) || defined(PROBE_VOLUMES_L2))
  99. #if UNITY_VERSION >= 60000012
  100. inputData.bakedGI = SAMPLE_GI(input.vertexSH,
  101. GetAbsolutePositionWS(inputData.positionWS),
  102. inputData.normalWS,
  103. inputData.viewDirectionWS,
  104. input.positionCS.xy,
  105. /* vertexProbeOcclusion */ 0, /* probeOcclusion */ 0
  106. );
  107. #else // UNITY_VERSION >= 60000012
  108. inputData.bakedGI = SAMPLE_GI(input.vertexSH,
  109. GetAbsolutePositionWS(inputData.positionWS),
  110. inputData.normalWS,
  111. inputData.viewDirectionWS,
  112. input.positionCS.xy
  113. );
  114. #endif // UNITY_VERSION >= 60000012
  115. #else // !defined(LIGHTMAP_ON) && (defined(PROBE_VOLUMES_L1) || defined(PROBE_VOLUMES_L2))
  116. inputData.bakedGI = SAMPLE_GI(input.staticLightmapUV, input.vertexSH, inputData.normalWS);
  117. #endif // !defined(LIGHTMAP_ON) && (defined(PROBE_VOLUMES_L1) || defined(PROBE_VOLUMES_L2))
  118. inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.positionCS);
  119. inputData.shadowMask = SAMPLE_SHADOWMASK(input.staticLightmapUV);
  120. #if defined(DEBUG_DISPLAY)
  121. #if defined(DYNAMICLIGHTMAP_ON)
  122. inputData.dynamicLightmapUV = input.dynamicLightmapUV.xy;
  123. #endif
  124. #if defined(LIGHTMAP_ON)
  125. inputData.staticLightmapUV = input.staticLightmapUV;
  126. #else
  127. inputData.vertexSH = input.vertexSH;
  128. #endif
  129. #endif
  130. }
  131. Varyings StylizedPassVertex(Attributes input)
  132. {
  133. #if defined(CURVEDWORLD_IS_INSTALLED) && !defined(CURVEDWORLD_DISABLED_ON)
  134. #ifdef CURVEDWORLD_NORMAL_TRANSFORMATION_ON
  135. CURVEDWORLD_TRANSFORM_VERTEX_AND_NORMAL(input.positionOS, input.normalOS, input.tangentOS)
  136. #else
  137. CURVEDWORLD_TRANSFORM_VERTEX(input.positionOS)
  138. #endif
  139. #endif
  140. Varyings output = (Varyings)0;
  141. UNITY_SETUP_INSTANCE_ID(input);
  142. UNITY_TRANSFER_INSTANCE_ID(input, output);
  143. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  144. const VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  145. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
  146. #if defined(_FOG_FRAGMENT)
  147. half fogFactor = 0;
  148. #else
  149. half fogFactor = ComputeFogFactor(vertexInput.positionCS.z);
  150. #endif
  151. output.uv = TRANSFORM_TEX(input.texcoord, _BaseMap);
  152. output.positionWS.xyz = vertexInput.positionWS;
  153. output.positionCS = vertexInput.positionCS;
  154. half3 viewDirWS = GetCameraPositionWS() - vertexInput.positionWS;
  155. #ifdef _NORMALMAP
  156. output.normalWS = half4(normalInput.normalWS, viewDirWS.x);
  157. output.tangentWS = half4(normalInput.tangentWS, viewDirWS.y);
  158. output.bitangentWS = half4(normalInput.bitangentWS, viewDirWS.z);
  159. #else
  160. output.normal = NormalizeNormalPerVertex(normalInput.normalWS);
  161. output.viewDir = viewDirWS;
  162. #endif
  163. OUTPUT_LIGHTMAP_UV(input.staticLightmapUV, unity_LightmapST, output.staticLightmapUV);
  164. #ifdef DYNAMICLIGHTMAP_ON
  165. output.dynamicLightmapUV = input.dynamicLightmapUV.xy * unity_DynamicLightmapST.xy + unity_DynamicLightmapST.zw;
  166. #endif
  167. #if UNITY_VERSION >= 60000010
  168. float4 probeOcclusion;
  169. OUTPUT_SH4(vertexInput.positionWS, output.normalWS.xyz, GetWorldSpaceNormalizeViewDir(vertexInput.positionWS), output.vertexSH, probeOcclusion);
  170. #elif UNITY_VERSION >= 202317
  171. OUTPUT_SH4(vertexInput.positionWS, output.normalWS.xyz, GetWorldSpaceNormalizeViewDir(vertexInput.positionWS), output.vertexSH);
  172. #elif UNITY_VERSION >= 202310
  173. OUTPUT_SH(vertexInput.positionWS, output.normalWS.xyz, GetWorldSpaceNormalizeViewDir(vertexInput.positionWS), output.vertexSH);
  174. #else
  175. OUTPUT_SH(output.normalWS.xyz, output.vertexSH);
  176. #endif
  177. #ifdef _ADDITIONAL_LIGHTS_VERTEX
  178. half3 vertexLight = VertexLighting(vertexInput.positionWS, normalInput.normalWS);
  179. output.fogFactorAndVertexLight = half4(fogFactor, vertexLight);
  180. #else
  181. output.fogFactor = fogFactor;
  182. #endif
  183. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  184. output.shadowCoord = GetShadowCoord(vertexInput);
  185. #endif
  186. #if defined(DR_VERTEX_COLORS_ON)
  187. output.VertexColor = input.color;
  188. #endif
  189. return output;
  190. }
  191. half4 StylizedPassFragment(Varyings input) : SV_Target
  192. {
  193. UNITY_SETUP_INSTANCE_ID(input);
  194. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  195. SurfaceData surfaceData;
  196. InitializeSimpleLitSurfaceData(input.uv, surfaceData);
  197. #if defined(LOD_FADE_CROSSFADE) && !VERSION_LOWER(13, 0)
  198. LODFadeCrossFade(input.positionCS);
  199. #endif
  200. InputData inputData;
  201. InitializeInputData_DR(input, surfaceData.normalTS, inputData);
  202. #if UNITY_VERSION >= 202330
  203. SETUP_DEBUG_TEXTURE_DATA(inputData, input.uv);
  204. #elif UNITY_VERSION >= 202210
  205. SETUP_DEBUG_TEXTURE_DATA(inputData, input.uv, _BaseMap);
  206. #endif
  207. // Apply vertex color before shading (default behavior). Remove the #if block and uncomment below to apply after
  208. // shading.
  209. #if defined(DR_VERTEX_COLORS_ON)
  210. _BaseColor.rgb *= input.VertexColor.rgb;
  211. #endif
  212. // Computes direct light contribution.
  213. half4 color = UniversalFragment_DSTRM(inputData, surfaceData, input.uv);
  214. /*
  215. #if defined(DR_VERTEX_COLORS_ON)
  216. color.rgb *= input.VertexColor.rgb;
  217. #endif
  218. */
  219. color.rgb = MixFog(color.rgb, inputData.fogCoord);
  220. #if UNITY_VERSION >= 202220
  221. color.a = OutputAlpha(color.a, IsSurfaceTypeTransparent(_Surface));
  222. #else
  223. color.a = OutputAlpha(color.a, _Surface);
  224. #endif
  225. return color;
  226. }
  227. #endif // FLATKIT_LIGHT_PASS_DR_INCLUDED