pbr.fragment.fx 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394
  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) {
  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. float 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) {
  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. float 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) {
  592. lightingInfo result;
  593. // Roughness
  594. // Do not touch roughness on hemispheric.
  595. // Diffuse
  596. float ndl = dot(vNormal, lightData.xyz) * 0.5 + 0.5;
  597. result.diffuse = mix(groundColor, diffuseColor, ndl);
  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. float NdotL = max(0.00000000001, ndl);
  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. #ifdef LIGHT0
  700. #ifndef SPECULARTERM
  701. vec3 vLightSpecular0 = vec3(0.0);
  702. #endif
  703. #ifdef SPOTLIGHT0
  704. lightingInfo info = computeSpotLighting(viewDirectionW, normalW, vLightData0, vLightDirection0, vLightDiffuse0.rgb, vLightSpecular0, vLightDiffuse0.a, roughness, NdotV, vLightRadiuses[0]);
  705. #endif
  706. #ifdef HEMILIGHT0
  707. lightingInfo info = computeHemisphericLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightSpecular0, vLightGround0, roughness, NdotV, vLightRadiuses[0]);
  708. #endif
  709. #if defined(POINTLIGHT0) || defined(DIRLIGHT0)
  710. lightingInfo info = computeLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightSpecular0, vLightDiffuse0.a, roughness, NdotV, vLightRadiuses[0]);
  711. #endif
  712. #ifdef SHADOW0
  713. #ifdef SHADOWVSM0
  714. notShadowLevel = computeShadowWithVSM(vPositionFromLight0, shadowSampler0, shadowsInfo0.z, shadowsInfo0.x);
  715. #else
  716. #ifdef SHADOWPCF0
  717. #if defined(POINTLIGHT0)
  718. notShadowLevel = computeShadowWithPCFCube(vLightData0.xyz, shadowSampler0, shadowsInfo0.y, shadowsInfo0.z, shadowsInfo0.x);
  719. #else
  720. notShadowLevel = computeShadowWithPCF(vPositionFromLight0, shadowSampler0, shadowsInfo0.y, shadowsInfo0.z, shadowsInfo0.x);
  721. #endif
  722. #else
  723. #if defined(POINTLIGHT0)
  724. notShadowLevel = computeShadowCube(vLightData0.xyz, shadowSampler0, shadowsInfo0.x, shadowsInfo0.z);
  725. #else
  726. notShadowLevel = computeShadow(vPositionFromLight0, shadowSampler0, shadowsInfo0.x, shadowsInfo0.z);
  727. #endif
  728. #endif
  729. #endif
  730. #else
  731. notShadowLevel = 1.;
  732. #endif
  733. lightDiffuseContribution += info.diffuse * notShadowLevel;
  734. #ifdef OVERLOADEDSHADOWVALUES
  735. shadowedOnlyLightDiffuseContribution *= notShadowLevel;
  736. #endif
  737. #ifdef SPECULARTERM
  738. lightSpecularContribution += info.specular * notShadowLevel;
  739. #endif
  740. #endif
  741. #ifdef LIGHT1
  742. #ifndef SPECULARTERM
  743. vec3 vLightSpecular1 = vec3(0.0);
  744. #endif
  745. #ifdef SPOTLIGHT1
  746. info = computeSpotLighting(viewDirectionW, normalW, vLightData1, vLightDirection1, vLightDiffuse1.rgb, vLightSpecular1, vLightDiffuse1.a, roughness, NdotV, vLightRadiuses[1]);
  747. #endif
  748. #ifdef HEMILIGHT1
  749. info = computeHemisphericLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightSpecular1, vLightGround1, roughness, NdotV, vLightRadiuses[1]);
  750. #endif
  751. #if defined(POINTLIGHT1) || defined(DIRLIGHT1)
  752. info = computeLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightSpecular1, vLightDiffuse1.a, roughness, NdotV, vLightRadiuses[1]);
  753. #endif
  754. #ifdef SHADOW1
  755. #ifdef SHADOWVSM1
  756. notShadowLevel = computeShadowWithVSM(vPositionFromLight1, shadowSampler1, shadowsInfo1.z, shadowsInfo1.x);
  757. #else
  758. #ifdef SHADOWPCF1
  759. #if defined(POINTLIGHT1)
  760. notShadowLevel = computeShadowWithPCFCube(vLightData1.xyz, shadowSampler1, shadowsInfo1.y, shadowsInfo1.z, shadowsInfo1.x);
  761. #else
  762. notShadowLevel = computeShadowWithPCF(vPositionFromLight1, shadowSampler1, shadowsInfo1.y, shadowsInfo1.z, shadowsInfo1.x);
  763. #endif
  764. #else
  765. #if defined(POINTLIGHT1)
  766. notShadowLevel = computeShadowCube(vLightData1.xyz, shadowSampler1, shadowsInfo1.x, shadowsInfo1.z);
  767. #else
  768. notShadowLevel = computeShadow(vPositionFromLight1, shadowSampler1, shadowsInfo1.x, shadowsInfo1.z);
  769. #endif
  770. #endif
  771. #endif
  772. #else
  773. notShadowLevel = 1.;
  774. #endif
  775. lightDiffuseContribution += info.diffuse * notShadowLevel;
  776. #ifdef OVERLOADEDSHADOWVALUES
  777. shadowedOnlyLightDiffuseContribution *= notShadowLevel;
  778. #endif
  779. #ifdef SPECULARTERM
  780. lightSpecularContribution += info.specular * notShadowLevel;
  781. #endif
  782. #endif
  783. #ifdef LIGHT2
  784. #ifndef SPECULARTERM
  785. vec3 vLightSpecular2 = vec3(0.0);
  786. #endif
  787. #ifdef SPOTLIGHT2
  788. info = computeSpotLighting(viewDirectionW, normalW, vLightData2, vLightDirection2, vLightDiffuse2.rgb, vLightSpecular2, vLightDiffuse2.a, roughness, NdotV, vLightRadiuses[2]);
  789. #endif
  790. #ifdef HEMILIGHT2
  791. info = computeHemisphericLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightSpecular2, vLightGround2, roughness, NdotV, vLightRadiuses[2]);
  792. #endif
  793. #if defined(POINTLIGHT2) || defined(DIRLIGHT2)
  794. info = computeLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightSpecular2, vLightDiffuse2.a, roughness, NdotV, vLightRadiuses[2]);
  795. #endif
  796. #ifdef SHADOW2
  797. #ifdef SHADOWVSM2
  798. notShadowLevel = computeShadowWithVSM(vPositionFromLight2, shadowSampler2, shadowsInfo2.z, shadowsInfo2.x);
  799. #else
  800. #ifdef SHADOWPCF2
  801. #if defined(POINTLIGHT2)
  802. notShadowLevel = computeShadowWithPCFCube(vLightData2.xyz, shadowSampler2, shadowsInfo2.y, shadowsInfo2.z, shadowsInfo2.x);
  803. #else
  804. notShadowLevel = computeShadowWithPCF(vPositionFromLight2, shadowSampler2, shadowsInfo2.y, shadowsInfo2.z, shadowsInfo2.x);
  805. #endif
  806. #else
  807. #if defined(POINTLIGHT2)
  808. notShadowLevel = computeShadowCube(vLightData2.xyz, shadowSampler2, shadowsInfo2.x, shadowsInfo2.z);
  809. #else
  810. notShadowLevel = computeShadow(vPositionFromLight2, shadowSampler2, shadowsInfo2.x, shadowsInfo2.z);
  811. #endif
  812. #endif
  813. #endif
  814. #else
  815. notShadowLevel = 1.;
  816. #endif
  817. lightDiffuseContribution += info.diffuse * notShadowLevel;
  818. #ifdef OVERLOADEDSHADOWVALUES
  819. shadowedOnlyLightDiffuseContribution *= notShadowLevel;
  820. #endif
  821. #ifdef SPECULARTERM
  822. lightSpecularContribution += info.specular * notShadowLevel;
  823. #endif
  824. #endif
  825. #ifdef LIGHT3
  826. #ifndef SPECULARTERM
  827. vec3 vLightSpecular3 = vec3(0.0);
  828. #endif
  829. #ifdef SPOTLIGHT3
  830. info = computeSpotLighting(viewDirectionW, normalW, vLightData3, vLightDirection3, vLightDiffuse3.rgb, vLightSpecular3, vLightDiffuse3.a, roughness, NdotV, vLightRadiuses[3]);
  831. #endif
  832. #ifdef HEMILIGHT3
  833. info = computeHemisphericLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightSpecular3, vLightGround3, roughness, NdotV, vLightRadiuses[3]);
  834. #endif
  835. #if defined(POINTLIGHT3) || defined(DIRLIGHT3)
  836. info = computeLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightSpecular3, vLightDiffuse3.a, roughness, NdotV, vLightRadiuses[3]);
  837. #endif
  838. #ifdef SHADOW3
  839. #ifdef SHADOWVSM3
  840. notShadowLevel = computeShadowWithVSM(vPositionFromLight3, shadowSampler3, shadowsInfo3.z, shadowsInfo3.x);
  841. #else
  842. #ifdef SHADOWPCF3
  843. #if defined(POINTLIGHT3)
  844. notShadowLevel = computeShadowWithPCFCube(vLightData3.xyz, shadowSampler3, shadowsInfo3.y, shadowsInfo3.z, shadowsInfo3.x);
  845. #else
  846. notShadowLevel = computeShadowWithPCF(vPositionFromLight3, shadowSampler3, shadowsInfo3.y, shadowsInfo3.z, shadowsInfo3.x);
  847. #endif
  848. #else
  849. #if defined(POINTLIGHT3)
  850. notShadowLevel = computeShadowCube(vLightData3.xyz, shadowSampler3, shadowsInfo3.x, shadowsInfo3.z);
  851. #else
  852. notShadowLevel = computeShadow(vPositionFromLight3, shadowSampler3, shadowsInfo3.x, shadowsInfo3.z);
  853. #endif
  854. #endif
  855. #endif
  856. #else
  857. notShadowLevel = 1.;
  858. #endif
  859. lightDiffuseContribution += info.diffuse * notShadowLevel;
  860. #ifdef OVERLOADEDSHADOWVALUES
  861. shadowedOnlyLightDiffuseContribution *= notShadowLevel;
  862. #endif
  863. #ifdef SPECULARTERM
  864. lightSpecularContribution += info.specular * notShadowLevel;
  865. #endif
  866. #endif
  867. #ifdef SPECULARTERM
  868. lightSpecularContribution *= vLightingIntensity.w;
  869. #endif
  870. #ifdef OPACITY
  871. vec4 opacityMap = texture2D(opacitySampler, vOpacityUV);
  872. #ifdef OPACITYRGB
  873. opacityMap.rgb = opacityMap.rgb * vec3(0.3, 0.59, 0.11);
  874. alpha *= (opacityMap.x + opacityMap.y + opacityMap.z)* vOpacityInfos.y;
  875. #else
  876. alpha *= opacityMap.a * vOpacityInfos.y;
  877. #endif
  878. #endif
  879. #ifdef VERTEXALPHA
  880. alpha *= vColor.a;
  881. #endif
  882. #ifdef OPACITYFRESNEL
  883. float opacityFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, opacityParts.z, opacityParts.w);
  884. alpha += opacityParts.x * (1.0 - opacityFresnelTerm) + opacityFresnelTerm * opacityParts.y;
  885. #endif
  886. // Refraction
  887. vec3 surfaceRefractionColor = vec3(0., 0., 0.);
  888. // Go mat -> blurry reflexion according to microSurface
  889. #ifdef LODBASEDMICROSFURACE
  890. float alphaG = convertRoughnessToAverageSlope(roughness);
  891. #endif
  892. #ifdef REFRACTION
  893. vec3 refractionVector = refract(-viewDirectionW, normalW, vRefractionInfos.y);
  894. #ifdef LODBASEDMICROSFURACE
  895. #ifdef USEPMREMREFRACTION
  896. float lodRefraction = getMipMapIndexFromAverageSlopeWithPMREM(vMicrosurfaceTextureLods.y, alphaG);
  897. #else
  898. float lodRefraction = getMipMapIndexFromAverageSlope(vMicrosurfaceTextureLods.y, alphaG);
  899. #endif
  900. #else
  901. float biasRefraction = (vMicrosurfaceTextureLods.y + 2.) * (1.0 - microSurface);
  902. #endif
  903. #ifdef REFRACTIONMAP_3D
  904. refractionVector.y = refractionVector.y * vRefractionInfos.w;
  905. if (dot(refractionVector, viewDirectionW) < 1.0)
  906. {
  907. #ifdef LODBASEDMICROSFURACE
  908. #ifdef USEPMREMREFRACTION
  909. // Empiric Threshold
  910. if (microSurface > 0.5)
  911. {
  912. // Bend to not reach edges.
  913. float scaleRefraction = 1. - exp2(lodRefraction) / exp2(vMicrosurfaceTextureLods.y); // CubemapSize is the size of the base mipmap
  914. float maxRefraction = max(max(abs(refractionVector.x), abs(refractionVector.y)), abs(refractionVector.z));
  915. if (abs(refractionVector.x) != maxRefraction) refractionVector.x *= scaleRefraction;
  916. if (abs(refractionVector.y) != maxRefraction) refractionVector.y *= scaleRefraction;
  917. if (abs(refractionVector.z) != maxRefraction) refractionVector.z *= scaleRefraction;
  918. }
  919. #endif
  920. surfaceRefractionColor = textureCubeLodEXT(refractionCubeSampler, refractionVector, lodRefraction).rgb * vRefractionInfos.x;
  921. #else
  922. surfaceRefractionColor = textureCube(refractionCubeSampler, refractionVector, biasRefraction).rgb * vRefractionInfos.x;
  923. #endif
  924. }
  925. #ifndef REFRACTIONMAPINLINEARSPACE
  926. surfaceRefractionColor = toLinearSpace(surfaceRefractionColor.rgb);
  927. #endif
  928. #else
  929. vec3 vRefractionUVW = vec3(refractionMatrix * (view * vec4(vPositionW + refractionVector * vRefractionInfos.z, 1.0)));
  930. vec2 refractionCoords = vRefractionUVW.xy / vRefractionUVW.z;
  931. refractionCoords.y = 1.0 - refractionCoords.y;
  932. #ifdef LODBASEDMICROSFURACE
  933. surfaceRefractionColor = texture2DLodEXT(refraction2DSampler, refractionCoords, lodRefraction).rgb * vRefractionInfos.x;
  934. #else
  935. surfaceRefractionColor = texture2D(refraction2DSampler, refractionCoords, biasRefraction).rgb * vRefractionInfos.x;
  936. #endif
  937. surfaceRefractionColor = toLinearSpace(surfaceRefractionColor.rgb);
  938. #endif
  939. #endif
  940. // Reflection
  941. vec3 environmentRadiance = vReflectionColor.rgb;
  942. vec3 environmentIrradiance = vReflectionColor.rgb;
  943. #ifdef REFLECTION
  944. vec3 vReflectionUVW = computeReflectionCoords(vec4(vPositionW, 1.0), normalW);
  945. #ifdef LODBASEDMICROSFURACE
  946. #ifdef USEPMREMREFLECTION
  947. float lodReflection = getMipMapIndexFromAverageSlopeWithPMREM(vMicrosurfaceTextureLods.x, alphaG);
  948. #else
  949. float lodReflection = getMipMapIndexFromAverageSlope(vMicrosurfaceTextureLods.x, alphaG);
  950. #endif
  951. #else
  952. float biasReflection = (vMicrosurfaceTextureLods.x + 2.) * (1.0 - microSurface);
  953. #endif
  954. #ifdef REFLECTIONMAP_3D
  955. #ifdef LODBASEDMICROSFURACE
  956. #ifdef USEPMREMREFLECTION
  957. // Empiric Threshold
  958. if (microSurface > 0.5)
  959. {
  960. // Bend to not reach edges.
  961. float scaleReflection = 1. - exp2(lodReflection) / exp2(vMicrosurfaceTextureLods.x); // CubemapSize is the size of the base mipmap
  962. float maxReflection = max(max(abs(vReflectionUVW.x), abs(vReflectionUVW.y)), abs(vReflectionUVW.z));
  963. if (abs(vReflectionUVW.x) != maxReflection) vReflectionUVW.x *= scaleReflection;
  964. if (abs(vReflectionUVW.y) != maxReflection) vReflectionUVW.y *= scaleReflection;
  965. if (abs(vReflectionUVW.z) != maxReflection) vReflectionUVW.z *= scaleReflection;
  966. }
  967. #endif
  968. environmentRadiance = textureCubeLodEXT(reflectionCubeSampler, vReflectionUVW, lodReflection).rgb * vReflectionInfos.x;
  969. #else
  970. environmentRadiance = textureCube(reflectionCubeSampler, vReflectionUVW, biasReflection).rgb * vReflectionInfos.x;
  971. #endif
  972. #ifdef PoissonSamplingEnvironment
  973. environmentRadiance = environmentSampler(reflectionCubeSampler, vReflectionUVW, alphaG) * vReflectionInfos.x;
  974. #endif
  975. #ifdef USESPHERICALFROMREFLECTIONMAP
  976. #ifndef REFLECTIONMAP_SKYBOX
  977. vec3 normalEnvironmentSpace = (reflectionMatrix * vec4(normalW, 1)).xyz;
  978. environmentIrradiance = EnvironmentIrradiance(normalEnvironmentSpace);
  979. #endif
  980. #else
  981. environmentRadiance = toLinearSpace(environmentRadiance.rgb);
  982. environmentIrradiance = textureCube(reflectionCubeSampler, normalW, 20.).rgb * vReflectionInfos.x;
  983. environmentIrradiance = toLinearSpace(environmentIrradiance.rgb);
  984. environmentIrradiance *= 0.2; // Hack in case of no hdr cube map use for environment.
  985. #endif
  986. #else
  987. vec2 coords = vReflectionUVW.xy;
  988. #ifdef REFLECTIONMAP_PROJECTION
  989. coords /= vReflectionUVW.z;
  990. #endif
  991. coords.y = 1.0 - coords.y;
  992. #ifdef LODBASEDMICROSFURACE
  993. environmentRadiance = texture2DLodEXT(reflection2DSampler, coords, lodReflection).rgb * vReflectionInfos.x;
  994. #else
  995. environmentRadiance = texture2D(reflection2DSampler, coords, biasReflection).rgb * vReflectionInfos.x;
  996. #endif
  997. environmentRadiance = toLinearSpace(environmentRadiance.rgb);
  998. environmentIrradiance = texture2D(reflection2DSampler, coords, 20.).rgb * vReflectionInfos.x;
  999. environmentIrradiance = toLinearSpace(environmentIrradiance.rgb);
  1000. #endif
  1001. #endif
  1002. #ifdef OVERLOADEDVALUES
  1003. environmentIrradiance = mix(environmentIrradiance, vOverloadedReflection, vOverloadedMicroSurface.z);
  1004. environmentRadiance = mix(environmentRadiance, vOverloadedReflection, vOverloadedMicroSurface.z);
  1005. #endif
  1006. environmentRadiance *= vLightingIntensity.z;
  1007. environmentIrradiance *= vLightingIntensity.z;
  1008. // Compute reflection specular fresnel
  1009. vec3 specularEnvironmentR0 = surfaceReflectivityColor.rgb;
  1010. vec3 specularEnvironmentR90 = vec3(1.0, 1.0, 1.0);
  1011. vec3 specularEnvironmentReflectance = FresnelSchlickEnvironmentGGX(clamp(NdotV, 0., 1.), specularEnvironmentR0, specularEnvironmentR90, sqrt(microSurface));
  1012. // Compute refractance
  1013. vec3 refractance = vec3(0.0 , 0.0, 0.0);
  1014. #ifdef REFRACTION
  1015. vec3 transmission = vec3(1.0 , 1.0, 1.0);
  1016. #ifdef LINKREFRACTIONTOTRANSPARENCY
  1017. // Transmission based on alpha.
  1018. transmission *= (1.0 - alpha);
  1019. // Tint the material with albedo.
  1020. // TODO. PBR Tinting.
  1021. vec3 mixedAlbedo = surfaceAlbedoContribution.rgb * surfaceAlbedo.rgb;
  1022. float maxChannel = max(max(mixedAlbedo.r, mixedAlbedo.g), mixedAlbedo.b);
  1023. vec3 tint = clamp(maxChannel * mixedAlbedo, 0.0, 1.0);
  1024. // Decrease Albedo Contribution
  1025. surfaceAlbedoContribution *= alpha;
  1026. // Decrease irradiance Contribution
  1027. environmentIrradiance *= alpha;
  1028. // Tint reflectance
  1029. surfaceRefractionColor *= tint;
  1030. // Put alpha back to 1;
  1031. alpha = 1.0;
  1032. #endif
  1033. // Add Multiple internal bounces.
  1034. vec3 bounceSpecularEnvironmentReflectance = (2.0 * specularEnvironmentReflectance) / (1.0 + specularEnvironmentReflectance);
  1035. specularEnvironmentReflectance = mix(bounceSpecularEnvironmentReflectance, specularEnvironmentReflectance, alpha);
  1036. // In theory T = 1 - R.
  1037. transmission *= 1.0 - specularEnvironmentReflectance;
  1038. // Should baked in diffuse.
  1039. refractance = surfaceRefractionColor * transmission;
  1040. #endif
  1041. // Apply Energy Conservation taking in account the environment level only if the environment is present.
  1042. float reflectance = max(max(surfaceReflectivityColor.r, surfaceReflectivityColor.g), surfaceReflectivityColor.b);
  1043. surfaceAlbedo.rgb = (1. - reflectance) * surfaceAlbedo.rgb;
  1044. refractance *= vLightingIntensity.z;
  1045. environmentRadiance *= specularEnvironmentReflectance;
  1046. // Emissive
  1047. vec3 surfaceEmissiveColor = vEmissiveColor;
  1048. #ifdef EMISSIVE
  1049. vec3 emissiveColorTex = texture2D(emissiveSampler, vEmissiveUV).rgb;
  1050. surfaceEmissiveColor = toLinearSpace(emissiveColorTex.rgb) * surfaceEmissiveColor * vEmissiveInfos.y;
  1051. #endif
  1052. #ifdef OVERLOADEDVALUES
  1053. surfaceEmissiveColor = mix(surfaceEmissiveColor, vOverloadedEmissive, vOverloadedIntensity.w);
  1054. #endif
  1055. #ifdef EMISSIVEFRESNEL
  1056. float emissiveFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, emissiveRightColor.a, emissiveLeftColor.a);
  1057. surfaceEmissiveColor *= emissiveLeftColor.rgb * (1.0 - emissiveFresnelTerm) + emissiveFresnelTerm * emissiveRightColor.rgb;
  1058. #endif
  1059. // Composition
  1060. #ifdef EMISSIVEASILLUMINATION
  1061. vec3 finalDiffuse = max(lightDiffuseContribution * surfaceAlbedoContribution + vAmbientColor, 0.0) * surfaceAlbedo.rgb;
  1062. #ifdef OVERLOADEDSHADOWVALUES
  1063. shadowedOnlyLightDiffuseContribution = max(shadowedOnlyLightDiffuseContribution * surfaceAlbedoContribution + vAmbientColor, 0.0) * surfaceAlbedo.rgb;
  1064. #endif
  1065. #else
  1066. #ifdef LINKEMISSIVEWITHALBEDO
  1067. vec3 finalDiffuse = max((lightDiffuseContribution + surfaceEmissiveColor) * surfaceAlbedoContribution + vAmbientColor, 0.0) * surfaceAlbedo.rgb;
  1068. #ifdef OVERLOADEDSHADOWVALUES
  1069. shadowedOnlyLightDiffuseContribution = max((shadowedOnlyLightDiffuseContribution + surfaceEmissiveColor) * surfaceAlbedoContribution + vAmbientColor, 0.0) * surfaceAlbedo.rgb;
  1070. #endif
  1071. #else
  1072. vec3 finalDiffuse = max(lightDiffuseContribution * surfaceAlbedoContribution + surfaceEmissiveColor + vAmbientColor, 0.0) * surfaceAlbedo.rgb;
  1073. #ifdef OVERLOADEDSHADOWVALUES
  1074. shadowedOnlyLightDiffuseContribution = max(shadowedOnlyLightDiffuseContribution * surfaceAlbedoContribution + surfaceEmissiveColor + vAmbientColor, 0.0) * surfaceAlbedo.rgb;
  1075. #endif
  1076. #endif
  1077. #endif
  1078. #ifdef OVERLOADEDSHADOWVALUES
  1079. finalDiffuse = mix(finalDiffuse, shadowedOnlyLightDiffuseContribution, (1.0 - vOverloadedShadowIntensity.y));
  1080. #endif
  1081. #ifdef SPECULARTERM
  1082. vec3 finalSpecular = lightSpecularContribution * surfaceReflectivityColor;
  1083. #else
  1084. vec3 finalSpecular = vec3(0.0);
  1085. #endif
  1086. #ifdef SPECULAROVERALPHA
  1087. alpha = clamp(alpha + getLuminance(finalSpecular), 0., 1.);
  1088. #endif
  1089. #ifdef RADIANCEOVERALPHA
  1090. alpha = clamp(alpha + getLuminance(environmentRadiance), 0., 1.);
  1091. #endif
  1092. // Composition
  1093. // Reflection already includes the environment intensity.
  1094. #ifdef EMISSIVEASILLUMINATION
  1095. vec4 finalColor = vec4(finalDiffuse * ambientColor * vLightingIntensity.x + surfaceAlbedo.rgb * environmentIrradiance + finalSpecular * vLightingIntensity.x + environmentRadiance + surfaceEmissiveColor * vLightingIntensity.y + refractance, alpha);
  1096. #else
  1097. vec4 finalColor = vec4(finalDiffuse * ambientColor * vLightingIntensity.x + surfaceAlbedo.rgb * environmentIrradiance + finalSpecular * vLightingIntensity.x + environmentRadiance + refractance, alpha);
  1098. #endif
  1099. #ifdef LIGHTMAP
  1100. vec3 lightmapColor = texture2D(lightmapSampler, vLightmapUV).rgb * vLightmapInfos.y;
  1101. #ifdef USELIGHTMAPASSHADOWMAP
  1102. finalColor.rgb *= lightmapColor;
  1103. #else
  1104. finalColor.rgb += lightmapColor;
  1105. #endif
  1106. #endif
  1107. finalColor = max(finalColor, 0.0);
  1108. #ifdef CAMERATONEMAP
  1109. finalColor.rgb = toneMaps(finalColor.rgb);
  1110. #endif
  1111. finalColor.rgb = toGammaSpace(finalColor.rgb);
  1112. #ifdef CAMERACONTRAST
  1113. finalColor = contrasts(finalColor);
  1114. #endif
  1115. // Normal Display.
  1116. // gl_FragColor = vec4(normalW * 0.5 + 0.5, 1.0);
  1117. // Ambient reflection color.
  1118. // gl_FragColor = vec4(ambientReflectionColor, 1.0);
  1119. // Reflection color.
  1120. // gl_FragColor = vec4(reflectionColor, 1.0);
  1121. // Base color.
  1122. // gl_FragColor = vec4(surfaceAlbedo.rgb, 1.0);
  1123. // Specular color.
  1124. // gl_FragColor = vec4(surfaceReflectivityColor.rgb, 1.0);
  1125. // MicroSurface color.
  1126. // gl_FragColor = vec4(microSurface, microSurface, microSurface, 1.0);
  1127. // Specular Map
  1128. // gl_FragColor = vec4(reflectivityMapColor.rgb, 1.0);
  1129. // Refractance
  1130. // gl_FragColor = vec4(refractance.rgb, 1.0);
  1131. //// Emissive Color
  1132. //vec2 test = vEmissiveUV * 0.5 + 0.5;
  1133. //gl_FragColor = vec4(test.x, test.y, 1.0, 1.0);
  1134. #include<logDepthFragment>
  1135. #include<fogFragment>(color, finalColor)
  1136. gl_FragColor = finalColor;
  1137. }