pbr.fragment.fx 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218
  1. precision highp float;
  2. // Constants
  3. #define RECIPROCAL_PI2 0.15915494
  4. #define FRESNEL_MAXIMUM_ON_ROUGH 0.25
  5. uniform vec3 vEyePosition;
  6. uniform vec3 vAmbientColor;
  7. uniform vec4 vDiffuseColor;
  8. // PBR CUSTOM CONTROLS
  9. uniform vec4 vPBRLightingIntensity;
  10. uniform vec4 vPBRShadowIntensity;
  11. uniform vec4 vPBRCameraInfos;
  12. uniform vec4 vPBROverloadedIntensity;
  13. uniform vec3 vPBROverloadedAmbient;
  14. uniform vec3 vPBROverloadedDiffuse;
  15. uniform vec3 vPBROverloadedSpecular;
  16. uniform vec3 vPBROverloadedEmissive;
  17. uniform vec3 vPBROverloadedSmoothness;
  18. // PBR CUSTOM CONSTANTS
  19. const float kPi = 3.1415926535897932384626433832795;
  20. // PBR HELPER METHODS
  21. float Square(float value)
  22. {
  23. return value * value;
  24. }
  25. float getLuminance(vec3 color)
  26. {
  27. return clamp(dot(color, vec3(0.2126, 0.7152, 0.0722)), 0., 1.);
  28. }
  29. float convertRoughnessToAverageSlope(float roughness)
  30. {
  31. // Calculate AlphaG as square of roughness; add epsilon to avoid numerical issues
  32. const float kMinimumVariance = 0.0005;
  33. float alphaG = Square(roughness) + kMinimumVariance;
  34. return alphaG;
  35. }
  36. // From Microfacet Models for Refraction through Rough Surfaces, Walter et al. 2007
  37. float smithVisibilityG1_TrowbridgeReitzGGX(float dot, float alphaG)
  38. {
  39. float tanSquared = (1.0 - dot * dot) / (dot * dot);
  40. return 2.0 / (1.0 + sqrt(1.0 + alphaG * alphaG * tanSquared));
  41. }
  42. float smithVisibilityG_TrowbridgeReitzGGX_Walter(float NdotL, float NdotV, float alphaG)
  43. {
  44. return smithVisibilityG1_TrowbridgeReitzGGX(NdotL, alphaG) * smithVisibilityG1_TrowbridgeReitzGGX(NdotV, alphaG);
  45. }
  46. // Trowbridge-Reitz (GGX)
  47. // Generalised Trowbridge-Reitz with gamma power=2.0
  48. float normalDistributionFunction_TrowbridgeReitzGGX(float NdotH, float alphaG)
  49. {
  50. // Note: alphaG is average slope (gradient) of the normals in slope-space.
  51. // It is also the (trigonometric) tangent of the median distribution value, i.e. 50% of normals have
  52. // a tangent (gradient) closer to the macrosurface than this slope.
  53. float a2 = Square(alphaG);
  54. float d = NdotH * NdotH * (a2 - 1.0) + 1.0;
  55. return a2 / (kPi * d * d);
  56. }
  57. vec3 fresnelSchlickGGX(float VdotH, vec3 reflectance0, vec3 reflectance90)
  58. {
  59. return reflectance0 + (reflectance90 - reflectance0) * pow(clamp(1.0 - VdotH, 0., 1.), 5.0);
  60. }
  61. vec3 FresnelSchlickEnvironmentGGX(float VdotN, vec3 reflectance0, vec3 reflectance90, float smoothness)
  62. {
  63. // Schlick fresnel approximation, extended with basic smoothness term so that rough surfaces do not approach reflectance90 at grazing angle
  64. float weight = mix(FRESNEL_MAXIMUM_ON_ROUGH, 1.0, smoothness);
  65. return reflectance0 + weight * (reflectance90 - reflectance0) * pow(clamp(1.0 - VdotN, 0., 1.), 5.0);
  66. }
  67. // Cook Torance Specular computation.
  68. vec3 computeSpecularTerm(float NdotH, float NdotL, float NdotV, float VdotH, float roughness, vec3 specularColor)
  69. {
  70. float alphaG = convertRoughnessToAverageSlope(roughness);
  71. float distribution = normalDistributionFunction_TrowbridgeReitzGGX(NdotH, alphaG);
  72. float visibility = smithVisibilityG_TrowbridgeReitzGGX_Walter(NdotL, NdotV, alphaG);
  73. visibility /= (4.0 * NdotL * NdotV); // Cook Torance Denominator integated in viibility to avoid issues when visibility function changes.
  74. vec3 fresnel = fresnelSchlickGGX(VdotH, specularColor, vec3(1., 1., 1.));
  75. float specTerm = max(0., visibility * distribution) * NdotL;
  76. return fresnel * specTerm * kPi; // TODO: audit pi constants
  77. }
  78. float computeDiffuseTerm(float NdotL, float NdotV, float VdotH, float roughness)
  79. {
  80. // Diffuse fresnel falloff as per Disney principled BRDF, and in the spirit of
  81. // of general coupled diffuse/specular models e.g. Ashikhmin Shirley.
  82. float diffuseFresnelNV = pow(clamp(1.0 - NdotL, 0.000001, 1.), 5.0);
  83. float diffuseFresnelNL = pow(clamp(1.0 - NdotV, 0.000001, 1.), 5.0);
  84. float diffuseFresnel90 = 0.5 + 2.0 * VdotH * VdotH * roughness;
  85. float diffuseFresnelTerm =
  86. (1.0 + (diffuseFresnel90 - 1.0) * diffuseFresnelNL) *
  87. (1.0 + (diffuseFresnel90 - 1.0) * diffuseFresnelNV);
  88. return diffuseFresnelTerm * NdotL;
  89. // PI Test
  90. // diffuseFresnelTerm /= kPi;
  91. }
  92. float computeDefaultGlossiness(float glossiness, vec3 specularColor)
  93. {
  94. float kSpecularNoAlphaWorkflow_SmoothnessMax = 0.95;
  95. float specularLuminance = getLuminance(specularColor);
  96. float specularLuma = sqrt(specularLuminance);
  97. glossiness = specularLuma * kSpecularNoAlphaWorkflow_SmoothnessMax;
  98. return glossiness;
  99. }
  100. vec3 toLinearSpace(vec3 color)
  101. {
  102. return vec3(pow(color.r, 2.2), pow(color.g, 2.2), pow(color.b, 2.2));
  103. }
  104. vec3 toGammaSpace(vec3 color)
  105. {
  106. return vec3(pow(color.r, 1.0 / 2.2), pow(color.g, 1.0 / 2.2), pow(color.b, 1.0 / 2.2));
  107. }
  108. vec3 toneMaps(vec3 color)
  109. {
  110. color = max(color, 0.0);
  111. // TONE MAPPING / EXPOSURE
  112. color.rgb = color.rgb * vPBRCameraInfos.x;
  113. float tuning = 1.5; // TODO: sync up so e.g. 18% greys are matched to exposure appropriately
  114. // PI Test
  115. // tuning *= kPi;
  116. vec3 tonemapped = 1.0 - exp2(-color.rgb * tuning); // simple local photographic tonemapper
  117. color.rgb = mix(color.rgb, tonemapped, 1.0);
  118. return color;
  119. }
  120. vec4 contrasts(vec4 color)
  121. {
  122. color = clamp(color, 0.0, 1.0);
  123. vec3 resultHighContrast = color.rgb * color.rgb * (3.0 - 2.0 * color.rgb);
  124. float contrast = vPBRCameraInfos.y;
  125. if (contrast < 1.0)
  126. {
  127. // Decrease contrast: interpolate towards zero-contrast image (flat grey)
  128. color.rgb = mix(vec3(0.5, 0.5, 0.5), color.rgb, contrast);
  129. }
  130. else
  131. {
  132. // Increase contrast: apply simple shoulder-toe high contrast curve
  133. color.rgb = mix(color.rgb, resultHighContrast, contrast - 1.0);
  134. }
  135. return color;
  136. }
  137. // END PBR HELPER METHODS
  138. #ifdef SPECULARTERM
  139. uniform vec4 vSpecularColor;
  140. #endif
  141. uniform vec3 vEmissiveColor;
  142. // Input
  143. varying vec3 vPositionW;
  144. #ifdef NORMAL
  145. varying vec3 vNormalW;
  146. #endif
  147. #ifdef VERTEXCOLOR
  148. varying vec4 vColor;
  149. #endif
  150. // Lights
  151. #ifdef LIGHT0
  152. uniform vec4 vLightData0;
  153. uniform vec4 vLightDiffuse0;
  154. #ifdef SPECULARTERM
  155. uniform vec3 vLightSpecular0;
  156. #endif
  157. #ifdef SHADOW0
  158. #if defined(SPOTLIGHT0) || defined(DIRLIGHT0)
  159. varying vec4 vPositionFromLight0;
  160. uniform sampler2D shadowSampler0;
  161. #else
  162. uniform samplerCube shadowSampler0;
  163. #endif
  164. uniform vec3 shadowsInfo0;
  165. #endif
  166. #ifdef SPOTLIGHT0
  167. uniform vec4 vLightDirection0;
  168. #endif
  169. #ifdef HEMILIGHT0
  170. uniform vec3 vLightGround0;
  171. #endif
  172. #endif
  173. #ifdef LIGHT1
  174. uniform vec4 vLightData1;
  175. uniform vec4 vLightDiffuse1;
  176. #ifdef SPECULARTERM
  177. uniform vec3 vLightSpecular1;
  178. #endif
  179. #ifdef SHADOW1
  180. #if defined(SPOTLIGHT1) || defined(DIRLIGHT1)
  181. varying vec4 vPositionFromLight1;
  182. uniform sampler2D shadowSampler1;
  183. #else
  184. uniform samplerCube shadowSampler1;
  185. #endif
  186. uniform vec3 shadowsInfo1;
  187. #endif
  188. #ifdef SPOTLIGHT1
  189. uniform vec4 vLightDirection1;
  190. #endif
  191. #ifdef HEMILIGHT1
  192. uniform vec3 vLightGround1;
  193. #endif
  194. #endif
  195. #ifdef LIGHT2
  196. uniform vec4 vLightData2;
  197. uniform vec4 vLightDiffuse2;
  198. #ifdef SPECULARTERM
  199. uniform vec3 vLightSpecular2;
  200. #endif
  201. #ifdef SHADOW2
  202. #if defined(SPOTLIGHT2) || defined(DIRLIGHT2)
  203. varying vec4 vPositionFromLight2;
  204. uniform sampler2D shadowSampler2;
  205. #else
  206. uniform samplerCube shadowSampler2;
  207. #endif
  208. uniform vec3 shadowsInfo2;
  209. #endif
  210. #ifdef SPOTLIGHT2
  211. uniform vec4 vLightDirection2;
  212. #endif
  213. #ifdef HEMILIGHT2
  214. uniform vec3 vLightGround2;
  215. #endif
  216. #endif
  217. #ifdef LIGHT3
  218. uniform vec4 vLightData3;
  219. uniform vec4 vLightDiffuse3;
  220. #ifdef SPECULARTERM
  221. uniform vec3 vLightSpecular3;
  222. #endif
  223. #ifdef SHADOW3
  224. #if defined(SPOTLIGHT3) || defined(DIRLIGHT3)
  225. varying vec4 vPositionFromLight3;
  226. uniform sampler2D shadowSampler3;
  227. #else
  228. uniform samplerCube shadowSampler3;
  229. #endif
  230. uniform vec3 shadowsInfo3;
  231. #endif
  232. #ifdef SPOTLIGHT3
  233. uniform vec4 vLightDirection3;
  234. #endif
  235. #ifdef HEMILIGHT3
  236. uniform vec3 vLightGround3;
  237. #endif
  238. #endif
  239. // Samplers
  240. #ifdef DIFFUSE
  241. varying vec2 vDiffuseUV;
  242. uniform sampler2D diffuseSampler;
  243. uniform vec2 vDiffuseInfos;
  244. #endif
  245. #ifdef AMBIENT
  246. varying vec2 vAmbientUV;
  247. uniform sampler2D ambientSampler;
  248. uniform vec2 vAmbientInfos;
  249. #endif
  250. #ifdef OPACITY
  251. varying vec2 vOpacityUV;
  252. uniform sampler2D opacitySampler;
  253. uniform vec2 vOpacityInfos;
  254. #endif
  255. #ifdef EMISSIVE
  256. varying vec2 vEmissiveUV;
  257. uniform vec2 vEmissiveInfos;
  258. uniform sampler2D emissiveSampler;
  259. #endif
  260. #ifdef LIGHTMAP
  261. varying vec2 vLightmapUV;
  262. uniform vec2 vLightmapInfos;
  263. uniform sampler2D lightmapSampler;
  264. #endif
  265. #if defined(SPECULAR) && defined(SPECULARTERM)
  266. varying vec2 vSpecularUV;
  267. uniform vec2 vSpecularInfos;
  268. uniform sampler2D specularSampler;
  269. #endif
  270. // Fresnel
  271. #ifdef FRESNEL
  272. float computeFresnelTerm(vec3 viewDirection, vec3 worldNormal, float bias, float power)
  273. {
  274. float fresnelTerm = pow(bias + abs(dot(viewDirection, worldNormal)), power);
  275. return clamp(fresnelTerm, 0., 1.);
  276. }
  277. #endif
  278. #ifdef DIFFUSEFRESNEL
  279. uniform vec4 diffuseLeftColor;
  280. uniform vec4 diffuseRightColor;
  281. #endif
  282. #ifdef OPACITYFRESNEL
  283. uniform vec4 opacityParts;
  284. #endif
  285. #ifdef EMISSIVEFRESNEL
  286. uniform vec4 emissiveLeftColor;
  287. uniform vec4 emissiveRightColor;
  288. #endif
  289. // Reflection
  290. #ifdef REFLECTION
  291. uniform vec2 vReflectionInfos;
  292. #ifdef REFLECTIONMAP_3D
  293. uniform samplerCube reflectionCubeSampler;
  294. #else
  295. uniform sampler2D reflection2DSampler;
  296. #endif
  297. #ifdef REFLECTIONMAP_SKYBOX
  298. varying vec3 vPositionUVW;
  299. #else
  300. #ifdef REFLECTIONMAP_EQUIRECTANGULAR
  301. varying vec3 vDirectionW;
  302. #endif
  303. #if defined(REFLECTIONMAP_PLANAR) || defined(REFLECTIONMAP_CUBIC) || defined(REFLECTIONMAP_PROJECTION)
  304. uniform mat4 reflectionMatrix;
  305. #endif
  306. #if defined(REFLECTIONMAP_SPHERICAL) || defined(REFLECTIONMAP_PROJECTION)
  307. uniform mat4 view;
  308. #endif
  309. #endif
  310. vec3 computeReflectionCoords(vec4 worldPos, vec3 worldNormal)
  311. {
  312. #ifdef REFLECTIONMAP_EQUIRECTANGULAR
  313. vec3 direction = normalize(vDirectionW);
  314. float t = clamp(direction.y * -0.5 + 0.5, 0., 1.0);
  315. float s = atan(direction.z, direction.x) * RECIPROCAL_PI2 + 0.5;
  316. return vec3(s, t, 0);
  317. #endif
  318. #ifdef REFLECTIONMAP_SPHERICAL
  319. vec3 viewDir = normalize(vec3(view * worldPos));
  320. vec3 viewNormal = normalize(vec3(view * vec4(worldNormal, 0.0)));
  321. vec3 r = reflect(viewDir, viewNormal);
  322. r.z = r.z - 1.0;
  323. float m = 2.0 * length(r);
  324. return vec3(r.x / m + 0.5, 1.0 - r.y / m - 0.5, 0);
  325. #endif
  326. #ifdef REFLECTIONMAP_PLANAR
  327. vec3 viewDir = worldPos.xyz - vEyePosition;
  328. vec3 coords = normalize(reflect(viewDir, worldNormal));
  329. return vec3(reflectionMatrix * vec4(coords, 1));
  330. #endif
  331. #ifdef REFLECTIONMAP_CUBIC
  332. vec3 viewDir = worldPos.xyz - vEyePosition;
  333. vec3 coords = reflect(viewDir, worldNormal);
  334. #ifdef INVERTCUBICMAP
  335. coords.y = 1.0 - coords.y;
  336. #endif
  337. return vec3(reflectionMatrix * vec4(coords, 0));
  338. #endif
  339. #ifdef REFLECTIONMAP_PROJECTION
  340. return vec3(reflectionMatrix * (view * worldPos));
  341. #endif
  342. #ifdef REFLECTIONMAP_SKYBOX
  343. return vPositionUVW;
  344. #endif
  345. #ifdef REFLECTIONMAP_EXPLICIT
  346. return vec3(0, 0, 0);
  347. #endif
  348. }
  349. #ifdef REFLECTIONFRESNEL
  350. uniform vec4 reflectionLeftColor;
  351. uniform vec4 reflectionRightColor;
  352. #endif
  353. #endif
  354. // Shadows
  355. #ifdef SHADOWS
  356. float unpack(vec4 color)
  357. {
  358. 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);
  359. return dot(color, bit_shift);
  360. }
  361. #if defined(POINTLIGHT0) || defined(POINTLIGHT1) || defined(POINTLIGHT2) || defined(POINTLIGHT3)
  362. float computeShadowCube(vec3 lightPosition, samplerCube shadowSampler, float darkness, float bias)
  363. {
  364. vec3 directionToLight = vPositionW - lightPosition;
  365. float depth = length(directionToLight);
  366. depth = clamp(depth, 0., 1.);
  367. directionToLight.y = 1.0 - directionToLight.y;
  368. float shadow = unpack(textureCube(shadowSampler, directionToLight)) + bias;
  369. if (depth > shadow)
  370. {
  371. return mix(1.0, darkness, vPBRShadowIntensity.x);
  372. }
  373. return 1.0;
  374. }
  375. float computeShadowWithPCFCube(vec3 lightPosition, samplerCube shadowSampler, float mapSize, float bias, float darkness)
  376. {
  377. vec3 directionToLight = vPositionW - lightPosition;
  378. float depth = length(directionToLight);
  379. float diskScale = (1.0 - (1.0 + depth * 3.0)) / mapSize;
  380. depth = clamp(depth, 0., 1.);
  381. directionToLight.y = 1.0 - directionToLight.y;
  382. float visibility = 1.;
  383. vec3 poissonDisk[4];
  384. poissonDisk[0] = vec3(-1.0, 1.0, -1.0);
  385. poissonDisk[1] = vec3(1.0, -1.0, -1.0);
  386. poissonDisk[2] = vec3(-1.0, -1.0, -1.0);
  387. poissonDisk[3] = vec3(1.0, -1.0, 1.0);
  388. // Poisson Sampling
  389. float biasedDepth = depth - bias;
  390. if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[0] * diskScale)) < biasedDepth) visibility -= 0.25;
  391. if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[1] * diskScale)) < biasedDepth) visibility -= 0.25;
  392. if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[2] * diskScale)) < biasedDepth) visibility -= 0.25;
  393. if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[3] * diskScale)) < biasedDepth) visibility -= 0.25;
  394. return min(1.0, mix(1.0, visibility + darkness, vPBRShadowIntensity.x));
  395. }
  396. #endif
  397. #if defined(SPOTLIGHT0) || defined(SPOTLIGHT1) || defined(SPOTLIGHT2) || defined(SPOTLIGHT3) || defined(DIRLIGHT0) || defined(DIRLIGHT1) || defined(DIRLIGHT2) || defined(DIRLIGHT3)
  398. float computeShadow(vec4 vPositionFromLight, sampler2D shadowSampler, float darkness, float bias)
  399. {
  400. vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
  401. depth = 0.5 * depth + vec3(0.5);
  402. vec2 uv = depth.xy;
  403. if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0)
  404. {
  405. return 1.0;
  406. }
  407. float shadow = unpack(texture2D(shadowSampler, uv)) + bias;
  408. if (depth.z > shadow)
  409. {
  410. return mix(1.0, darkness, vPBRShadowIntensity.x);
  411. }
  412. return 1.;
  413. }
  414. float computeShadowWithPCF(vec4 vPositionFromLight, sampler2D shadowSampler, float mapSize, float bias, float darkness)
  415. {
  416. vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
  417. depth = 0.5 * depth + vec3(0.5);
  418. vec2 uv = depth.xy;
  419. if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0)
  420. {
  421. return 1.0;
  422. }
  423. float visibility = 1.;
  424. vec2 poissonDisk[4];
  425. poissonDisk[0] = vec2(-0.94201624, -0.39906216);
  426. poissonDisk[1] = vec2(0.94558609, -0.76890725);
  427. poissonDisk[2] = vec2(-0.094184101, -0.92938870);
  428. poissonDisk[3] = vec2(0.34495938, 0.29387760);
  429. // Poisson Sampling
  430. float biasedDepth = depth.z - bias;
  431. if (unpack(texture2D(shadowSampler, uv + poissonDisk[0] / mapSize)) < biasedDepth) visibility -= 0.25;
  432. if (unpack(texture2D(shadowSampler, uv + poissonDisk[1] / mapSize)) < biasedDepth) visibility -= 0.25;
  433. if (unpack(texture2D(shadowSampler, uv + poissonDisk[2] / mapSize)) < biasedDepth) visibility -= 0.25;
  434. if (unpack(texture2D(shadowSampler, uv + poissonDisk[3] / mapSize)) < biasedDepth) visibility -= 0.25;
  435. return min(1.0, mix(1.0, visibility + darkness, vPBRShadowIntensity.x));
  436. }
  437. // Thanks to http://devmaster.net/
  438. float unpackHalf(vec2 color)
  439. {
  440. return color.x + (color.y / 255.0);
  441. }
  442. float linstep(float low, float high, float v) {
  443. return clamp((v - low) / (high - low), 0.0, 1.0);
  444. }
  445. float ChebychevInequality(vec2 moments, float compare, float bias)
  446. {
  447. float p = smoothstep(compare - bias, compare, moments.x);
  448. float variance = max(moments.y - moments.x * moments.x, 0.02);
  449. float d = compare - moments.x;
  450. float p_max = linstep(0.2, 1.0, variance / (variance + d * d));
  451. return clamp(max(p, p_max), 0.0, 1.0);
  452. }
  453. float computeShadowWithVSM(vec4 vPositionFromLight, sampler2D shadowSampler, float bias, float darkness)
  454. {
  455. vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
  456. depth = 0.5 * depth + vec3(0.5);
  457. vec2 uv = depth.xy;
  458. if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0 || depth.z >= 1.0)
  459. {
  460. return 1.0;
  461. }
  462. vec4 texel = texture2D(shadowSampler, uv);
  463. vec2 moments = vec2(unpackHalf(texel.xy), unpackHalf(texel.zw));
  464. return min(1.0, mix(1.0, 1.0 - ChebychevInequality(moments, depth.z, bias) + darkness, vPBRShadowIntensity.x));
  465. }
  466. #endif
  467. #endif
  468. // Bump
  469. #ifdef BUMP
  470. #extension GL_OES_standard_derivatives : enable
  471. varying vec2 vBumpUV;
  472. uniform vec2 vBumpInfos;
  473. uniform sampler2D bumpSampler;
  474. // Thanks to http://www.thetenthplanet.de/archives/1180
  475. mat3 cotangent_frame(vec3 normal, vec3 p, vec2 uv)
  476. {
  477. // get edge vectors of the pixel triangle
  478. vec3 dp1 = dFdx(p);
  479. vec3 dp2 = dFdy(p);
  480. vec2 duv1 = dFdx(uv);
  481. vec2 duv2 = dFdy(uv);
  482. // solve the linear system
  483. vec3 dp2perp = cross(dp2, normal);
  484. vec3 dp1perp = cross(normal, dp1);
  485. vec3 tangent = dp2perp * duv1.x + dp1perp * duv2.x;
  486. vec3 binormal = dp2perp * duv1.y + dp1perp * duv2.y;
  487. // construct a scale-invariant frame
  488. float invmax = inversesqrt(max(dot(tangent, tangent), dot(binormal, binormal)));
  489. return mat3(tangent * invmax, binormal * invmax, normal);
  490. }
  491. vec3 perturbNormal(vec3 viewDir)
  492. {
  493. vec3 map = texture2D(bumpSampler, vBumpUV).xyz;
  494. map = map * 255. / 127. - 128. / 127.;
  495. mat3 TBN = cotangent_frame(vNormalW * vBumpInfos.y, -viewDir, vBumpUV);
  496. return normalize(TBN * map);
  497. }
  498. #endif
  499. #ifdef CLIPPLANE
  500. varying float fClipDistance;
  501. #endif
  502. // Fog
  503. #ifdef FOG
  504. #define FOGMODE_NONE 0.
  505. #define FOGMODE_EXP 1.
  506. #define FOGMODE_EXP2 2.
  507. #define FOGMODE_LINEAR 3.
  508. #define E 2.71828
  509. uniform vec4 vFogInfos;
  510. uniform vec3 vFogColor;
  511. varying float fFogDistance;
  512. float CalcFogFactor()
  513. {
  514. float fogCoeff = 1.0;
  515. float fogStart = vFogInfos.y;
  516. float fogEnd = vFogInfos.z;
  517. float fogDensity = vFogInfos.w;
  518. if (FOGMODE_LINEAR == vFogInfos.x)
  519. {
  520. fogCoeff = (fogEnd - fFogDistance) / (fogEnd - fogStart);
  521. }
  522. else if (FOGMODE_EXP == vFogInfos.x)
  523. {
  524. fogCoeff = 1.0 / pow(E, fFogDistance * fogDensity);
  525. }
  526. else if (FOGMODE_EXP2 == vFogInfos.x)
  527. {
  528. fogCoeff = 1.0 / pow(E, fFogDistance * fFogDistance * fogDensity * fogDensity);
  529. }
  530. return clamp(fogCoeff, 0.0, 1.0);
  531. }
  532. #endif
  533. // Light Computing
  534. struct lightingInfo
  535. {
  536. vec3 diffuse;
  537. #ifdef SPECULARTERM
  538. vec3 specular;
  539. #endif
  540. };
  541. lightingInfo computeLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, vec3 specularColor, float range, float roughness, float NdotV) {
  542. lightingInfo result;
  543. vec3 lightVectorW;
  544. float attenuation = 1.0;
  545. if (lightData.w == 0.)
  546. {
  547. vec3 direction = lightData.xyz - vPositionW;
  548. attenuation = max(0., 1.0 - length(direction) / range);
  549. lightVectorW = normalize(direction);
  550. }
  551. else
  552. {
  553. lightVectorW = normalize(-lightData.xyz);
  554. }
  555. // diffuse
  556. vec3 H = normalize(viewDirectionW + lightVectorW);
  557. float NdotL = max(0.00000000001, dot(vNormal, lightVectorW));
  558. float VdotH = clamp(0.00000000001, 1.0, dot(viewDirectionW, H));
  559. float diffuseTerm = computeDiffuseTerm(NdotL, NdotV, VdotH, roughness);
  560. result.diffuse = diffuseTerm * diffuseColor * attenuation;
  561. #ifdef SPECULARTERM
  562. // Specular
  563. float NdotH = max(0.00000000001, dot(vNormal, H));
  564. vec3 specTerm = computeSpecularTerm(NdotH, NdotL, NdotV, VdotH, roughness, specularColor);
  565. result.specular = specTerm * attenuation;
  566. #endif
  567. return result;
  568. }
  569. lightingInfo computeSpotLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec4 lightDirection, vec3 diffuseColor, vec3 specularColor, float range, float roughness, float NdotV) {
  570. lightingInfo result;
  571. vec3 direction = lightData.xyz - vPositionW;
  572. vec3 lightVectorW = normalize(direction);
  573. float attenuation = max(0., 1.0 - length(direction) / range);
  574. // diffuse
  575. float cosAngle = max(0.0000001, dot(-lightDirection.xyz, lightVectorW));
  576. float spotAtten = 0.0;
  577. if (cosAngle >= lightDirection.w)
  578. {
  579. cosAngle = max(0., pow(cosAngle, lightData.w));
  580. spotAtten = clamp((cosAngle - lightDirection.w) / (1. - cosAngle), 0.0, 1.0);
  581. // Diffuse
  582. vec3 H = normalize(viewDirectionW - lightDirection.xyz);
  583. float NdotL = max(0.00000000001, dot(vNormal, -lightDirection.xyz));
  584. float VdotH = clamp(dot(viewDirectionW, H), 0.00000000001, 1.0);
  585. float diffuseTerm = computeDiffuseTerm(NdotL, NdotV, VdotH, roughness);
  586. result.diffuse = diffuseTerm * diffuseColor * attenuation * spotAtten;
  587. #ifdef SPECULARTERM
  588. // Specular
  589. float NdotH = max(0.00000000001, dot(vNormal, H));
  590. vec3 specTerm = computeSpecularTerm(NdotH, NdotL, NdotV, VdotH, roughness, specularColor);
  591. result.specular = specTerm * attenuation * spotAtten;
  592. #endif
  593. return result;
  594. }
  595. result.diffuse = vec3(0.);
  596. #ifdef SPECULARTERM
  597. result.specular = vec3(0.);
  598. #endif
  599. return result;
  600. }
  601. lightingInfo computeHemisphericLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, vec3 specularColor, vec3 groundColor, float roughness, float NdotV) {
  602. lightingInfo result;
  603. vec3 lightVectorW = normalize(lightData.xyz);
  604. // Diffuse
  605. float ndl = dot(vNormal, lightData.xyz) * 0.5 + 0.5;
  606. result.diffuse = mix(groundColor, diffuseColor, ndl);
  607. #ifdef SPECULARTERM
  608. // Specular
  609. vec3 H = normalize(viewDirectionW + lightVectorW);
  610. float NdotH = max(0.00000000001, dot(vNormal, H));
  611. float NdotL = max(0.00000000001, ndl);
  612. float VdotH = clamp(0.00000000001, 1.0, dot(viewDirectionW, H));
  613. vec3 specTerm = computeSpecularTerm(NdotH, NdotL, NdotV, VdotH, roughness, specularColor);
  614. result.specular = specTerm;
  615. #endif
  616. return result;
  617. }
  618. void main(void) {
  619. // Clip plane
  620. #ifdef CLIPPLANE
  621. if (fClipDistance > 0.0)
  622. discard;
  623. #endif
  624. vec3 viewDirectionW = normalize(vEyePosition - vPositionW);
  625. // Base color
  626. vec4 baseColor = vec4(1., 1., 1., 1.);
  627. vec3 diffuseColor = vDiffuseColor.rgb;
  628. // Alpha
  629. float alpha = vDiffuseColor.a;
  630. #ifdef DIFFUSE
  631. baseColor = texture2D(diffuseSampler, vDiffuseUV);
  632. baseColor = vec4(toLinearSpace(baseColor.rgb), baseColor.a);
  633. #ifdef ALPHATEST
  634. if (baseColor.a < 0.4)
  635. discard;
  636. #endif
  637. #ifdef ALPHAFROMDIFFUSE
  638. alpha *= baseColor.a;
  639. #endif
  640. baseColor.rgb *= vDiffuseInfos.y;
  641. #endif
  642. baseColor.rgb = mix(baseColor.rgb, vPBROverloadedDiffuse, vPBROverloadedIntensity.y);
  643. #ifdef VERTEXCOLOR
  644. baseColor.rgb *= vColor.rgb;
  645. #endif
  646. // Bump
  647. #ifdef NORMAL
  648. vec3 normalW = normalize(vNormalW);
  649. #else
  650. vec3 normalW = vec3(1.0, 1.0, 1.0);
  651. #endif
  652. #ifdef BUMP
  653. normalW = perturbNormal(viewDirectionW);
  654. #endif
  655. // Ambient color
  656. vec3 baseAmbientColor = vec3(1., 1., 1.);
  657. #ifdef AMBIENT
  658. baseAmbientColor = texture2D(ambientSampler, vAmbientUV).rgb * vAmbientInfos.y;
  659. baseAmbientColor.rgb = mix(baseAmbientColor.rgb, vPBROverloadedAmbient, vPBROverloadedIntensity.x);
  660. #endif
  661. // Specular map
  662. #ifdef SPECULARTERM
  663. float glossiness = vSpecularColor.a;
  664. vec3 specularColor = vSpecularColor.rgb;
  665. specularColor.rgb = mix(specularColor.rgb, vPBROverloadedSpecular, vPBROverloadedIntensity.z);
  666. #ifdef SPECULAR
  667. vec4 specularMapColor = texture2D(specularSampler, vSpecularUV);
  668. specularColor = toLinearSpace(specularMapColor.rgb);
  669. specularColor.rgb = mix(specularColor.rgb, vPBROverloadedSpecular, vPBROverloadedIntensity.z);
  670. #ifdef GLOSSINESS
  671. glossiness = specularMapColor.a;
  672. #else
  673. glossiness = computeDefaultGlossiness(glossiness, specularColor);
  674. #endif
  675. #endif
  676. glossiness = mix(glossiness, vPBROverloadedSmoothness.x, vPBROverloadedSmoothness.y);
  677. #else
  678. float glossiness = 0.;
  679. glossiness = mix(glossiness, vPBROverloadedSmoothness.x, vPBROverloadedSmoothness.y);
  680. vec3 specularColor = vec3(0., 0., 0);
  681. specularColor.rgb = mix(specularColor.rgb, vPBROverloadedSpecular, vPBROverloadedIntensity.z);
  682. #endif
  683. // Apply Energy Conservation.
  684. float reflectance = max(max(specularColor.r, specularColor.g), specularColor.b);
  685. baseColor.rgb = (1. - reflectance) * baseColor.rgb;
  686. // Compute Specular Fresnel + Reflectance.
  687. float NdotV = max(0.00000000001, dot(normalW, viewDirectionW));
  688. // Adapt glossiness.
  689. glossiness = clamp(glossiness, 0., 1.) * 0.98;
  690. // Call rough to not conflict with previous one.
  691. float rough = clamp(1. - glossiness, 0.000001, 1.0);
  692. // Lighting
  693. vec3 diffuseBase = vec3(0., 0., 0.);
  694. vec3 shadowedOnly = vPBROverloadedDiffuse;
  695. #ifdef SPECULARTERM
  696. vec3 specularBase = vec3(0., 0., 0.);
  697. #endif
  698. float shadow = 1.;
  699. #ifdef LIGHT0
  700. #ifndef SPECULARTERM
  701. vec3 vLightSpecular0 = vec3(0.0);
  702. #endif
  703. #ifdef SPOTLIGHT0
  704. lightingInfo info = computeSpotLighting(viewDirectionW, normalW, vLightData0, vLightDirection0, vLightDiffuse0.rgb, vLightSpecular0, vLightDiffuse0.a, rough, NdotV);
  705. #endif
  706. #ifdef HEMILIGHT0
  707. lightingInfo info = computeHemisphericLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightSpecular0, vLightGround0, rough, NdotV);
  708. #endif
  709. #if defined(POINTLIGHT0) || defined(DIRLIGHT0)
  710. lightingInfo info = computeLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightSpecular0, vLightDiffuse0.a, rough, NdotV);
  711. #endif
  712. #ifdef SHADOW0
  713. #ifdef SHADOWVSM0
  714. shadow = computeShadowWithVSM(vPositionFromLight0, shadowSampler0, shadowsInfo0.z, shadowsInfo0.x);
  715. #else
  716. #ifdef SHADOWPCF0
  717. #if defined(POINTLIGHT0)
  718. shadow = computeShadowWithPCFCube(vLightData0.xyz, shadowSampler0, shadowsInfo0.y, shadowsInfo0.z, shadowsInfo0.x);
  719. #else
  720. shadow = computeShadowWithPCF(vPositionFromLight0, shadowSampler0, shadowsInfo0.y, shadowsInfo0.z, shadowsInfo0.x);
  721. #endif
  722. #else
  723. #if defined(POINTLIGHT0)
  724. shadow = computeShadowCube(vLightData0.xyz, shadowSampler0, shadowsInfo0.x, shadowsInfo0.z);
  725. #else
  726. shadow = computeShadow(vPositionFromLight0, shadowSampler0, shadowsInfo0.x, shadowsInfo0.z);
  727. #endif
  728. #endif
  729. #endif
  730. #else
  731. shadow = 1.;
  732. #endif
  733. diffuseBase += info.diffuse * shadow;
  734. shadowedOnly *= shadow;
  735. #ifdef SPECULARTERM
  736. specularBase += info.specular * shadow;
  737. #endif
  738. #endif
  739. #ifdef LIGHT1
  740. #ifndef SPECULARTERM
  741. vec3 vLightSpecular1 = vec3(0.0);
  742. #endif
  743. #ifdef SPOTLIGHT1
  744. info = computeSpotLighting(viewDirectionW, normalW, vLightData1, vLightDirection1, vLightDiffuse1.rgb, vLightSpecular1, vLightDiffuse1.a, rough, NdotV);
  745. #endif
  746. #ifdef HEMILIGHT1
  747. info = computeHemisphericLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightSpecular1, vLightGround1, rough, NdotV);
  748. #endif
  749. #if defined(POINTLIGHT1) || defined(DIRLIGHT1)
  750. info = computeLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightSpecular1, vLightDiffuse1.a, rough, NdotV);
  751. #endif
  752. #ifdef SHADOW1
  753. #ifdef SHADOWVSM1
  754. shadow = computeShadowWithVSM(vPositionFromLight1, shadowSampler1, shadowsInfo1.z, shadowsInfo1.x);
  755. #else
  756. #ifdef SHADOWPCF1
  757. #if defined(POINTLIGHT1)
  758. shadow = computeShadowWithPCFCube(vLightData1.xyz, shadowSampler1, shadowsInfo1.y, shadowsInfo1.z, shadowsInfo1.x);
  759. #else
  760. shadow = computeShadowWithPCF(vPositionFromLight1, shadowSampler1, shadowsInfo1.y, shadowsInfo1.z, shadowsInfo1.x);
  761. #endif
  762. #else
  763. #if defined(POINTLIGHT1)
  764. shadow = computeShadowCube(vLightData1.xyz, shadowSampler1, shadowsInfo1.x, shadowsInfo1.z);
  765. #else
  766. shadow = computeShadow(vPositionFromLight1, shadowSampler1, shadowsInfo1.x, shadowsInfo1.z);
  767. #endif
  768. #endif
  769. #endif
  770. #else
  771. shadow = 1.;
  772. #endif
  773. diffuseBase += info.diffuse * shadow;
  774. shadowedOnly *= shadow;
  775. #ifdef SPECULARTERM
  776. specularBase += info.specular * shadow;
  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, rough, NdotV);
  785. #endif
  786. #ifdef HEMILIGHT2
  787. info = computeHemisphericLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightSpecular2, vLightGround2, rough, NdotV);
  788. #endif
  789. #if defined(POINTLIGHT2) || defined(DIRLIGHT2)
  790. info = computeLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightSpecular2, vLightDiffuse2.a, rough, NdotV);
  791. #endif
  792. #ifdef SHADOW2
  793. #ifdef SHADOWVSM2
  794. shadow = computeShadowWithVSM(vPositionFromLight2, shadowSampler2, shadowsInfo2.z, shadowsInfo2.x);
  795. #else
  796. #ifdef SHADOWPCF2
  797. #if defined(POINTLIGHT2)
  798. shadow = computeShadowWithPCFCube(vLightData2.xyz, shadowSampler2, shadowsInfo2.y, shadowsInfo2.z, shadowsInfo2.x);
  799. #else
  800. shadow = computeShadowWithPCF(vPositionFromLight2, shadowSampler2, shadowsInfo2.y, shadowsInfo2.z, shadowsInfo2.x);
  801. #endif
  802. #else
  803. #if defined(POINTLIGHT2)
  804. shadow = computeShadowCube(vLightData2.xyz, shadowSampler2, shadowsInfo2.x, shadowsInfo2.z);
  805. #else
  806. shadow = computeShadow(vPositionFromLight2, shadowSampler2, shadowsInfo2.x, shadowsInfo2.z);
  807. #endif
  808. #endif
  809. #endif
  810. #else
  811. shadow = 1.;
  812. #endif
  813. diffuseBase += info.diffuse * shadow;
  814. shadowedOnly *= shadow;
  815. #ifdef SPECULARTERM
  816. specularBase += info.specular * shadow;
  817. #endif
  818. #endif
  819. #ifdef LIGHT3
  820. #ifndef SPECULARTERM
  821. vec3 vLightSpecular3 = vec3(0.0);
  822. #endif
  823. #ifdef SPOTLIGHT3
  824. info = computeSpotLighting(viewDirectionW, normalW, vLightData3, vLightDirection3, vLightDiffuse3.rgb, vLightSpecular3, vLightDiffuse3.a, rough, NdotV);
  825. #endif
  826. #ifdef HEMILIGHT3
  827. info = computeHemisphericLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightSpecular3, vLightGround3, rough, NdotV);
  828. #endif
  829. #if defined(POINTLIGHT3) || defined(DIRLIGHT3)
  830. info = computeLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightSpecular3, vLightDiffuse3.a, rough, NdotV);
  831. #endif
  832. #ifdef SHADOW3
  833. #ifdef SHADOWVSM3
  834. shadow = computeShadowWithVSM(vPositionFromLight3, shadowSampler3, shadowsInfo3.z, shadowsInfo3.x);
  835. #else
  836. #ifdef SHADOWPCF3
  837. #if defined(POINTLIGHT3)
  838. shadow = computeShadowWithPCFCube(vLightData3.xyz, shadowSampler3, shadowsInfo3.y, shadowsInfo3.z, shadowsInfo3.x);
  839. #else
  840. shadow = computeShadowWithPCF(vPositionFromLight3, shadowSampler3, shadowsInfo3.y, shadowsInfo3.z, shadowsInfo3.x);
  841. #endif
  842. #else
  843. #if defined(POINTLIGHT3)
  844. shadow = computeShadowCube(vLightData3.xyz, shadowSampler3, shadowsInfo3.x, shadowsInfo3.z);
  845. #else
  846. shadow = computeShadow(vPositionFromLight3, shadowSampler3, shadowsInfo3.x, shadowsInfo3.z);
  847. #endif
  848. #endif
  849. #endif
  850. #else
  851. shadow = 1.;
  852. #endif
  853. diffuseBase += info.diffuse * shadow;
  854. shadowedOnly *= shadow;
  855. #ifdef SPECULARTERM
  856. specularBase += info.specular * shadow;
  857. #endif
  858. #endif
  859. // Reflection
  860. vec3 reflectionColor = vec3(0., 0., 0.);
  861. #ifdef REFLECTION
  862. vec3 vReflectionUVW = computeReflectionCoords(vec4(vPositionW, 1.0), normalW);
  863. vec3 ambientReflectionColor = vec3(0.1, 0.1, 0.1);
  864. #ifdef REFLECTIONMAP_3D
  865. float bias = 0.;
  866. #ifdef ROUGHNESS
  867. bias = 20.;
  868. #ifdef SPECULARTERM
  869. bias *= (1.0 - glossiness);
  870. #endif
  871. #endif
  872. reflectionColor = textureCube(reflectionCubeSampler, vReflectionUVW, bias).rgb * vReflectionInfos.x;
  873. reflectionColor = toLinearSpace(reflectionColor.rgb);
  874. ambientReflectionColor = textureCube(reflectionCubeSampler, normalW, 20.).rgb;
  875. ambientReflectionColor = toLinearSpace(ambientReflectionColor.rgb);
  876. reflectionColor = reflectionColor * vReflectionInfos.y;
  877. //reflectionColor = reflectionColor * vReflectionInfos.y * shadow;
  878. #else
  879. vec2 coords = vReflectionUVW.xy;
  880. #ifdef REFLECTIONMAP_PROJECTION
  881. coords /= vReflectionUVW.z;
  882. #endif
  883. coords.y = 1.0 - coords.y;
  884. reflectionColor = texture2D(reflection2DSampler, coords).rgb * vReflectionInfos.x;
  885. reflectionColor = toLinearSpace(reflectionColor.rgb);
  886. ambientReflectionColor = texture2D(reflection2DSampler, coords, 10.).rgb;
  887. ambientReflectionColor = toLinearSpace(ambientReflectionColor.rgb);
  888. #endif
  889. #ifdef REFLECTIONFRESNEL
  890. #ifdef REFLECTIONFRESNELFROMSPECULAR
  891. // Compute reflection specular fresnel
  892. vec3 specularEnvironmentR0 = specularColor.rgb;
  893. vec3 specularEnvironmentR90 = reflectionLeftColor.rgb;
  894. vec3 specularEnvironmentReflectanceViewer = FresnelSchlickEnvironmentGGX(clamp(NdotV, 0., 1.), specularEnvironmentR0, specularEnvironmentR90, sqrt(glossiness));
  895. reflectionColor *= specularEnvironmentReflectanceViewer;
  896. #else
  897. float reflectionFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, reflectionRightColor.a, reflectionLeftColor.a);
  898. reflectionColor *= reflectionLeftColor.rgb * (1.0 - reflectionFresnelTerm) + reflectionFresnelTerm * reflectionRightColor.rgb;
  899. #endif
  900. #endif
  901. #endif
  902. #ifdef OPACITY
  903. vec4 opacityMap = texture2D(opacitySampler, vOpacityUV);
  904. #ifdef OPACITYRGB
  905. opacityMap.rgb = opacityMap.rgb * vec3(0.3, 0.59, 0.11);
  906. alpha *= (opacityMap.x + opacityMap.y + opacityMap.z)* vOpacityInfos.y;
  907. #else
  908. alpha *= opacityMap.a * vOpacityInfos.y;
  909. #endif
  910. #endif
  911. #ifdef VERTEXALPHA
  912. alpha *= vColor.a;
  913. #endif
  914. #ifdef OPACITYFRESNEL
  915. float opacityFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, opacityParts.z, opacityParts.w);
  916. alpha += opacityParts.x * (1.0 - opacityFresnelTerm) + opacityFresnelTerm * opacityParts.y;
  917. #endif
  918. // Emissive
  919. vec3 emissiveColor = vEmissiveColor;
  920. #ifdef EMISSIVE
  921. vec3 emissiveColorTex = texture2D(emissiveSampler, vEmissiveUV).rgb;
  922. emissiveColor = toLinearSpace(emissiveColorTex.rgb) * emissiveColor * vEmissiveInfos.y;
  923. emissiveColor = mix(emissiveColor, vPBROverloadedEmissive, vPBROverloadedIntensity.w);
  924. #endif
  925. #ifdef EMISSIVEFRESNEL
  926. float emissiveFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, emissiveRightColor.a, emissiveLeftColor.a);
  927. emissiveColor *= emissiveLeftColor.rgb * (1.0 - emissiveFresnelTerm) + emissiveFresnelTerm * emissiveRightColor.rgb;
  928. #endif
  929. // Fresnel
  930. #ifdef DIFFUSEFRESNEL
  931. float diffuseFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, diffuseRightColor.a, diffuseLeftColor.a);
  932. diffuseBase *= diffuseLeftColor.rgb * (1.0 - diffuseFresnelTerm) + diffuseFresnelTerm * diffuseRightColor.rgb;
  933. #endif
  934. // Composition
  935. #ifdef EMISSIVEASILLUMINATION
  936. vec3 finalDiffuse = max(diffuseBase * diffuseColor + vAmbientColor, 0.0) * baseColor.rgb;
  937. #else
  938. #ifdef LINKEMISSIVEWITHDIFFUSE
  939. vec3 finalDiffuse = clamp((diffuseBase + emissiveColor) * diffuseColor + vAmbientColor, 0.0, 1.0) * baseColor.rgb;
  940. #else
  941. vec3 finalDiffuse = clamp(diffuseBase * diffuseColor + emissiveColor + vAmbientColor, 0.0, 1.0) * baseColor.rgb;
  942. #endif
  943. #endif
  944. #ifdef REFLECTION
  945. // diffuse lighting from environment
  946. finalDiffuse += baseColor.rgb * ambientReflectionColor * 0.2 * vPBRLightingIntensity.z;
  947. #endif
  948. #ifdef SPECULARTERM
  949. vec3 finalSpecular = specularBase * specularColor;
  950. #else
  951. vec3 finalSpecular = vec3(0.0);
  952. #endif
  953. #ifdef SPECULAROVERALPHA
  954. alpha = clamp(alpha + dot(finalSpecular, vec3(0.3, 0.59, 0.11)), 0., 1.);
  955. #endif
  956. // Composition
  957. #ifdef EMISSIVEASILLUMINATION
  958. vec4 color = vec4(finalDiffuse * baseAmbientColor * vPBRLightingIntensity.x + finalSpecular * vPBRLightingIntensity.x + reflectionColor * vPBRLightingIntensity.z + emissiveColor * vPBRLightingIntensity.y, alpha);
  959. #else
  960. vec4 color = vec4(finalDiffuse * baseAmbientColor + finalSpecular + reflectionColor, alpha);
  961. #endif
  962. #ifdef LIGHTMAP
  963. vec3 lightmapColor = texture2D(lightmapSampler, vLightmapUV).rgb * vLightmapInfos.y;
  964. #ifdef USELIGHTMAPASSHADOWMAP
  965. color.rgb *= lightmapColor;
  966. #else
  967. color.rgb += lightmapColor;
  968. #endif
  969. #endif
  970. #ifdef FOG
  971. float fog = CalcFogFactor();
  972. color.rgb = fog * color.rgb + (1.0 - fog) * vFogColor;
  973. #endif
  974. color = max(color, 0.0);
  975. color.rgb = toneMaps(color.rgb);
  976. color.rgb = toGammaSpace(color.rgb);
  977. color = contrasts(color);
  978. color.rgb = mix(color.rgb, shadowedOnly, (1.0 - vPBRShadowIntensity.y));
  979. // Normal Display.
  980. // gl_FragColor = vec4(normalW * 0.5 + 0.5, 1.0);
  981. // Ambient reflection color.
  982. // gl_FragColor = vec4(ambientReflectionColor, 1.0);
  983. // Reflection color.
  984. // gl_FragColor = vec4(reflectionColor, 1.0);
  985. // Base color.
  986. // gl_FragColor = vec4(baseColor.rgb, 1.0);
  987. // Specular color.
  988. // gl_FragColor = vec4(specularColor.rgb, 1.0);
  989. // Glossiness color.
  990. // gl_FragColor = vec4(glossiness, glossiness, glossiness, 1.0);
  991. // Specular Map
  992. // gl_FragColor = vec4(specularMapColor.rgb, 1.0);
  993. //// Emissive Color
  994. //vec2 test = vEmissiveUV * 0.5 + 0.5;
  995. //gl_FragColor = vec4(test.x, test.y, 1.0, 1.0);
  996. gl_FragColor = color;
  997. }