pbr.fragment.fx 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  1. #ifdef BUMP
  2. #extension GL_OES_standard_derivatives : enable
  3. #endif
  4. #ifdef LODBASEDMICROSFURACE
  5. #extension GL_EXT_shader_texture_lod : enable
  6. #endif
  7. #ifdef LOGARITHMICDEPTH
  8. #extension GL_EXT_frag_depth : enable
  9. #endif
  10. precision highp float;
  11. // Constants
  12. #define RECIPROCAL_PI2 0.15915494
  13. #define FRESNEL_MAXIMUM_ON_ROUGH 0.25
  14. uniform vec3 vEyePosition;
  15. uniform vec3 vAmbientColor;
  16. uniform vec3 vReflectionColor;
  17. uniform vec4 vAlbedoColor;
  18. uniform vec4 vLightRadiuses;
  19. // CUSTOM CONTROLS
  20. uniform vec4 vLightingIntensity;
  21. uniform vec4 vCameraInfos;
  22. #ifdef OVERLOADEDVALUES
  23. uniform vec4 vOverloadedIntensity;
  24. uniform vec3 vOverloadedAmbient;
  25. uniform vec3 vOverloadedAlbedo;
  26. uniform vec3 vOverloadedReflectivity;
  27. uniform vec3 vOverloadedEmissive;
  28. uniform vec3 vOverloadedReflection;
  29. uniform vec3 vOverloadedMicroSurface;
  30. #endif
  31. #ifdef OVERLOADEDSHADOWVALUES
  32. uniform vec4 vOverloadedShadowIntensity;
  33. #endif
  34. #ifdef USESPHERICALFROMREFLECTIONMAP
  35. uniform vec3 vSphericalX;
  36. uniform vec3 vSphericalY;
  37. uniform vec3 vSphericalZ;
  38. uniform vec3 vSphericalXX;
  39. uniform vec3 vSphericalYY;
  40. uniform vec3 vSphericalZZ;
  41. uniform vec3 vSphericalXY;
  42. uniform vec3 vSphericalYZ;
  43. uniform vec3 vSphericalZX;
  44. vec3 EnvironmentIrradiance(vec3 normal)
  45. {
  46. // Note: 'normal' is assumed to be normalised (or near normalised)
  47. // This isn't as critical as it is with other calculations (e.g. specular highlight), but the result will be incorrect nonetheless.
  48. // TODO: switch to optimal implementation
  49. vec3 result =
  50. vSphericalX * normal.x +
  51. vSphericalY * normal.y +
  52. vSphericalZ * normal.z +
  53. vSphericalXX * normal.x * normal.x +
  54. vSphericalYY * normal.y * normal.y +
  55. vSphericalZZ * normal.z * normal.z +
  56. vSphericalYZ * normal.y * normal.z +
  57. vSphericalZX * normal.z * normal.x +
  58. vSphericalXY * normal.x * normal.y;
  59. return result.rgb;
  60. }
  61. #endif
  62. #ifdef LODBASEDMICROSFURACE
  63. uniform vec2 vMicrosurfaceTextureLods;
  64. #endif
  65. // PBR CUSTOM CONSTANTS
  66. const float kPi = 3.1415926535897932384626433832795;
  67. const float kRougnhessToAlphaScale = 0.1;
  68. const float kRougnhessToAlphaOffset = 0.29248125;
  69. #ifdef PoissonSamplingEnvironment
  70. const int poissonSphereSamplersCount = 32;
  71. vec3 poissonSphereSamplers[poissonSphereSamplersCount];
  72. void initSamplers()
  73. {
  74. poissonSphereSamplers[0] = vec3( -0.552198926093, 0.801049753814, -0.0322487480415 );
  75. poissonSphereSamplers[1] = vec3( 0.344874796559, -0.650989584719, 0.283038477033 );
  76. poissonSphereSamplers[2] = vec3( -0.0710183703467, 0.163770497767, -0.95022416734 );
  77. poissonSphereSamplers[3] = vec3( 0.422221832073, 0.576613638193, 0.519157625948 );
  78. poissonSphereSamplers[4] = vec3( -0.561872200916, -0.665581249881, -0.131630473211 );
  79. poissonSphereSamplers[5] = vec3( -0.409905973809, 0.0250731510778, 0.674676954809 );
  80. poissonSphereSamplers[6] = vec3( 0.206829570551, -0.190199352704, 0.919073906156 );
  81. poissonSphereSamplers[7] = vec3( -0.857514664463, 0.0274425010091, -0.475068738967 );
  82. poissonSphereSamplers[8] = vec3( -0.816275009951, -0.0432916479141, 0.40394579291 );
  83. poissonSphereSamplers[9] = vec3( 0.397976181928, -0.633227519667, -0.617794410447 );
  84. poissonSphereSamplers[10] = vec3( -0.181484199014, 0.0155418272003, -0.34675720703 );
  85. poissonSphereSamplers[11] = vec3( 0.591734926919, 0.489930882201, -0.51675303188 );
  86. poissonSphereSamplers[12] = vec3( -0.264514973057, 0.834248662136, 0.464624235985 );
  87. poissonSphereSamplers[13] = vec3( -0.125845223505, 0.812029586099, -0.46213797731 );
  88. poissonSphereSamplers[14] = vec3( 0.0345715424639, 0.349983742938, 0.855109899027 );
  89. poissonSphereSamplers[15] = vec3( 0.694340492749, -0.281052190209, -0.379600605543 );
  90. poissonSphereSamplers[16] = vec3( -0.241055518078, -0.580199280578, 0.435381168431 );
  91. poissonSphereSamplers[17] = vec3( 0.126313722289, 0.715113642744, 0.124385788055 );
  92. poissonSphereSamplers[18] = vec3( 0.752862552387, 0.277075021888, 0.275059597549 );
  93. poissonSphereSamplers[19] = vec3( -0.400896300918, -0.309374534321, -0.74285782627 );
  94. poissonSphereSamplers[20] = vec3( 0.121843331941, -0.00381197918195, 0.322441835258 );
  95. poissonSphereSamplers[21] = vec3( 0.741656771351, -0.472083016745, 0.14589173819 );
  96. poissonSphereSamplers[22] = vec3( -0.120347565985, -0.397252703556, -0.00153836114051 );
  97. poissonSphereSamplers[23] = vec3( -0.846258835203, -0.433763808754, 0.168732209784 );
  98. poissonSphereSamplers[24] = vec3( 0.257765618362, -0.546470581239, -0.242234375624 );
  99. poissonSphereSamplers[25] = vec3( -0.640343473361, 0.51920903395, 0.549310644325 );
  100. poissonSphereSamplers[26] = vec3( -0.894309984621, 0.297394061018, 0.0884583225292 );
  101. poissonSphereSamplers[27] = vec3( -0.126241933628, -0.535151016335, -0.440093659672 );
  102. poissonSphereSamplers[28] = vec3( -0.158176440297, -0.393125021578, 0.890727226039 );
  103. poissonSphereSamplers[29] = vec3( 0.896024272938, 0.203068725821, -0.11198597748 );
  104. poissonSphereSamplers[30] = vec3( 0.568671758933, -0.314144243629, 0.509070768816 );
  105. poissonSphereSamplers[31] = vec3( 0.289665332178, 0.104356977462, -0.348379247171 );
  106. }
  107. vec3 environmentSampler(samplerCube cubeMapSampler, vec3 centralDirection, float microsurfaceAverageSlope)
  108. {
  109. vec3 result = vec3(0., 0., 0.);
  110. for(int i = 0; i < poissonSphereSamplersCount; i++)
  111. {
  112. vec3 offset = poissonSphereSamplers[i];
  113. vec3 direction = centralDirection + microsurfaceAverageSlope * offset;
  114. result += textureCube(cubeMapSampler, direction, 0.).rgb;
  115. }
  116. result /= 32.0;
  117. return result;
  118. }
  119. #endif
  120. // PBR HELPER METHODS
  121. float Square(float value)
  122. {
  123. return value * value;
  124. }
  125. float getLuminance(vec3 color)
  126. {
  127. return clamp(dot(color, vec3(0.2126, 0.7152, 0.0722)), 0., 1.);
  128. }
  129. float convertRoughnessToAverageSlope(float roughness)
  130. {
  131. // Calculate AlphaG as square of roughness; add epsilon to avoid numerical issues
  132. const float kMinimumVariance = 0.0005;
  133. float alphaG = Square(roughness) + kMinimumVariance;
  134. return alphaG;
  135. }
  136. // Based on Beckamm roughness to Blinn exponent + http://casual-effects.blogspot.ca/2011/08/plausible-environment-lighting-in-two.html
  137. float getMipMapIndexFromAverageSlope(float maxMipLevel, float alpha)
  138. {
  139. // do not take in account lower mips hence -1... and wait from proper preprocess.
  140. // formula comes from approximation of the mathematical solution.
  141. //float mip = maxMipLevel + kRougnhessToAlphaOffset + 0.5 * log2(alpha);
  142. // In the mean time
  143. // Always [0..1] goes from max mip to min mip in a log2 way.
  144. // Change 5 to nummip below.
  145. // http://www.wolframalpha.com/input/?i=x+in+0..1+plot+(+5+%2B+0.3+%2B+0.1+*+5+*+log2(+(1+-+x)+*+(1+-+x)+%2B+0.0005))
  146. float mip = kRougnhessToAlphaOffset + maxMipLevel + (maxMipLevel * kRougnhessToAlphaScale * log2(alpha));
  147. return clamp(mip, 0., maxMipLevel);
  148. }
  149. // From Microfacet Models for Refraction through Rough Surfaces, Walter et al. 2007
  150. float smithVisibilityG1_TrowbridgeReitzGGX(float dot, float alphaG)
  151. {
  152. float tanSquared = (1.0 - dot * dot) / (dot * dot);
  153. return 2.0 / (1.0 + sqrt(1.0 + alphaG * alphaG * tanSquared));
  154. }
  155. float smithVisibilityG_TrowbridgeReitzGGX_Walter(float NdotL, float NdotV, float alphaG)
  156. {
  157. return smithVisibilityG1_TrowbridgeReitzGGX(NdotL, alphaG) * smithVisibilityG1_TrowbridgeReitzGGX(NdotV, alphaG);
  158. }
  159. // Trowbridge-Reitz (GGX)
  160. // Generalised Trowbridge-Reitz with gamma power=2.0
  161. float normalDistributionFunction_TrowbridgeReitzGGX(float NdotH, float alphaG)
  162. {
  163. // Note: alphaG is average slope (gradient) of the normals in slope-space.
  164. // It is also the (trigonometric) tangent of the median distribution value, i.e. 50% of normals have
  165. // a tangent (gradient) closer to the macrosurface than this slope.
  166. float a2 = Square(alphaG);
  167. float d = NdotH * NdotH * (a2 - 1.0) + 1.0;
  168. return a2 / (kPi * d * d);
  169. }
  170. vec3 fresnelSchlickGGX(float VdotH, vec3 reflectance0, vec3 reflectance90)
  171. {
  172. return reflectance0 + (reflectance90 - reflectance0) * pow(clamp(1.0 - VdotH, 0., 1.), 5.0);
  173. }
  174. vec3 FresnelSchlickEnvironmentGGX(float VdotN, vec3 reflectance0, vec3 reflectance90, float smoothness)
  175. {
  176. // Schlick fresnel approximation, extended with basic smoothness term so that rough surfaces do not approach reflectance90 at grazing angle
  177. float weight = mix(FRESNEL_MAXIMUM_ON_ROUGH, 1.0, smoothness);
  178. return reflectance0 + weight * (reflectance90 - reflectance0) * pow(clamp(1.0 - VdotN, 0., 1.), 5.0);
  179. }
  180. // Cook Torance Specular computation.
  181. vec3 computeSpecularTerm(float NdotH, float NdotL, float NdotV, float VdotH, float roughness, vec3 specularColor)
  182. {
  183. float alphaG = convertRoughnessToAverageSlope(roughness);
  184. float distribution = normalDistributionFunction_TrowbridgeReitzGGX(NdotH, alphaG);
  185. float visibility = smithVisibilityG_TrowbridgeReitzGGX_Walter(NdotL, NdotV, alphaG);
  186. visibility /= (4.0 * NdotL * NdotV); // Cook Torance Denominator integated in viibility to avoid issues when visibility function changes.
  187. vec3 fresnel = fresnelSchlickGGX(VdotH, specularColor, vec3(1., 1., 1.));
  188. float specTerm = max(0., visibility * distribution) * NdotL;
  189. return fresnel * specTerm * kPi; // TODO: audit pi constants
  190. }
  191. float computeDiffuseTerm(float NdotL, float NdotV, float VdotH, float roughness)
  192. {
  193. // Diffuse fresnel falloff as per Disney principled BRDF, and in the spirit of
  194. // of general coupled diffuse/specular models e.g. Ashikhmin Shirley.
  195. float diffuseFresnelNV = pow(clamp(1.0 - NdotL, 0.000001, 1.), 5.0);
  196. float diffuseFresnelNL = pow(clamp(1.0 - NdotV, 0.000001, 1.), 5.0);
  197. float diffuseFresnel90 = 0.5 + 2.0 * VdotH * VdotH * roughness;
  198. float diffuseFresnelTerm =
  199. (1.0 + (diffuseFresnel90 - 1.0) * diffuseFresnelNL) *
  200. (1.0 + (diffuseFresnel90 - 1.0) * diffuseFresnelNV);
  201. return diffuseFresnelTerm * NdotL;
  202. // PI Test
  203. // diffuseFresnelTerm /= kPi;
  204. }
  205. float adjustRoughnessFromLightProperties(float roughness, float lightRadius, float lightDistance)
  206. {
  207. // At small angle this approximation works.
  208. float lightRoughness = lightRadius / lightDistance;
  209. // Distribution can sum.
  210. float totalRoughness = clamp(lightRoughness + roughness, 0., 1.);
  211. return totalRoughness;
  212. }
  213. float computeDefaultMicroSurface(float microSurface, vec3 reflectivityColor)
  214. {
  215. float kReflectivityNoAlphaWorkflow_SmoothnessMax = 0.95;
  216. float reflectivityLuminance = getLuminance(reflectivityColor);
  217. float reflectivityLuma = sqrt(reflectivityLuminance);
  218. microSurface = reflectivityLuma * kReflectivityNoAlphaWorkflow_SmoothnessMax;
  219. return microSurface;
  220. }
  221. vec3 toLinearSpace(vec3 color)
  222. {
  223. return vec3(pow(color.r, 2.2), pow(color.g, 2.2), pow(color.b, 2.2));
  224. }
  225. vec3 toGammaSpace(vec3 color)
  226. {
  227. return vec3(pow(color.r, 1.0 / 2.2), pow(color.g, 1.0 / 2.2), pow(color.b, 1.0 / 2.2));
  228. }
  229. float computeLightFalloff(vec3 lightOffset, float lightDistanceSquared, float range)
  230. {
  231. #ifdef USEPHYSICALLIGHTFALLOFF
  232. float lightDistanceFalloff = 1.0 / ((lightDistanceSquared + 0.0001));
  233. return lightDistanceFalloff;
  234. #else
  235. float lightFalloff = max(0., 1.0 - length(lightOffset) / range);
  236. return lightFalloff;
  237. #endif
  238. }
  239. #ifdef CAMERATONEMAP
  240. vec3 toneMaps(vec3 color)
  241. {
  242. color = max(color, 0.0);
  243. // TONE MAPPING / EXPOSURE
  244. color.rgb = color.rgb * vCameraInfos.x;
  245. float tuning = 1.5; // TODO: sync up so e.g. 18% greys are matched to exposure appropriately
  246. // PI Test
  247. // tuning *= kPi;
  248. vec3 tonemapped = 1.0 - exp2(-color.rgb * tuning); // simple local photographic tonemapper
  249. color.rgb = mix(color.rgb, tonemapped, 1.0);
  250. return color;
  251. }
  252. #endif
  253. #ifdef CAMERACONTRAST
  254. vec4 contrasts(vec4 color)
  255. {
  256. color = clamp(color, 0.0, 1.0);
  257. vec3 resultHighContrast = color.rgb * color.rgb * (3.0 - 2.0 * color.rgb);
  258. float contrast = vCameraInfos.y;
  259. if (contrast < 1.0)
  260. {
  261. // Decrease contrast: interpolate towards zero-contrast image (flat grey)
  262. color.rgb = mix(vec3(0.5, 0.5, 0.5), color.rgb, contrast);
  263. }
  264. else
  265. {
  266. // Increase contrast: apply simple shoulder-toe high contrast curve
  267. color.rgb = mix(color.rgb, resultHighContrast, contrast - 1.0);
  268. }
  269. return color;
  270. }
  271. #endif
  272. // END PBR HELPER METHODS
  273. uniform vec4 vReflectivityColor;
  274. uniform vec3 vEmissiveColor;
  275. // Input
  276. varying vec3 vPositionW;
  277. #ifdef NORMAL
  278. varying vec3 vNormalW;
  279. #endif
  280. #ifdef VERTEXCOLOR
  281. varying vec4 vColor;
  282. #endif
  283. // Lights
  284. #include<light0FragmentDeclaration>
  285. #include<light1FragmentDeclaration>
  286. #include<light2FragmentDeclaration>
  287. #include<light3FragmentDeclaration>
  288. // Samplers
  289. #ifdef ALBEDO
  290. varying vec2 vAlbedoUV;
  291. uniform sampler2D albedoSampler;
  292. uniform vec2 vAlbedoInfos;
  293. #endif
  294. #ifdef AMBIENT
  295. varying vec2 vAmbientUV;
  296. uniform sampler2D ambientSampler;
  297. uniform vec2 vAmbientInfos;
  298. #endif
  299. #ifdef OPACITY
  300. varying vec2 vOpacityUV;
  301. uniform sampler2D opacitySampler;
  302. uniform vec2 vOpacityInfos;
  303. #endif
  304. #ifdef EMISSIVE
  305. varying vec2 vEmissiveUV;
  306. uniform vec2 vEmissiveInfos;
  307. uniform sampler2D emissiveSampler;
  308. #endif
  309. #ifdef LIGHTMAP
  310. varying vec2 vLightmapUV;
  311. uniform vec2 vLightmapInfos;
  312. uniform sampler2D lightmapSampler;
  313. #endif
  314. #if defined(REFLECTIVITY)
  315. varying vec2 vReflectivityUV;
  316. uniform vec2 vReflectivityInfos;
  317. uniform sampler2D reflectivitySampler;
  318. #endif
  319. // Fresnel
  320. #include<fresnelFunction>
  321. #ifdef OPACITYFRESNEL
  322. uniform vec4 opacityParts;
  323. #endif
  324. #ifdef EMISSIVEFRESNEL
  325. uniform vec4 emissiveLeftColor;
  326. uniform vec4 emissiveRightColor;
  327. #endif
  328. // Refraction Reflection
  329. #if defined(REFLECTIONMAP_SPHERICAL) || defined(REFLECTIONMAP_PROJECTION) || defined(REFRACTION)
  330. uniform mat4 view;
  331. #endif
  332. // Refraction
  333. #ifdef REFRACTION
  334. uniform vec4 vRefractionInfos;
  335. #ifdef REFRACTIONMAP_3D
  336. uniform samplerCube refractionCubeSampler;
  337. #else
  338. uniform sampler2D refraction2DSampler;
  339. uniform mat4 refractionMatrix;
  340. #endif
  341. #endif
  342. // Reflection
  343. #ifdef REFLECTION
  344. uniform vec2 vReflectionInfos;
  345. #ifdef REFLECTIONMAP_3D
  346. uniform samplerCube reflectionCubeSampler;
  347. #else
  348. uniform sampler2D reflection2DSampler;
  349. #endif
  350. #ifdef REFLECTIONMAP_SKYBOX
  351. varying vec3 vPositionUVW;
  352. #else
  353. #ifdef REFLECTIONMAP_EQUIRECTANGULAR_FIXED
  354. varying vec3 vDirectionW;
  355. #endif
  356. #if defined(REFLECTIONMAP_PLANAR) || defined(REFLECTIONMAP_CUBIC) || defined(REFLECTIONMAP_PROJECTION)
  357. uniform mat4 reflectionMatrix;
  358. #endif
  359. #endif
  360. #include<reflectionFunction>
  361. #endif
  362. // Shadows
  363. #ifdef SHADOWS
  364. float unpack(vec4 color)
  365. {
  366. const vec4 bit_shift = vec4(1.0 / (255.0 * 255.0 * 255.0), 1.0 / (255.0 * 255.0), 1.0 / 255.0, 1.0);
  367. return dot(color, bit_shift);
  368. }
  369. #if defined(POINTLIGHT0) || defined(POINTLIGHT1) || defined(POINTLIGHT2) || defined(POINTLIGHT3)
  370. uniform vec2 depthValues;
  371. float computeShadowCube(vec3 lightPosition, samplerCube shadowSampler, float darkness, float bias)
  372. {
  373. vec3 directionToLight = vPositionW - lightPosition;
  374. float depth = length(directionToLight);
  375. depth = clamp(depth, 0., 1.0);
  376. directionToLight = normalize(directionToLight);
  377. directionToLight.y = - directionToLight.y;
  378. float shadow = unpack(textureCube(shadowSampler, directionToLight)) + bias;
  379. if (depth > shadow)
  380. {
  381. #ifdef OVERLOADEDSHADOWVALUES
  382. return mix(1.0, darkness, vOverloadedShadowIntensity.x);
  383. #else
  384. return darkness;
  385. #endif
  386. }
  387. return 1.0;
  388. }
  389. float computeShadowWithPCFCube(vec3 lightPosition, samplerCube shadowSampler, float mapSize, float bias, float darkness)
  390. {
  391. vec3 directionToLight = vPositionW - lightPosition;
  392. float depth = length(directionToLight);
  393. depth = (depth - depthValues.x) / (depthValues.y - depthValues.x);
  394. depth = clamp(depth, 0., 1.0);
  395. directionToLight = normalize(directionToLight);
  396. directionToLight.y = -directionToLight.y;
  397. float visibility = 1.;
  398. vec3 poissonDisk[4];
  399. poissonDisk[0] = vec3(-1.0, 1.0, -1.0);
  400. poissonDisk[1] = vec3(1.0, -1.0, -1.0);
  401. poissonDisk[2] = vec3(-1.0, -1.0, -1.0);
  402. poissonDisk[3] = vec3(1.0, -1.0, 1.0);
  403. // Poisson Sampling
  404. float biasedDepth = depth - bias;
  405. if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[0] * mapSize)) < biasedDepth) visibility -= 0.25;
  406. if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[1] * mapSize)) < biasedDepth) visibility -= 0.25;
  407. if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[2] * mapSize)) < biasedDepth) visibility -= 0.25;
  408. if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[3] * mapSize)) < biasedDepth) visibility -= 0.25;
  409. #ifdef OVERLOADEDSHADOWVALUES
  410. return min(1.0, mix(1.0, visibility + darkness, vOverloadedShadowIntensity.x));
  411. #else
  412. return min(1.0, visibility + darkness);
  413. #endif
  414. }
  415. #endif
  416. #if defined(SPOTLIGHT0) || defined(SPOTLIGHT1) || defined(SPOTLIGHT2) || defined(SPOTLIGHT3) || defined(DIRLIGHT0) || defined(DIRLIGHT1) || defined(DIRLIGHT2) || defined(DIRLIGHT3)
  417. float computeShadow(vec4 vPositionFromLight, sampler2D shadowSampler, float darkness, float bias)
  418. {
  419. vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
  420. depth = 0.5 * depth + vec3(0.5);
  421. vec2 uv = depth.xy;
  422. if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0)
  423. {
  424. return 1.0;
  425. }
  426. float shadow = unpack(texture2D(shadowSampler, uv)) + bias;
  427. if (depth.z > shadow)
  428. {
  429. #ifdef OVERLOADEDSHADOWVALUES
  430. return mix(1.0, darkness, vOverloadedShadowIntensity.x);
  431. #else
  432. return darkness;
  433. #endif
  434. }
  435. return 1.;
  436. }
  437. float computeShadowWithPCF(vec4 vPositionFromLight, sampler2D shadowSampler, float mapSize, float bias, float darkness)
  438. {
  439. vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
  440. depth = 0.5 * depth + vec3(0.5);
  441. vec2 uv = depth.xy;
  442. if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0)
  443. {
  444. return 1.0;
  445. }
  446. float visibility = 1.;
  447. vec2 poissonDisk[4];
  448. poissonDisk[0] = vec2(-0.94201624, -0.39906216);
  449. poissonDisk[1] = vec2(0.94558609, -0.76890725);
  450. poissonDisk[2] = vec2(-0.094184101, -0.92938870);
  451. poissonDisk[3] = vec2(0.34495938, 0.29387760);
  452. // Poisson Sampling
  453. float biasedDepth = depth.z - bias;
  454. if (unpack(texture2D(shadowSampler, uv + poissonDisk[0] * mapSize)) < biasedDepth) visibility -= 0.25;
  455. if (unpack(texture2D(shadowSampler, uv + poissonDisk[1] * mapSize)) < biasedDepth) visibility -= 0.25;
  456. if (unpack(texture2D(shadowSampler, uv + poissonDisk[2] * mapSize)) < biasedDepth) visibility -= 0.25;
  457. if (unpack(texture2D(shadowSampler, uv + poissonDisk[3] * mapSize)) < biasedDepth) visibility -= 0.25;
  458. #ifdef OVERLOADEDSHADOWVALUES
  459. return min(1.0, mix(1.0, visibility + darkness, vOverloadedShadowIntensity.x));
  460. #else
  461. return min(1.0, visibility + darkness);
  462. #endif
  463. }
  464. // Thanks to http://devmaster.net/
  465. float unpackHalf(vec2 color)
  466. {
  467. return color.x + (color.y / 255.0);
  468. }
  469. float linstep(float low, float high, float v) {
  470. return clamp((v - low) / (high - low), 0.0, 1.0);
  471. }
  472. float ChebychevInequality(vec2 moments, float compare, float bias)
  473. {
  474. float p = smoothstep(compare - bias, compare, moments.x);
  475. float variance = max(moments.y - moments.x * moments.x, 0.02);
  476. float d = compare - moments.x;
  477. float p_max = linstep(0.2, 1.0, variance / (variance + d * d));
  478. return clamp(max(p, p_max), 0.0, 1.0);
  479. }
  480. float computeShadowWithVSM(vec4 vPositionFromLight, sampler2D shadowSampler, float bias, float darkness)
  481. {
  482. vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
  483. depth = 0.5 * depth + vec3(0.5);
  484. vec2 uv = depth.xy;
  485. if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0 || depth.z >= 1.0)
  486. {
  487. return 1.0;
  488. }
  489. vec4 texel = texture2D(shadowSampler, uv);
  490. vec2 moments = vec2(unpackHalf(texel.xy), unpackHalf(texel.zw));
  491. #ifdef OVERLOADEDSHADOWVALUES
  492. return min(1.0, mix(1.0, 1.0 - ChebychevInequality(moments, depth.z, bias) + darkness, vOverloadedShadowIntensity.x));
  493. #else
  494. return min(1.0, 1.0 - ChebychevInequality(moments, depth.z, bias) + darkness);
  495. #endif
  496. }
  497. #endif
  498. #endif
  499. #include<bumpFragmentFunctions>
  500. #include<clipPlaneFragmentDeclaration>
  501. #include<logDepthDeclaration>
  502. // Fog
  503. #include<fogFragmentDeclaration>
  504. // Light Computing
  505. struct lightingInfo
  506. {
  507. vec3 diffuse;
  508. #ifdef SPECULARTERM
  509. vec3 specular;
  510. #endif
  511. };
  512. lightingInfo computeLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, vec3 specularColor, float range, float roughness, float NdotV, float lightRadius) {
  513. lightingInfo result;
  514. vec3 lightDirection;
  515. float attenuation = 1.0;
  516. float lightDistance;
  517. // Point
  518. if (lightData.w == 0.)
  519. {
  520. vec3 lightOffset = lightData.xyz - vPositionW;
  521. float lightDistanceSquared = dot(lightOffset, lightOffset);
  522. attenuation = computeLightFalloff(lightOffset, lightDistanceSquared, range);
  523. lightDistance = sqrt(lightDistanceSquared);
  524. lightDirection = normalize(lightOffset);
  525. }
  526. // Directional
  527. else
  528. {
  529. lightDistance = length(-lightData.xyz);
  530. lightDirection = normalize(-lightData.xyz);
  531. }
  532. // Roughness
  533. roughness = adjustRoughnessFromLightProperties(roughness, lightRadius, lightDistance);
  534. // diffuse
  535. vec3 H = normalize(viewDirectionW + lightDirection);
  536. float NdotL = max(0.00000000001, dot(vNormal, lightDirection));
  537. float VdotH = clamp(0.00000000001, 1.0, dot(viewDirectionW, H));
  538. float diffuseTerm = computeDiffuseTerm(NdotL, NdotV, VdotH, roughness);
  539. result.diffuse = diffuseTerm * diffuseColor * attenuation;
  540. #ifdef SPECULARTERM
  541. // Specular
  542. float NdotH = max(0.00000000001, dot(vNormal, H));
  543. vec3 specTerm = computeSpecularTerm(NdotH, NdotL, NdotV, VdotH, roughness, specularColor);
  544. result.specular = specTerm * attenuation;
  545. #endif
  546. return result;
  547. }
  548. lightingInfo computeSpotLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec4 lightDirection, vec3 diffuseColor, vec3 specularColor, float range, float roughness, float NdotV, float lightRadius) {
  549. lightingInfo result;
  550. vec3 lightOffset = lightData.xyz - vPositionW;
  551. vec3 lightVectorW = normalize(lightOffset);
  552. // diffuse
  553. float cosAngle = max(0.000000000000001, dot(-lightDirection.xyz, lightVectorW));
  554. if (cosAngle >= lightDirection.w)
  555. {
  556. cosAngle = max(0., pow(cosAngle, lightData.w));
  557. // Inverse squared falloff.
  558. float lightDistanceSquared = dot(lightOffset, lightOffset);
  559. float attenuation = computeLightFalloff(lightOffset, lightDistanceSquared, range);
  560. // Directional falloff.
  561. attenuation *= cosAngle;
  562. // Roughness.
  563. float lightDistance = sqrt(lightDistanceSquared);
  564. roughness = adjustRoughnessFromLightProperties(roughness, lightRadius, lightDistance);
  565. // Diffuse
  566. vec3 H = normalize(viewDirectionW - lightDirection.xyz);
  567. float NdotL = max(0.00000000001, dot(vNormal, -lightDirection.xyz));
  568. float VdotH = clamp(dot(viewDirectionW, H), 0.00000000001, 1.0);
  569. float diffuseTerm = computeDiffuseTerm(NdotL, NdotV, VdotH, roughness);
  570. result.diffuse = diffuseTerm * diffuseColor * attenuation;
  571. #ifdef SPECULARTERM
  572. // Specular
  573. float NdotH = max(0.00000000001, dot(vNormal, H));
  574. vec3 specTerm = computeSpecularTerm(NdotH, NdotL, NdotV, VdotH, roughness, specularColor);
  575. result.specular = specTerm * attenuation;
  576. #endif
  577. return result;
  578. }
  579. result.diffuse = vec3(0.);
  580. #ifdef SPECULARTERM
  581. result.specular = vec3(0.);
  582. #endif
  583. return result;
  584. }
  585. lightingInfo computeHemisphericLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, vec3 specularColor, vec3 groundColor, float roughness, float NdotV, float lightRadius) {
  586. lightingInfo result;
  587. // Roughness
  588. // Do not touch roughness on hemispheric.
  589. // Diffuse
  590. float ndl = dot(vNormal, lightData.xyz) * 0.5 + 0.5;
  591. result.diffuse = mix(groundColor, diffuseColor, ndl);
  592. #ifdef SPECULARTERM
  593. // Specular
  594. vec3 lightVectorW = normalize(lightData.xyz);
  595. vec3 H = normalize(viewDirectionW + lightVectorW);
  596. float NdotH = max(0.00000000001, dot(vNormal, H));
  597. float NdotL = max(0.00000000001, ndl);
  598. float VdotH = clamp(0.00000000001, 1.0, dot(viewDirectionW, H));
  599. vec3 specTerm = computeSpecularTerm(NdotH, NdotL, NdotV, VdotH, roughness, specularColor);
  600. result.specular = specTerm;
  601. #endif
  602. return result;
  603. }
  604. void main(void) {
  605. #include<clipPlaneFragment>
  606. #ifdef PoissonSamplingEnvironment
  607. initSamplers();
  608. #endif
  609. vec3 viewDirectionW = normalize(vEyePosition - vPositionW);
  610. // Albedo
  611. vec4 surfaceAlbedo = vec4(1., 1., 1., 1.);
  612. vec3 surfaceAlbedoContribution = vAlbedoColor.rgb;
  613. // Alpha
  614. float alpha = vAlbedoColor.a;
  615. #ifdef ALBEDO
  616. surfaceAlbedo = texture2D(albedoSampler, vAlbedoUV);
  617. surfaceAlbedo = vec4(toLinearSpace(surfaceAlbedo.rgb), surfaceAlbedo.a);
  618. #ifndef LINKREFRACTIONTOTRANSPARENCY
  619. #ifdef ALPHATEST
  620. if (surfaceAlbedo.a < 0.4)
  621. discard;
  622. #endif
  623. #endif
  624. #ifdef ALPHAFROMALBEDO
  625. alpha *= surfaceAlbedo.a;
  626. #endif
  627. surfaceAlbedo.rgb *= vAlbedoInfos.y;
  628. #else
  629. // No Albedo texture.
  630. surfaceAlbedo.rgb = surfaceAlbedoContribution;
  631. surfaceAlbedoContribution = vec3(1., 1., 1.);
  632. #endif
  633. #ifdef VERTEXCOLOR
  634. surfaceAlbedo.rgb *= vColor.rgb;
  635. #endif
  636. #ifdef OVERLOADEDVALUES
  637. surfaceAlbedo.rgb = mix(surfaceAlbedo.rgb, vOverloadedAlbedo, vOverloadedIntensity.y);
  638. #endif
  639. // Bump
  640. #ifdef NORMAL
  641. vec3 normalW = normalize(vNormalW);
  642. #else
  643. vec3 normalW = vec3(1.0, 1.0, 1.0);
  644. #endif
  645. #ifdef BUMP
  646. mat3 TBN = cotangent_frame(vNormalW * vBumpInfos.y, -viewDirectionW, vBumpUV);
  647. normalW = perturbNormal(viewDirectionW, TBN, vBumpUV);
  648. #endif
  649. // Ambient color
  650. vec3 ambientColor = vec3(1., 1., 1.);
  651. #ifdef AMBIENT
  652. ambientColor = texture2D(ambientSampler, vAmbientUV).rgb * vAmbientInfos.y;
  653. #ifdef OVERLOADEDVALUES
  654. ambientColor.rgb = mix(ambientColor.rgb, vOverloadedAmbient, vOverloadedIntensity.x);
  655. #endif
  656. #endif
  657. // Specular map
  658. float microSurface = vReflectivityColor.a;
  659. vec3 surfaceReflectivityColor = vReflectivityColor.rgb;
  660. #ifdef OVERLOADEDVALUES
  661. surfaceReflectivityColor.rgb = mix(surfaceReflectivityColor.rgb, vOverloadedReflectivity, vOverloadedIntensity.z);
  662. #endif
  663. #ifdef REFLECTIVITY
  664. vec4 surfaceReflectivityColorMap = texture2D(reflectivitySampler, vReflectivityUV);
  665. surfaceReflectivityColor = surfaceReflectivityColorMap.rgb;
  666. surfaceReflectivityColor = toLinearSpace(surfaceReflectivityColor);
  667. #ifdef OVERLOADEDVALUES
  668. surfaceReflectivityColor = mix(surfaceReflectivityColor, vOverloadedReflectivity, vOverloadedIntensity.z);
  669. #endif
  670. #ifdef MICROSURFACEFROMREFLECTIVITYMAP
  671. microSurface = surfaceReflectivityColorMap.a;
  672. #else
  673. #ifdef MICROSURFACEAUTOMATIC
  674. microSurface = computeDefaultMicroSurface(microSurface, surfaceReflectivityColor);
  675. #endif
  676. #endif
  677. #endif
  678. #ifdef OVERLOADEDVALUES
  679. microSurface = mix(microSurface, vOverloadedMicroSurface.x, vOverloadedMicroSurface.y);
  680. #endif
  681. // Compute N dot V.
  682. float NdotV = max(0.00000000001, dot(normalW, viewDirectionW));
  683. // Adapt microSurface.
  684. microSurface = clamp(microSurface, 0., 1.) * 0.98;
  685. // Compute roughness.
  686. float roughness = clamp(1. - microSurface, 0.000001, 1.0);
  687. // Lighting
  688. vec3 lightDiffuseContribution = vec3(0., 0., 0.);
  689. #ifdef OVERLOADEDSHADOWVALUES
  690. vec3 shadowedOnlyLightDiffuseContribution = vec3(1., 1., 1.);
  691. #endif
  692. #ifdef SPECULARTERM
  693. vec3 lightSpecularContribution= vec3(0., 0., 0.);
  694. #endif
  695. float notShadowLevel = 1.; // 1 - shadowLevel
  696. #ifdef LIGHT0
  697. #ifndef SPECULARTERM
  698. vec3 vLightSpecular0 = vec3(0.0);
  699. #endif
  700. #ifdef SPOTLIGHT0
  701. lightingInfo info = computeSpotLighting(viewDirectionW, normalW, vLightData0, vLightDirection0, vLightDiffuse0.rgb, vLightSpecular0, vLightDiffuse0.a, roughness, NdotV, vLightRadiuses[0]);
  702. #endif
  703. #ifdef HEMILIGHT0
  704. lightingInfo info = computeHemisphericLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightSpecular0, vLightGround0, roughness, NdotV, vLightRadiuses[0]);
  705. #endif
  706. #if defined(POINTLIGHT0) || defined(DIRLIGHT0)
  707. lightingInfo info = computeLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightSpecular0, vLightDiffuse0.a, roughness, NdotV, vLightRadiuses[0]);
  708. #endif
  709. #ifdef SHADOW0
  710. #ifdef SHADOWVSM0
  711. notShadowLevel = computeShadowWithVSM(vPositionFromLight0, shadowSampler0, shadowsInfo0.z, shadowsInfo0.x);
  712. #else
  713. #ifdef SHADOWPCF0
  714. #if defined(POINTLIGHT0)
  715. notShadowLevel = computeShadowWithPCFCube(vLightData0.xyz, shadowSampler0, shadowsInfo0.y, shadowsInfo0.z, shadowsInfo0.x);
  716. #else
  717. notShadowLevel = computeShadowWithPCF(vPositionFromLight0, shadowSampler0, shadowsInfo0.y, shadowsInfo0.z, shadowsInfo0.x);
  718. #endif
  719. #else
  720. #if defined(POINTLIGHT0)
  721. notShadowLevel = computeShadowCube(vLightData0.xyz, shadowSampler0, shadowsInfo0.x, shadowsInfo0.z);
  722. #else
  723. notShadowLevel = computeShadow(vPositionFromLight0, shadowSampler0, shadowsInfo0.x, shadowsInfo0.z);
  724. #endif
  725. #endif
  726. #endif
  727. #else
  728. notShadowLevel = 1.;
  729. #endif
  730. lightDiffuseContribution += info.diffuse * notShadowLevel;
  731. #ifdef OVERLOADEDSHADOWVALUES
  732. shadowedOnlyLightDiffuseContribution *= notShadowLevel;
  733. #endif
  734. #ifdef SPECULARTERM
  735. lightSpecularContribution += info.specular * notShadowLevel;
  736. #endif
  737. #endif
  738. #ifdef LIGHT1
  739. #ifndef SPECULARTERM
  740. vec3 vLightSpecular1 = vec3(0.0);
  741. #endif
  742. #ifdef SPOTLIGHT1
  743. info = computeSpotLighting(viewDirectionW, normalW, vLightData1, vLightDirection1, vLightDiffuse1.rgb, vLightSpecular1, vLightDiffuse1.a, roughness, NdotV, vLightRadiuses[1]);
  744. #endif
  745. #ifdef HEMILIGHT1
  746. info = computeHemisphericLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightSpecular1, vLightGround1, roughness, NdotV, vLightRadiuses[1]);
  747. #endif
  748. #if defined(POINTLIGHT1) || defined(DIRLIGHT1)
  749. info = computeLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightSpecular1, vLightDiffuse1.a, roughness, NdotV, vLightRadiuses[1]);
  750. #endif
  751. #ifdef SHADOW1
  752. #ifdef SHADOWVSM1
  753. notShadowLevel = computeShadowWithVSM(vPositionFromLight1, shadowSampler1, shadowsInfo1.z, shadowsInfo1.x);
  754. #else
  755. #ifdef SHADOWPCF1
  756. #if defined(POINTLIGHT1)
  757. notShadowLevel = computeShadowWithPCFCube(vLightData1.xyz, shadowSampler1, shadowsInfo1.y, shadowsInfo1.z, shadowsInfo1.x);
  758. #else
  759. notShadowLevel = computeShadowWithPCF(vPositionFromLight1, shadowSampler1, shadowsInfo1.y, shadowsInfo1.z, shadowsInfo1.x);
  760. #endif
  761. #else
  762. #if defined(POINTLIGHT1)
  763. notShadowLevel = computeShadowCube(vLightData1.xyz, shadowSampler1, shadowsInfo1.x, shadowsInfo1.z);
  764. #else
  765. notShadowLevel = computeShadow(vPositionFromLight1, shadowSampler1, shadowsInfo1.x, shadowsInfo1.z);
  766. #endif
  767. #endif
  768. #endif
  769. #else
  770. notShadowLevel = 1.;
  771. #endif
  772. lightDiffuseContribution += info.diffuse * notShadowLevel;
  773. #ifdef OVERLOADEDSHADOWVALUES
  774. shadowedOnlyLightDiffuseContribution *= notShadowLevel;
  775. #endif
  776. #ifdef SPECULARTERM
  777. lightSpecularContribution += info.specular * notShadowLevel;
  778. #endif
  779. #endif
  780. #ifdef LIGHT2
  781. #ifndef SPECULARTERM
  782. vec3 vLightSpecular2 = vec3(0.0);
  783. #endif
  784. #ifdef SPOTLIGHT2
  785. info = computeSpotLighting(viewDirectionW, normalW, vLightData2, vLightDirection2, vLightDiffuse2.rgb, vLightSpecular2, vLightDiffuse2.a, roughness, NdotV, vLightRadiuses[2]);
  786. #endif
  787. #ifdef HEMILIGHT2
  788. info = computeHemisphericLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightSpecular2, vLightGround2, roughness, NdotV, vLightRadiuses[2]);
  789. #endif
  790. #if defined(POINTLIGHT2) || defined(DIRLIGHT2)
  791. info = computeLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightSpecular2, vLightDiffuse2.a, roughness, NdotV, vLightRadiuses[2]);
  792. #endif
  793. #ifdef SHADOW2
  794. #ifdef SHADOWVSM2
  795. notShadowLevel = computeShadowWithVSM(vPositionFromLight2, shadowSampler2, shadowsInfo2.z, shadowsInfo2.x);
  796. #else
  797. #ifdef SHADOWPCF2
  798. #if defined(POINTLIGHT2)
  799. notShadowLevel = computeShadowWithPCFCube(vLightData2.xyz, shadowSampler2, shadowsInfo2.y, shadowsInfo2.z, shadowsInfo2.x);
  800. #else
  801. notShadowLevel = computeShadowWithPCF(vPositionFromLight2, shadowSampler2, shadowsInfo2.y, shadowsInfo2.z, shadowsInfo2.x);
  802. #endif
  803. #else
  804. #if defined(POINTLIGHT2)
  805. notShadowLevel = computeShadowCube(vLightData2.xyz, shadowSampler2, shadowsInfo2.x, shadowsInfo2.z);
  806. #else
  807. notShadowLevel = computeShadow(vPositionFromLight2, shadowSampler2, shadowsInfo2.x, shadowsInfo2.z);
  808. #endif
  809. #endif
  810. #endif
  811. #else
  812. notShadowLevel = 1.;
  813. #endif
  814. lightDiffuseContribution += info.diffuse * notShadowLevel;
  815. #ifdef OVERLOADEDSHADOWVALUES
  816. shadowedOnlyLightDiffuseContribution *= notShadowLevel;
  817. #endif
  818. #ifdef SPECULARTERM
  819. lightSpecularContribution += info.specular * notShadowLevel;
  820. #endif
  821. #endif
  822. #ifdef LIGHT3
  823. #ifndef SPECULARTERM
  824. vec3 vLightSpecular3 = vec3(0.0);
  825. #endif
  826. #ifdef SPOTLIGHT3
  827. info = computeSpotLighting(viewDirectionW, normalW, vLightData3, vLightDirection3, vLightDiffuse3.rgb, vLightSpecular3, vLightDiffuse3.a, roughness, NdotV, vLightRadiuses[3]);
  828. #endif
  829. #ifdef HEMILIGHT3
  830. info = computeHemisphericLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightSpecular3, vLightGround3, roughness, NdotV, vLightRadiuses[3]);
  831. #endif
  832. #if defined(POINTLIGHT3) || defined(DIRLIGHT3)
  833. info = computeLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightSpecular3, vLightDiffuse3.a, roughness, NdotV, vLightRadiuses[3]);
  834. #endif
  835. #ifdef SHADOW3
  836. #ifdef SHADOWVSM3
  837. notShadowLevel = computeShadowWithVSM(vPositionFromLight3, shadowSampler3, shadowsInfo3.z, shadowsInfo3.x);
  838. #else
  839. #ifdef SHADOWPCF3
  840. #if defined(POINTLIGHT3)
  841. notShadowLevel = computeShadowWithPCFCube(vLightData3.xyz, shadowSampler3, shadowsInfo3.y, shadowsInfo3.z, shadowsInfo3.x);
  842. #else
  843. notShadowLevel = computeShadowWithPCF(vPositionFromLight3, shadowSampler3, shadowsInfo3.y, shadowsInfo3.z, shadowsInfo3.x);
  844. #endif
  845. #else
  846. #if defined(POINTLIGHT3)
  847. notShadowLevel = computeShadowCube(vLightData3.xyz, shadowSampler3, shadowsInfo3.x, shadowsInfo3.z);
  848. #else
  849. notShadowLevel = computeShadow(vPositionFromLight3, shadowSampler3, shadowsInfo3.x, shadowsInfo3.z);
  850. #endif
  851. #endif
  852. #endif
  853. #else
  854. notShadowLevel = 1.;
  855. #endif
  856. lightDiffuseContribution += info.diffuse * notShadowLevel;
  857. #ifdef OVERLOADEDSHADOWVALUES
  858. shadowedOnlyLightDiffuseContribution *= notShadowLevel;
  859. #endif
  860. #ifdef SPECULARTERM
  861. lightSpecularContribution += info.specular * notShadowLevel;
  862. #endif
  863. #endif
  864. #ifdef SPECULARTERM
  865. lightSpecularContribution *= vLightingIntensity.w;
  866. #endif
  867. #ifdef OPACITY
  868. vec4 opacityMap = texture2D(opacitySampler, vOpacityUV);
  869. #ifdef OPACITYRGB
  870. opacityMap.rgb = opacityMap.rgb * vec3(0.3, 0.59, 0.11);
  871. alpha *= (opacityMap.x + opacityMap.y + opacityMap.z)* vOpacityInfos.y;
  872. #else
  873. alpha *= opacityMap.a * vOpacityInfos.y;
  874. #endif
  875. #endif
  876. #ifdef VERTEXALPHA
  877. alpha *= vColor.a;
  878. #endif
  879. #ifdef OPACITYFRESNEL
  880. float opacityFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, opacityParts.z, opacityParts.w);
  881. alpha += opacityParts.x * (1.0 - opacityFresnelTerm) + opacityFresnelTerm * opacityParts.y;
  882. #endif
  883. // Refraction
  884. vec3 surfaceRefractionColor = vec3(0., 0., 0.);
  885. // Go mat -> blurry reflexion according to microSurface
  886. #ifdef LODBASEDMICROSFURACE
  887. float alphaG = convertRoughnessToAverageSlope(roughness);
  888. #else
  889. float bias = 20. * (1.0 - microSurface);
  890. #endif
  891. #ifdef REFRACTION
  892. vec3 refractionVector = normalize(refract(-viewDirectionW, normalW, vRefractionInfos.y));
  893. #ifdef LODBASEDMICROSFURACE
  894. float lodRefraction = getMipMapIndexFromAverageSlope(vMicrosurfaceTextureLods.y, alphaG);
  895. #endif
  896. #ifdef REFRACTIONMAP_3D
  897. refractionVector.y = refractionVector.y * vRefractionInfos.w;
  898. if (dot(refractionVector, viewDirectionW) < 1.0)
  899. {
  900. #ifdef LODBASEDMICROSFURACE
  901. surfaceRefractionColor = textureCubeLodEXT(refractionCubeSampler, refractionVector, lodRefraction).rgb * vRefractionInfos.x;
  902. #else
  903. surfaceRefractionColor = textureCube(refractionCubeSampler, refractionVector, bias).rgb * vRefractionInfos.x;
  904. #endif
  905. }
  906. #ifndef REFRACTIONMAPINLINEARSPACE
  907. surfaceRefractionColor = toLinearSpace(surfaceRefractionColor.rgb);
  908. #endif
  909. #else
  910. vec3 vRefractionUVW = vec3(refractionMatrix * (view * vec4(vPositionW + refractionVector * vRefractionInfos.z, 1.0)));
  911. vec2 refractionCoords = vRefractionUVW.xy / vRefractionUVW.z;
  912. refractionCoords.y = 1.0 - refractionCoords.y;
  913. #ifdef LODBASEDMICROSFURACE
  914. surfaceRefractionColor = texture2DLodEXT(refraction2DSampler, refractionCoords, lodRefraction).rgb * vRefractionInfos.x;
  915. #else
  916. surfaceRefractionColor = texture2D(refraction2DSampler, refractionCoords, bias).rgb * vRefractionInfos.x;
  917. #endif
  918. surfaceRefractionColor = toLinearSpace(surfaceRefractionColor.rgb);
  919. #endif
  920. #endif
  921. // Reflection
  922. vec3 environmentRadiance = vReflectionColor.rgb;
  923. vec3 environmentIrradiance = vReflectionColor.rgb;
  924. #ifdef REFLECTION
  925. vec3 vReflectionUVW = computeReflectionCoords(vec4(vPositionW, 1.0), normalW);
  926. #ifdef LODBASEDMICROSFURACE
  927. float lodReflection = getMipMapIndexFromAverageSlope(vMicrosurfaceTextureLods.x, alphaG);
  928. #endif
  929. #ifdef REFLECTIONMAP_3D
  930. #ifdef LODBASEDMICROSFURACE
  931. environmentRadiance = textureCubeLodEXT(reflectionCubeSampler, vReflectionUVW, lodReflection).rgb * vReflectionInfos.x;
  932. #else
  933. environmentRadiance = textureCube(reflectionCubeSampler, vReflectionUVW, bias).rgb * vReflectionInfos.x;
  934. #endif
  935. #ifdef PoissonSamplingEnvironment
  936. environmentRadiance = environmentSampler(reflectionCubeSampler, vReflectionUVW, alphaG) * vReflectionInfos.x;
  937. #endif
  938. #ifdef USESPHERICALFROMREFLECTIONMAP
  939. #ifndef REFLECTIONMAP_SKYBOX
  940. vec3 normalEnvironmentSpace = (reflectionMatrix * vec4(normalW, 1)).xyz;
  941. environmentIrradiance = EnvironmentIrradiance(normalEnvironmentSpace);
  942. #endif
  943. #else
  944. environmentRadiance = toLinearSpace(environmentRadiance.rgb);
  945. environmentIrradiance = textureCube(reflectionCubeSampler, normalW, 20.).rgb * vReflectionInfos.x;
  946. environmentIrradiance = toLinearSpace(environmentIrradiance.rgb);
  947. environmentIrradiance *= 0.2; // Hack in case of no hdr cube map use for environment.
  948. #endif
  949. #else
  950. vec2 coords = vReflectionUVW.xy;
  951. #ifdef REFLECTIONMAP_PROJECTION
  952. coords /= vReflectionUVW.z;
  953. #endif
  954. coords.y = 1.0 - coords.y;
  955. #ifdef LODBASEDMICROSFURACE
  956. environmentRadiance = texture2DLodEXT(reflection2DSampler, coords, lodReflection).rgb * vReflectionInfos.x;
  957. #else
  958. environmentRadiance = texture2D(reflection2DSampler, coords, bias).rgb * vReflectionInfos.x;
  959. #endif
  960. environmentRadiance = toLinearSpace(environmentRadiance.rgb);
  961. environmentIrradiance = texture2D(reflection2DSampler, coords, 20.).rgb * vReflectionInfos.x;
  962. environmentIrradiance = toLinearSpace(environmentIrradiance.rgb);
  963. #endif
  964. #endif
  965. #ifdef OVERLOADEDVALUES
  966. environmentIrradiance = mix(environmentIrradiance, vOverloadedReflection, vOverloadedMicroSurface.z);
  967. environmentRadiance = mix(environmentRadiance, vOverloadedReflection, vOverloadedMicroSurface.z);
  968. #endif
  969. environmentRadiance *= vLightingIntensity.z;
  970. environmentIrradiance *= vLightingIntensity.z;
  971. // Compute reflection specular fresnel
  972. vec3 specularEnvironmentR0 = surfaceReflectivityColor.rgb;
  973. vec3 specularEnvironmentR90 = vec3(1.0, 1.0, 1.0);
  974. vec3 specularEnvironmentReflectance = FresnelSchlickEnvironmentGGX(clamp(NdotV, 0., 1.), specularEnvironmentR0, specularEnvironmentR90, sqrt(microSurface));
  975. // Compute refractance
  976. vec3 refractance = vec3(0.0 , 0.0, 0.0);
  977. #ifdef REFRACTION
  978. vec3 transmission = vec3(1.0 , 1.0, 1.0);
  979. #ifdef LINKREFRACTIONTOTRANSPARENCY
  980. // Transmission based on alpha.
  981. transmission *= (1.0 - alpha);
  982. // Tint the material with albedo.
  983. // TODO. PBR Tinting.
  984. vec3 mixedAlbedo = surfaceAlbedoContribution.rgb * surfaceAlbedo.rgb;
  985. float maxChannel = max(max(mixedAlbedo.r, mixedAlbedo.g), mixedAlbedo.b);
  986. vec3 tint = clamp(maxChannel * mixedAlbedo, 0.0, 1.0);
  987. // Decrease Albedo Contribution
  988. surfaceAlbedoContribution *= alpha;
  989. // Decrease irradiance Contribution
  990. environmentIrradiance *= alpha;
  991. // Tint reflectance
  992. surfaceRefractionColor *= tint;
  993. // Put alpha back to 1;
  994. alpha = 1.0;
  995. #endif
  996. // Add Multiple internal bounces.
  997. vec3 bounceSpecularEnvironmentReflectance = (2.0 * specularEnvironmentReflectance) / (1.0 + specularEnvironmentReflectance);
  998. specularEnvironmentReflectance = mix(bounceSpecularEnvironmentReflectance, specularEnvironmentReflectance, alpha);
  999. // In theory T = 1 - R.
  1000. transmission *= 1.0 - specularEnvironmentReflectance;
  1001. // Should baked in diffuse.
  1002. refractance = surfaceRefractionColor * transmission;
  1003. #endif
  1004. // Apply Energy Conservation taking in account the environment level only if the environment is present.
  1005. float reflectance = max(max(surfaceReflectivityColor.r, surfaceReflectivityColor.g), surfaceReflectivityColor.b);
  1006. surfaceAlbedo.rgb = (1. - reflectance) * surfaceAlbedo.rgb;
  1007. refractance *= vLightingIntensity.z;
  1008. environmentRadiance *= specularEnvironmentReflectance;
  1009. // Emissive
  1010. vec3 surfaceEmissiveColor = vEmissiveColor;
  1011. #ifdef EMISSIVE
  1012. vec3 emissiveColorTex = texture2D(emissiveSampler, vEmissiveUV).rgb;
  1013. surfaceEmissiveColor = toLinearSpace(emissiveColorTex.rgb) * surfaceEmissiveColor * vEmissiveInfos.y;
  1014. #endif
  1015. #ifdef OVERLOADEDVALUES
  1016. surfaceEmissiveColor = mix(surfaceEmissiveColor, vOverloadedEmissive, vOverloadedIntensity.w);
  1017. #endif
  1018. #ifdef EMISSIVEFRESNEL
  1019. float emissiveFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, emissiveRightColor.a, emissiveLeftColor.a);
  1020. surfaceEmissiveColor *= emissiveLeftColor.rgb * (1.0 - emissiveFresnelTerm) + emissiveFresnelTerm * emissiveRightColor.rgb;
  1021. #endif
  1022. // Composition
  1023. #ifdef EMISSIVEASILLUMINATION
  1024. vec3 finalDiffuse = max(lightDiffuseContribution * surfaceAlbedoContribution + vAmbientColor, 0.0) * surfaceAlbedo.rgb;
  1025. #ifdef OVERLOADEDSHADOWVALUES
  1026. shadowedOnlyLightDiffuseContribution = max(shadowedOnlyLightDiffuseContribution * surfaceAlbedoContribution + vAmbientColor, 0.0) * surfaceAlbedo.rgb;
  1027. #endif
  1028. #else
  1029. #ifdef LINKEMISSIVEWITHALBEDO
  1030. vec3 finalDiffuse = max((lightDiffuseContribution + surfaceEmissiveColor) * surfaceAlbedoContribution + vAmbientColor, 0.0) * surfaceAlbedo.rgb;
  1031. #ifdef OVERLOADEDSHADOWVALUES
  1032. shadowedOnlyLightDiffuseContribution = max((shadowedOnlyLightDiffuseContribution + surfaceEmissiveColor) * surfaceAlbedoContribution + vAmbientColor, 0.0) * surfaceAlbedo.rgb;
  1033. #endif
  1034. #else
  1035. vec3 finalDiffuse = max(lightDiffuseContribution * surfaceAlbedoContribution + surfaceEmissiveColor + vAmbientColor, 0.0) * surfaceAlbedo.rgb;
  1036. #ifdef OVERLOADEDSHADOWVALUES
  1037. shadowedOnlyLightDiffuseContribution = max(shadowedOnlyLightDiffuseContribution * surfaceAlbedoContribution + surfaceEmissiveColor + vAmbientColor, 0.0) * surfaceAlbedo.rgb;
  1038. #endif
  1039. #endif
  1040. #endif
  1041. #ifdef OVERLOADEDSHADOWVALUES
  1042. finalDiffuse = mix(finalDiffuse, shadowedOnlyLightDiffuseContribution, (1.0 - vOverloadedShadowIntensity.y));
  1043. #endif
  1044. #ifdef SPECULARTERM
  1045. vec3 finalSpecular = lightSpecularContribution * surfaceReflectivityColor;
  1046. #else
  1047. vec3 finalSpecular = vec3(0.0);
  1048. #endif
  1049. #ifdef SPECULAROVERALPHA
  1050. alpha = clamp(alpha + getLuminance(finalSpecular), 0., 1.);
  1051. #endif
  1052. #ifdef RADIANCEOVERALPHA
  1053. alpha = clamp(alpha + getLuminance(environmentRadiance), 0., 1.);
  1054. #endif
  1055. // Composition
  1056. // Reflection already includes the environment intensity.
  1057. #ifdef EMISSIVEASILLUMINATION
  1058. vec4 finalColor = vec4(finalDiffuse * ambientColor * vLightingIntensity.x + surfaceAlbedo.rgb * environmentIrradiance + finalSpecular * vLightingIntensity.x + environmentRadiance + surfaceEmissiveColor * vLightingIntensity.y + refractance, alpha);
  1059. #else
  1060. vec4 finalColor = vec4(finalDiffuse * ambientColor * vLightingIntensity.x + surfaceAlbedo.rgb * environmentIrradiance + finalSpecular * vLightingIntensity.x + environmentRadiance + refractance, alpha);
  1061. #endif
  1062. #ifdef LIGHTMAP
  1063. vec3 lightmapColor = texture2D(lightmapSampler, vLightmapUV).rgb * vLightmapInfos.y;
  1064. #ifdef USELIGHTMAPASSHADOWMAP
  1065. finalColor.rgb *= lightmapColor;
  1066. #else
  1067. finalColor.rgb += lightmapColor;
  1068. #endif
  1069. #endif
  1070. finalColor = max(finalColor, 0.0);
  1071. #ifdef CAMERATONEMAP
  1072. finalColor.rgb = toneMaps(finalColor.rgb);
  1073. #endif
  1074. finalColor.rgb = toGammaSpace(finalColor.rgb);
  1075. #ifdef CAMERACONTRAST
  1076. finalColor = contrasts(finalColor);
  1077. #endif
  1078. // Normal Display.
  1079. // gl_FragColor = vec4(normalW * 0.5 + 0.5, 1.0);
  1080. // Ambient reflection color.
  1081. // gl_FragColor = vec4(ambientReflectionColor, 1.0);
  1082. // Reflection color.
  1083. // gl_FragColor = vec4(reflectionColor, 1.0);
  1084. // Base color.
  1085. // gl_FragColor = vec4(surfaceAlbedo.rgb, 1.0);
  1086. // Specular color.
  1087. // gl_FragColor = vec4(surfaceReflectivityColor.rgb, 1.0);
  1088. // MicroSurface color.
  1089. // gl_FragColor = vec4(microSurface, microSurface, microSurface, 1.0);
  1090. // Specular Map
  1091. // gl_FragColor = vec4(reflectivityMapColor.rgb, 1.0);
  1092. // Refractance
  1093. // gl_FragColor = vec4(refractance.rgb, 1.0);
  1094. //// Emissive Color
  1095. //vec2 test = vEmissiveUV * 0.5 + 0.5;
  1096. //gl_FragColor = vec4(test.x, test.y, 1.0, 1.0);
  1097. #include<logDepthFragment>
  1098. #include<fogFragment>(color, finalColor)
  1099. gl_FragColor = finalColor;
  1100. }