pbr.fragment.fx 47 KB

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