pbr.fragment.fx 46 KB

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