pbr.fragment.fx 17 KB

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