pbr.fragment.fx 46 KB

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