pbrBlockSheen.fx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #ifdef SHEEN
  2. struct sheenOutParams
  3. {
  4. float sheenIntensity;
  5. vec3 sheenColor;
  6. float sheenRoughness;
  7. #ifdef SHEEN_LINKWITHALBEDO
  8. vec3 surfaceAlbedo;
  9. #endif
  10. #if defined(ENVIRONMENTBRDF) && defined(SHEEN_ALBEDOSCALING)
  11. float sheenAlbedoScaling;
  12. #endif
  13. #if defined(REFLECTION) && defined(ENVIRONMENTBRDF)
  14. vec3 finalSheenRadianceScaled;
  15. #endif
  16. #if DEBUGMODE > 0
  17. vec4 sheenMapData;
  18. vec3 sheenEnvironmentReflectance;
  19. #endif
  20. };
  21. void sheenBlock(
  22. const in vec4 vSheenColor,
  23. #ifdef SHEEN_ROUGHNESS
  24. const in float vSheenRoughness,
  25. #endif
  26. const in float roughness,
  27. #ifdef SHEEN_TEXTURE
  28. const in vec2 vSheenUV,
  29. const in vec2 vSheenInfos,
  30. const in vec2 uvOffset,
  31. const in sampler2D sheenSampler,
  32. #endif
  33. const in float reflectance,
  34. #ifdef SHEEN_LINKWITHALBEDO
  35. const in vec3 baseColor,
  36. const in vec3 surfaceAlbedo,
  37. #endif
  38. #ifdef ENVIRONMENTBRDF
  39. const in float NdotV,
  40. const in vec3 environmentBrdf,
  41. #endif
  42. #if defined(REFLECTION) && defined(ENVIRONMENTBRDF)
  43. const in vec2 AARoughnessFactors,
  44. const in vec3 vReflectionMicrosurfaceInfos,
  45. const in vec2 vReflectionInfos,
  46. const in vec3 vReflectionColor,
  47. const in vec4 vLightingIntensity,
  48. #ifdef REFLECTIONMAP_3D
  49. const in samplerCube reflectionSampler,
  50. const in vec3 reflectionCoords,
  51. #else
  52. const in sampler2D reflectionSampler,
  53. const in vec2 reflectionCoords,
  54. #endif
  55. const in float NdotVUnclamped,
  56. #ifndef LODBASEDMICROSFURACE
  57. const in sampler2D reflectionSamplerLow,
  58. const in sampler2D reflectionSamplerHigh,
  59. #endif
  60. #if !defined(REFLECTIONMAP_SKYBOX) && defined(RADIANCEOCCLUSION)
  61. const in float seo,
  62. #endif
  63. #if !defined(REFLECTIONMAP_SKYBOX) && defined(HORIZONOCCLUSION) && defined(BUMP) && defined(REFLECTIONMAP_3D)
  64. const in float eho,
  65. #endif
  66. #endif
  67. out sheenOutParams outParams
  68. )
  69. {
  70. float sheenIntensity = vSheenColor.a;
  71. #ifdef SHEEN_TEXTURE
  72. vec4 sheenMapData = texture2D(sheenSampler, vSheenUV + uvOffset) * vSheenInfos.y;
  73. sheenIntensity *= sheenMapData.a;
  74. #if DEBUGMODE > 0
  75. outParams.sheenMapData = sheenMapData;
  76. #endif
  77. #endif
  78. #ifdef SHEEN_LINKWITHALBEDO
  79. float sheenFactor = pow5(1.0-sheenIntensity);
  80. vec3 sheenColor = baseColor.rgb*(1.0-sheenFactor);
  81. float sheenRoughness = sheenIntensity;
  82. outParams.surfaceAlbedo = surfaceAlbedo * sheenFactor;
  83. #else
  84. vec3 sheenColor = vSheenColor.rgb;
  85. #ifdef SHEEN_TEXTURE
  86. sheenColor.rgb *= toLinearSpace(sheenMapData.rgb);
  87. #endif
  88. #ifdef SHEEN_ROUGHNESS
  89. float sheenRoughness = vSheenRoughness;
  90. #else
  91. float sheenRoughness = roughness;
  92. #endif
  93. // Sheen Lobe Layering.
  94. #if !defined(SHEEN_ALBEDOSCALING)
  95. sheenIntensity *= (1. - reflectance);
  96. #endif
  97. // Remap F0 and sheen.
  98. sheenColor *= sheenIntensity;
  99. #endif
  100. // _____________________________ Sheen Environment __________________________
  101. #ifdef ENVIRONMENTBRDF
  102. /*#ifdef SHEEN_SOFTER
  103. vec3 environmentSheenBrdf = vec3(0., 0., getBRDFLookupCharlieSheen(NdotV, sheenRoughness));
  104. #else*/
  105. #ifdef SHEEN_ROUGHNESS
  106. vec3 environmentSheenBrdf = getBRDFLookup(NdotV, sheenRoughness);
  107. #else
  108. vec3 environmentSheenBrdf = environmentBrdf;
  109. #endif
  110. /*#endif*/
  111. #endif
  112. #if defined(REFLECTION) && defined(ENVIRONMENTBRDF)
  113. float sheenAlphaG = convertRoughnessToAverageSlope(sheenRoughness);
  114. #ifdef SPECULARAA
  115. // Adapt linear roughness (alphaG) to geometric curvature of the current pixel.
  116. sheenAlphaG += AARoughnessFactors.y;
  117. #endif
  118. vec4 environmentSheenRadiance = vec4(0., 0., 0., 0.);
  119. // _____________________________ 2D vs 3D Maps ________________________________
  120. #if defined(LODINREFLECTIONALPHA) && !defined(REFLECTIONMAP_SKYBOX)
  121. float sheenReflectionLOD = getLodFromAlphaG(vReflectionMicrosurfaceInfos.x, sheenAlphaG, NdotVUnclamped);
  122. #elif defined(LINEARSPECULARREFLECTION)
  123. float sheenReflectionLOD = getLinearLodFromRoughness(vReflectionMicrosurfaceInfos.x, sheenRoughness);
  124. #else
  125. float sheenReflectionLOD = getLodFromAlphaG(vReflectionMicrosurfaceInfos.x, sheenAlphaG);
  126. #endif
  127. #ifdef LODBASEDMICROSFURACE
  128. // Apply environment convolution scale/offset filter tuning parameters to the mipmap LOD selection
  129. sheenReflectionLOD = sheenReflectionLOD * vReflectionMicrosurfaceInfos.y + vReflectionMicrosurfaceInfos.z;
  130. environmentSheenRadiance = sampleReflectionLod(reflectionSampler, reflectionCoords, sheenReflectionLOD);
  131. #else
  132. float lodSheenReflectionNormalized = saturate(sheenReflectionLOD / log2(vReflectionMicrosurfaceInfos.x));
  133. float lodSheenReflectionNormalizedDoubled = lodSheenReflectionNormalized * 2.0;
  134. vec4 environmentSheenMid = sampleReflection(reflectionSampler, reflectionCoords);
  135. if (lodSheenReflectionNormalizedDoubled < 1.0){
  136. environmentSheenRadiance = mix(
  137. sampleReflection(reflectionSamplerHigh, reflectionCoords),
  138. environmentSheenMid,
  139. lodSheenReflectionNormalizedDoubled
  140. );
  141. } else {
  142. environmentSheenRadiance = mix(
  143. environmentSheenMid,
  144. sampleReflection(reflectionSamplerLow, reflectionCoords),
  145. lodSheenReflectionNormalizedDoubled - 1.0
  146. );
  147. }
  148. #endif
  149. #ifdef RGBDREFLECTION
  150. environmentSheenRadiance.rgb = fromRGBD(environmentSheenRadiance);
  151. #endif
  152. #ifdef GAMMAREFLECTION
  153. environmentSheenRadiance.rgb = toLinearSpace(environmentSheenRadiance.rgb);
  154. #endif
  155. // _____________________________ Levels _____________________________________
  156. environmentSheenRadiance.rgb *= vReflectionInfos.x;
  157. environmentSheenRadiance.rgb *= vReflectionColor.rgb;
  158. vec3 sheenEnvironmentReflectance = getSheenReflectanceFromBRDFLookup(sheenColor, environmentSheenBrdf);
  159. #if !defined(REFLECTIONMAP_SKYBOX) && defined(RADIANCEOCCLUSION)
  160. sheenEnvironmentReflectance *= seo;
  161. #endif
  162. #if !defined(REFLECTIONMAP_SKYBOX) && defined(HORIZONOCCLUSION) && defined(BUMP) && defined(REFLECTIONMAP_3D)
  163. sheenEnvironmentReflectance *= eho;
  164. #endif
  165. #if DEBUGMODE > 0
  166. outParams.sheenEnvironmentReflectance = sheenEnvironmentReflectance;
  167. #endif
  168. outParams.finalSheenRadianceScaled =
  169. environmentSheenRadiance.rgb *
  170. sheenEnvironmentReflectance *
  171. vLightingIntensity.z;
  172. // #if defined(MS_BRDF_ENERGY_CONSERVATION)
  173. // The sheen does not use the same BRDF so not energy conservation is possible
  174. // Should be less a problem as it is usually not metallic
  175. // finalSheenScaled *= energyConservationFactor;
  176. // #endif
  177. #endif
  178. #if defined(ENVIRONMENTBRDF) && defined(SHEEN_ALBEDOSCALING)
  179. // Sheen Lobe Layering.
  180. // environmentSheenBrdf.b is (integral on hemisphere)[f_sheen*cos(theta)*dtheta*dphi], which happens to also be the directional albedo needed for albedo scaling.
  181. // See section 6.2.3 in https://dassaultsystemes-technology.github.io/EnterprisePBRShadingModel/spec-2021x.md.html#components/sheen
  182. outParams.sheenAlbedoScaling = 1.0 - sheenIntensity * max(max(sheenColor.r, sheenColor.g), sheenColor.b) * environmentSheenBrdf.b;
  183. #endif
  184. // _____________________________ Out parameters __________________________
  185. outParams.sheenIntensity = sheenIntensity;
  186. outParams.sheenColor = sheenColor;
  187. outParams.sheenRoughness = sheenRoughness;
  188. }
  189. #endif