ShaderVariablesFunctions.hlsl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. #ifndef UNITY_SHADER_VARIABLES_FUNCTIONS_INCLUDED
  2. #define UNITY_SHADER_VARIABLES_FUNCTIONS_INCLUDED
  3. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderVariablesFunctions.deprecated.hlsl"
  4. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Debug/DebuggingCommon.hlsl"
  5. VertexPositionInputs GetVertexPositionInputs(float3 positionOS)
  6. {
  7. VertexPositionInputs input;
  8. input.positionWS = TransformObjectToWorld(positionOS);
  9. input.positionVS = TransformWorldToView(input.positionWS);
  10. input.positionCS = TransformWorldToHClip(input.positionWS);
  11. float4 ndc = input.positionCS * 0.5f;
  12. input.positionNDC.xy = float2(ndc.x, ndc.y * _ProjectionParams.x) + ndc.w;
  13. input.positionNDC.zw = input.positionCS.zw;
  14. return input;
  15. }
  16. VertexNormalInputs GetVertexNormalInputs(float3 normalOS)
  17. {
  18. VertexNormalInputs tbn;
  19. tbn.tangentWS = real3(1.0, 0.0, 0.0);
  20. tbn.bitangentWS = real3(0.0, 1.0, 0.0);
  21. tbn.normalWS = TransformObjectToWorldNormal(normalOS);
  22. return tbn;
  23. }
  24. VertexNormalInputs GetVertexNormalInputs(float3 normalOS, float4 tangentOS)
  25. {
  26. VertexNormalInputs tbn;
  27. // mikkts space compliant. only normalize when extracting normal at frag.
  28. real sign = real(tangentOS.w) * GetOddNegativeScale();
  29. tbn.normalWS = TransformObjectToWorldNormal(normalOS);
  30. tbn.tangentWS = real3(TransformObjectToWorldDir(tangentOS.xyz));
  31. tbn.bitangentWS = real3(cross(tbn.normalWS, float3(tbn.tangentWS))) * sign;
  32. return tbn;
  33. }
  34. float4 GetScaledScreenParams()
  35. {
  36. return _ScaledScreenParams;
  37. }
  38. // Returns 'true' if the current view performs a perspective projection.
  39. bool IsPerspectiveProjection()
  40. {
  41. return (unity_OrthoParams.w == 0);
  42. }
  43. float3 GetCameraPositionWS()
  44. {
  45. // Currently we do not support Camera Relative Rendering so
  46. // we simply return the _WorldSpaceCameraPos until then
  47. return _WorldSpaceCameraPos;
  48. // We will replace the code above with this one once
  49. // we start supporting Camera Relative Rendering
  50. //#if (SHADEROPTIONS_CAMERA_RELATIVE_RENDERING != 0)
  51. // return float3(0, 0, 0);
  52. //#else
  53. // return _WorldSpaceCameraPos;
  54. //#endif
  55. }
  56. // Could be e.g. the position of a primary camera or a shadow-casting light.
  57. float3 GetCurrentViewPosition()
  58. {
  59. // Currently we do not support Camera Relative Rendering so
  60. // we simply return the _WorldSpaceCameraPos until then
  61. return GetCameraPositionWS();
  62. // We will replace the code above with this one once
  63. // we start supporting Camera Relative Rendering
  64. //#if defined(SHADERPASS) && (SHADERPASS != SHADERPASS_SHADOWS)
  65. // return GetCameraPositionWS();
  66. //#else
  67. // // This is a generic solution.
  68. // // However, for the primary camera, using '_WorldSpaceCameraPos' is better for cache locality,
  69. // // and in case we enable camera-relative rendering, we can statically set the position is 0.
  70. // return UNITY_MATRIX_I_V._14_24_34;
  71. //#endif
  72. }
  73. // Returns the forward (central) direction of the current view in the world space.
  74. float3 GetViewForwardDir()
  75. {
  76. float4x4 viewMat = GetWorldToViewMatrix();
  77. return -viewMat[2].xyz;
  78. }
  79. // Computes the world space view direction (pointing towards the viewer).
  80. float3 GetWorldSpaceViewDir(float3 positionWS)
  81. {
  82. if (IsPerspectiveProjection())
  83. {
  84. // Perspective
  85. return GetCurrentViewPosition() - positionWS;
  86. }
  87. else
  88. {
  89. // Orthographic
  90. return -GetViewForwardDir();
  91. }
  92. }
  93. // Computes the object space view direction (pointing towards the viewer).
  94. half3 GetObjectSpaceNormalizeViewDir(float3 positionOS)
  95. {
  96. if (IsPerspectiveProjection())
  97. {
  98. // Perspective
  99. float3 V = TransformWorldToObject(GetCurrentViewPosition()) - positionOS;
  100. return half3(normalize(V));
  101. }
  102. else
  103. {
  104. // Orthographic
  105. return half3(TransformWorldToObjectNormal(-GetViewForwardDir()));
  106. }
  107. }
  108. half3 GetWorldSpaceNormalizeViewDir(float3 positionWS)
  109. {
  110. if (IsPerspectiveProjection())
  111. {
  112. // Perspective
  113. float3 V = GetCurrentViewPosition() - positionWS;
  114. return half3(normalize(V));
  115. }
  116. else
  117. {
  118. // Orthographic
  119. return half3(-GetViewForwardDir());
  120. }
  121. }
  122. // UNITY_MATRIX_V defines a right-handed view space with the Z axis pointing towards the viewer.
  123. // This function reverses the direction of the Z axis (so that it points forward),
  124. // making the view space coordinate system left-handed.
  125. void GetLeftHandedViewSpaceMatrices(out float4x4 viewMatrix, out float4x4 projMatrix)
  126. {
  127. viewMatrix = UNITY_MATRIX_V;
  128. viewMatrix._31_32_33_34 = -viewMatrix._31_32_33_34;
  129. projMatrix = UNITY_MATRIX_P;
  130. projMatrix._13_23_33_43 = -projMatrix._13_23_33_43;
  131. }
  132. void AlphaDiscard(real alpha, real cutoff, real offset = real(0.0))
  133. {
  134. #ifdef _ALPHATEST_ON
  135. if (IsAlphaDiscardEnabled())
  136. clip(alpha - cutoff + offset);
  137. #endif
  138. }
  139. half OutputAlpha(half outputAlpha, half surfaceType = half(0.0))
  140. {
  141. return surfaceType == 1 ? outputAlpha : half(1.0);
  142. }
  143. // A word on normalization of normals:
  144. // For better quality normals should be normalized before and after
  145. // interpolation.
  146. // 1) In vertex, skinning or blend shapes might vary significantly the lenght of normal.
  147. // 2) In fragment, because even outputting unit-length normals interpolation can make it non-unit.
  148. // 3) In fragment when using normal map, because mikktspace sets up non orthonormal basis.
  149. // However we will try to balance performance vs quality here as also let users configure that as
  150. // shader quality tiers.
  151. // Low Quality Tier: Don't normalize per-vertex.
  152. // Medium Quality Tier: Always normalize per-vertex.
  153. // High Quality Tier: Always normalize per-vertex.
  154. //
  155. // Always normalize per-pixel.
  156. // Too many bug like lighting quality issues otherwise.
  157. half3 NormalizeNormalPerVertex(half3 normalWS)
  158. {
  159. #if defined(SHADER_QUALITY_LOW) && defined(_NORMALMAP)
  160. return normalWS;
  161. #else
  162. return normalize(normalWS);
  163. #endif
  164. }
  165. float3 NormalizeNormalPerVertex(float3 normalWS)
  166. {
  167. #if defined(SHADER_QUALITY_LOW) && defined(_NORMALMAP)
  168. return normalWS;
  169. #else
  170. return normalize(normalWS);
  171. #endif
  172. }
  173. half3 NormalizeNormalPerPixel(half3 normalWS)
  174. {
  175. // With XYZ normal map encoding we sporadically sample normals with near-zero-length causing Inf/NaN
  176. #if defined(UNITY_NO_DXT5nm) && defined(_NORMALMAP)
  177. return SafeNormalize(normalWS);
  178. #else
  179. return normalize(normalWS);
  180. #endif
  181. }
  182. float3 NormalizeNormalPerPixel(float3 normalWS)
  183. {
  184. #if defined(UNITY_NO_DXT5nm) && defined(_NORMALMAP)
  185. return SafeNormalize(normalWS);
  186. #else
  187. return normalize(normalWS);
  188. #endif
  189. }
  190. real ComputeFogFactorZ0ToFar(float z)
  191. {
  192. #if defined(FOG_LINEAR)
  193. // factor = (end-z)/(end-start) = z * (-1/(end-start)) + (end/(end-start))
  194. float fogFactor = saturate(z * unity_FogParams.z + unity_FogParams.w);
  195. return real(fogFactor);
  196. #elif defined(FOG_EXP) || defined(FOG_EXP2)
  197. // factor = exp(-(density*z)^2)
  198. // -density * z computed at vertex
  199. return real(unity_FogParams.x * z);
  200. #else
  201. return real(0.0);
  202. #endif
  203. }
  204. real ComputeFogFactor(float zPositionCS)
  205. {
  206. float clipZ_0Far = UNITY_Z_0_FAR_FROM_CLIPSPACE(zPositionCS);
  207. return ComputeFogFactorZ0ToFar(clipZ_0Far);
  208. }
  209. half ComputeFogIntensity(half fogFactor)
  210. {
  211. half fogIntensity = half(0.0);
  212. #if defined(FOG_LINEAR) || defined(FOG_EXP) || defined(FOG_EXP2)
  213. #if defined(FOG_EXP)
  214. // factor = exp(-density*z)
  215. // fogFactor = density*z compute at vertex
  216. fogIntensity = saturate(exp2(-fogFactor));
  217. #elif defined(FOG_EXP2)
  218. // factor = exp(-(density*z)^2)
  219. // fogFactor = density*z compute at vertex
  220. fogIntensity = saturate(exp2(-fogFactor * fogFactor));
  221. #elif defined(FOG_LINEAR)
  222. fogIntensity = fogFactor;
  223. #endif
  224. #endif
  225. return fogIntensity;
  226. }
  227. // Force enable fog fragment shader evaluation
  228. #define _FOG_FRAGMENT 1
  229. real InitializeInputDataFog(float4 positionWS, real vertFogFactor)
  230. {
  231. real fogFactor = 0.0;
  232. #if defined(_FOG_FRAGMENT)
  233. #if (defined(FOG_LINEAR) || defined(FOG_EXP) || defined(FOG_EXP2))
  234. // Compiler eliminates unused math --> matrix.column_z * vec
  235. float viewZ = -(mul(UNITY_MATRIX_V, positionWS).z);
  236. // View Z is 0 at camera pos, remap 0 to near plane.
  237. float nearToFarZ = max(viewZ - _ProjectionParams.y, 0);
  238. fogFactor = ComputeFogFactorZ0ToFar(nearToFarZ);
  239. #endif
  240. #else
  241. fogFactor = vertFogFactor;
  242. #endif
  243. return fogFactor;
  244. }
  245. float ComputeFogIntensity(float fogFactor)
  246. {
  247. float fogIntensity = 0.0;
  248. #if defined(FOG_LINEAR) || defined(FOG_EXP) || defined(FOG_EXP2)
  249. #if defined(FOG_EXP)
  250. // factor = exp(-density*z)
  251. // fogFactor = density*z compute at vertex
  252. fogIntensity = saturate(exp2(-fogFactor));
  253. #elif defined(FOG_EXP2)
  254. // factor = exp(-(density*z)^2)
  255. // fogFactor = density*z compute at vertex
  256. fogIntensity = saturate(exp2(-fogFactor * fogFactor));
  257. #elif defined(FOG_LINEAR)
  258. fogIntensity = fogFactor;
  259. #endif
  260. #endif
  261. return fogIntensity;
  262. }
  263. half3 MixFogColor(half3 fragColor, half3 fogColor, half fogFactor)
  264. {
  265. #if defined(FOG_LINEAR) || defined(FOG_EXP) || defined(FOG_EXP2)
  266. half fogIntensity = ComputeFogIntensity(fogFactor);
  267. fragColor = lerp(fogColor, fragColor, fogIntensity);
  268. #endif
  269. return fragColor;
  270. }
  271. float3 MixFogColor(float3 fragColor, float3 fogColor, float fogFactor)
  272. {
  273. #if defined(FOG_LINEAR) || defined(FOG_EXP) || defined(FOG_EXP2)
  274. if (IsFogEnabled())
  275. {
  276. float fogIntensity = ComputeFogIntensity(fogFactor);
  277. fragColor = lerp(fogColor, fragColor, fogIntensity);
  278. }
  279. #endif
  280. return fragColor;
  281. }
  282. half3 MixFog(half3 fragColor, half fogFactor)
  283. {
  284. return MixFogColor(fragColor, unity_FogColor.rgb, fogFactor);
  285. }
  286. float3 MixFog(float3 fragColor, float fogFactor)
  287. {
  288. return MixFogColor(fragColor, unity_FogColor.rgb, fogFactor);
  289. }
  290. // Linear depth buffer value between [0, 1] or [1, 0] to eye depth value between [near, far]
  291. half LinearDepthToEyeDepth(half rawDepth)
  292. {
  293. #if UNITY_REVERSED_Z
  294. return half(_ProjectionParams.z - (_ProjectionParams.z - _ProjectionParams.y) * rawDepth);
  295. #else
  296. return half(_ProjectionParams.y + (_ProjectionParams.z - _ProjectionParams.y) * rawDepth);
  297. #endif
  298. }
  299. float LinearDepthToEyeDepth(float rawDepth)
  300. {
  301. #if UNITY_REVERSED_Z
  302. return _ProjectionParams.z - (_ProjectionParams.z - _ProjectionParams.y) * rawDepth;
  303. #else
  304. return _ProjectionParams.y + (_ProjectionParams.z - _ProjectionParams.y) * rawDepth;
  305. #endif
  306. }
  307. void TransformScreenUV(inout float2 uv, float screenHeight)
  308. {
  309. #if UNITY_UV_STARTS_AT_TOP
  310. uv.y = screenHeight - (uv.y * _ScaleBiasRt.x + _ScaleBiasRt.y * screenHeight);
  311. #endif
  312. }
  313. void TransformScreenUV(inout float2 uv)
  314. {
  315. #if UNITY_UV_STARTS_AT_TOP
  316. TransformScreenUV(uv, GetScaledScreenParams().y);
  317. #endif
  318. }
  319. void TransformNormalizedScreenUV(inout float2 uv)
  320. {
  321. #if UNITY_UV_STARTS_AT_TOP
  322. TransformScreenUV(uv, 1.0);
  323. #endif
  324. }
  325. float2 GetNormalizedScreenSpaceUV(float2 positionCS)
  326. {
  327. float2 normalizedScreenSpaceUV = positionCS.xy * rcp(GetScaledScreenParams().xy);
  328. TransformNormalizedScreenUV(normalizedScreenSpaceUV);
  329. return normalizedScreenSpaceUV;
  330. }
  331. float2 GetNormalizedScreenSpaceUV(float4 positionCS)
  332. {
  333. return GetNormalizedScreenSpaceUV(positionCS.xy);
  334. }
  335. #if defined(UNITY_SINGLE_PASS_STEREO)
  336. float2 TransformStereoScreenSpaceTex(float2 uv, float w)
  337. {
  338. // TODO: RVS support can be added here, if Universal decides to support it
  339. float4 scaleOffset = unity_StereoScaleOffset[unity_StereoEyeIndex];
  340. return uv.xy * scaleOffset.xy + scaleOffset.zw * w;
  341. }
  342. float2 UnityStereoTransformScreenSpaceTex(float2 uv)
  343. {
  344. return TransformStereoScreenSpaceTex(saturate(uv), 1.0);
  345. }
  346. #else
  347. #define UnityStereoTransformScreenSpaceTex(uv) uv
  348. #endif // defined(UNITY_SINGLE_PASS_STEREO)
  349. #endif // UNITY_SHADER_VARIABLES_FUNCTIONS_INCLUDED