pbr.fragment.fx 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411
  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. #if defined(REFLECTION) || defined(REFRACTION)
  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. float getMipMapIndexFromAverageSlopeWithPMREM(float maxMipLevel, float alphaG)
  150. {
  151. float specularPower = clamp(2. / alphaG - 2., 0.000001, 2048.);
  152. // Based on CubeMapGen for cosine power with 2048 spec default and 0.25 dropoff
  153. return clamp(- 0.5 * log2(specularPower) + 5.5, 0., maxMipLevel);
  154. }
  155. // From Microfacet Models for Refraction through Rough Surfaces, Walter et al. 2007
  156. float smithVisibilityG1_TrowbridgeReitzGGX(float dot, float alphaG)
  157. {
  158. float tanSquared = (1.0 - dot * dot) / (dot * dot);
  159. return 2.0 / (1.0 + sqrt(1.0 + alphaG * alphaG * tanSquared));
  160. }
  161. float smithVisibilityG_TrowbridgeReitzGGX_Walter(float NdotL, float NdotV, float alphaG)
  162. {
  163. return smithVisibilityG1_TrowbridgeReitzGGX(NdotL, alphaG) * smithVisibilityG1_TrowbridgeReitzGGX(NdotV, alphaG);
  164. }
  165. // Trowbridge-Reitz (GGX)
  166. // Generalised Trowbridge-Reitz with gamma power=2.0
  167. float normalDistributionFunction_TrowbridgeReitzGGX(float NdotH, float alphaG)
  168. {
  169. // Note: alphaG is average slope (gradient) of the normals in slope-space.
  170. // It is also the (trigonometric) tangent of the median distribution value, i.e. 50% of normals have
  171. // a tangent (gradient) closer to the macrosurface than this slope.
  172. float a2 = Square(alphaG);
  173. float d = NdotH * NdotH * (a2 - 1.0) + 1.0;
  174. return a2 / (kPi * d * d);
  175. }
  176. vec3 fresnelSchlickGGX(float VdotH, vec3 reflectance0, vec3 reflectance90)
  177. {
  178. return reflectance0 + (reflectance90 - reflectance0) * pow(clamp(1.0 - VdotH, 0., 1.), 5.0);
  179. }
  180. vec3 FresnelSchlickEnvironmentGGX(float VdotN, vec3 reflectance0, vec3 reflectance90, float smoothness)
  181. {
  182. // Schlick fresnel approximation, extended with basic smoothness term so that rough surfaces do not approach reflectance90 at grazing angle
  183. float weight = mix(FRESNEL_MAXIMUM_ON_ROUGH, 1.0, smoothness);
  184. return reflectance0 + weight * (reflectance90 - reflectance0) * pow(clamp(1.0 - VdotN, 0., 1.), 5.0);
  185. }
  186. // Cook Torance Specular computation.
  187. vec3 computeSpecularTerm(float NdotH, float NdotL, float NdotV, float VdotH, float roughness, vec3 specularColor)
  188. {
  189. float alphaG = convertRoughnessToAverageSlope(roughness);
  190. float distribution = normalDistributionFunction_TrowbridgeReitzGGX(NdotH, alphaG);
  191. float visibility = smithVisibilityG_TrowbridgeReitzGGX_Walter(NdotL, NdotV, alphaG);
  192. visibility /= (4.0 * NdotL * NdotV); // Cook Torance Denominator integated in viibility to avoid issues when visibility function changes.
  193. vec3 fresnel = fresnelSchlickGGX(VdotH, specularColor, vec3(1., 1., 1.));
  194. float specTerm = max(0., visibility * distribution) * NdotL;
  195. return fresnel * specTerm * kPi; // TODO: audit pi constants
  196. }
  197. float computeDiffuseTerm(float NdotL, float NdotV, float VdotH, float roughness)
  198. {
  199. // Diffuse fresnel falloff as per Disney principled BRDF, and in the spirit of
  200. // of general coupled diffuse/specular models e.g. Ashikhmin Shirley.
  201. float diffuseFresnelNV = pow(clamp(1.0 - NdotL, 0.000001, 1.), 5.0);
  202. float diffuseFresnelNL = pow(clamp(1.0 - NdotV, 0.000001, 1.), 5.0);
  203. float diffuseFresnel90 = 0.5 + 2.0 * VdotH * VdotH * roughness;
  204. float diffuseFresnelTerm =
  205. (1.0 + (diffuseFresnel90 - 1.0) * diffuseFresnelNL) *
  206. (1.0 + (diffuseFresnel90 - 1.0) * diffuseFresnelNV);
  207. return diffuseFresnelTerm * NdotL;
  208. // PI Test
  209. // diffuseFresnelTerm /= kPi;
  210. }
  211. float adjustRoughnessFromLightProperties(float roughness, float lightRadius, float lightDistance)
  212. {
  213. // At small angle this approximation works.
  214. float lightRoughness = lightRadius / lightDistance;
  215. // Distribution can sum.
  216. float totalRoughness = clamp(lightRoughness + roughness, 0., 1.);
  217. return totalRoughness;
  218. }
  219. float computeDefaultMicroSurface(float microSurface, vec3 reflectivityColor)
  220. {
  221. float kReflectivityNoAlphaWorkflow_SmoothnessMax = 0.95;
  222. float reflectivityLuminance = getLuminance(reflectivityColor);
  223. float reflectivityLuma = sqrt(reflectivityLuminance);
  224. microSurface = reflectivityLuma * kReflectivityNoAlphaWorkflow_SmoothnessMax;
  225. return microSurface;
  226. }
  227. vec3 toLinearSpace(vec3 color)
  228. {
  229. return vec3(pow(color.r, 2.2), pow(color.g, 2.2), pow(color.b, 2.2));
  230. }
  231. vec3 toGammaSpace(vec3 color)
  232. {
  233. return vec3(pow(color.r, 1.0 / 2.2), pow(color.g, 1.0 / 2.2), pow(color.b, 1.0 / 2.2));
  234. }
  235. float computeLightFalloff(vec3 lightOffset, float lightDistanceSquared, float range)
  236. {
  237. #ifdef USEPHYSICALLIGHTFALLOFF
  238. float lightDistanceFalloff = 1.0 / ((lightDistanceSquared + 0.0001));
  239. return lightDistanceFalloff;
  240. #else
  241. float lightFalloff = max(0., 1.0 - length(lightOffset) / range);
  242. return lightFalloff;
  243. #endif
  244. }
  245. #ifdef CAMERATONEMAP
  246. vec3 toneMaps(vec3 color)
  247. {
  248. color = max(color, 0.0);
  249. // TONE MAPPING / EXPOSURE
  250. color.rgb = color.rgb * vCameraInfos.x;
  251. float tuning = 1.5; // TODO: sync up so e.g. 18% greys are matched to exposure appropriately
  252. // PI Test
  253. // tuning *= kPi;
  254. vec3 tonemapped = 1.0 - exp2(-color.rgb * tuning); // simple local photographic tonemapper
  255. color.rgb = mix(color.rgb, tonemapped, 1.0);
  256. return color;
  257. }
  258. #endif
  259. #ifdef CAMERACONTRAST
  260. vec4 contrasts(vec4 color)
  261. {
  262. color = clamp(color, 0.0, 1.0);
  263. vec3 resultHighContrast = color.rgb * color.rgb * (3.0 - 2.0 * color.rgb);
  264. float contrast = vCameraInfos.y;
  265. if (contrast < 1.0)
  266. {
  267. // Decrease contrast: interpolate towards zero-contrast image (flat grey)
  268. color.rgb = mix(vec3(0.5, 0.5, 0.5), color.rgb, contrast);
  269. }
  270. else
  271. {
  272. // Increase contrast: apply simple shoulder-toe high contrast curve
  273. color.rgb = mix(color.rgb, resultHighContrast, contrast - 1.0);
  274. }
  275. return color;
  276. }
  277. #endif
  278. // END PBR HELPER METHODS
  279. uniform vec4 vReflectivityColor;
  280. uniform vec3 vEmissiveColor;
  281. // Input
  282. varying vec3 vPositionW;
  283. #ifdef NORMAL
  284. varying vec3 vNormalW;
  285. #endif
  286. #ifdef VERTEXCOLOR
  287. varying vec4 vColor;
  288. #endif
  289. // Lights
  290. #include<lightFragmentDeclaration>[0]
  291. #include<lightFragmentDeclaration>[1]
  292. #include<lightFragmentDeclaration>[2]
  293. #include<lightFragmentDeclaration>[3]
  294. // Samplers
  295. #ifdef ALBEDO
  296. varying vec2 vAlbedoUV;
  297. uniform sampler2D albedoSampler;
  298. uniform vec2 vAlbedoInfos;
  299. #endif
  300. #ifdef AMBIENT
  301. varying vec2 vAmbientUV;
  302. uniform sampler2D ambientSampler;
  303. uniform vec2 vAmbientInfos;
  304. #endif
  305. #ifdef OPACITY
  306. varying vec2 vOpacityUV;
  307. uniform sampler2D opacitySampler;
  308. uniform vec2 vOpacityInfos;
  309. #endif
  310. #ifdef EMISSIVE
  311. varying vec2 vEmissiveUV;
  312. uniform vec2 vEmissiveInfos;
  313. uniform sampler2D emissiveSampler;
  314. #endif
  315. #ifdef LIGHTMAP
  316. varying vec2 vLightmapUV;
  317. uniform vec2 vLightmapInfos;
  318. uniform sampler2D lightmapSampler;
  319. #endif
  320. #if defined(REFLECTIVITY)
  321. varying vec2 vReflectivityUV;
  322. uniform vec2 vReflectivityInfos;
  323. uniform sampler2D reflectivitySampler;
  324. #endif
  325. // Fresnel
  326. #include<fresnelFunction>
  327. #ifdef OPACITYFRESNEL
  328. uniform vec4 opacityParts;
  329. #endif
  330. #ifdef EMISSIVEFRESNEL
  331. uniform vec4 emissiveLeftColor;
  332. uniform vec4 emissiveRightColor;
  333. #endif
  334. // Refraction Reflection
  335. #if defined(REFLECTIONMAP_SPHERICAL) || defined(REFLECTIONMAP_PROJECTION) || defined(REFRACTION)
  336. uniform mat4 view;
  337. #endif
  338. // Refraction
  339. #ifdef REFRACTION
  340. uniform vec4 vRefractionInfos;
  341. #ifdef REFRACTIONMAP_3D
  342. uniform samplerCube refractionCubeSampler;
  343. #else
  344. uniform sampler2D refraction2DSampler;
  345. uniform mat4 refractionMatrix;
  346. #endif
  347. #endif
  348. // Reflection
  349. #ifdef REFLECTION
  350. uniform vec2 vReflectionInfos;
  351. #ifdef REFLECTIONMAP_3D
  352. uniform samplerCube reflectionCubeSampler;
  353. #else
  354. uniform sampler2D reflection2DSampler;
  355. #endif
  356. #ifdef REFLECTIONMAP_SKYBOX
  357. varying vec3 vPositionUVW;
  358. #else
  359. #ifdef REFLECTIONMAP_EQUIRECTANGULAR_FIXED
  360. varying vec3 vDirectionW;
  361. #endif
  362. #if defined(REFLECTIONMAP_PLANAR) || defined(REFLECTIONMAP_CUBIC) || defined(REFLECTIONMAP_PROJECTION)
  363. uniform mat4 reflectionMatrix;
  364. #endif
  365. #endif
  366. #include<reflectionFunction>
  367. #endif
  368. // Shadows
  369. #ifdef SHADOWS
  370. float unpack(vec4 color)
  371. {
  372. 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);
  373. return dot(color, bit_shift);
  374. }
  375. #if defined(POINTLIGHT0) || defined(POINTLIGHT1) || defined(POINTLIGHT2) || defined(POINTLIGHT3)
  376. uniform vec2 depthValues;
  377. float computeShadowCube(vec3 lightPosition, samplerCube shadowSampler, float darkness, float bias)
  378. {
  379. vec3 directionToLight = vPositionW - lightPosition;
  380. float depth = length(directionToLight);
  381. depth = clamp(depth, 0., 1.0);
  382. directionToLight = normalize(directionToLight);
  383. directionToLight.y = - directionToLight.y;
  384. float shadow = unpack(textureCube(shadowSampler, directionToLight)) + bias;
  385. if (depth > shadow)
  386. {
  387. #ifdef OVERLOADEDSHADOWVALUES
  388. return mix(1.0, darkness, vOverloadedShadowIntensity.x);
  389. #else
  390. return darkness;
  391. #endif
  392. }
  393. return 1.0;
  394. }
  395. float computeShadowWithPCFCube(vec3 lightPosition, samplerCube shadowSampler, float mapSize, float bias, float darkness)
  396. {
  397. vec3 directionToLight = vPositionW - lightPosition;
  398. float depth = length(directionToLight);
  399. depth = (depth - depthValues.x) / (depthValues.y - depthValues.x);
  400. depth = clamp(depth, 0., 1.0);
  401. directionToLight = normalize(directionToLight);
  402. directionToLight.y = -directionToLight.y;
  403. float visibility = 1.;
  404. vec3 poissonDisk[4];
  405. poissonDisk[0] = vec3(-1.0, 1.0, -1.0);
  406. poissonDisk[1] = vec3(1.0, -1.0, -1.0);
  407. poissonDisk[2] = vec3(-1.0, -1.0, -1.0);
  408. poissonDisk[3] = vec3(1.0, -1.0, 1.0);
  409. // Poisson Sampling
  410. float biasedDepth = depth - bias;
  411. if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[0] * mapSize)) < biasedDepth) visibility -= 0.25;
  412. if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[1] * mapSize)) < biasedDepth) visibility -= 0.25;
  413. if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[2] * mapSize)) < biasedDepth) visibility -= 0.25;
  414. if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[3] * mapSize)) < biasedDepth) visibility -= 0.25;
  415. #ifdef OVERLOADEDSHADOWVALUES
  416. return min(1.0, mix(1.0, visibility + darkness, vOverloadedShadowIntensity.x));
  417. #else
  418. return min(1.0, visibility + darkness);
  419. #endif
  420. }
  421. #endif
  422. #if defined(SPOTLIGHT0) || defined(SPOTLIGHT1) || defined(SPOTLIGHT2) || defined(SPOTLIGHT3) || defined(DIRLIGHT0) || defined(DIRLIGHT1) || defined(DIRLIGHT2) || defined(DIRLIGHT3)
  423. float computeShadow(vec4 vPositionFromLight, sampler2D shadowSampler, float darkness, float bias)
  424. {
  425. vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
  426. depth = 0.5 * depth + vec3(0.5);
  427. vec2 uv = depth.xy;
  428. if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0)
  429. {
  430. return 1.0;
  431. }
  432. float shadow = unpack(texture2D(shadowSampler, uv)) + bias;
  433. if (depth.z > shadow)
  434. {
  435. #ifdef OVERLOADEDSHADOWVALUES
  436. return mix(1.0, darkness, vOverloadedShadowIntensity.x);
  437. #else
  438. return darkness;
  439. #endif
  440. }
  441. return 1.;
  442. }
  443. float computeShadowWithPCF(vec4 vPositionFromLight, sampler2D shadowSampler, float mapSize, float bias, float darkness)
  444. {
  445. vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
  446. depth = 0.5 * depth + vec3(0.5);
  447. vec2 uv = depth.xy;
  448. if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0)
  449. {
  450. return 1.0;
  451. }
  452. float visibility = 1.;
  453. vec2 poissonDisk[4];
  454. poissonDisk[0] = vec2(-0.94201624, -0.39906216);
  455. poissonDisk[1] = vec2(0.94558609, -0.76890725);
  456. poissonDisk[2] = vec2(-0.094184101, -0.92938870);
  457. poissonDisk[3] = vec2(0.34495938, 0.29387760);
  458. // Poisson Sampling
  459. float biasedDepth = depth.z - bias;
  460. if (unpack(texture2D(shadowSampler, uv + poissonDisk[0] * mapSize)) < biasedDepth) visibility -= 0.25;
  461. if (unpack(texture2D(shadowSampler, uv + poissonDisk[1] * mapSize)) < biasedDepth) visibility -= 0.25;
  462. if (unpack(texture2D(shadowSampler, uv + poissonDisk[2] * mapSize)) < biasedDepth) visibility -= 0.25;
  463. if (unpack(texture2D(shadowSampler, uv + poissonDisk[3] * mapSize)) < biasedDepth) visibility -= 0.25;
  464. #ifdef OVERLOADEDSHADOWVALUES
  465. return min(1.0, mix(1.0, visibility + darkness, vOverloadedShadowIntensity.x));
  466. #else
  467. return min(1.0, visibility + darkness);
  468. #endif
  469. }
  470. // Thanks to http://devmaster.net/
  471. float unpackHalf(vec2 color)
  472. {
  473. return color.x + (color.y / 255.0);
  474. }
  475. float linstep(float low, float high, float v) {
  476. return clamp((v - low) / (high - low), 0.0, 1.0);
  477. }
  478. float ChebychevInequality(vec2 moments, float compare, float bias)
  479. {
  480. float p = smoothstep(compare - bias, compare, moments.x);
  481. float variance = max(moments.y - moments.x * moments.x, 0.02);
  482. float d = compare - moments.x;
  483. float p_max = linstep(0.2, 1.0, variance / (variance + d * d));
  484. return clamp(max(p, p_max), 0.0, 1.0);
  485. }
  486. float computeShadowWithVSM(vec4 vPositionFromLight, sampler2D shadowSampler, float bias, float darkness)
  487. {
  488. vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
  489. depth = 0.5 * depth + vec3(0.5);
  490. vec2 uv = depth.xy;
  491. if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0 || depth.z >= 1.0)
  492. {
  493. return 1.0;
  494. }
  495. vec4 texel = texture2D(shadowSampler, uv);
  496. vec2 moments = vec2(unpackHalf(texel.xy), unpackHalf(texel.zw));
  497. #ifdef OVERLOADEDSHADOWVALUES
  498. return min(1.0, mix(1.0, 1.0 - ChebychevInequality(moments, depth.z, bias) + darkness, vOverloadedShadowIntensity.x));
  499. #else
  500. return min(1.0, 1.0 - ChebychevInequality(moments, depth.z, bias) + darkness);
  501. #endif
  502. }
  503. #endif
  504. #endif
  505. #include<bumpFragmentFunctions>
  506. #include<clipPlaneFragmentDeclaration>
  507. #include<logDepthDeclaration>
  508. // Fog
  509. #include<fogFragmentDeclaration>
  510. // Light Computing
  511. struct lightingInfo
  512. {
  513. vec3 diffuse;
  514. #ifdef SPECULARTERM
  515. vec3 specular;
  516. #endif
  517. };
  518. lightingInfo computeLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, vec3 specularColor, float range, float roughness, float NdotV, float lightRadius, out float NdotL) {
  519. lightingInfo result;
  520. vec3 lightDirection;
  521. float attenuation = 1.0;
  522. float lightDistance;
  523. // Point
  524. if (lightData.w == 0.)
  525. {
  526. vec3 lightOffset = lightData.xyz - vPositionW;
  527. float lightDistanceSquared = dot(lightOffset, lightOffset);
  528. attenuation = computeLightFalloff(lightOffset, lightDistanceSquared, range);
  529. lightDistance = sqrt(lightDistanceSquared);
  530. lightDirection = normalize(lightOffset);
  531. }
  532. // Directional
  533. else
  534. {
  535. lightDistance = length(-lightData.xyz);
  536. lightDirection = normalize(-lightData.xyz);
  537. }
  538. // Roughness
  539. roughness = adjustRoughnessFromLightProperties(roughness, lightRadius, lightDistance);
  540. // diffuse
  541. vec3 H = normalize(viewDirectionW + lightDirection);
  542. NdotL = max(0.00000000001, dot(vNormal, lightDirection));
  543. float VdotH = clamp(0.00000000001, 1.0, dot(viewDirectionW, H));
  544. float diffuseTerm = computeDiffuseTerm(NdotL, NdotV, VdotH, roughness);
  545. result.diffuse = diffuseTerm * diffuseColor * attenuation;
  546. #ifdef SPECULARTERM
  547. // Specular
  548. float NdotH = max(0.00000000001, dot(vNormal, H));
  549. vec3 specTerm = computeSpecularTerm(NdotH, NdotL, NdotV, VdotH, roughness, specularColor);
  550. result.specular = specTerm * attenuation;
  551. #endif
  552. return result;
  553. }
  554. lightingInfo computeSpotLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec4 lightDirection, vec3 diffuseColor, vec3 specularColor, float range, float roughness, float NdotV, float lightRadius, out float NdotL) {
  555. lightingInfo result;
  556. vec3 lightOffset = lightData.xyz - vPositionW;
  557. vec3 lightVectorW = normalize(lightOffset);
  558. // diffuse
  559. float cosAngle = max(0.000000000000001, dot(-lightDirection.xyz, lightVectorW));
  560. if (cosAngle >= lightDirection.w)
  561. {
  562. cosAngle = max(0., pow(cosAngle, lightData.w));
  563. // Inverse squared falloff.
  564. float lightDistanceSquared = dot(lightOffset, lightOffset);
  565. float attenuation = computeLightFalloff(lightOffset, lightDistanceSquared, range);
  566. // Directional falloff.
  567. attenuation *= cosAngle;
  568. // Roughness.
  569. float lightDistance = sqrt(lightDistanceSquared);
  570. roughness = adjustRoughnessFromLightProperties(roughness, lightRadius, lightDistance);
  571. // Diffuse
  572. vec3 H = normalize(viewDirectionW - lightDirection.xyz);
  573. NdotL = max(0.00000000001, dot(vNormal, -lightDirection.xyz));
  574. float VdotH = clamp(dot(viewDirectionW, H), 0.00000000001, 1.0);
  575. float diffuseTerm = computeDiffuseTerm(NdotL, NdotV, VdotH, roughness);
  576. result.diffuse = diffuseTerm * diffuseColor * attenuation;
  577. #ifdef SPECULARTERM
  578. // Specular
  579. float NdotH = max(0.00000000001, dot(vNormal, H));
  580. vec3 specTerm = computeSpecularTerm(NdotH, NdotL, NdotV, VdotH, roughness, specularColor);
  581. result.specular = specTerm * attenuation;
  582. #endif
  583. return result;
  584. }
  585. result.diffuse = vec3(0.);
  586. #ifdef SPECULARTERM
  587. result.specular = vec3(0.);
  588. #endif
  589. return result;
  590. }
  591. lightingInfo computeHemisphericLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, vec3 specularColor, vec3 groundColor, float roughness, float NdotV, float lightRadius, out float NdotL) {
  592. lightingInfo result;
  593. // Roughness
  594. // Do not touch roughness on hemispheric.
  595. // Diffuse
  596. NdotL = dot(vNormal, lightData.xyz) * 0.5 + 0.5;
  597. result.diffuse = mix(groundColor, diffuseColor, NdotL);
  598. #ifdef SPECULARTERM
  599. // Specular
  600. vec3 lightVectorW = normalize(lightData.xyz);
  601. vec3 H = normalize(viewDirectionW + lightVectorW);
  602. float NdotH = max(0.00000000001, dot(vNormal, H));
  603. NdotL = max(0.00000000001, NdotL);
  604. float VdotH = clamp(0.00000000001, 1.0, dot(viewDirectionW, H));
  605. vec3 specTerm = computeSpecularTerm(NdotH, NdotL, NdotV, VdotH, roughness, specularColor);
  606. result.specular = specTerm;
  607. #endif
  608. return result;
  609. }
  610. void main(void) {
  611. #include<clipPlaneFragment>
  612. #ifdef PoissonSamplingEnvironment
  613. initSamplers();
  614. #endif
  615. vec3 viewDirectionW = normalize(vEyePosition - vPositionW);
  616. // Albedo
  617. vec4 surfaceAlbedo = vec4(1., 1., 1., 1.);
  618. vec3 surfaceAlbedoContribution = vAlbedoColor.rgb;
  619. // Alpha
  620. float alpha = vAlbedoColor.a;
  621. #ifdef ALBEDO
  622. surfaceAlbedo = texture2D(albedoSampler, vAlbedoUV);
  623. surfaceAlbedo = vec4(toLinearSpace(surfaceAlbedo.rgb), surfaceAlbedo.a);
  624. #ifndef LINKREFRACTIONTOTRANSPARENCY
  625. #ifdef ALPHATEST
  626. if (surfaceAlbedo.a < 0.4)
  627. discard;
  628. #endif
  629. #endif
  630. #ifdef ALPHAFROMALBEDO
  631. alpha *= surfaceAlbedo.a;
  632. #endif
  633. surfaceAlbedo.rgb *= vAlbedoInfos.y;
  634. #else
  635. // No Albedo texture.
  636. surfaceAlbedo.rgb = surfaceAlbedoContribution;
  637. surfaceAlbedoContribution = vec3(1., 1., 1.);
  638. #endif
  639. #ifdef VERTEXCOLOR
  640. surfaceAlbedo.rgb *= vColor.rgb;
  641. #endif
  642. #ifdef OVERLOADEDVALUES
  643. surfaceAlbedo.rgb = mix(surfaceAlbedo.rgb, vOverloadedAlbedo, vOverloadedIntensity.y);
  644. #endif
  645. // Bump
  646. #ifdef NORMAL
  647. vec3 normalW = normalize(vNormalW);
  648. #else
  649. vec3 normalW = vec3(1.0, 1.0, 1.0);
  650. #endif
  651. #include<bumpFragment>
  652. // Ambient color
  653. vec3 ambientColor = vec3(1., 1., 1.);
  654. #ifdef AMBIENT
  655. ambientColor = texture2D(ambientSampler, vAmbientUV).rgb * vAmbientInfos.y;
  656. #ifdef OVERLOADEDVALUES
  657. ambientColor.rgb = mix(ambientColor.rgb, vOverloadedAmbient, vOverloadedIntensity.x);
  658. #endif
  659. #endif
  660. // Specular map
  661. float microSurface = vReflectivityColor.a;
  662. vec3 surfaceReflectivityColor = vReflectivityColor.rgb;
  663. #ifdef OVERLOADEDVALUES
  664. surfaceReflectivityColor.rgb = mix(surfaceReflectivityColor.rgb, vOverloadedReflectivity, vOverloadedIntensity.z);
  665. #endif
  666. #ifdef REFLECTIVITY
  667. vec4 surfaceReflectivityColorMap = texture2D(reflectivitySampler, vReflectivityUV);
  668. surfaceReflectivityColor = surfaceReflectivityColorMap.rgb;
  669. surfaceReflectivityColor = toLinearSpace(surfaceReflectivityColor);
  670. #ifdef OVERLOADEDVALUES
  671. surfaceReflectivityColor = mix(surfaceReflectivityColor, vOverloadedReflectivity, vOverloadedIntensity.z);
  672. #endif
  673. #ifdef MICROSURFACEFROMREFLECTIVITYMAP
  674. microSurface = surfaceReflectivityColorMap.a;
  675. #else
  676. #ifdef MICROSURFACEAUTOMATIC
  677. microSurface = computeDefaultMicroSurface(microSurface, surfaceReflectivityColor);
  678. #endif
  679. #endif
  680. #endif
  681. #ifdef OVERLOADEDVALUES
  682. microSurface = mix(microSurface, vOverloadedMicroSurface.x, vOverloadedMicroSurface.y);
  683. #endif
  684. // Compute N dot V.
  685. float NdotV = max(0.00000000001, dot(normalW, viewDirectionW));
  686. // Adapt microSurface.
  687. microSurface = clamp(microSurface, 0., 1.) * 0.98;
  688. // Compute roughness.
  689. float roughness = clamp(1. - microSurface, 0.000001, 1.0);
  690. // Lighting
  691. vec3 lightDiffuseContribution = vec3(0., 0., 0.);
  692. #ifdef OVERLOADEDSHADOWVALUES
  693. vec3 shadowedOnlyLightDiffuseContribution = vec3(1., 1., 1.);
  694. #endif
  695. #ifdef SPECULARTERM
  696. vec3 lightSpecularContribution= vec3(0., 0., 0.);
  697. #endif
  698. float notShadowLevel = 1.; // 1 - shadowLevel
  699. float NdotL = -1.;
  700. #ifdef LIGHT0
  701. #ifndef SPECULARTERM
  702. vec3 vLightSpecular0 = vec3(0.0);
  703. #endif
  704. #ifdef SPOTLIGHT0
  705. lightingInfo info = computeSpotLighting(viewDirectionW, normalW, vLightData0, vLightDirection0, vLightDiffuse0.rgb, vLightSpecular0, vLightDiffuse0.a, roughness, NdotV, vLightRadiuses[0], NdotL);
  706. #endif
  707. #ifdef HEMILIGHT0
  708. lightingInfo info = computeHemisphericLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightSpecular0, vLightGround0, roughness, NdotV, vLightRadiuses[0], NdotL);
  709. #endif
  710. #if defined(POINTLIGHT0) || defined(DIRLIGHT0)
  711. lightingInfo info = computeLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightSpecular0, vLightDiffuse0.a, roughness, NdotV, vLightRadiuses[0], NdotL);
  712. #endif
  713. #ifdef SHADOW0
  714. #ifdef SHADOWVSM0
  715. notShadowLevel = computeShadowWithVSM(vPositionFromLight0, shadowSampler0, shadowsInfo0.z, shadowsInfo0.x);
  716. #else
  717. #ifdef SHADOWPCF0
  718. #if defined(POINTLIGHT0)
  719. notShadowLevel = computeShadowWithPCFCube(vLightData0.xyz, shadowSampler0, shadowsInfo0.y, shadowsInfo0.z, shadowsInfo0.x);
  720. #else
  721. notShadowLevel = computeShadowWithPCF(vPositionFromLight0, shadowSampler0, shadowsInfo0.y, shadowsInfo0.z, shadowsInfo0.x);
  722. #endif
  723. #else
  724. #if defined(POINTLIGHT0)
  725. notShadowLevel = computeShadowCube(vLightData0.xyz, shadowSampler0, shadowsInfo0.x, shadowsInfo0.z);
  726. #else
  727. notShadowLevel = computeShadow(vPositionFromLight0, shadowSampler0, shadowsInfo0.x, shadowsInfo0.z);
  728. #endif
  729. #endif
  730. #endif
  731. #else
  732. notShadowLevel = 1.;
  733. #endif
  734. lightDiffuseContribution += info.diffuse * notShadowLevel;
  735. #ifdef OVERLOADEDSHADOWVALUES
  736. if (NdotL < 0.000000000011)
  737. {
  738. notShadowLevel = 1.;
  739. }
  740. shadowedOnlyLightDiffuseContribution *= notShadowLevel;
  741. #endif
  742. #ifdef SPECULARTERM
  743. lightSpecularContribution += info.specular * notShadowLevel;
  744. #endif
  745. #endif
  746. #ifdef LIGHT1
  747. #ifndef SPECULARTERM
  748. vec3 vLightSpecular1 = vec3(0.0);
  749. #endif
  750. #ifdef SPOTLIGHT1
  751. info = computeSpotLighting(viewDirectionW, normalW, vLightData1, vLightDirection1, vLightDiffuse1.rgb, vLightSpecular1, vLightDiffuse1.a, roughness, NdotV, vLightRadiuses[1], NdotL);
  752. #endif
  753. #ifdef HEMILIGHT1
  754. info = computeHemisphericLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightSpecular1, vLightGround1, roughness, NdotV, vLightRadiuses[1], NdotL);
  755. #endif
  756. #if defined(POINTLIGHT1) || defined(DIRLIGHT1)
  757. info = computeLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightSpecular1, vLightDiffuse1.a, roughness, NdotV, vLightRadiuses[1], NdotL);
  758. #endif
  759. #ifdef SHADOW1
  760. #ifdef SHADOWVSM1
  761. notShadowLevel = computeShadowWithVSM(vPositionFromLight1, shadowSampler1, shadowsInfo1.z, shadowsInfo1.x);
  762. #else
  763. #ifdef SHADOWPCF1
  764. #if defined(POINTLIGHT1)
  765. notShadowLevel = computeShadowWithPCFCube(vLightData1.xyz, shadowSampler1, shadowsInfo1.y, shadowsInfo1.z, shadowsInfo1.x);
  766. #else
  767. notShadowLevel = computeShadowWithPCF(vPositionFromLight1, shadowSampler1, shadowsInfo1.y, shadowsInfo1.z, shadowsInfo1.x);
  768. #endif
  769. #else
  770. #if defined(POINTLIGHT1)
  771. notShadowLevel = computeShadowCube(vLightData1.xyz, shadowSampler1, shadowsInfo1.x, shadowsInfo1.z);
  772. #else
  773. notShadowLevel = computeShadow(vPositionFromLight1, shadowSampler1, shadowsInfo1.x, shadowsInfo1.z);
  774. #endif
  775. #endif
  776. #endif
  777. #else
  778. notShadowLevel = 1.;
  779. #endif
  780. lightDiffuseContribution += info.diffuse * notShadowLevel;
  781. #ifdef OVERLOADEDSHADOWVALUES
  782. if (NdotL < 0.000000000011)
  783. {
  784. notShadowLevel = 1.;
  785. }
  786. shadowedOnlyLightDiffuseContribution *= notShadowLevel;
  787. #endif
  788. #ifdef SPECULARTERM
  789. lightSpecularContribution += info.specular * notShadowLevel;
  790. #endif
  791. #endif
  792. #ifdef LIGHT2
  793. #ifndef SPECULARTERM
  794. vec3 vLightSpecular2 = vec3(0.0);
  795. #endif
  796. #ifdef SPOTLIGHT2
  797. info = computeSpotLighting(viewDirectionW, normalW, vLightData2, vLightDirection2, vLightDiffuse2.rgb, vLightSpecular2, vLightDiffuse2.a, roughness, NdotV, vLightRadiuses[2], NdotL);
  798. #endif
  799. #ifdef HEMILIGHT2
  800. info = computeHemisphericLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightSpecular2, vLightGround2, roughness, NdotV, vLightRadiuses[2], NdotL);
  801. #endif
  802. #if defined(POINTLIGHT2) || defined(DIRLIGHT2)
  803. info = computeLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightSpecular2, vLightDiffuse2.a, roughness, NdotV, vLightRadiuses[2], NdotL);
  804. #endif
  805. #ifdef SHADOW2
  806. #ifdef SHADOWVSM2
  807. notShadowLevel = computeShadowWithVSM(vPositionFromLight2, shadowSampler2, shadowsInfo2.z, shadowsInfo2.x);
  808. #else
  809. #ifdef SHADOWPCF2
  810. #if defined(POINTLIGHT2)
  811. notShadowLevel = computeShadowWithPCFCube(vLightData2.xyz, shadowSampler2, shadowsInfo2.y, shadowsInfo2.z, shadowsInfo2.x);
  812. #else
  813. notShadowLevel = computeShadowWithPCF(vPositionFromLight2, shadowSampler2, shadowsInfo2.y, shadowsInfo2.z, shadowsInfo2.x);
  814. #endif
  815. #else
  816. #if defined(POINTLIGHT2)
  817. notShadowLevel = computeShadowCube(vLightData2.xyz, shadowSampler2, shadowsInfo2.x, shadowsInfo2.z);
  818. #else
  819. notShadowLevel = computeShadow(vPositionFromLight2, shadowSampler2, shadowsInfo2.x, shadowsInfo2.z);
  820. #endif
  821. #endif
  822. #endif
  823. #else
  824. notShadowLevel = 1.;
  825. #endif
  826. lightDiffuseContribution += info.diffuse * notShadowLevel;
  827. #ifdef OVERLOADEDSHADOWVALUES
  828. if (NdotL < 0.000000000011)
  829. {
  830. notShadowLevel = 1.;
  831. }
  832. shadowedOnlyLightDiffuseContribution *= notShadowLevel;
  833. #endif
  834. #ifdef SPECULARTERM
  835. lightSpecularContribution += info.specular * notShadowLevel;
  836. #endif
  837. #endif
  838. #ifdef LIGHT3
  839. #ifndef SPECULARTERM
  840. vec3 vLightSpecular3 = vec3(0.0);
  841. #endif
  842. #ifdef SPOTLIGHT3
  843. info = computeSpotLighting(viewDirectionW, normalW, vLightData3, vLightDirection3, vLightDiffuse3.rgb, vLightSpecular3, vLightDiffuse3.a, roughness, NdotV, vLightRadiuses[3], NdotL);
  844. #endif
  845. #ifdef HEMILIGHT3
  846. info = computeHemisphericLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightSpecular3, vLightGround3, roughness, NdotV, vLightRadiuses[3], NdotL);
  847. #endif
  848. #if defined(POINTLIGHT3) || defined(DIRLIGHT3)
  849. info = computeLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightSpecular3, vLightDiffuse3.a, roughness, NdotV, vLightRadiuses[3], NdotL);
  850. #endif
  851. #ifdef SHADOW3
  852. #ifdef SHADOWVSM3
  853. notShadowLevel = computeShadowWithVSM(vPositionFromLight3, shadowSampler3, shadowsInfo3.z, shadowsInfo3.x);
  854. #else
  855. #ifdef SHADOWPCF3
  856. #if defined(POINTLIGHT3)
  857. notShadowLevel = computeShadowWithPCFCube(vLightData3.xyz, shadowSampler3, shadowsInfo3.y, shadowsInfo3.z, shadowsInfo3.x);
  858. #else
  859. notShadowLevel = computeShadowWithPCF(vPositionFromLight3, shadowSampler3, shadowsInfo3.y, shadowsInfo3.z, shadowsInfo3.x);
  860. #endif
  861. #else
  862. #if defined(POINTLIGHT3)
  863. notShadowLevel = computeShadowCube(vLightData3.xyz, shadowSampler3, shadowsInfo3.x, shadowsInfo3.z);
  864. #else
  865. notShadowLevel = computeShadow(vPositionFromLight3, shadowSampler3, shadowsInfo3.x, shadowsInfo3.z);
  866. #endif
  867. #endif
  868. #endif
  869. #else
  870. notShadowLevel = 1.;
  871. #endif
  872. lightDiffuseContribution += info.diffuse * notShadowLevel;
  873. #ifdef OVERLOADEDSHADOWVALUES
  874. if (NdotL < 0.000000000011)
  875. {
  876. notShadowLevel = 1.;
  877. }
  878. shadowedOnlyLightDiffuseContribution *= notShadowLevel;
  879. #endif
  880. #ifdef SPECULARTERM
  881. lightSpecularContribution += info.specular * notShadowLevel;
  882. #endif
  883. #endif
  884. #ifdef SPECULARTERM
  885. lightSpecularContribution *= vLightingIntensity.w;
  886. #endif
  887. #ifdef OPACITY
  888. vec4 opacityMap = texture2D(opacitySampler, vOpacityUV);
  889. #ifdef OPACITYRGB
  890. opacityMap.rgb = opacityMap.rgb * vec3(0.3, 0.59, 0.11);
  891. alpha *= (opacityMap.x + opacityMap.y + opacityMap.z)* vOpacityInfos.y;
  892. #else
  893. alpha *= opacityMap.a * vOpacityInfos.y;
  894. #endif
  895. #endif
  896. #ifdef VERTEXALPHA
  897. alpha *= vColor.a;
  898. #endif
  899. #ifdef OPACITYFRESNEL
  900. float opacityFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, opacityParts.z, opacityParts.w);
  901. alpha += opacityParts.x * (1.0 - opacityFresnelTerm) + opacityFresnelTerm * opacityParts.y;
  902. #endif
  903. // Refraction
  904. vec3 surfaceRefractionColor = vec3(0., 0., 0.);
  905. // Go mat -> blurry reflexion according to microSurface
  906. #ifdef LODBASEDMICROSFURACE
  907. float alphaG = convertRoughnessToAverageSlope(roughness);
  908. #endif
  909. #ifdef REFRACTION
  910. vec3 refractionVector = refract(-viewDirectionW, normalW, vRefractionInfos.y);
  911. #ifdef LODBASEDMICROSFURACE
  912. #ifdef USEPMREMREFRACTION
  913. float lodRefraction = getMipMapIndexFromAverageSlopeWithPMREM(vMicrosurfaceTextureLods.y, alphaG);
  914. #else
  915. float lodRefraction = getMipMapIndexFromAverageSlope(vMicrosurfaceTextureLods.y, alphaG);
  916. #endif
  917. #else
  918. float biasRefraction = (vMicrosurfaceTextureLods.y + 2.) * (1.0 - microSurface);
  919. #endif
  920. #ifdef REFRACTIONMAP_3D
  921. refractionVector.y = refractionVector.y * vRefractionInfos.w;
  922. if (dot(refractionVector, viewDirectionW) < 1.0)
  923. {
  924. #ifdef LODBASEDMICROSFURACE
  925. #ifdef USEPMREMREFRACTION
  926. // Empiric Threshold
  927. if (microSurface > 0.5)
  928. {
  929. // Bend to not reach edges.
  930. float scaleRefraction = 1. - exp2(lodRefraction) / exp2(vMicrosurfaceTextureLods.y); // CubemapSize is the size of the base mipmap
  931. float maxRefraction = max(max(abs(refractionVector.x), abs(refractionVector.y)), abs(refractionVector.z));
  932. if (abs(refractionVector.x) != maxRefraction) refractionVector.x *= scaleRefraction;
  933. if (abs(refractionVector.y) != maxRefraction) refractionVector.y *= scaleRefraction;
  934. if (abs(refractionVector.z) != maxRefraction) refractionVector.z *= scaleRefraction;
  935. }
  936. #endif
  937. surfaceRefractionColor = textureCubeLodEXT(refractionCubeSampler, refractionVector, lodRefraction).rgb * vRefractionInfos.x;
  938. #else
  939. surfaceRefractionColor = textureCube(refractionCubeSampler, refractionVector, biasRefraction).rgb * vRefractionInfos.x;
  940. #endif
  941. }
  942. #ifndef REFRACTIONMAPINLINEARSPACE
  943. surfaceRefractionColor = toLinearSpace(surfaceRefractionColor.rgb);
  944. #endif
  945. #else
  946. vec3 vRefractionUVW = vec3(refractionMatrix * (view * vec4(vPositionW + refractionVector * vRefractionInfos.z, 1.0)));
  947. vec2 refractionCoords = vRefractionUVW.xy / vRefractionUVW.z;
  948. refractionCoords.y = 1.0 - refractionCoords.y;
  949. #ifdef LODBASEDMICROSFURACE
  950. surfaceRefractionColor = texture2DLodEXT(refraction2DSampler, refractionCoords, lodRefraction).rgb * vRefractionInfos.x;
  951. #else
  952. surfaceRefractionColor = texture2D(refraction2DSampler, refractionCoords, biasRefraction).rgb * vRefractionInfos.x;
  953. #endif
  954. surfaceRefractionColor = toLinearSpace(surfaceRefractionColor.rgb);
  955. #endif
  956. #endif
  957. // Reflection
  958. vec3 environmentRadiance = vReflectionColor.rgb;
  959. vec3 environmentIrradiance = vReflectionColor.rgb;
  960. #ifdef REFLECTION
  961. vec3 vReflectionUVW = computeReflectionCoords(vec4(vPositionW, 1.0), normalW);
  962. #ifdef LODBASEDMICROSFURACE
  963. #ifdef USEPMREMREFLECTION
  964. float lodReflection = getMipMapIndexFromAverageSlopeWithPMREM(vMicrosurfaceTextureLods.x, alphaG);
  965. #else
  966. float lodReflection = getMipMapIndexFromAverageSlope(vMicrosurfaceTextureLods.x, alphaG);
  967. #endif
  968. #else
  969. float biasReflection = (vMicrosurfaceTextureLods.x + 2.) * (1.0 - microSurface);
  970. #endif
  971. #ifdef REFLECTIONMAP_3D
  972. #ifdef LODBASEDMICROSFURACE
  973. #ifdef USEPMREMREFLECTION
  974. // Empiric Threshold
  975. if (microSurface > 0.5)
  976. {
  977. // Bend to not reach edges.
  978. float scaleReflection = 1. - exp2(lodReflection) / exp2(vMicrosurfaceTextureLods.x); // CubemapSize is the size of the base mipmap
  979. float maxReflection = max(max(abs(vReflectionUVW.x), abs(vReflectionUVW.y)), abs(vReflectionUVW.z));
  980. if (abs(vReflectionUVW.x) != maxReflection) vReflectionUVW.x *= scaleReflection;
  981. if (abs(vReflectionUVW.y) != maxReflection) vReflectionUVW.y *= scaleReflection;
  982. if (abs(vReflectionUVW.z) != maxReflection) vReflectionUVW.z *= scaleReflection;
  983. }
  984. #endif
  985. environmentRadiance = textureCubeLodEXT(reflectionCubeSampler, vReflectionUVW, lodReflection).rgb * vReflectionInfos.x;
  986. #else
  987. environmentRadiance = textureCube(reflectionCubeSampler, vReflectionUVW, biasReflection).rgb * vReflectionInfos.x;
  988. #endif
  989. #ifdef PoissonSamplingEnvironment
  990. environmentRadiance = environmentSampler(reflectionCubeSampler, vReflectionUVW, alphaG) * vReflectionInfos.x;
  991. #endif
  992. #ifdef USESPHERICALFROMREFLECTIONMAP
  993. #ifndef REFLECTIONMAP_SKYBOX
  994. vec3 normalEnvironmentSpace = (reflectionMatrix * vec4(normalW, 1)).xyz;
  995. environmentIrradiance = EnvironmentIrradiance(normalEnvironmentSpace);
  996. #endif
  997. #else
  998. environmentRadiance = toLinearSpace(environmentRadiance.rgb);
  999. environmentIrradiance = textureCube(reflectionCubeSampler, normalW, 20.).rgb * vReflectionInfos.x;
  1000. environmentIrradiance = toLinearSpace(environmentIrradiance.rgb);
  1001. environmentIrradiance *= 0.2; // Hack in case of no hdr cube map use for environment.
  1002. #endif
  1003. #else
  1004. vec2 coords = vReflectionUVW.xy;
  1005. #ifdef REFLECTIONMAP_PROJECTION
  1006. coords /= vReflectionUVW.z;
  1007. #endif
  1008. coords.y = 1.0 - coords.y;
  1009. #ifdef LODBASEDMICROSFURACE
  1010. environmentRadiance = texture2DLodEXT(reflection2DSampler, coords, lodReflection).rgb * vReflectionInfos.x;
  1011. #else
  1012. environmentRadiance = texture2D(reflection2DSampler, coords, biasReflection).rgb * vReflectionInfos.x;
  1013. #endif
  1014. environmentRadiance = toLinearSpace(environmentRadiance.rgb);
  1015. environmentIrradiance = texture2D(reflection2DSampler, coords, 20.).rgb * vReflectionInfos.x;
  1016. environmentIrradiance = toLinearSpace(environmentIrradiance.rgb);
  1017. #endif
  1018. #endif
  1019. #ifdef OVERLOADEDVALUES
  1020. environmentIrradiance = mix(environmentIrradiance, vOverloadedReflection, vOverloadedMicroSurface.z);
  1021. environmentRadiance = mix(environmentRadiance, vOverloadedReflection, vOverloadedMicroSurface.z);
  1022. #endif
  1023. environmentRadiance *= vLightingIntensity.z;
  1024. environmentIrradiance *= vLightingIntensity.z;
  1025. // Compute reflection specular fresnel
  1026. vec3 specularEnvironmentR0 = surfaceReflectivityColor.rgb;
  1027. vec3 specularEnvironmentR90 = vec3(1.0, 1.0, 1.0);
  1028. vec3 specularEnvironmentReflectance = FresnelSchlickEnvironmentGGX(clamp(NdotV, 0., 1.), specularEnvironmentR0, specularEnvironmentR90, sqrt(microSurface));
  1029. // Compute refractance
  1030. vec3 refractance = vec3(0.0 , 0.0, 0.0);
  1031. #ifdef REFRACTION
  1032. vec3 transmission = vec3(1.0 , 1.0, 1.0);
  1033. #ifdef LINKREFRACTIONTOTRANSPARENCY
  1034. // Transmission based on alpha.
  1035. transmission *= (1.0 - alpha);
  1036. // Tint the material with albedo.
  1037. // TODO. PBR Tinting.
  1038. vec3 mixedAlbedo = surfaceAlbedoContribution.rgb * surfaceAlbedo.rgb;
  1039. float maxChannel = max(max(mixedAlbedo.r, mixedAlbedo.g), mixedAlbedo.b);
  1040. vec3 tint = clamp(maxChannel * mixedAlbedo, 0.0, 1.0);
  1041. // Decrease Albedo Contribution
  1042. surfaceAlbedoContribution *= alpha;
  1043. // Decrease irradiance Contribution
  1044. environmentIrradiance *= alpha;
  1045. // Tint reflectance
  1046. surfaceRefractionColor *= tint;
  1047. // Put alpha back to 1;
  1048. alpha = 1.0;
  1049. #endif
  1050. // Add Multiple internal bounces.
  1051. vec3 bounceSpecularEnvironmentReflectance = (2.0 * specularEnvironmentReflectance) / (1.0 + specularEnvironmentReflectance);
  1052. specularEnvironmentReflectance = mix(bounceSpecularEnvironmentReflectance, specularEnvironmentReflectance, alpha);
  1053. // In theory T = 1 - R.
  1054. transmission *= 1.0 - specularEnvironmentReflectance;
  1055. // Should baked in diffuse.
  1056. refractance = surfaceRefractionColor * transmission;
  1057. #endif
  1058. // Apply Energy Conservation taking in account the environment level only if the environment is present.
  1059. float reflectance = max(max(surfaceReflectivityColor.r, surfaceReflectivityColor.g), surfaceReflectivityColor.b);
  1060. surfaceAlbedo.rgb = (1. - reflectance) * surfaceAlbedo.rgb;
  1061. refractance *= vLightingIntensity.z;
  1062. environmentRadiance *= specularEnvironmentReflectance;
  1063. // Emissive
  1064. vec3 surfaceEmissiveColor = vEmissiveColor;
  1065. #ifdef EMISSIVE
  1066. vec3 emissiveColorTex = texture2D(emissiveSampler, vEmissiveUV).rgb;
  1067. surfaceEmissiveColor = toLinearSpace(emissiveColorTex.rgb) * surfaceEmissiveColor * vEmissiveInfos.y;
  1068. #endif
  1069. #ifdef OVERLOADEDVALUES
  1070. surfaceEmissiveColor = mix(surfaceEmissiveColor, vOverloadedEmissive, vOverloadedIntensity.w);
  1071. #endif
  1072. #ifdef EMISSIVEFRESNEL
  1073. float emissiveFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, emissiveRightColor.a, emissiveLeftColor.a);
  1074. surfaceEmissiveColor *= emissiveLeftColor.rgb * (1.0 - emissiveFresnelTerm) + emissiveFresnelTerm * emissiveRightColor.rgb;
  1075. #endif
  1076. // Composition
  1077. #ifdef EMISSIVEASILLUMINATION
  1078. vec3 finalDiffuse = max(lightDiffuseContribution * surfaceAlbedoContribution + vAmbientColor, 0.0) * surfaceAlbedo.rgb;
  1079. #ifdef OVERLOADEDSHADOWVALUES
  1080. shadowedOnlyLightDiffuseContribution = max(shadowedOnlyLightDiffuseContribution * surfaceAlbedoContribution + vAmbientColor, 0.0) * surfaceAlbedo.rgb;
  1081. #endif
  1082. #else
  1083. #ifdef LINKEMISSIVEWITHALBEDO
  1084. vec3 finalDiffuse = max((lightDiffuseContribution + surfaceEmissiveColor) * surfaceAlbedoContribution + vAmbientColor, 0.0) * surfaceAlbedo.rgb;
  1085. #ifdef OVERLOADEDSHADOWVALUES
  1086. shadowedOnlyLightDiffuseContribution = max((shadowedOnlyLightDiffuseContribution + surfaceEmissiveColor) * surfaceAlbedoContribution + vAmbientColor, 0.0) * surfaceAlbedo.rgb;
  1087. #endif
  1088. #else
  1089. vec3 finalDiffuse = max(lightDiffuseContribution * surfaceAlbedoContribution + surfaceEmissiveColor + vAmbientColor, 0.0) * surfaceAlbedo.rgb;
  1090. #ifdef OVERLOADEDSHADOWVALUES
  1091. shadowedOnlyLightDiffuseContribution = max(shadowedOnlyLightDiffuseContribution * surfaceAlbedoContribution + surfaceEmissiveColor + vAmbientColor, 0.0) * surfaceAlbedo.rgb;
  1092. #endif
  1093. #endif
  1094. #endif
  1095. #ifdef OVERLOADEDSHADOWVALUES
  1096. finalDiffuse = mix(finalDiffuse, shadowedOnlyLightDiffuseContribution, (1.0 - vOverloadedShadowIntensity.y));
  1097. #endif
  1098. #ifdef SPECULARTERM
  1099. vec3 finalSpecular = lightSpecularContribution * surfaceReflectivityColor;
  1100. #else
  1101. vec3 finalSpecular = vec3(0.0);
  1102. #endif
  1103. #ifdef SPECULAROVERALPHA
  1104. alpha = clamp(alpha + getLuminance(finalSpecular), 0., 1.);
  1105. #endif
  1106. #ifdef RADIANCEOVERALPHA
  1107. alpha = clamp(alpha + getLuminance(environmentRadiance), 0., 1.);
  1108. #endif
  1109. // Composition
  1110. // Reflection already includes the environment intensity.
  1111. #ifdef EMISSIVEASILLUMINATION
  1112. vec4 finalColor = vec4(finalDiffuse * ambientColor * vLightingIntensity.x + surfaceAlbedo.rgb * environmentIrradiance + finalSpecular * vLightingIntensity.x + environmentRadiance + surfaceEmissiveColor * vLightingIntensity.y + refractance, alpha);
  1113. #else
  1114. vec4 finalColor = vec4(finalDiffuse * ambientColor * vLightingIntensity.x + surfaceAlbedo.rgb * environmentIrradiance + finalSpecular * vLightingIntensity.x + environmentRadiance + refractance, alpha);
  1115. #endif
  1116. #ifdef LIGHTMAP
  1117. vec3 lightmapColor = texture2D(lightmapSampler, vLightmapUV).rgb * vLightmapInfos.y;
  1118. #ifdef USELIGHTMAPASSHADOWMAP
  1119. finalColor.rgb *= lightmapColor;
  1120. #else
  1121. finalColor.rgb += lightmapColor;
  1122. #endif
  1123. #endif
  1124. finalColor = max(finalColor, 0.0);
  1125. #ifdef CAMERATONEMAP
  1126. finalColor.rgb = toneMaps(finalColor.rgb);
  1127. #endif
  1128. finalColor.rgb = toGammaSpace(finalColor.rgb);
  1129. #ifdef CAMERACONTRAST
  1130. finalColor = contrasts(finalColor);
  1131. #endif
  1132. // Normal Display.
  1133. // gl_FragColor = vec4(normalW * 0.5 + 0.5, 1.0);
  1134. // Ambient reflection color.
  1135. // gl_FragColor = vec4(ambientReflectionColor, 1.0);
  1136. // Reflection color.
  1137. // gl_FragColor = vec4(reflectionColor, 1.0);
  1138. // Base color.
  1139. // gl_FragColor = vec4(surfaceAlbedo.rgb, 1.0);
  1140. // Specular color.
  1141. // gl_FragColor = vec4(surfaceReflectivityColor.rgb, 1.0);
  1142. // MicroSurface color.
  1143. // gl_FragColor = vec4(microSurface, microSurface, microSurface, 1.0);
  1144. // Specular Map
  1145. // gl_FragColor = vec4(reflectivityMapColor.rgb, 1.0);
  1146. // Refractance
  1147. // gl_FragColor = vec4(refractance.rgb, 1.0);
  1148. //// Emissive Color
  1149. //vec2 test = vEmissiveUV * 0.5 + 0.5;
  1150. //gl_FragColor = vec4(test.x, test.y, 1.0, 1.0);
  1151. #include<logDepthFragment>
  1152. #include<fogFragment>(color, finalColor)
  1153. gl_FragColor = finalColor;
  1154. }