pbrBlockReflectivity.fx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. struct reflectivityOutParams
  2. {
  3. float microSurface;
  4. float roughness;
  5. vec3 surfaceReflectivityColor;
  6. #ifdef METALLICWORKFLOW
  7. vec3 surfaceAlbedo;
  8. #endif
  9. #if defined(METALLICWORKFLOW) && defined(REFLECTIVITY) && defined(AOSTOREINMETALMAPRED)
  10. vec3 ambientOcclusionColor;
  11. #endif
  12. #if DEBUGMODE > 0
  13. vec4 surfaceMetallicColorMap;
  14. vec4 surfaceReflectivityColorMap;
  15. vec2 metallicRoughness;
  16. vec3 metallicF0;
  17. #endif
  18. };
  19. void reflectivityBlock(
  20. const in vec4 vReflectivityColor,
  21. #ifdef METALLICWORKFLOW
  22. const in vec3 surfaceAlbedo,
  23. const in vec4 metallicReflectanceFactors,
  24. #endif
  25. #ifdef REFLECTIVITY
  26. const in vec3 vReflectivityInfos,
  27. const in vec4 surfaceMetallicOrReflectivityColorMap,
  28. #endif
  29. #if defined(METALLICWORKFLOW) && defined(REFLECTIVITY) && defined(AOSTOREINMETALMAPRED)
  30. const in vec3 ambientOcclusionColor,
  31. #endif
  32. #ifdef MICROSURFACEMAP
  33. const in vec4 microSurfaceTexel,
  34. #endif
  35. out reflectivityOutParams outParams
  36. )
  37. {
  38. float microSurface = vReflectivityColor.a;
  39. vec3 surfaceReflectivityColor = vReflectivityColor.rgb;
  40. #ifdef METALLICWORKFLOW
  41. vec2 metallicRoughness = surfaceReflectivityColor.rg;
  42. #ifdef REFLECTIVITY
  43. #if DEBUGMODE > 0
  44. outParams.surfaceMetallicColorMap = surfaceMetallicOrReflectivityColorMap;
  45. #endif
  46. #ifdef AOSTOREINMETALMAPRED
  47. vec3 aoStoreInMetalMap = vec3(surfaceMetallicOrReflectivityColorMap.r, surfaceMetallicOrReflectivityColorMap.r, surfaceMetallicOrReflectivityColorMap.r);
  48. outParams.ambientOcclusionColor = mix(ambientOcclusionColor, aoStoreInMetalMap, vReflectivityInfos.z);
  49. #endif
  50. #ifdef METALLNESSSTOREINMETALMAPBLUE
  51. metallicRoughness.r *= surfaceMetallicOrReflectivityColorMap.b;
  52. #else
  53. metallicRoughness.r *= surfaceMetallicOrReflectivityColorMap.r;
  54. #endif
  55. #ifdef ROUGHNESSSTOREINMETALMAPALPHA
  56. metallicRoughness.g *= surfaceMetallicOrReflectivityColorMap.a;
  57. #else
  58. #ifdef ROUGHNESSSTOREINMETALMAPGREEN
  59. metallicRoughness.g *= surfaceMetallicOrReflectivityColorMap.g;
  60. #endif
  61. #endif
  62. #endif
  63. #ifdef MICROSURFACEMAP
  64. metallicRoughness.g *= microSurfaceTexel.r;
  65. #endif
  66. #if DEBUGMODE > 0
  67. outParams.metallicRoughness = metallicRoughness;
  68. #endif
  69. #define CUSTOM_FRAGMENT_UPDATE_METALLICROUGHNESS
  70. // Compute microsurface from roughness.
  71. microSurface = 1.0 - metallicRoughness.g;
  72. // Diffuse is used as the base of the reflectivity.
  73. vec3 baseColor = surfaceAlbedo;
  74. #ifdef FROSTBITE_REFLECTANCE
  75. // *** NOT USED ANYMORE ***
  76. // Following Frostbite Remapping,
  77. // https://seblagarde.files.wordpress.com/2015/07/course_notes_moving_frostbite_to_pbr_v32.pdf page 115
  78. // vec3 f0 = 0.16 * reflectance * reflectance * (1.0 - metallic) + baseColor * metallic;
  79. // where 0.16 * reflectance * reflectance remaps the reflectance to allow storage in 8 bit texture
  80. // Compute the converted diffuse.
  81. outParams.surfaceAlbedo = baseColor.rgb * (1.0 - metallicRoughness.r);
  82. // Compute the converted reflectivity.
  83. surfaceReflectivityColor = mix(0.16 * reflectance * reflectance, baseColor, metallicRoughness.r);
  84. #else
  85. vec3 metallicF0 = metallicReflectanceFactors.rgb;
  86. #if DEBUGMODE > 0
  87. outParams.metallicF0 = metallicF0;
  88. #endif
  89. // Compute the converted diffuse.
  90. outParams.surfaceAlbedo = mix(baseColor.rgb * (1.0 - metallicF0), vec3(0., 0., 0.), metallicRoughness.r);
  91. // Compute the converted reflectivity.
  92. surfaceReflectivityColor = mix(metallicF0, baseColor, metallicRoughness.r);
  93. #endif
  94. #else
  95. #ifdef REFLECTIVITY
  96. surfaceReflectivityColor *= surfaceMetallicOrReflectivityColorMap.rgb;
  97. #if DEBUGMODE > 0
  98. outParams.surfaceReflectivityColorMap = surfaceMetallicOrReflectivityColorMap;
  99. #endif
  100. #ifdef MICROSURFACEFROMREFLECTIVITYMAP
  101. microSurface *= surfaceMetallicOrReflectivityColorMap.a;
  102. microSurface *= vReflectivityInfos.z;
  103. #else
  104. #ifdef MICROSURFACEAUTOMATIC
  105. microSurface *= computeDefaultMicroSurface(microSurface, surfaceReflectivityColor);
  106. #endif
  107. #ifdef MICROSURFACEMAP
  108. microSurface *= microSurfaceTexel.r;
  109. #endif
  110. #define CUSTOM_FRAGMENT_UPDATE_MICROSURFACE
  111. #endif
  112. #endif
  113. #endif
  114. // Adapt microSurface.
  115. microSurface = saturate(microSurface);
  116. // Compute roughness.
  117. float roughness = 1. - microSurface;
  118. outParams.microSurface = microSurface;
  119. outParams.roughness = roughness;
  120. outParams.surfaceReflectivityColor = surfaceReflectivityColor;
  121. }