pbr.fragment.fx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. #if defined(BUMP) || !defined(NORMAL) || defined(FORCENORMALFORWARD) || defined(SPECULARAA) || defined(CLEARCOAT_BUMP) || defined(ANISOTROPIC)
  2. #extension GL_OES_standard_derivatives : enable
  3. #endif
  4. #ifdef LODBASEDMICROSFURACE
  5. #extension GL_EXT_shader_texture_lod : enable
  6. #endif
  7. #define CUSTOM_FRAGMENT_BEGIN
  8. #ifdef LOGARITHMICDEPTH
  9. #extension GL_EXT_frag_depth : enable
  10. #endif
  11. precision highp float;
  12. #include<deferredDeclaration>[SCENE_MRT_COUNT]
  13. // Forces linear space for image processing
  14. #ifndef FROMLINEARSPACE
  15. #define FROMLINEARSPACE
  16. #endif
  17. // Declaration
  18. #include<__decl__pbrFragment>
  19. #include<pbrFragmentExtraDeclaration>
  20. #include<__decl__lightFragment>[0..maxSimultaneousLights]
  21. #include<pbrFragmentSamplersDeclaration>
  22. #include<imageProcessingDeclaration>
  23. #include<clipPlaneFragmentDeclaration>
  24. #include<logDepthDeclaration>
  25. #include<fogFragmentDeclaration>
  26. // Helper Functions
  27. #include<helperFunctions>
  28. #include<importanceSampling>
  29. #include<pbrHelperFunctions>
  30. #include<imageProcessingFunctions>
  31. #include<shadowsFragmentFunctions>
  32. #include<harmonicsFunctions>
  33. #include<pbrDirectLightingSetupFunctions>
  34. #include<pbrDirectLightingFalloffFunctions>
  35. #include<pbrBRDFFunctions>
  36. #include<hdrFilteringFunctions>
  37. #include<pbrDirectLightingFunctions>
  38. #include<pbrIBLFunctions>
  39. #include<bumpFragmentMainFunctions>
  40. #include<bumpFragmentFunctions>
  41. #ifdef REFLECTION
  42. #include<reflectionFunction>
  43. #endif
  44. #include<pbrBlockAlbedoOpacity>
  45. #include<pbrBlockReflectivity>
  46. #include<pbrBlockAmbientOcclusion>
  47. #include<pbrBlockAlphaFresnel>
  48. #include<pbrBlockAnisotropic>
  49. #include<pbrBlockReflection>
  50. #include<pbrBlockSheen>
  51. #include<pbrBlockClearcoat>
  52. #include<pbrBlockSubSurface>
  53. #define CUSTOM_FRAGMENT_DEFINITIONS
  54. // _____________________________ MAIN FUNCTION ____________________________
  55. void main(void) {
  56. #define CUSTOM_FRAGMENT_MAIN_BEGIN
  57. #include<clipPlaneFragment>
  58. // _____________________________ Geometry Information ____________________________
  59. #include<pbrBlockNormalGeometric>
  60. #include<bumpFragment>
  61. #include<pbrBlockNormalFinal>
  62. // _____________________________ Albedo & Opacity ______________________________
  63. albedoOpacityOutParams albedoOpacityOut;
  64. #ifdef ALBEDO
  65. vec4 albedoTexture = texture2D(albedoSampler, vAlbedoUV + uvOffset);
  66. #endif
  67. #ifdef OPACITY
  68. vec4 opacityMap = texture2D(opacitySampler, vOpacityUV + uvOffset);
  69. #endif
  70. albedoOpacityBlock(
  71. vAlbedoColor,
  72. #ifdef ALBEDO
  73. albedoTexture,
  74. vAlbedoInfos,
  75. #endif
  76. #ifdef OPACITY
  77. opacityMap,
  78. vOpacityInfos,
  79. #endif
  80. albedoOpacityOut
  81. );
  82. vec3 surfaceAlbedo = albedoOpacityOut.surfaceAlbedo;
  83. float alpha = albedoOpacityOut.alpha;
  84. #define CUSTOM_FRAGMENT_UPDATE_ALPHA
  85. #include<depthPrePass>
  86. #define CUSTOM_FRAGMENT_BEFORE_LIGHTS
  87. // _____________________________ AO _______________________________
  88. ambientOcclusionOutParams aoOut;
  89. #ifdef AMBIENT
  90. vec3 ambientOcclusionColorMap = texture2D(ambientSampler, vAmbientUV + uvOffset).rgb;
  91. #endif
  92. ambientOcclusionBlock(
  93. #ifdef AMBIENT
  94. ambientOcclusionColorMap,
  95. vAmbientInfos,
  96. #endif
  97. aoOut
  98. );
  99. #ifdef UNLIT
  100. vec3 diffuseBase = vec3(1., 1., 1.);
  101. #else
  102. // _____________________________ Reflectivity _______________________________
  103. vec3 baseColor = surfaceAlbedo;
  104. reflectivityOutParams reflectivityOut;
  105. #if defined(REFLECTIVITY)
  106. vec4 surfaceMetallicOrReflectivityColorMap = texture2D(reflectivitySampler, vReflectivityUV + uvOffset);
  107. #ifndef METALLICWORKFLOW
  108. surfaceMetallicOrReflectivityColorMap = toLinearSpace(surfaceMetallicOrReflectivityColorMap);
  109. surfaceMetallicOrReflectivityColorMap.rgb *= vReflectivityInfos.y;
  110. #endif
  111. #endif
  112. #if defined(MICROSURFACEMAP)
  113. vec4 microSurfaceTexel = texture2D(microSurfaceSampler, vMicroSurfaceSamplerUV + uvOffset) * vMicroSurfaceSamplerInfos.y;
  114. #endif
  115. #ifdef METALLICWORKFLOW
  116. vec4 metallicReflectanceFactors = vMetallicReflectanceFactors;
  117. #ifdef METALLIC_REFLECTANCE
  118. vec4 metallicReflectanceFactorsMap = texture2D(metallicReflectanceSampler, vMetallicReflectanceUV + uvOffset);
  119. metallicReflectanceFactorsMap = toLinearSpace(metallicReflectanceFactorsMap);
  120. metallicReflectanceFactors *= metallicReflectanceFactorsMap;
  121. #endif
  122. #endif
  123. reflectivityBlock(
  124. vReflectivityColor,
  125. #ifdef METALLICWORKFLOW
  126. surfaceAlbedo,
  127. metallicReflectanceFactors,
  128. #endif
  129. #ifdef REFLECTIVITY
  130. vReflectivityInfos,
  131. surfaceMetallicOrReflectivityColorMap,
  132. #endif
  133. #if defined(METALLICWORKFLOW) && defined(REFLECTIVITY) && defined(AOSTOREINMETALMAPRED)
  134. aoOut.ambientOcclusionColor,
  135. #endif
  136. #ifdef MICROSURFACEMAP
  137. microSurfaceTexel,
  138. #endif
  139. reflectivityOut
  140. );
  141. float microSurface = reflectivityOut.microSurface;
  142. float roughness = reflectivityOut.roughness;
  143. #ifdef METALLICWORKFLOW
  144. surfaceAlbedo = reflectivityOut.surfaceAlbedo;
  145. #endif
  146. #if defined(METALLICWORKFLOW) && defined(REFLECTIVITY) && defined(AOSTOREINMETALMAPRED)
  147. aoOut.ambientOcclusionColor = reflectivityOut.ambientOcclusionColor;
  148. #endif
  149. // _____________________________ Alpha Fresnel ___________________________________
  150. #ifdef ALPHAFRESNEL
  151. #if defined(ALPHATEST) || defined(ALPHABLEND)
  152. alphaFresnelOutParams alphaFresnelOut;
  153. alphaFresnelBlock(
  154. normalW,
  155. viewDirectionW,
  156. alpha,
  157. microSurface,
  158. alphaFresnelOut
  159. );
  160. alpha = alphaFresnelOut.alpha;
  161. #endif
  162. #endif
  163. // _____________________________ Compute Geometry info _________________________________
  164. #include<pbrBlockGeometryInfo>
  165. // _____________________________ Anisotropy _______________________________________
  166. #ifdef ANISOTROPIC
  167. anisotropicOutParams anisotropicOut;
  168. #ifdef ANISOTROPIC_TEXTURE
  169. vec3 anisotropyMapData = texture2D(anisotropySampler, vAnisotropyUV + uvOffset).rgb * vAnisotropyInfos.y;
  170. #endif
  171. anisotropicBlock(
  172. vAnisotropy,
  173. #ifdef ANISOTROPIC_TEXTURE
  174. anisotropyMapData,
  175. #endif
  176. TBN,
  177. normalW,
  178. viewDirectionW,
  179. anisotropicOut
  180. );
  181. #endif
  182. // _____________________________ Reflection Info _______________________________________
  183. #ifdef REFLECTION
  184. reflectionOutParams reflectionOut;
  185. reflectionBlock(
  186. vPositionW,
  187. normalW,
  188. alphaG,
  189. vReflectionMicrosurfaceInfos,
  190. vReflectionInfos,
  191. vReflectionColor,
  192. #ifdef ANISOTROPIC
  193. anisotropicOut,
  194. #endif
  195. #if defined(LODINREFLECTIONALPHA) && !defined(REFLECTIONMAP_SKYBOX)
  196. NdotVUnclamped,
  197. #endif
  198. #ifdef LINEARSPECULARREFLECTION
  199. roughness,
  200. #endif
  201. reflectionSampler,
  202. #if defined(NORMAL) && defined(USESPHERICALINVERTEX)
  203. vEnvironmentIrradiance,
  204. #endif
  205. #ifdef USESPHERICALFROMREFLECTIONMAP
  206. #if !defined(NORMAL) || !defined(USESPHERICALINVERTEX)
  207. reflectionMatrix,
  208. #endif
  209. #endif
  210. #ifdef USEIRRADIANCEMAP
  211. irradianceSampler,
  212. #endif
  213. #ifndef LODBASEDMICROSFURACE
  214. reflectionSamplerLow,
  215. reflectionSamplerHigh,
  216. #endif
  217. reflectionOut
  218. );
  219. #endif
  220. // ___________________ Compute Reflectance aka R0 F0 info _________________________
  221. #include<pbrBlockReflectance0>
  222. // ________________________________ Sheen ______________________________
  223. #ifdef SHEEN
  224. sheenOutParams sheenOut;
  225. #ifdef SHEEN_TEXTURE
  226. vec4 sheenMapData = toLinearSpace(texture2D(sheenSampler, vSheenUV + uvOffset)) * vSheenInfos.y;
  227. #endif
  228. sheenBlock(
  229. vSheenColor,
  230. #ifdef SHEEN_ROUGHNESS
  231. vSheenRoughness,
  232. #endif
  233. roughness,
  234. #ifdef SHEEN_TEXTURE
  235. sheenMapData,
  236. #endif
  237. reflectance,
  238. #ifdef SHEEN_LINKWITHALBEDO
  239. baseColor,
  240. surfaceAlbedo,
  241. #endif
  242. #ifdef ENVIRONMENTBRDF
  243. NdotV,
  244. environmentBrdf,
  245. #endif
  246. #if defined(REFLECTION) && defined(ENVIRONMENTBRDF)
  247. AARoughnessFactors,
  248. vReflectionMicrosurfaceInfos,
  249. vReflectionInfos,
  250. vReflectionColor,
  251. vLightingIntensity,
  252. reflectionSampler,
  253. reflectionOut.reflectionCoords,
  254. NdotVUnclamped,
  255. #ifndef LODBASEDMICROSFURACE
  256. reflectionSamplerLow,
  257. reflectionSamplerHigh,
  258. #endif
  259. #if !defined(REFLECTIONMAP_SKYBOX) && defined(RADIANCEOCCLUSION)
  260. seo,
  261. #endif
  262. #if !defined(REFLECTIONMAP_SKYBOX) && defined(HORIZONOCCLUSION) && defined(BUMP) && defined(REFLECTIONMAP_3D)
  263. eho,
  264. #endif
  265. #endif
  266. sheenOut
  267. );
  268. #ifdef SHEEN_LINKWITHALBEDO
  269. surfaceAlbedo = sheenOut.surfaceAlbedo;
  270. #endif
  271. #endif
  272. // _____________________________ Clear Coat ____________________________
  273. clearcoatOutParams clearcoatOut;
  274. #ifdef CLEARCOAT
  275. #ifdef CLEARCOAT_TEXTURE
  276. vec2 clearCoatMapData = texture2D(clearCoatSampler, vClearCoatUV + uvOffset).rg * vClearCoatInfos.y;
  277. #endif
  278. #if defined(CLEARCOAT_TINT) && defined(CLEARCOAT_TINT_TEXTURE)
  279. vec4 clearCoatTintMapData = toLinearSpace(texture2D(clearCoatTintSampler, vClearCoatTintUV + uvOffset));
  280. #endif
  281. #ifdef CLEARCOAT_BUMP
  282. vec4 clearCoatBumpMapData = texture2D(clearCoatBumpSampler, vClearCoatBumpUV + uvOffset);
  283. #endif
  284. clearcoatBlock(
  285. vPositionW,
  286. geometricNormalW,
  287. viewDirectionW,
  288. vClearCoatParams,
  289. specularEnvironmentR0,
  290. #ifdef CLEARCOAT_TEXTURE
  291. clearCoatMapData,
  292. #endif
  293. #ifdef CLEARCOAT_TINT
  294. vClearCoatTintParams,
  295. clearCoatColorAtDistance,
  296. vClearCoatRefractionParams,
  297. #ifdef CLEARCOAT_TINT_TEXTURE
  298. clearCoatTintMapData,
  299. #endif
  300. #endif
  301. #ifdef CLEARCOAT_BUMP
  302. vClearCoatBumpInfos,
  303. clearCoatBumpMapData,
  304. vClearCoatBumpUV,
  305. #if defined(TANGENT) && defined(NORMAL)
  306. vTBN,
  307. #else
  308. vClearCoatTangentSpaceParams,
  309. #endif
  310. #ifdef OBJECTSPACE_NORMALMAP
  311. normalMatrix,
  312. #endif
  313. #endif
  314. #if defined(FORCENORMALFORWARD) && defined(NORMAL)
  315. faceNormal,
  316. #endif
  317. #ifdef REFLECTION
  318. vReflectionMicrosurfaceInfos,
  319. vReflectionInfos,
  320. vReflectionColor,
  321. vLightingIntensity,
  322. reflectionSampler,
  323. #ifndef LODBASEDMICROSFURACE
  324. reflectionSamplerLow,
  325. reflectionSamplerHigh,
  326. #endif
  327. #endif
  328. #if defined(ENVIRONMENTBRDF) && !defined(REFLECTIONMAP_SKYBOX)
  329. #ifdef RADIANCEOCCLUSION
  330. ambientMonochrome,
  331. #endif
  332. #endif
  333. clearcoatOut
  334. );
  335. #else
  336. clearcoatOut.specularEnvironmentR0 = specularEnvironmentR0;
  337. #endif
  338. // _________________________ Specular Environment Reflectance __________________________
  339. #include<pbrBlockReflectance>
  340. // ___________________________________ SubSurface ______________________________________
  341. subSurfaceOutParams subSurfaceOut;
  342. #ifdef SUBSURFACE
  343. #ifdef SS_THICKNESSANDMASK_TEXTURE
  344. vec4 thicknessMap = texture2D(thicknessSampler, vThicknessUV + uvOffset);
  345. #endif
  346. subSurfaceBlock(
  347. vSubSurfaceIntensity,
  348. vThicknessParam,
  349. vTintColor,
  350. normalW,
  351. specularEnvironmentReflectance,
  352. #ifdef SS_THICKNESSANDMASK_TEXTURE
  353. thicknessMap,
  354. #endif
  355. #ifdef REFLECTION
  356. #ifdef SS_TRANSLUCENCY
  357. reflectionMatrix,
  358. #ifdef USESPHERICALFROMREFLECTIONMAP
  359. #if !defined(NORMAL) || !defined(USESPHERICALINVERTEX)
  360. reflectionOut.irradianceVector,
  361. #endif
  362. #endif
  363. #ifdef USEIRRADIANCEMAP
  364. irradianceSampler,
  365. #endif
  366. #endif
  367. #endif
  368. #ifdef SS_REFRACTION
  369. vPositionW,
  370. viewDirectionW,
  371. view,
  372. surfaceAlbedo,
  373. vRefractionInfos,
  374. refractionMatrix,
  375. vRefractionMicrosurfaceInfos,
  376. vLightingIntensity,
  377. #ifdef SS_LINKREFRACTIONTOTRANSPARENCY
  378. alpha,
  379. #endif
  380. #ifdef SS_LODINREFRACTIONALPHA
  381. NdotVUnclamped,
  382. #endif
  383. #ifdef SS_LINEARSPECULARREFRACTION
  384. roughness,
  385. #else
  386. alphaG,
  387. #endif
  388. refractionSampler,
  389. #ifndef LODBASEDMICROSFURACE
  390. refractionSamplerLow,
  391. refractionSamplerHigh,
  392. #endif
  393. #ifdef ANISOTROPIC
  394. anisotropicOut,
  395. #endif
  396. #endif
  397. #ifdef SS_TRANSLUCENCY
  398. vDiffusionDistance,
  399. #endif
  400. subSurfaceOut
  401. );
  402. #ifdef SS_REFRACTION
  403. surfaceAlbedo = subSurfaceOut.surfaceAlbedo;
  404. #ifdef SS_LINKREFRACTIONTOTRANSPARENCY
  405. alpha = subSurfaceOut.alpha;
  406. #endif
  407. #endif
  408. #else
  409. subSurfaceOut.specularEnvironmentReflectance = specularEnvironmentReflectance;
  410. #endif
  411. // _____________________________ Direct Lighting Info __________________________________
  412. #include<pbrBlockDirectLighting>
  413. #include<lightFragment>[0..maxSimultaneousLights]
  414. // _____________________________ Compute Final Lit Components ________________________
  415. #include<pbrBlockFinalLitComponents>
  416. #endif // UNLIT
  417. #include<pbrBlockFinalUnlitComponents>
  418. #include<pbrBlockFinalColorComposition>
  419. #include<logDepthFragment>
  420. #include<fogFragment>(color, finalColor)
  421. #include<pbrBlockImageProcessing>
  422. #define CUSTOM_FRAGMENT_BEFORE_FRAGCOLOR
  423. #ifdef HIGH_DEFINITION_PIPELINE
  424. vec3 irradiance = finalDiffuse;
  425. #ifndef UNLIT
  426. #ifdef REFLECTION
  427. irradiance += finalIrradiance;
  428. #endif
  429. #endif
  430. // finalDiffuse and finalIrradiance are already multiplied by surfaceAlbedo
  431. // What about :
  432. // Lightmaps ? (can we consider them as pure diffuse ?)
  433. // AO and shadows, should they dim the diffuseLight ? (right now they are)
  434. gl_FragData[0] = finalColor;
  435. gl_FragData[1] = vec4(irradiance / sqrt(surfaceAlbedo), 1.0); // pre and post scatter
  436. gl_FragData[2] = vec4(vViewPos.z, 0.0, 0.0, 1.0);
  437. gl_FragData[3] = vec4(sqrt(surfaceAlbedo), 1.0);
  438. gl_FragData[4] = vec4(finalColor.rgb - irradiance, 0.0);
  439. #endif
  440. gl_FragColor = finalColor;
  441. #include<pbrDebug>
  442. }