BSDF.hlsl 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. #ifndef UNITY_BSDF_INCLUDED
  2. #define UNITY_BSDF_INCLUDED
  3. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
  4. // Note: All NDF and diffuse term have a version with and without divide by PI.
  5. // Version with divide by PI are use for direct lighting.
  6. // Version without divide by PI are use for image based lighting where often the PI cancel during importance sampling
  7. //-----------------------------------------------------------------------------
  8. // Help for BSDF evaluation
  9. //-----------------------------------------------------------------------------
  10. // Cosine-weighted BSDF (a BSDF taking the projected solid angle into account).
  11. // If some of the values are monochromatic, the compiler will optimize accordingly.
  12. struct CBSDF
  13. {
  14. float3 diffR; // Diffuse reflection (T -> MS -> T, same sides)
  15. float3 specR; // Specular reflection (R, RR, TRT, etc)
  16. float3 diffT; // Diffuse transmission (rough T or TT, opposite sides)
  17. float3 specT; // Specular transmission (T, TT, TRRT, etc)
  18. };
  19. //-----------------------------------------------------------------------------
  20. // Fresnel term
  21. //-----------------------------------------------------------------------------
  22. real F_Schlick(real f0, real f90, real u)
  23. {
  24. real x = 1.0 - u;
  25. real x2 = x * x;
  26. real x5 = x * x2 * x2;
  27. return (f90 - f0) * x5 + f0; // sub mul mul mul sub mad
  28. }
  29. real F_Schlick(real f0, real u)
  30. {
  31. return F_Schlick(f0, 1.0, u); // sub mul mul mul sub mad
  32. }
  33. real3 F_Schlick(real3 f0, real f90, real u)
  34. {
  35. real x = 1.0 - u;
  36. real x2 = x * x;
  37. real x5 = x * x2 * x2;
  38. return f0 * (1.0 - x5) + (f90 * x5); // sub mul mul mul sub mul mad*3
  39. }
  40. real3 F_Schlick(real3 f0, real u)
  41. {
  42. return F_Schlick(f0, 1.0, u); // sub mul mul mul sub mad*3
  43. }
  44. // Does not handle TIR.
  45. real F_Transm_Schlick(real f0, real f90, real u)
  46. {
  47. real x = 1.0 - u;
  48. real x2 = x * x;
  49. real x5 = x * x2 * x2;
  50. return (1.0 - f90 * x5) - f0 * (1.0 - x5); // sub mul mul mul mad sub mad
  51. }
  52. // Does not handle TIR.
  53. real F_Transm_Schlick(real f0, real u)
  54. {
  55. return F_Transm_Schlick(f0, 1.0, u); // sub mul mul mad mad
  56. }
  57. // Does not handle TIR.
  58. real3 F_Transm_Schlick(real3 f0, real f90, real u)
  59. {
  60. real x = 1.0 - u;
  61. real x2 = x * x;
  62. real x5 = x * x2 * x2;
  63. return (1.0 - f90 * x5) - f0 * (1.0 - x5); // sub mul mul mul mad sub mad*3
  64. }
  65. // Does not handle TIR.
  66. real3 F_Transm_Schlick(real3 f0, real u)
  67. {
  68. return F_Transm_Schlick(f0, 1.0, u); // sub mul mul mad mad*3
  69. }
  70. // Ref: https://seblagarde.wordpress.com/2013/04/29/memo-on-fresnel-equations/
  71. // Fresnel dielectric / dielectric
  72. real F_FresnelDielectric(real ior, real u)
  73. {
  74. real g = sqrt(Sq(ior) + Sq(u) - 1.0);
  75. // The "1.0 - saturate(1.0 - result)" formulation allows to recover form cases where g is undefined, for IORs < 1
  76. return 1.0 - saturate(1.0 - 0.5 * Sq((g - u) / (g + u)) * (1.0 + Sq(((g + u) * u - 1.0) / ((g - u) * u + 1.0))));
  77. }
  78. // Fresnel dieletric / conductor
  79. // Note: etak2 = etak * etak (optimization for Artist Friendly Metallic Fresnel below)
  80. // eta = eta_t / eta_i and etak = k_t / n_i
  81. real3 F_FresnelConductor(real3 eta, real3 etak2, real cosTheta)
  82. {
  83. real cosTheta2 = cosTheta * cosTheta;
  84. real sinTheta2 = 1.0 - cosTheta2;
  85. real3 eta2 = eta * eta;
  86. real3 t0 = eta2 - etak2 - sinTheta2;
  87. real3 a2plusb2 = sqrt(t0 * t0 + 4.0 * eta2 * etak2);
  88. real3 t1 = a2plusb2 + cosTheta2;
  89. real3 a = sqrt(0.5 * (a2plusb2 + t0));
  90. real3 t2 = 2.0 * a * cosTheta;
  91. real3 Rs = (t1 - t2) / (t1 + t2);
  92. real3 t3 = cosTheta2 * a2plusb2 + sinTheta2 * sinTheta2;
  93. real3 t4 = t2 * sinTheta2;
  94. real3 Rp = Rs * (t3 - t4) / (t3 + t4);
  95. return 0.5 * (Rp + Rs);
  96. }
  97. // Conversion FO/IOR
  98. TEMPLATE_2_REAL(IorToFresnel0, transmittedIor, incidentIor, return Sq((transmittedIor - incidentIor) / (transmittedIor + incidentIor)) )
  99. // ior is a value between 1.0 and 3.0. 1.0 is air interface
  100. real IorToFresnel0(real transmittedIor)
  101. {
  102. return IorToFresnel0(transmittedIor, 1.0);
  103. }
  104. // Assume air interface for top
  105. // Note: We don't handle the case fresnel0 == 1
  106. //real Fresnel0ToIor(real fresnel0)
  107. //{
  108. // real sqrtF0 = sqrt(fresnel0);
  109. // return (1.0 + sqrtF0) / (1.0 - sqrtF0);
  110. //}
  111. TEMPLATE_1_REAL(Fresnel0ToIor, fresnel0, return ((1.0 + sqrt(fresnel0)) / (1.0 - sqrt(fresnel0))) )
  112. // This function is a coarse approximation of computing fresnel0 for a different top than air (here clear coat of IOR 1.5) when we only have fresnel0 with air interface
  113. // This function is equivalent to IorToFresnel0(Fresnel0ToIor(fresnel0), 1.5)
  114. // mean
  115. // real sqrtF0 = sqrt(fresnel0);
  116. // return Sq(1.0 - 5.0 * sqrtF0) / Sq(5.0 - sqrtF0);
  117. // Optimization: Fit of the function (3 mad) for range [0.04 (should return 0), 1 (should return 1)]
  118. TEMPLATE_1_REAL(ConvertF0ForAirInterfaceToF0ForClearCoat15, fresnel0, return saturate(-0.0256868 + fresnel0 * (0.326846 + (0.978946 - 0.283835 * fresnel0) * fresnel0)))
  119. // Artist Friendly Metallic Fresnel Ref: http://jcgt.org/published/0003/04/03/paper.pdf
  120. real3 GetIorN(real3 f0, real3 edgeTint)
  121. {
  122. real3 sqrtF0 = sqrt(f0);
  123. return lerp((1.0 - f0) / (1.0 + f0), (1.0 + sqrtF0) / (1.0 - sqrt(f0)), edgeTint);
  124. }
  125. real3 getIorK2(real3 f0, real3 n)
  126. {
  127. real3 nf0 = Sq(n + 1.0) * f0 - Sq(f0 - 1.0);
  128. return nf0 / (1.0 - f0);
  129. }
  130. // same as regular refract except there is not the test for total internal reflection + the vector is flipped for processing
  131. real3 CoatRefract(real3 X, real3 N, real ieta)
  132. {
  133. real XdotN = saturate(dot(N, X));
  134. return ieta * X + (sqrt(1 + ieta * ieta * (XdotN * XdotN - 1)) - ieta * XdotN) * N;
  135. }
  136. //-----------------------------------------------------------------------------
  137. // Specular BRDF
  138. //-----------------------------------------------------------------------------
  139. real D_GGXNoPI(real NdotH, real roughness)
  140. {
  141. real a2 = Sq(roughness);
  142. real s = (NdotH * a2 - NdotH) * NdotH + 1.0;
  143. // If roughness is 0, returns (NdotH == 1 ? 1 : 0).
  144. // That is, it returns 1 for perfect mirror reflection, and 0 otherwise.
  145. return SafeDiv(a2, s * s);
  146. }
  147. real D_GGX(real NdotH, real roughness)
  148. {
  149. return INV_PI * D_GGXNoPI(NdotH, roughness);
  150. }
  151. // Ref: Understanding the Masking-Shadowing Function in Microfacet-Based BRDFs, p. 19, 29.
  152. // p. 84 (37/60)
  153. real G_MaskingSmithGGX(real NdotV, real roughness)
  154. {
  155. // G1(V, H) = HeavisideStep(VdotH) / (1 + Lambda(V)).
  156. // Lambda(V) = -0.5 + 0.5 * sqrt(1 + 1 / a^2).
  157. // a = 1 / (roughness * tan(theta)).
  158. // 1 + Lambda(V) = 0.5 + 0.5 * sqrt(1 + roughness^2 * tan^2(theta)).
  159. // tan^2(theta) = (1 - cos^2(theta)) / cos^2(theta) = 1 / cos^2(theta) - 1.
  160. // Assume that (VdotH > 0), e.i. (acos(LdotV) < Pi).
  161. return 1.0 / (0.5 + 0.5 * sqrt(1.0 + Sq(roughness) * (1.0 / Sq(NdotV) - 1.0)));
  162. }
  163. // Ref: Understanding the Masking-Shadowing Function in Microfacet-Based BRDFs, p. 12.
  164. real D_GGX_Visible(real NdotH, real NdotV, real VdotH, real roughness)
  165. {
  166. return D_GGX(NdotH, roughness) * G_MaskingSmithGGX(NdotV, roughness) * VdotH / NdotV;
  167. }
  168. // Precompute part of lambdaV
  169. real GetSmithJointGGXPartLambdaV(real NdotV, real roughness)
  170. {
  171. real a2 = Sq(roughness);
  172. return sqrt((-NdotV * a2 + NdotV) * NdotV + a2);
  173. }
  174. // Note: V = G / (4 * NdotL * NdotV)
  175. // Ref: http://jcgt.org/published/0003/02/03/paper.pdf
  176. real V_SmithJointGGX(real NdotL, real NdotV, real roughness, real partLambdaV)
  177. {
  178. real a2 = Sq(roughness);
  179. // Original formulation:
  180. // lambda_v = (-1 + sqrt(a2 * (1 - NdotL2) / NdotL2 + 1)) * 0.5
  181. // lambda_l = (-1 + sqrt(a2 * (1 - NdotV2) / NdotV2 + 1)) * 0.5
  182. // G = 1 / (1 + lambda_v + lambda_l);
  183. // Reorder code to be more optimal:
  184. real lambdaV = NdotL * partLambdaV;
  185. real lambdaL = NdotV * sqrt((-NdotL * a2 + NdotL) * NdotL + a2);
  186. // Simplify visibility term: (2.0 * NdotL * NdotV) / ((4.0 * NdotL * NdotV) * (lambda_v + lambda_l))
  187. return 0.5 / (lambdaV + lambdaL);
  188. }
  189. real V_SmithJointGGX(real NdotL, real NdotV, real roughness)
  190. {
  191. real partLambdaV = GetSmithJointGGXPartLambdaV(NdotV, roughness);
  192. return V_SmithJointGGX(NdotL, NdotV, roughness, partLambdaV);
  193. }
  194. // Inline D_GGX() * V_SmithJointGGX() together for better code generation.
  195. real DV_SmithJointGGX(real NdotH, real NdotL, real NdotV, real roughness, real partLambdaV)
  196. {
  197. real a2 = Sq(roughness);
  198. real s = (NdotH * a2 - NdotH) * NdotH + 1.0;
  199. real lambdaV = NdotL * partLambdaV;
  200. real lambdaL = NdotV * sqrt((-NdotL * a2 + NdotL) * NdotL + a2);
  201. real2 D = real2(a2, s * s); // Fraction without the multiplier (1/Pi)
  202. real2 G = real2(1, lambdaV + lambdaL); // Fraction without the multiplier (1/2)
  203. // This function is only used for direct lighting.
  204. // If roughness is 0, the probability of hitting a punctual or directional light is also 0.
  205. // Therefore, we return 0. The most efficient way to do it is with a max().
  206. return INV_PI * 0.5 * (D.x * G.x) / max(D.y * G.y, REAL_MIN);
  207. }
  208. real DV_SmithJointGGX(real NdotH, real NdotL, real NdotV, real roughness)
  209. {
  210. real partLambdaV = GetSmithJointGGXPartLambdaV(NdotV, roughness);
  211. return DV_SmithJointGGX(NdotH, NdotL, NdotV, roughness, partLambdaV);
  212. }
  213. // Precompute a part of LambdaV.
  214. // Note on this linear approximation.
  215. // Exact for roughness values of 0 and 1. Also, exact when the cosine is 0 or 1.
  216. // Otherwise, the worst case relative error is around 10%.
  217. // https://www.desmos.com/calculator/wtp8lnjutx
  218. real GetSmithJointGGXPartLambdaVApprox(real NdotV, real roughness)
  219. {
  220. real a = roughness;
  221. return NdotV * (1 - a) + a;
  222. }
  223. real V_SmithJointGGXApprox(real NdotL, real NdotV, real roughness, real partLambdaV)
  224. {
  225. real a = roughness;
  226. real lambdaV = NdotL * partLambdaV;
  227. real lambdaL = NdotV * (NdotL * (1 - a) + a);
  228. return 0.5 / (lambdaV + lambdaL);
  229. }
  230. real V_SmithJointGGXApprox(real NdotL, real NdotV, real roughness)
  231. {
  232. real partLambdaV = GetSmithJointGGXPartLambdaVApprox(NdotV, roughness);
  233. return V_SmithJointGGXApprox(NdotL, NdotV, roughness, partLambdaV);
  234. }
  235. // roughnessT -> roughness in tangent direction
  236. // roughnessB -> roughness in bitangent direction
  237. real D_GGXAnisoNoPI(real TdotH, real BdotH, real NdotH, real roughnessT, real roughnessB)
  238. {
  239. real a2 = roughnessT * roughnessB;
  240. real3 v = real3(roughnessB * TdotH, roughnessT * BdotH, a2 * NdotH);
  241. real s = dot(v, v);
  242. // If roughness is 0, returns (NdotH == 1 ? 1 : 0).
  243. // That is, it returns 1 for perfect mirror reflection, and 0 otherwise.
  244. return SafeDiv(a2 * a2 * a2, s * s);
  245. }
  246. real D_GGXAniso(real TdotH, real BdotH, real NdotH, real roughnessT, real roughnessB)
  247. {
  248. return INV_PI * D_GGXAnisoNoPI(TdotH, BdotH, NdotH, roughnessT, roughnessB);
  249. }
  250. real GetSmithJointGGXAnisoPartLambdaV(real TdotV, real BdotV, real NdotV, real roughnessT, real roughnessB)
  251. {
  252. return length(real3(roughnessT * TdotV, roughnessB * BdotV, NdotV));
  253. }
  254. // Note: V = G / (4 * NdotL * NdotV)
  255. // Ref: https://cedec.cesa.or.jp/2015/session/ENG/14698.html The Rendering Materials of Far Cry 4
  256. real V_SmithJointGGXAniso(real TdotV, real BdotV, real NdotV, real TdotL, real BdotL, real NdotL, real roughnessT, real roughnessB, real partLambdaV)
  257. {
  258. real lambdaV = NdotL * partLambdaV;
  259. real lambdaL = NdotV * length(real3(roughnessT * TdotL, roughnessB * BdotL, NdotL));
  260. return 0.5 / (lambdaV + lambdaL);
  261. }
  262. real V_SmithJointGGXAniso(real TdotV, real BdotV, real NdotV, real TdotL, real BdotL, real NdotL, real roughnessT, real roughnessB)
  263. {
  264. real partLambdaV = GetSmithJointGGXAnisoPartLambdaV(TdotV, BdotV, NdotV, roughnessT, roughnessB);
  265. return V_SmithJointGGXAniso(TdotV, BdotV, NdotV, TdotL, BdotL, NdotL, roughnessT, roughnessB, partLambdaV);
  266. }
  267. // Inline D_GGXAniso() * V_SmithJointGGXAniso() together for better code generation.
  268. real DV_SmithJointGGXAniso(real TdotH, real BdotH, real NdotH, real NdotV,
  269. real TdotL, real BdotL, real NdotL,
  270. real roughnessT, real roughnessB, real partLambdaV)
  271. {
  272. real a2 = roughnessT * roughnessB;
  273. real3 v = real3(roughnessB * TdotH, roughnessT * BdotH, a2 * NdotH);
  274. real s = dot(v, v);
  275. real lambdaV = NdotL * partLambdaV;
  276. real lambdaL = NdotV * length(real3(roughnessT * TdotL, roughnessB * BdotL, NdotL));
  277. real2 D = real2(a2 * a2 * a2, s * s); // Fraction without the multiplier (1/Pi)
  278. real2 G = real2(1, lambdaV + lambdaL); // Fraction without the multiplier (1/2)
  279. // This function is only used for direct lighting.
  280. // If roughness is 0, the probability of hitting a punctual or directional light is also 0.
  281. // Therefore, we return 0. The most efficient way to do it is with a max().
  282. return (INV_PI * 0.5) * (D.x * G.x) / max(D.y * G.y, REAL_MIN);
  283. }
  284. real DV_SmithJointGGXAniso(real TdotH, real BdotH, real NdotH,
  285. real TdotV, real BdotV, real NdotV,
  286. real TdotL, real BdotL, real NdotL,
  287. real roughnessT, real roughnessB)
  288. {
  289. real partLambdaV = GetSmithJointGGXAnisoPartLambdaV(TdotV, BdotV, NdotV, roughnessT, roughnessB);
  290. return DV_SmithJointGGXAniso(TdotH, BdotH, NdotH, NdotV,
  291. TdotL, BdotL, NdotL,
  292. roughnessT, roughnessB, partLambdaV);
  293. }
  294. // Get projected roughness for a certain normalized direction V in tangent space
  295. // and an anisotropic roughness
  296. // Ref: Understanding the Masking-Shadowing Function in Microfacet-Based BRDFs, Heitz 2014, pp. 86, 88 - 39/60, 41/60
  297. float GetProjectedRoughness(float TdotV, float BdotV, float NdotV, float roughnessT, float roughnessB)
  298. {
  299. float2 roughness = float2(roughnessT, roughnessB);
  300. float sinTheta2 = max((1 - Sq(NdotV)), FLT_MIN);
  301. // if sinTheta^2 = 0, NdotV = 1, TdotV = BdotV = 0 and roughness is arbitrary, no real azimuth
  302. // as there's a breakdown of the spherical parameterization, so we clamp under by FLT_MIN in any case
  303. // for safe division
  304. // Note:
  305. // sin(thetaV)^2 * cos(phiV)^2 = (TdotV)^2
  306. // sin(thetaV)^2 * sin(phiV)^2 = (BdotV)^2
  307. float2 vProj2 = Sq(float2(TdotV, BdotV)) * rcp(sinTheta2);
  308. // vProj2 = (cos^2(phi), sin^2(phi))
  309. float projRoughness = sqrt(dot(vProj2, roughness*roughness));
  310. return projRoughness;
  311. }
  312. //-----------------------------------------------------------------------------
  313. // Diffuse BRDF - diffuseColor is expected to be multiply by the caller
  314. //-----------------------------------------------------------------------------
  315. real LambertNoPI()
  316. {
  317. return 1.0;
  318. }
  319. real Lambert()
  320. {
  321. return INV_PI;
  322. }
  323. real DisneyDiffuseNoPI(real NdotV, real NdotL, real LdotV, real perceptualRoughness)
  324. {
  325. // (2 * LdotH * LdotH) = 1 + LdotV
  326. // real fd90 = 0.5 + (2 * LdotH * LdotH) * perceptualRoughness;
  327. real fd90 = 0.5 + (perceptualRoughness + perceptualRoughness * LdotV);
  328. // Two schlick fresnel term
  329. real lightScatter = F_Schlick(1.0, fd90, NdotL);
  330. real viewScatter = F_Schlick(1.0, fd90, NdotV);
  331. // Normalize the BRDF for polar view angles of up to (Pi/4).
  332. // We use the worst case of (roughness = albedo = 1), and, for each view angle,
  333. // integrate (brdf * cos(theta_light)) over all light directions.
  334. // The resulting value is for (theta_view = 0), which is actually a little bit larger
  335. // than the value of the integral for (theta_view = Pi/4).
  336. // Hopefully, the compiler folds the constant together with (1/Pi).
  337. return rcp(1.03571) * (lightScatter * viewScatter);
  338. }
  339. real DisneyDiffuse(real NdotV, real NdotL, real LdotV, real perceptualRoughness)
  340. {
  341. return INV_PI * DisneyDiffuseNoPI(NdotV, NdotL, LdotV, perceptualRoughness);
  342. }
  343. // Ref: Diffuse Lighting for GGX + Smith Microsurfaces, p. 113.
  344. real3 DiffuseGGXNoPI(real3 albedo, real NdotV, real NdotL, real NdotH, real LdotV, real roughness)
  345. {
  346. real facing = 0.5 + 0.5 * LdotV; // (LdotH)^2
  347. real rough = facing * (0.9 - 0.4 * facing) * (0.5 / NdotH + 1);
  348. real transmitL = F_Transm_Schlick(0, NdotL);
  349. real transmitV = F_Transm_Schlick(0, NdotV);
  350. real smooth = transmitL * transmitV * 1.05; // Normalize F_t over the hemisphere
  351. real single = lerp(smooth, rough, roughness); // Rescaled by PI
  352. real multiple = roughness * (0.1159 * PI); // Rescaled by PI
  353. return single + albedo * multiple;
  354. }
  355. real3 DiffuseGGX(real3 albedo, real NdotV, real NdotL, real NdotH, real LdotV, real roughness)
  356. {
  357. // Note that we could save 2 cycles by inlining the multiplication by INV_PI.
  358. return INV_PI * DiffuseGGXNoPI(albedo, NdotV, NdotL, NdotH, LdotV, roughness);
  359. }
  360. //-----------------------------------------------------------------------------
  361. // Iridescence
  362. //-----------------------------------------------------------------------------
  363. // Ref: https://belcour.github.io/blog/research/2017/05/01/brdf-thin-film.html
  364. // Evaluation XYZ sensitivity curves in Fourier space
  365. real3 EvalSensitivity(real opd, real shift)
  366. {
  367. // Use Gaussian fits, given by 3 parameters: val, pos and var
  368. real phase = 2.0 * PI * opd * 1e-6;
  369. real3 val = real3(5.4856e-13, 4.4201e-13, 5.2481e-13);
  370. real3 pos = real3(1.6810e+06, 1.7953e+06, 2.2084e+06);
  371. real3 var = real3(4.3278e+09, 9.3046e+09, 6.6121e+09);
  372. real3 xyz = val * sqrt(2.0 * PI * var) * cos(pos * phase + shift) * exp(-var * phase * phase);
  373. xyz.x += 9.7470e-14 * sqrt(2.0 * PI * 4.5282e+09) * cos(2.2399e+06 * phase + shift) * exp(-4.5282e+09 * phase * phase);
  374. xyz /= 1.0685e-7;
  375. // Convert to linear sRGb color space here.
  376. // EvalIridescence works in linear sRGB color space and does not switch...
  377. real3 srgb = mul(XYZ_2_REC709_MAT, xyz);
  378. return srgb;
  379. }
  380. // Evaluate the reflectance for a thin-film layer on top of a dielectric medum.
  381. real3 EvalIridescence(real eta_1, real cosTheta1, real iridescenceThickness, real3 baseLayerFresnel0, real iorOverBaseLayer = 0.0)
  382. {
  383. real3 I;
  384. // iridescenceThickness unit is micrometer for this equation here. Mean 0.5 is 500nm.
  385. real Dinc = 3.0 * iridescenceThickness;
  386. // Note: Unlike the code provide with the paper, here we use schlick approximation
  387. // Schlick is a very poor approximation when dealing with iridescence to the Fresnel
  388. // term and there is no "neutral" value in this unlike in the original paper.
  389. // We use Iridescence mask here to allow to have neutral value
  390. // Hack: In order to use only one parameter (DInc), we deduced the ior of iridescence from current Dinc iridescenceThickness
  391. // and we use mask instead to fade out the effect
  392. real eta_2 = lerp(2.0, 1.0, iridescenceThickness);
  393. // Following line from original code is not needed for us, it create a discontinuity
  394. // Force eta_2 -> eta_1 when Dinc -> 0.0
  395. // real eta_2 = lerp(eta_1, eta_2, smoothstep(0.0, 0.03, Dinc));
  396. // Evaluate the cosTheta on the base layer (Snell law)
  397. real sinTheta2Sq = Sq(eta_1 / eta_2) * (1.0 - Sq(cosTheta1));
  398. // Handle TIR:
  399. // (Also note that with just testing sinTheta2Sq > 1.0, (1.0 - sinTheta2Sq) can be negative, as emitted instructions
  400. // can eg be a mad giving a small negative for (1.0 - sinTheta2Sq), while sinTheta2Sq still testing equal to 1.0), so we actually
  401. // test the operand [cosTheta2Sq := (1.0 - sinTheta2Sq)] < 0 directly:)
  402. real cosTheta2Sq = (1.0 - sinTheta2Sq);
  403. // Or use this "artistic hack" to get more continuity even though wrong (no TIR, continue the effect by mirroring it):
  404. // if( cosTheta2Sq < 0.0 ) => { sinTheta2Sq = 2 - sinTheta2Sq; => so cosTheta2Sq = sinTheta2Sq - 1 }
  405. // ie don't test and simply do
  406. // real cosTheta2Sq = abs(1.0 - sinTheta2Sq);
  407. if (cosTheta2Sq < 0.0)
  408. I = real3(1.0, 1.0, 1.0);
  409. else
  410. {
  411. real cosTheta2 = sqrt(cosTheta2Sq);
  412. // First interface
  413. real R0 = IorToFresnel0(eta_2, eta_1);
  414. real R12 = F_Schlick(R0, cosTheta1);
  415. real R21 = R12;
  416. real T121 = 1.0 - R12;
  417. real phi12 = 0.0;
  418. real phi21 = PI - phi12;
  419. // Second interface
  420. // The f0 or the base should account for the new computed eta_2 on top.
  421. // This is optionally done if we are given the needed current ior over the base layer that is accounted for
  422. // in the baseLayerFresnel0 parameter:
  423. if (iorOverBaseLayer > 0.0)
  424. {
  425. // Fresnel0ToIor will give us a ratio of baseIor/topIor, hence we * iorOverBaseLayer to get the baseIor
  426. real3 baseIor = iorOverBaseLayer * Fresnel0ToIor(baseLayerFresnel0 + 0.0001); // guard against 1.0
  427. baseLayerFresnel0 = IorToFresnel0(baseIor, eta_2);
  428. }
  429. real3 R23 = F_Schlick(baseLayerFresnel0, cosTheta2);
  430. real phi23 = 0.0;
  431. // Phase shift
  432. real OPD = Dinc * cosTheta2;
  433. real phi = phi21 + phi23;
  434. // Compound terms
  435. real3 R123 = clamp(R12 * R23, 1e-5, 0.9999);
  436. real3 r123 = sqrt(R123);
  437. real3 Rs = Sq(T121) * R23 / (real3(1.0, 1.0, 1.0) - R123);
  438. // Reflectance term for m = 0 (DC term amplitude)
  439. real3 C0 = R12 + Rs;
  440. I = C0;
  441. // Reflectance term for m > 0 (pairs of diracs)
  442. real3 Cm = Rs - T121;
  443. for (int m = 1; m <= 2; ++m)
  444. {
  445. Cm *= r123;
  446. real3 Sm = 2.0 * EvalSensitivity(m * OPD, m * phi);
  447. //vec3 SmP = 2.0 * evalSensitivity(m*OPD, m*phi2.y);
  448. I += Cm * Sm;
  449. }
  450. // Since out of gamut colors might be produced, negative color values are clamped to 0.
  451. I = max(I, float3(0.0, 0.0, 0.0));
  452. }
  453. return I;
  454. }
  455. //-----------------------------------------------------------------------------
  456. // Fabric
  457. //-----------------------------------------------------------------------------
  458. // Ref: https://knarkowicz.wordpress.com/2018/01/04/cloth-shading/
  459. real D_CharlieNoPI(real NdotH, real roughness)
  460. {
  461. float invR = rcp(roughness);
  462. float cos2h = NdotH * NdotH;
  463. float sin2h = 1.0 - cos2h;
  464. // Note: We have sin^2 so multiply by 0.5 to cancel it
  465. return (2.0 + invR) * PositivePow(sin2h, invR * 0.5) / 2.0;
  466. }
  467. real D_Charlie(real NdotH, real roughness)
  468. {
  469. return INV_PI * D_CharlieNoPI(NdotH, roughness);
  470. }
  471. real CharlieL(real x, real r)
  472. {
  473. r = saturate(r);
  474. r = 1.0 - (1.0 - r) * (1.0 - r);
  475. float a = lerp(25.3245, 21.5473, r);
  476. float b = lerp(3.32435, 3.82987, r);
  477. float c = lerp(0.16801, 0.19823, r);
  478. float d = lerp(-1.27393, -1.97760, r);
  479. float e = lerp(-4.85967, -4.32054, r);
  480. return a / (1. + b * PositivePow(x, c)) + d * x + e;
  481. }
  482. // Note: This version don't include the softening of the paper: Production Friendly Microfacet Sheen BRDF
  483. real V_Charlie(real NdotL, real NdotV, real roughness)
  484. {
  485. real lambdaV = NdotV < 0.5 ? exp(CharlieL(NdotV, roughness)) : exp(2.0 * CharlieL(0.5, roughness) - CharlieL(1.0 - NdotV, roughness));
  486. real lambdaL = NdotL < 0.5 ? exp(CharlieL(NdotL, roughness)) : exp(2.0 * CharlieL(0.5, roughness) - CharlieL(1.0 - NdotL, roughness));
  487. return 1.0 / ((1.0 + lambdaV + lambdaL) * (4.0 * NdotV * NdotL));
  488. }
  489. // We use V_Ashikhmin instead of V_Charlie in practice for game due to the cost of V_Charlie
  490. real V_Ashikhmin(real NdotL, real NdotV)
  491. {
  492. // Use soft visibility term introduce in: Crafting a Next-Gen Material Pipeline for The Order : 1886
  493. return 1.0 / (4.0 * (NdotL + NdotV - NdotL * NdotV));
  494. }
  495. // A diffuse term use with fabric done by tech artist - empirical
  496. real FabricLambertNoPI(real roughness)
  497. {
  498. return lerp(1.0, 0.5, roughness);
  499. }
  500. real FabricLambert(real roughness)
  501. {
  502. return INV_PI * FabricLambertNoPI(roughness);
  503. }
  504. real G_CookTorrance(real NdotH, real NdotV, real NdotL, real HdotV)
  505. {
  506. return min(1.0, 2.0 * NdotH * min(NdotV, NdotL) / HdotV);
  507. }
  508. //-----------------------------------------------------------------------------
  509. // Hair
  510. //-----------------------------------------------------------------------------
  511. //http://web.engr.oregonstate.edu/~mjb/cs519/Projects/Papers/HairRendering.pdf
  512. real3 ShiftTangent(real3 T, real3 N, real shift)
  513. {
  514. return normalize(T + N * shift);
  515. }
  516. // Note: this is Blinn-Phong, the original paper uses Phong.
  517. real3 D_KajiyaKay(real3 T, real3 H, real specularExponent)
  518. {
  519. real TdotH = dot(T, H);
  520. real sinTHSq = saturate(1.0 - TdotH * TdotH);
  521. real dirAttn = saturate(TdotH + 1.0); // Evgenii: this seems like a hack? Do we really need this?
  522. // Note: Kajiya-Kay is not energy conserving.
  523. // We attempt at least some energy conservation by approximately normalizing Blinn-Phong NDF.
  524. // We use the formulation with the NdotL.
  525. // See http://www.thetenthplanet.de/archives/255.
  526. real n = specularExponent;
  527. real norm = (n + 2) * rcp(2 * PI);
  528. return dirAttn * norm * PositivePow(sinTHSq, 0.5 * n);
  529. }
  530. #endif // UNITY_BSDF_INCLUDED