pbr.fragment.fx 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412
  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. #ifdef FRESNEL
  311. float computeFresnelTerm(vec3 viewDirection, vec3 worldNormal, float bias, float power)
  312. {
  313. float fresnelTerm = pow(bias + abs(dot(viewDirection, worldNormal)), power);
  314. return clamp(fresnelTerm, 0., 1.);
  315. }
  316. #endif
  317. #ifdef OPACITYFRESNEL
  318. uniform vec4 opacityParts;
  319. #endif
  320. #ifdef EMISSIVEFRESNEL
  321. uniform vec4 emissiveLeftColor;
  322. uniform vec4 emissiveRightColor;
  323. #endif
  324. // Refraction Reflection
  325. #if defined(REFLECTIONMAP_SPHERICAL) || defined(REFLECTIONMAP_PROJECTION) || defined(REFRACTION)
  326. uniform mat4 view;
  327. #endif
  328. // Refraction
  329. #ifdef REFRACTION
  330. uniform vec4 vRefractionInfos;
  331. #ifdef REFRACTIONMAP_3D
  332. uniform samplerCube refractionCubeSampler;
  333. #else
  334. uniform sampler2D refraction2DSampler;
  335. uniform mat4 refractionMatrix;
  336. #endif
  337. #endif
  338. // Reflection
  339. #ifdef REFLECTION
  340. uniform vec2 vReflectionInfos;
  341. #ifdef REFLECTIONMAP_3D
  342. uniform samplerCube reflectionCubeSampler;
  343. #else
  344. uniform sampler2D reflection2DSampler;
  345. #endif
  346. #ifdef REFLECTIONMAP_SKYBOX
  347. varying vec3 vPositionUVW;
  348. #else
  349. #ifdef REFLECTIONMAP_EQUIRECTANGULAR_FIXED
  350. varying vec3 vDirectionW;
  351. #endif
  352. #if defined(REFLECTIONMAP_PLANAR) || defined(REFLECTIONMAP_CUBIC) || defined(REFLECTIONMAP_PROJECTION)
  353. uniform mat4 reflectionMatrix;
  354. #endif
  355. #endif
  356. vec3 computeReflectionCoords(vec4 worldPos, vec3 worldNormal)
  357. {
  358. #ifdef REFLECTIONMAP_EQUIRECTANGULAR_FIXED
  359. vec3 direction = normalize(vDirectionW);
  360. float t = clamp(direction.y * -0.5 + 0.5, 0., 1.0);
  361. float s = atan(direction.z, direction.x) * RECIPROCAL_PI2 + 0.5;
  362. return vec3(s, t, 0);
  363. #endif
  364. #ifdef REFLECTIONMAP_EQUIRECTANGULAR
  365. vec3 cameraToVertex = normalize(worldPos.xyz - vEyePosition);
  366. vec3 r = reflect(cameraToVertex, worldNormal);
  367. float t = clamp(r.y * -0.5 + 0.5, 0., 1.0);
  368. float s = atan(r.z, r.x) * RECIPROCAL_PI2 + 0.5;
  369. return vec3(s, t, 0);
  370. #endif
  371. #ifdef REFLECTIONMAP_SPHERICAL
  372. vec3 viewDir = normalize(vec3(view * worldPos));
  373. vec3 viewNormal = normalize(vec3(view * vec4(worldNormal, 0.0)));
  374. vec3 r = reflect(viewDir, viewNormal);
  375. r.z = r.z - 1.0;
  376. float m = 2.0 * length(r);
  377. return vec3(r.x / m + 0.5, 1.0 - r.y / m - 0.5, 0);
  378. #endif
  379. #ifdef REFLECTIONMAP_PLANAR
  380. vec3 viewDir = worldPos.xyz - vEyePosition;
  381. vec3 coords = normalize(reflect(viewDir, worldNormal));
  382. return vec3(reflectionMatrix * vec4(coords, 1));
  383. #endif
  384. #ifdef REFLECTIONMAP_CUBIC
  385. vec3 viewDir = worldPos.xyz - vEyePosition;
  386. vec3 coords = reflect(viewDir, worldNormal);
  387. #ifdef INVERTCUBICMAP
  388. coords.y = 1.0 - coords.y;
  389. #endif
  390. return vec3(reflectionMatrix * vec4(coords, 0));
  391. #endif
  392. #ifdef REFLECTIONMAP_PROJECTION
  393. return vec3(reflectionMatrix * (view * worldPos));
  394. #endif
  395. #ifdef REFLECTIONMAP_SKYBOX
  396. return vPositionUVW;
  397. #endif
  398. #ifdef REFLECTIONMAP_EXPLICIT
  399. return vec3(0, 0, 0);
  400. #endif
  401. }
  402. #endif
  403. // Shadows
  404. #ifdef SHADOWS
  405. float unpack(vec4 color)
  406. {
  407. 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);
  408. return dot(color, bit_shift);
  409. }
  410. #if defined(POINTLIGHT0) || defined(POINTLIGHT1) || defined(POINTLIGHT2) || defined(POINTLIGHT3)
  411. uniform vec2 depthValues;
  412. float computeShadowCube(vec3 lightPosition, samplerCube shadowSampler, float darkness, float bias)
  413. {
  414. vec3 directionToLight = vPositionW - lightPosition;
  415. float depth = length(directionToLight);
  416. depth = clamp(depth, 0., 1.0);
  417. directionToLight = normalize(directionToLight);
  418. directionToLight.y = - directionToLight.y;
  419. float shadow = unpack(textureCube(shadowSampler, directionToLight)) + bias;
  420. if (depth > shadow)
  421. {
  422. #ifdef OVERLOADEDSHADOWVALUES
  423. return mix(1.0, darkness, vOverloadedShadowIntensity.x);
  424. #else
  425. return darkness;
  426. #endif
  427. }
  428. return 1.0;
  429. }
  430. float computeShadowWithPCFCube(vec3 lightPosition, samplerCube shadowSampler, float mapSize, float bias, float darkness)
  431. {
  432. vec3 directionToLight = vPositionW - lightPosition;
  433. float depth = length(directionToLight);
  434. depth = (depth - depthValues.x) / (depthValues.y - depthValues.x);
  435. depth = clamp(depth, 0., 1.0);
  436. directionToLight = normalize(directionToLight);
  437. directionToLight.y = -directionToLight.y;
  438. float visibility = 1.;
  439. vec3 poissonDisk[4];
  440. poissonDisk[0] = vec3(-1.0, 1.0, -1.0);
  441. poissonDisk[1] = vec3(1.0, -1.0, -1.0);
  442. poissonDisk[2] = vec3(-1.0, -1.0, -1.0);
  443. poissonDisk[3] = vec3(1.0, -1.0, 1.0);
  444. // Poisson Sampling
  445. float biasedDepth = depth - bias;
  446. if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[0] * mapSize)) < biasedDepth) visibility -= 0.25;
  447. if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[1] * mapSize)) < biasedDepth) visibility -= 0.25;
  448. if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[2] * mapSize)) < biasedDepth) visibility -= 0.25;
  449. if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[3] * mapSize)) < biasedDepth) visibility -= 0.25;
  450. #ifdef OVERLOADEDSHADOWVALUES
  451. return min(1.0, mix(1.0, visibility + darkness, vOverloadedShadowIntensity.x));
  452. #else
  453. return min(1.0, visibility + darkness);
  454. #endif
  455. }
  456. #endif
  457. #if defined(SPOTLIGHT0) || defined(SPOTLIGHT1) || defined(SPOTLIGHT2) || defined(SPOTLIGHT3) || defined(DIRLIGHT0) || defined(DIRLIGHT1) || defined(DIRLIGHT2) || defined(DIRLIGHT3)
  458. float computeShadow(vec4 vPositionFromLight, sampler2D shadowSampler, float darkness, float bias)
  459. {
  460. vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
  461. depth = 0.5 * depth + vec3(0.5);
  462. vec2 uv = depth.xy;
  463. if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0)
  464. {
  465. return 1.0;
  466. }
  467. float shadow = unpack(texture2D(shadowSampler, uv)) + bias;
  468. if (depth.z > shadow)
  469. {
  470. #ifdef OVERLOADEDSHADOWVALUES
  471. return mix(1.0, darkness, vOverloadedShadowIntensity.x);
  472. #else
  473. return darkness;
  474. #endif
  475. }
  476. return 1.;
  477. }
  478. float computeShadowWithPCF(vec4 vPositionFromLight, sampler2D shadowSampler, float mapSize, float bias, float darkness)
  479. {
  480. vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
  481. depth = 0.5 * depth + vec3(0.5);
  482. vec2 uv = depth.xy;
  483. if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0)
  484. {
  485. return 1.0;
  486. }
  487. float visibility = 1.;
  488. vec2 poissonDisk[4];
  489. poissonDisk[0] = vec2(-0.94201624, -0.39906216);
  490. poissonDisk[1] = vec2(0.94558609, -0.76890725);
  491. poissonDisk[2] = vec2(-0.094184101, -0.92938870);
  492. poissonDisk[3] = vec2(0.34495938, 0.29387760);
  493. // Poisson Sampling
  494. float biasedDepth = depth.z - bias;
  495. if (unpack(texture2D(shadowSampler, uv + poissonDisk[0] * mapSize)) < biasedDepth) visibility -= 0.25;
  496. if (unpack(texture2D(shadowSampler, uv + poissonDisk[1] * mapSize)) < biasedDepth) visibility -= 0.25;
  497. if (unpack(texture2D(shadowSampler, uv + poissonDisk[2] * mapSize)) < biasedDepth) visibility -= 0.25;
  498. if (unpack(texture2D(shadowSampler, uv + poissonDisk[3] * mapSize)) < biasedDepth) visibility -= 0.25;
  499. #ifdef OVERLOADEDSHADOWVALUES
  500. return min(1.0, mix(1.0, visibility + darkness, vOverloadedShadowIntensity.x));
  501. #else
  502. return min(1.0, visibility + darkness);
  503. #endif
  504. }
  505. // Thanks to http://devmaster.net/
  506. float unpackHalf(vec2 color)
  507. {
  508. return color.x + (color.y / 255.0);
  509. }
  510. float linstep(float low, float high, float v) {
  511. return clamp((v - low) / (high - low), 0.0, 1.0);
  512. }
  513. float ChebychevInequality(vec2 moments, float compare, float bias)
  514. {
  515. float p = smoothstep(compare - bias, compare, moments.x);
  516. float variance = max(moments.y - moments.x * moments.x, 0.02);
  517. float d = compare - moments.x;
  518. float p_max = linstep(0.2, 1.0, variance / (variance + d * d));
  519. return clamp(max(p, p_max), 0.0, 1.0);
  520. }
  521. float computeShadowWithVSM(vec4 vPositionFromLight, sampler2D shadowSampler, float bias, float darkness)
  522. {
  523. vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
  524. depth = 0.5 * depth + vec3(0.5);
  525. vec2 uv = depth.xy;
  526. if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0 || depth.z >= 1.0)
  527. {
  528. return 1.0;
  529. }
  530. vec4 texel = texture2D(shadowSampler, uv);
  531. vec2 moments = vec2(unpackHalf(texel.xy), unpackHalf(texel.zw));
  532. #ifdef OVERLOADEDSHADOWVALUES
  533. return min(1.0, mix(1.0, 1.0 - ChebychevInequality(moments, depth.z, bias) + darkness, vOverloadedShadowIntensity.x));
  534. #else
  535. return min(1.0, 1.0 - ChebychevInequality(moments, depth.z, bias) + darkness);
  536. #endif
  537. }
  538. #endif
  539. #endif
  540. #include<bumpFragmentFunctions>
  541. #include<clipPlaneFragmentDeclaration>
  542. #include<logDepthDeclaration>
  543. // Fog
  544. #include<fogFragmentDeclaration>
  545. // Light Computing
  546. struct lightingInfo
  547. {
  548. vec3 diffuse;
  549. #ifdef SPECULARTERM
  550. vec3 specular;
  551. #endif
  552. };
  553. lightingInfo computeLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, vec3 specularColor, float range, float roughness, float NdotV, float lightRadius) {
  554. lightingInfo result;
  555. vec3 lightDirection;
  556. float attenuation = 1.0;
  557. float lightDistance;
  558. // Point
  559. if (lightData.w == 0.)
  560. {
  561. vec3 lightOffset = lightData.xyz - vPositionW;
  562. // Inverse squared falloff.
  563. float lightDistanceSquared = dot(lightOffset, lightOffset);
  564. float lightDistanceFalloff = 1.0 / ((lightDistanceSquared + 0.0001) * range);
  565. attenuation = lightDistanceFalloff;
  566. lightDistance = sqrt(lightDistanceSquared);
  567. lightDirection = normalize(lightOffset);
  568. }
  569. // Directional
  570. else
  571. {
  572. lightDistance = length(-lightData.xyz);
  573. lightDirection = normalize(-lightData.xyz);
  574. }
  575. // Roughness
  576. roughness = adjustRoughnessFromLightProperties(roughness, lightRadius, lightDistance);
  577. // diffuse
  578. vec3 H = normalize(viewDirectionW + lightDirection);
  579. float NdotL = max(0.00000000001, dot(vNormal, lightDirection));
  580. float VdotH = clamp(0.00000000001, 1.0, dot(viewDirectionW, H));
  581. float diffuseTerm = computeDiffuseTerm(NdotL, NdotV, VdotH, roughness);
  582. result.diffuse = diffuseTerm * diffuseColor * attenuation;
  583. #ifdef SPECULARTERM
  584. // Specular
  585. float NdotH = max(0.00000000001, dot(vNormal, H));
  586. vec3 specTerm = computeSpecularTerm(NdotH, NdotL, NdotV, VdotH, roughness, specularColor);
  587. result.specular = specTerm * attenuation;
  588. #endif
  589. return result;
  590. }
  591. lightingInfo computeSpotLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec4 lightDirection, vec3 diffuseColor, vec3 specularColor, float range, float roughness, float NdotV, float lightRadius) {
  592. lightingInfo result;
  593. vec3 lightOffset = lightData.xyz - vPositionW;
  594. vec3 lightVectorW = normalize(lightOffset);
  595. // diffuse
  596. float cosAngle = max(0.000000000000001, dot(-lightDirection.xyz, lightVectorW));
  597. if (cosAngle >= lightDirection.w)
  598. {
  599. cosAngle = max(0., pow(cosAngle, lightData.w));
  600. // Inverse squared falloff.
  601. float lightDistanceSquared = dot(lightOffset, lightOffset);
  602. float lightDistanceFalloff = 1.0 / ((lightDistanceSquared + 0.0001) * range);
  603. float attenuation = lightDistanceFalloff;
  604. // Directional falloff.
  605. attenuation *= cosAngle;
  606. // Roughness.
  607. float lightDistance = sqrt(lightDistanceSquared);
  608. roughness = adjustRoughnessFromLightProperties(roughness, lightRadius, lightDistance);
  609. // Diffuse
  610. vec3 H = normalize(viewDirectionW - lightDirection.xyz);
  611. float NdotL = max(0.00000000001, dot(vNormal, -lightDirection.xyz));
  612. float VdotH = clamp(dot(viewDirectionW, H), 0.00000000001, 1.0);
  613. float diffuseTerm = computeDiffuseTerm(NdotL, NdotV, VdotH, roughness);
  614. result.diffuse = diffuseTerm * diffuseColor * attenuation;
  615. #ifdef SPECULARTERM
  616. // Specular
  617. float NdotH = max(0.00000000001, dot(vNormal, H));
  618. vec3 specTerm = computeSpecularTerm(NdotH, NdotL, NdotV, VdotH, roughness, specularColor);
  619. result.specular = specTerm * attenuation;
  620. #endif
  621. return result;
  622. }
  623. result.diffuse = vec3(0.);
  624. #ifdef SPECULARTERM
  625. result.specular = vec3(0.);
  626. #endif
  627. return result;
  628. }
  629. lightingInfo computeHemisphericLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, vec3 specularColor, vec3 groundColor, float roughness, float NdotV, float lightRadius) {
  630. lightingInfo result;
  631. // Roughness
  632. // Do not touch roughness on hemispheric.
  633. // Diffuse
  634. float ndl = dot(vNormal, lightData.xyz) * 0.5 + 0.5;
  635. result.diffuse = mix(groundColor, diffuseColor, ndl);
  636. #ifdef SPECULARTERM
  637. // Specular
  638. vec3 lightVectorW = normalize(lightData.xyz);
  639. vec3 H = normalize(viewDirectionW + lightVectorW);
  640. float NdotH = max(0.00000000001, dot(vNormal, H));
  641. float NdotL = max(0.00000000001, ndl);
  642. float VdotH = clamp(0.00000000001, 1.0, dot(viewDirectionW, H));
  643. vec3 specTerm = computeSpecularTerm(NdotH, NdotL, NdotV, VdotH, roughness, specularColor);
  644. result.specular = specTerm;
  645. #endif
  646. return result;
  647. }
  648. void main(void) {
  649. #include<clipPlaneFragment>
  650. #ifdef PoissonSamplingEnvironment
  651. initSamplers();
  652. #endif
  653. vec3 viewDirectionW = normalize(vEyePosition - vPositionW);
  654. // Albedo
  655. vec4 surfaceAlbedo = vec4(1., 1., 1., 1.);
  656. vec3 surfaceAlbedoContribution = vAlbedoColor.rgb;
  657. // Alpha
  658. float alpha = vAlbedoColor.a;
  659. #ifdef ALBEDO
  660. surfaceAlbedo = texture2D(albedoSampler, vAlbedoUV);
  661. surfaceAlbedo = vec4(toLinearSpace(surfaceAlbedo.rgb), surfaceAlbedo.a);
  662. #ifndef LINKREFRACTIONTOTRANSPARENCY
  663. #ifdef ALPHATEST
  664. if (surfaceAlbedo.a < 0.4)
  665. discard;
  666. #endif
  667. #endif
  668. #ifdef ALPHAFROMALBEDO
  669. alpha *= surfaceAlbedo.a;
  670. #endif
  671. surfaceAlbedo.rgb *= vAlbedoInfos.y;
  672. #else
  673. // No Albedo texture.
  674. surfaceAlbedo.rgb = surfaceAlbedoContribution;
  675. surfaceAlbedoContribution = vec3(1., 1., 1.);
  676. #endif
  677. #ifdef VERTEXCOLOR
  678. surfaceAlbedo.rgb *= vColor.rgb;
  679. #endif
  680. #ifdef OVERLOADEDVALUES
  681. surfaceAlbedo.rgb = mix(surfaceAlbedo.rgb, vOverloadedAlbedo, vOverloadedIntensity.y);
  682. #endif
  683. // Bump
  684. #ifdef NORMAL
  685. vec3 normalW = normalize(vNormalW);
  686. #else
  687. vec3 normalW = vec3(1.0, 1.0, 1.0);
  688. #endif
  689. #ifdef BUMP
  690. normalW = perturbNormal(viewDirectionW);
  691. #endif
  692. // Ambient color
  693. vec3 ambientColor = vec3(1., 1., 1.);
  694. #ifdef AMBIENT
  695. ambientColor = texture2D(ambientSampler, vAmbientUV).rgb * vAmbientInfos.y;
  696. #ifdef OVERLOADEDVALUES
  697. ambientColor.rgb = mix(ambientColor.rgb, vOverloadedAmbient, vOverloadedIntensity.x);
  698. #endif
  699. #endif
  700. // Specular map
  701. float microSurface = vReflectivityColor.a;
  702. vec3 surfaceReflectivityColor = vReflectivityColor.rgb;
  703. #ifdef OVERLOADEDVALUES
  704. surfaceReflectivityColor.rgb = mix(surfaceReflectivityColor.rgb, vOverloadedReflectivity, vOverloadedIntensity.z);
  705. #endif
  706. #ifdef REFLECTIVITY
  707. vec4 surfaceReflectivityColorMap = texture2D(reflectivitySampler, vReflectivityUV);
  708. surfaceReflectivityColor = surfaceReflectivityColorMap.rgb;
  709. surfaceReflectivityColor = toLinearSpace(surfaceReflectivityColor);
  710. #ifdef OVERLOADEDVALUES
  711. surfaceReflectivityColor = mix(surfaceReflectivityColor, vOverloadedReflectivity, vOverloadedIntensity.z);
  712. #endif
  713. #ifdef MICROSURFACEFROMREFLECTIVITYMAP
  714. microSurface = surfaceReflectivityColorMap.a;
  715. #else
  716. #ifdef MICROSURFACEAUTOMATIC
  717. microSurface = computeDefaultMicroSurface(microSurface, surfaceReflectivityColor);
  718. #endif
  719. #endif
  720. #endif
  721. #ifdef OVERLOADEDVALUES
  722. microSurface = mix(microSurface, vOverloadedMicroSurface.x, vOverloadedMicroSurface.y);
  723. #endif
  724. // Compute N dot V.
  725. float NdotV = max(0.00000000001, dot(normalW, viewDirectionW));
  726. // Adapt microSurface.
  727. microSurface = clamp(microSurface, 0., 1.) * 0.98;
  728. // Compute roughness.
  729. float roughness = clamp(1. - microSurface, 0.000001, 1.0);
  730. // Lighting
  731. vec3 lightDiffuseContribution = vec3(0., 0., 0.);
  732. #ifdef OVERLOADEDSHADOWVALUES
  733. vec3 shadowedOnlyLightDiffuseContribution = vec3(1., 1., 1.);
  734. #endif
  735. #ifdef SPECULARTERM
  736. vec3 lightSpecularContribution= vec3(0., 0., 0.);
  737. #endif
  738. float notShadowLevel = 1.; // 1 - shadowLevel
  739. #ifdef LIGHT0
  740. #ifndef SPECULARTERM
  741. vec3 vLightSpecular0 = vec3(0.0);
  742. #endif
  743. #ifdef SPOTLIGHT0
  744. lightingInfo info = computeSpotLighting(viewDirectionW, normalW, vLightData0, vLightDirection0, vLightDiffuse0.rgb, vLightSpecular0, vLightDiffuse0.a, roughness, NdotV, vLightRadiuses[0]);
  745. #endif
  746. #ifdef HEMILIGHT0
  747. lightingInfo info = computeHemisphericLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightSpecular0, vLightGround0, roughness, NdotV, vLightRadiuses[0]);
  748. #endif
  749. #if defined(POINTLIGHT0) || defined(DIRLIGHT0)
  750. lightingInfo info = computeLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightSpecular0, vLightDiffuse0.a, roughness, NdotV, vLightRadiuses[0]);
  751. #endif
  752. #ifdef SHADOW0
  753. #ifdef SHADOWVSM0
  754. notShadowLevel = computeShadowWithVSM(vPositionFromLight0, shadowSampler0, shadowsInfo0.z, shadowsInfo0.x);
  755. #else
  756. #ifdef SHADOWPCF0
  757. #if defined(POINTLIGHT0)
  758. notShadowLevel = computeShadowWithPCFCube(vLightData0.xyz, shadowSampler0, shadowsInfo0.y, shadowsInfo0.z, shadowsInfo0.x);
  759. #else
  760. notShadowLevel = computeShadowWithPCF(vPositionFromLight0, shadowSampler0, shadowsInfo0.y, shadowsInfo0.z, shadowsInfo0.x);
  761. #endif
  762. #else
  763. #if defined(POINTLIGHT0)
  764. notShadowLevel = computeShadowCube(vLightData0.xyz, shadowSampler0, shadowsInfo0.x, shadowsInfo0.z);
  765. #else
  766. notShadowLevel = computeShadow(vPositionFromLight0, shadowSampler0, shadowsInfo0.x, shadowsInfo0.z);
  767. #endif
  768. #endif
  769. #endif
  770. #else
  771. notShadowLevel = 1.;
  772. #endif
  773. lightDiffuseContribution += info.diffuse * notShadowLevel;
  774. #ifdef OVERLOADEDSHADOWVALUES
  775. shadowedOnlyLightDiffuseContribution *= notShadowLevel;
  776. #endif
  777. #ifdef SPECULARTERM
  778. lightSpecularContribution += info.specular * notShadowLevel;
  779. #endif
  780. #endif
  781. #ifdef LIGHT1
  782. #ifndef SPECULARTERM
  783. vec3 vLightSpecular1 = vec3(0.0);
  784. #endif
  785. #ifdef SPOTLIGHT1
  786. info = computeSpotLighting(viewDirectionW, normalW, vLightData1, vLightDirection1, vLightDiffuse1.rgb, vLightSpecular1, vLightDiffuse1.a, roughness, NdotV, vLightRadiuses[1]);
  787. #endif
  788. #ifdef HEMILIGHT1
  789. info = computeHemisphericLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightSpecular1, vLightGround1, roughness, NdotV, vLightRadiuses[1]);
  790. #endif
  791. #if defined(POINTLIGHT1) || defined(DIRLIGHT1)
  792. info = computeLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightSpecular1, vLightDiffuse1.a, roughness, NdotV, vLightRadiuses[1]);
  793. #endif
  794. #ifdef SHADOW1
  795. #ifdef SHADOWVSM1
  796. notShadowLevel = computeShadowWithVSM(vPositionFromLight1, shadowSampler1, shadowsInfo1.z, shadowsInfo1.x);
  797. #else
  798. #ifdef SHADOWPCF1
  799. #if defined(POINTLIGHT1)
  800. notShadowLevel = computeShadowWithPCFCube(vLightData1.xyz, shadowSampler1, shadowsInfo1.y, shadowsInfo1.z, shadowsInfo1.x);
  801. #else
  802. notShadowLevel = computeShadowWithPCF(vPositionFromLight1, shadowSampler1, shadowsInfo1.y, shadowsInfo1.z, shadowsInfo1.x);
  803. #endif
  804. #else
  805. #if defined(POINTLIGHT1)
  806. notShadowLevel = computeShadowCube(vLightData1.xyz, shadowSampler1, shadowsInfo1.x, shadowsInfo1.z);
  807. #else
  808. notShadowLevel = computeShadow(vPositionFromLight1, shadowSampler1, shadowsInfo1.x, shadowsInfo1.z);
  809. #endif
  810. #endif
  811. #endif
  812. #else
  813. notShadowLevel = 1.;
  814. #endif
  815. lightDiffuseContribution += info.diffuse * notShadowLevel;
  816. #ifdef OVERLOADEDSHADOWVALUES
  817. shadowedOnlyLightDiffuseContribution *= notShadowLevel;
  818. #endif
  819. #ifdef SPECULARTERM
  820. lightSpecularContribution += info.specular * notShadowLevel;
  821. #endif
  822. #endif
  823. #ifdef LIGHT2
  824. #ifndef SPECULARTERM
  825. vec3 vLightSpecular2 = vec3(0.0);
  826. #endif
  827. #ifdef SPOTLIGHT2
  828. info = computeSpotLighting(viewDirectionW, normalW, vLightData2, vLightDirection2, vLightDiffuse2.rgb, vLightSpecular2, vLightDiffuse2.a, roughness, NdotV, vLightRadiuses[2]);
  829. #endif
  830. #ifdef HEMILIGHT2
  831. info = computeHemisphericLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightSpecular2, vLightGround2, roughness, NdotV, vLightRadiuses[2]);
  832. #endif
  833. #if defined(POINTLIGHT2) || defined(DIRLIGHT2)
  834. info = computeLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightSpecular2, vLightDiffuse2.a, roughness, NdotV, vLightRadiuses[2]);
  835. #endif
  836. #ifdef SHADOW2
  837. #ifdef SHADOWVSM2
  838. notShadowLevel = computeShadowWithVSM(vPositionFromLight2, shadowSampler2, shadowsInfo2.z, shadowsInfo2.x);
  839. #else
  840. #ifdef SHADOWPCF2
  841. #if defined(POINTLIGHT2)
  842. notShadowLevel = computeShadowWithPCFCube(vLightData2.xyz, shadowSampler2, shadowsInfo2.y, shadowsInfo2.z, shadowsInfo2.x);
  843. #else
  844. notShadowLevel = computeShadowWithPCF(vPositionFromLight2, shadowSampler2, shadowsInfo2.y, shadowsInfo2.z, shadowsInfo2.x);
  845. #endif
  846. #else
  847. #if defined(POINTLIGHT2)
  848. notShadowLevel = computeShadowCube(vLightData2.xyz, shadowSampler2, shadowsInfo2.x, shadowsInfo2.z);
  849. #else
  850. notShadowLevel = computeShadow(vPositionFromLight2, shadowSampler2, shadowsInfo2.x, shadowsInfo2.z);
  851. #endif
  852. #endif
  853. #endif
  854. #else
  855. notShadowLevel = 1.;
  856. #endif
  857. lightDiffuseContribution += info.diffuse * notShadowLevel;
  858. #ifdef OVERLOADEDSHADOWVALUES
  859. shadowedOnlyLightDiffuseContribution *= notShadowLevel;
  860. #endif
  861. #ifdef SPECULARTERM
  862. lightSpecularContribution += info.specular * notShadowLevel;
  863. #endif
  864. #endif
  865. #ifdef LIGHT3
  866. #ifndef SPECULARTERM
  867. vec3 vLightSpecular3 = vec3(0.0);
  868. #endif
  869. #ifdef SPOTLIGHT3
  870. info = computeSpotLighting(viewDirectionW, normalW, vLightData3, vLightDirection3, vLightDiffuse3.rgb, vLightSpecular3, vLightDiffuse3.a, roughness, NdotV, vLightRadiuses[3]);
  871. #endif
  872. #ifdef HEMILIGHT3
  873. info = computeHemisphericLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightSpecular3, vLightGround3, roughness, NdotV, vLightRadiuses[3]);
  874. #endif
  875. #if defined(POINTLIGHT3) || defined(DIRLIGHT3)
  876. info = computeLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightSpecular3, vLightDiffuse3.a, roughness, NdotV, vLightRadiuses[3]);
  877. #endif
  878. #ifdef SHADOW3
  879. #ifdef SHADOWVSM3
  880. notShadowLevel = computeShadowWithVSM(vPositionFromLight3, shadowSampler3, shadowsInfo3.z, shadowsInfo3.x);
  881. #else
  882. #ifdef SHADOWPCF3
  883. #if defined(POINTLIGHT3)
  884. notShadowLevel = computeShadowWithPCFCube(vLightData3.xyz, shadowSampler3, shadowsInfo3.y, shadowsInfo3.z, shadowsInfo3.x);
  885. #else
  886. notShadowLevel = computeShadowWithPCF(vPositionFromLight3, shadowSampler3, shadowsInfo3.y, shadowsInfo3.z, shadowsInfo3.x);
  887. #endif
  888. #else
  889. #if defined(POINTLIGHT3)
  890. notShadowLevel = computeShadowCube(vLightData3.xyz, shadowSampler3, shadowsInfo3.x, shadowsInfo3.z);
  891. #else
  892. notShadowLevel = computeShadow(vPositionFromLight3, shadowSampler3, shadowsInfo3.x, shadowsInfo3.z);
  893. #endif
  894. #endif
  895. #endif
  896. #else
  897. notShadowLevel = 1.;
  898. #endif
  899. lightDiffuseContribution += info.diffuse * notShadowLevel;
  900. #ifdef OVERLOADEDSHADOWVALUES
  901. shadowedOnlyLightDiffuseContribution *= notShadowLevel;
  902. #endif
  903. #ifdef SPECULARTERM
  904. lightSpecularContribution += info.specular * notShadowLevel;
  905. #endif
  906. #endif
  907. #ifdef SPECULARTERM
  908. lightSpecularContribution *= vLightingIntensity.w;
  909. #endif
  910. #ifdef OPACITY
  911. vec4 opacityMap = texture2D(opacitySampler, vOpacityUV);
  912. #ifdef OPACITYRGB
  913. opacityMap.rgb = opacityMap.rgb * vec3(0.3, 0.59, 0.11);
  914. alpha *= (opacityMap.x + opacityMap.y + opacityMap.z)* vOpacityInfos.y;
  915. #else
  916. alpha *= opacityMap.a * vOpacityInfos.y;
  917. #endif
  918. #endif
  919. #ifdef VERTEXALPHA
  920. alpha *= vColor.a;
  921. #endif
  922. #ifdef OPACITYFRESNEL
  923. float opacityFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, opacityParts.z, opacityParts.w);
  924. alpha += opacityParts.x * (1.0 - opacityFresnelTerm) + opacityFresnelTerm * opacityParts.y;
  925. #endif
  926. // Refraction
  927. vec3 surfaceRefractionColor = vec3(0., 0., 0.);
  928. // Go mat -> blurry reflexion according to microSurface
  929. #ifndef LODBASEDMICROSFURACE
  930. float bias = 20. * (1.0 - microSurface);
  931. #else
  932. float alphaG = convertRoughnessToAverageSlope(roughness);
  933. #endif
  934. #ifdef REFRACTION
  935. vec3 refractionVector = normalize(refract(-viewDirectionW, normalW, vRefractionInfos.y));
  936. #ifdef LODBASEDMICROSFURACE
  937. float lodRefraction = getMipMapIndexFromAverageSlope(vMicrosurfaceTextureLods.y, alphaG);
  938. #endif
  939. #ifdef REFRACTIONMAP_3D
  940. refractionVector.y = refractionVector.y * vRefractionInfos.w;
  941. if (dot(refractionVector, viewDirectionW) < 1.0)
  942. {
  943. #ifdef LODBASEDMICROSFURACE
  944. surfaceRefractionColor = textureCubeLodEXT(refractionCubeSampler, refractionVector, lodRefraction).rgb * vRefractionInfos.x;
  945. #else
  946. surfaceRefractionColor = textureCube(refractionCubeSampler, refractionVector, bias).rgb * vRefractionInfos.x;
  947. #endif
  948. }
  949. #ifndef REFRACTIONMAPINLINEARSPACE
  950. surfaceRefractionColor = toLinearSpace(surfaceRefractionColor.rgb);
  951. #endif
  952. #else
  953. vec3 vRefractionUVW = vec3(refractionMatrix * (view * vec4(vPositionW + refractionVector * vRefractionInfos.z, 1.0)));
  954. vec2 refractionCoords = vRefractionUVW.xy / vRefractionUVW.z;
  955. refractionCoords.y = 1.0 - refractionCoords.y;
  956. #ifdef LODBASEDMICROSFURACE
  957. surfaceRefractionColor = texture2DLodEXT(refraction2DSampler, refractionCoords, lodRefraction).rgb * vRefractionInfos.x;
  958. #else
  959. surfaceRefractionColor = texture2D(refraction2DSampler, refractionCoords).rgb * vRefractionInfos.x;
  960. #endif
  961. surfaceRefractionColor = toLinearSpace(surfaceRefractionColor.rgb);
  962. #endif
  963. #endif
  964. // Reflection
  965. vec3 environmentRadiance = vReflectionColor.rgb;
  966. vec3 environmentIrradiance = vReflectionColor.rgb;
  967. #ifdef REFLECTION
  968. vec3 vReflectionUVW = computeReflectionCoords(vec4(vPositionW, 1.0), normalW);
  969. #ifdef LODBASEDMICROSFURACE
  970. float lodReflection = getMipMapIndexFromAverageSlope(vMicrosurfaceTextureLods.x, alphaG);
  971. #endif
  972. #ifdef REFLECTIONMAP_3D
  973. #ifdef LODBASEDMICROSFURACE
  974. environmentRadiance = textureCubeLodEXT(reflectionCubeSampler, vReflectionUVW, lodReflection).rgb * vReflectionInfos.x;
  975. #else
  976. environmentRadiance = textureCube(reflectionCubeSampler, vReflectionUVW, bias).rgb * vReflectionInfos.x;
  977. #endif
  978. #ifdef PoissonSamplingEnvironment
  979. environmentRadiance = environmentSampler(reflectionCubeSampler, vReflectionUVW, alphaG) * vReflectionInfos.x;
  980. #endif
  981. #ifdef USESPHERICALFROMREFLECTIONMAP
  982. #ifndef REFLECTIONMAP_SKYBOX
  983. vec3 normalEnvironmentSpace = (reflectionMatrix * vec4(normalW, 1)).xyz;
  984. environmentIrradiance = EnvironmentIrradiance(normalEnvironmentSpace);
  985. #endif
  986. #else
  987. environmentRadiance = toLinearSpace(environmentRadiance.rgb);
  988. environmentIrradiance = textureCube(reflectionCubeSampler, normalW, 20.).rgb * vReflectionInfos.x;
  989. environmentIrradiance = toLinearSpace(environmentIrradiance.rgb);
  990. environmentIrradiance *= 0.2; // Hack in case of no hdr cube map use for environment.
  991. #endif
  992. #else
  993. vec2 coords = vReflectionUVW.xy;
  994. #ifdef REFLECTIONMAP_PROJECTION
  995. coords /= vReflectionUVW.z;
  996. #endif
  997. coords.y = 1.0 - coords.y;
  998. #ifdef LODBASEDMICROSFURACE
  999. environmentRadiance = texture2DLodExt(reflection2DSampler, coords, lodReflection).rgb * vReflectionInfos.x;
  1000. #else
  1001. environmentRadiance = texture2D(reflection2DSampler, coords).rgb * vReflectionInfos.x;
  1002. #endif
  1003. environmentRadiance = toLinearSpace(environmentRadiance.rgb);
  1004. environmentIrradiance = texture2D(reflection2DSampler, coords, 20.).rgb * vReflectionInfos.x;
  1005. environmentIrradiance = toLinearSpace(environmentIrradiance.rgb);
  1006. #endif
  1007. #endif
  1008. #ifdef OVERLOADEDVALUES
  1009. environmentIrradiance = mix(environmentIrradiance, vOverloadedReflection, vOverloadedMicroSurface.z);
  1010. environmentRadiance = mix(environmentRadiance, vOverloadedReflection, vOverloadedMicroSurface.z);
  1011. #endif
  1012. environmentRadiance *= vLightingIntensity.z;
  1013. environmentIrradiance *= vLightingIntensity.z;
  1014. // Compute reflection specular fresnel
  1015. vec3 specularEnvironmentR0 = surfaceReflectivityColor.rgb;
  1016. vec3 specularEnvironmentR90 = vec3(1.0, 1.0, 1.0);
  1017. vec3 specularEnvironmentReflectance = FresnelSchlickEnvironmentGGX(clamp(NdotV, 0., 1.), specularEnvironmentR0, specularEnvironmentR90, sqrt(microSurface));
  1018. // Compute refractance
  1019. vec3 refractance = vec3(0.0 , 0.0, 0.0);
  1020. #ifdef REFRACTION
  1021. vec3 transmission = vec3(1.0 , 1.0, 1.0);
  1022. #ifdef LINKREFRACTIONTOTRANSPARENCY
  1023. // Transmission based on alpha.
  1024. transmission *= (1.0 - alpha);
  1025. // Tint the material with albedo.
  1026. // TODO. PBR Tinting.
  1027. vec3 mixedAlbedo = surfaceAlbedoContribution.rgb * surfaceAlbedo.rgb;
  1028. float maxChannel = max(max(mixedAlbedo.r, mixedAlbedo.g), mixedAlbedo.b);
  1029. vec3 tint = clamp(maxChannel * mixedAlbedo, 0.0, 1.0);
  1030. // Decrease Albedo Contribution
  1031. surfaceAlbedoContribution *= alpha;
  1032. // Decrease irradiance Contribution
  1033. environmentIrradiance *= alpha;
  1034. // Tint reflectance
  1035. surfaceRefractionColor *= tint;
  1036. // Put alpha back to 1;
  1037. alpha = 1.0;
  1038. #endif
  1039. // Add Multiple internal bounces.
  1040. vec3 bounceSpecularEnvironmentReflectance = (2.0 * specularEnvironmentReflectance) / (1.0 + specularEnvironmentReflectance);
  1041. specularEnvironmentReflectance = mix(bounceSpecularEnvironmentReflectance, specularEnvironmentReflectance, alpha);
  1042. // In theory T = 1 - R.
  1043. transmission *= 1.0 - specularEnvironmentReflectance;
  1044. // Should baked in diffuse.
  1045. refractance = surfaceRefractionColor * transmission;
  1046. #endif
  1047. // Apply Energy Conservation taking in account the environment level only if the environment is present.
  1048. float reflectance = max(max(surfaceReflectivityColor.r, surfaceReflectivityColor.g), surfaceReflectivityColor.b);
  1049. surfaceAlbedo.rgb = (1. - reflectance) * surfaceAlbedo.rgb;
  1050. refractance *= vLightingIntensity.z;
  1051. environmentRadiance *= specularEnvironmentReflectance;
  1052. // Emissive
  1053. vec3 surfaceEmissiveColor = vEmissiveColor;
  1054. #ifdef EMISSIVE
  1055. vec3 emissiveColorTex = texture2D(emissiveSampler, vEmissiveUV).rgb;
  1056. surfaceEmissiveColor = toLinearSpace(emissiveColorTex.rgb) * surfaceEmissiveColor * vEmissiveInfos.y;
  1057. #endif
  1058. #ifdef OVERLOADEDVALUES
  1059. surfaceEmissiveColor = mix(surfaceEmissiveColor, vOverloadedEmissive, vOverloadedIntensity.w);
  1060. #endif
  1061. #ifdef EMISSIVEFRESNEL
  1062. float emissiveFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, emissiveRightColor.a, emissiveLeftColor.a);
  1063. surfaceEmissiveColor *= emissiveLeftColor.rgb * (1.0 - emissiveFresnelTerm) + emissiveFresnelTerm * emissiveRightColor.rgb;
  1064. #endif
  1065. // Composition
  1066. #ifdef EMISSIVEASILLUMINATION
  1067. vec3 finalDiffuse = max(lightDiffuseContribution * surfaceAlbedoContribution + vAmbientColor, 0.0) * surfaceAlbedo.rgb;
  1068. #ifdef OVERLOADEDSHADOWVALUES
  1069. shadowedOnlyLightDiffuseContribution = max(shadowedOnlyLightDiffuseContribution * surfaceAlbedoContribution + vAmbientColor, 0.0) * surfaceAlbedo.rgb;
  1070. #endif
  1071. #else
  1072. #ifdef LINKEMISSIVEWITHALBEDO
  1073. vec3 finalDiffuse = max((lightDiffuseContribution + surfaceEmissiveColor) * surfaceAlbedoContribution + vAmbientColor, 0.0) * surfaceAlbedo.rgb;
  1074. #ifdef OVERLOADEDSHADOWVALUES
  1075. shadowedOnlyLightDiffuseContribution = max((shadowedOnlyLightDiffuseContribution + surfaceEmissiveColor) * surfaceAlbedoContribution + vAmbientColor, 0.0) * surfaceAlbedo.rgb;
  1076. #endif
  1077. #else
  1078. vec3 finalDiffuse = max(lightDiffuseContribution * surfaceAlbedoContribution + surfaceEmissiveColor + vAmbientColor, 0.0) * surfaceAlbedo.rgb;
  1079. #ifdef OVERLOADEDSHADOWVALUES
  1080. shadowedOnlyLightDiffuseContribution = max(shadowedOnlyLightDiffuseContribution * surfaceAlbedoContribution + surfaceEmissiveColor + vAmbientColor, 0.0) * surfaceAlbedo.rgb;
  1081. #endif
  1082. #endif
  1083. #endif
  1084. #ifdef OVERLOADEDSHADOWVALUES
  1085. finalDiffuse = mix(finalDiffuse, shadowedOnlyLightDiffuseContribution, (1.0 - vOverloadedShadowIntensity.y));
  1086. #endif
  1087. #ifdef SPECULARTERM
  1088. vec3 finalSpecular = lightSpecularContribution * surfaceReflectivityColor;
  1089. #else
  1090. vec3 finalSpecular = vec3(0.0);
  1091. #endif
  1092. #ifdef OVERLOADEDSHADOWVALUES
  1093. finalSpecular = mix(finalSpecular, vec3(0.0), (1.0 - vOverloadedShadowIntensity.y));
  1094. #endif
  1095. #ifdef SPECULAROVERALPHA
  1096. alpha = clamp(alpha + dot(finalSpecular, vec3(0.3, 0.59, 0.11)), 0., 1.);
  1097. #endif
  1098. // Composition
  1099. // Reflection already includes the environment intensity.
  1100. #ifdef EMISSIVEASILLUMINATION
  1101. vec4 finalColor = vec4(finalDiffuse * ambientColor * vLightingIntensity.x + surfaceAlbedo.rgb * environmentIrradiance + finalSpecular * vLightingIntensity.x + environmentRadiance + surfaceEmissiveColor * vLightingIntensity.y + refractance, alpha);
  1102. #else
  1103. vec4 finalColor = vec4(finalDiffuse * ambientColor * vLightingIntensity.x + surfaceAlbedo.rgb * environmentIrradiance + finalSpecular * vLightingIntensity.x + environmentRadiance + refractance, alpha);
  1104. #endif
  1105. #ifdef LIGHTMAP
  1106. vec3 lightmapColor = texture2D(lightmapSampler, vLightmapUV).rgb * vLightmapInfos.y;
  1107. #ifdef USELIGHTMAPASSHADOWMAP
  1108. finalColor.rgb *= lightmapColor;
  1109. #else
  1110. finalColor.rgb += lightmapColor;
  1111. #endif
  1112. #endif
  1113. finalColor = max(finalColor, 0.0);
  1114. #ifdef CAMERATONEMAP
  1115. finalColor.rgb = toneMaps(finalColor.rgb);
  1116. #endif
  1117. finalColor.rgb = toGammaSpace(finalColor.rgb);
  1118. #ifdef CAMERACONTRAST
  1119. finalColor = contrasts(finalColor);
  1120. #endif
  1121. // Normal Display.
  1122. // gl_FragColor = vec4(normalW * 0.5 + 0.5, 1.0);
  1123. // Ambient reflection color.
  1124. // gl_FragColor = vec4(ambientReflectionColor, 1.0);
  1125. // Reflection color.
  1126. // gl_FragColor = vec4(reflectionColor, 1.0);
  1127. // Base color.
  1128. // gl_FragColor = vec4(surfaceAlbedo.rgb, 1.0);
  1129. // Specular color.
  1130. // gl_FragColor = vec4(surfaceReflectivityColor.rgb, 1.0);
  1131. // MicroSurface color.
  1132. // gl_FragColor = vec4(microSurface, microSurface, microSurface, 1.0);
  1133. // Specular Map
  1134. // gl_FragColor = vec4(reflectivityMapColor.rgb, 1.0);
  1135. // Refractance
  1136. // gl_FragColor = vec4(refractance.rgb, 1.0);
  1137. //// Emissive Color
  1138. //vec2 test = vEmissiveUV * 0.5 + 0.5;
  1139. //gl_FragColor = vec4(test.x, test.y, 1.0, 1.0);
  1140. #include<logDepthFragment>
  1141. #include<fogFragment>(color, finalColor)
  1142. gl_FragColor = finalColor;
  1143. }