legacypbr.fragment.fx 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. precision mediump 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. uniform vec3 vReflectionColor;
  9. // CUSTOM CONTROLS
  10. uniform vec4 vLightingIntensity;
  11. uniform vec4 vCameraInfos;
  12. #ifdef OVERLOADEDVALUES
  13. uniform vec4 vOverloadedIntensity;
  14. uniform vec3 vOverloadedAmbient;
  15. uniform vec3 vOverloadedDiffuse;
  16. uniform vec3 vOverloadedSpecular;
  17. uniform vec3 vOverloadedEmissive;
  18. uniform vec3 vOverloadedReflection;
  19. uniform vec3 vOverloadedGlossiness;
  20. #endif
  21. #ifdef OVERLOADEDSHADOWVALUES
  22. uniform vec4 vOverloadedShadowIntensity;
  23. #endif
  24. // PBR CUSTOM CONSTANTS
  25. const float kPi = 3.1415926535897932384626433832795;
  26. // PBR HELPER METHODS
  27. float Square(float value)
  28. {
  29. return value * value;
  30. }
  31. float getLuminance(vec3 color)
  32. {
  33. return clamp(dot(color, vec3(0.2126, 0.7152, 0.0722)), 0., 1.);
  34. }
  35. float convertRoughnessToAverageSlope(float roughness)
  36. {
  37. // Calculate AlphaG as square of roughness; add epsilon to avoid numerical issues
  38. const float kMinimumVariance = 0.0005;
  39. float alphaG = Square(roughness) + kMinimumVariance;
  40. return alphaG;
  41. }
  42. // From Microfacet Models for Refraction through Rough Surfaces, Walter et al. 2007
  43. float smithVisibilityG1_TrowbridgeReitzGGX(float dot, float alphaG)
  44. {
  45. float tanSquared = (1.0 - dot * dot) / (dot * dot);
  46. return 2.0 / (1.0 + sqrt(1.0 + alphaG * alphaG * tanSquared));
  47. }
  48. float smithVisibilityG_TrowbridgeReitzGGX_Walter(float NdotL, float NdotV, float alphaG)
  49. {
  50. return smithVisibilityG1_TrowbridgeReitzGGX(NdotL, alphaG) * smithVisibilityG1_TrowbridgeReitzGGX(NdotV, alphaG);
  51. }
  52. // Trowbridge-Reitz (GGX)
  53. // Generalised Trowbridge-Reitz with gamma power=2.0
  54. float normalDistributionFunction_TrowbridgeReitzGGX(float NdotH, float alphaG)
  55. {
  56. // Note: alphaG is average slope (gradient) of the normals in slope-space.
  57. // It is also the (trigonometric) tangent of the median distribution value, i.e. 50% of normals have
  58. // a tangent (gradient) closer to the macrosurface than this slope.
  59. float a2 = Square(alphaG);
  60. float d = NdotH * NdotH * (a2 - 1.0) + 1.0;
  61. return a2 / (kPi * d * d);
  62. }
  63. vec3 fresnelSchlickGGX(float VdotH, vec3 reflectance0, vec3 reflectance90)
  64. {
  65. return reflectance0 + (reflectance90 - reflectance0) * pow(clamp(1.0 - VdotH, 0., 1.), 5.0);
  66. }
  67. vec3 FresnelSchlickEnvironmentGGX(float VdotN, vec3 reflectance0, vec3 reflectance90, float smoothness)
  68. {
  69. // Schlick fresnel approximation, extended with basic smoothness term so that rough surfaces do not approach reflectance90 at grazing angle
  70. float weight = mix(FRESNEL_MAXIMUM_ON_ROUGH, 1.0, smoothness);
  71. return reflectance0 + weight * (reflectance90 - reflectance0) * pow(clamp(1.0 - VdotN, 0., 1.), 5.0);
  72. }
  73. // Cook Torance Specular computation.
  74. vec3 computeSpecularTerm(float NdotH, float NdotL, float NdotV, float VdotH, float roughness, vec3 specularColor)
  75. {
  76. float alphaG = convertRoughnessToAverageSlope(roughness);
  77. float distribution = normalDistributionFunction_TrowbridgeReitzGGX(NdotH, alphaG);
  78. float visibility = smithVisibilityG_TrowbridgeReitzGGX_Walter(NdotL, NdotV, alphaG);
  79. visibility /= (4.0 * NdotL * NdotV); // Cook Torance Denominator integated in viibility to avoid issues when visibility function changes.
  80. vec3 fresnel = fresnelSchlickGGX(VdotH, specularColor, vec3(1., 1., 1.));
  81. float specTerm = max(0., visibility * distribution) * NdotL;
  82. return fresnel * specTerm;
  83. }
  84. float computeDiffuseTerm(float NdotL, float NdotV, float VdotH, float roughness)
  85. {
  86. // Diffuse fresnel falloff as per Disney principled BRDF, and in the spirit of
  87. // of general coupled diffuse/specular models e.g. Ashikhmin Shirley.
  88. float diffuseFresnelNV = pow(clamp(1.0 - NdotL, 0.000001, 1.), 5.0);
  89. float diffuseFresnelNL = pow(clamp(1.0 - NdotV, 0.000001, 1.), 5.0);
  90. float diffuseFresnel90 = 0.5 + 2.0 * VdotH * VdotH * roughness;
  91. float diffuseFresnelTerm =
  92. (1.0 + (diffuseFresnel90 - 1.0) * diffuseFresnelNL) *
  93. (1.0 + (diffuseFresnel90 - 1.0) * diffuseFresnelNV);
  94. return diffuseFresnelTerm * NdotL;
  95. }
  96. float computeDefaultGlossiness(float glossiness, vec3 specularColor)
  97. {
  98. if (glossiness == 0.)
  99. {
  100. float kSpecularNoAlphaWorkflow_SmoothnessMax = 0.95;
  101. float specularLuminance = getLuminance(specularColor);
  102. float specularLuma = sqrt(specularLuminance);
  103. glossiness = specularLuma * kSpecularNoAlphaWorkflow_SmoothnessMax;
  104. }
  105. return glossiness;
  106. }
  107. vec3 toLinearSpace(vec3 color)
  108. {
  109. return vec3(pow(color.r, 2.2), pow(color.g, 2.2), pow(color.b, 2.2));
  110. }
  111. vec3 toGammaSpace(vec3 color)
  112. {
  113. return vec3(pow(color.r, 1.0 / 2.2), pow(color.g, 1.0 / 2.2), pow(color.b, 1.0 / 2.2));
  114. }
  115. #ifdef CAMERATONEMAP
  116. vec3 toneMaps(vec3 color)
  117. {
  118. color = max(color, 0.0);
  119. // TONE MAPPING / EXPOSURE
  120. color.rgb = color.rgb * vCameraInfos.x;
  121. float tuning = 1.5; // TODO: sync up so e.g. 18% greys are matched to exposure appropriately
  122. vec3 tonemapped = 1.0 - exp2(-color.rgb * tuning); // simple local photographic tonemapper
  123. color.rgb = mix(color.rgb, tonemapped, 1.0);
  124. return color;
  125. }
  126. #endif
  127. #ifdef CAMERACONTRAST
  128. vec4 contrasts(vec4 color)
  129. {
  130. color = clamp(color, 0.0, 1.0);
  131. vec3 resultHighContrast = color.rgb * color.rgb * (3.0 - 2.0 * color.rgb);
  132. float contrast = vCameraInfos.y;
  133. if (contrast < 1.0)
  134. {
  135. // Decrease contrast: interpolate towards zero-contrast image (flat grey)
  136. color.rgb = mix(vec3(0.5, 0.5, 0.5), color.rgb, contrast);
  137. }
  138. else
  139. {
  140. // Increase contrast: apply simple shoulder-toe high contrast curve
  141. color.rgb = mix(color.rgb, resultHighContrast, contrast - 1.0);
  142. }
  143. return color;
  144. }
  145. #endif
  146. // END PBR HELPER METHODS
  147. #ifdef SPECULARTERM
  148. uniform vec4 vSpecularColor;
  149. #endif
  150. uniform vec3 vEmissiveColor;
  151. // Input
  152. varying vec3 vPositionW;
  153. #ifdef NORMAL
  154. varying vec3 vNormalW;
  155. #endif
  156. #ifdef VERTEXCOLOR
  157. varying vec4 vColor;
  158. #endif
  159. // Lights
  160. #ifdef LIGHT0
  161. uniform vec4 vLightData0;
  162. uniform vec4 vLightDiffuse0;
  163. #ifdef SPECULARTERM
  164. uniform vec3 vLightSpecular0;
  165. #endif
  166. #ifdef SHADOW0
  167. #if defined(SPOTLIGHT0) || defined(DIRLIGHT0)
  168. varying vec4 vPositionFromLight0;
  169. uniform sampler2D shadowSampler0;
  170. #else
  171. uniform samplerCube shadowSampler0;
  172. #endif
  173. uniform vec3 shadowsInfo0;
  174. #endif
  175. #ifdef SPOTLIGHT0
  176. uniform vec4 vLightDirection0;
  177. #endif
  178. #ifdef HEMILIGHT0
  179. uniform vec3 vLightGround0;
  180. #endif
  181. #endif
  182. #ifdef LIGHT1
  183. uniform vec4 vLightData1;
  184. uniform vec4 vLightDiffuse1;
  185. #ifdef SPECULARTERM
  186. uniform vec3 vLightSpecular1;
  187. #endif
  188. #ifdef SHADOW1
  189. #if defined(SPOTLIGHT1) || defined(DIRLIGHT1)
  190. varying vec4 vPositionFromLight1;
  191. uniform sampler2D shadowSampler1;
  192. #else
  193. uniform samplerCube shadowSampler1;
  194. #endif
  195. uniform vec3 shadowsInfo1;
  196. #endif
  197. #ifdef SPOTLIGHT1
  198. uniform vec4 vLightDirection1;
  199. #endif
  200. #ifdef HEMILIGHT1
  201. uniform vec3 vLightGround1;
  202. #endif
  203. #endif
  204. #ifdef LIGHT2
  205. uniform vec4 vLightData2;
  206. uniform vec4 vLightDiffuse2;
  207. #ifdef SPECULARTERM
  208. uniform vec3 vLightSpecular2;
  209. #endif
  210. #ifdef SHADOW2
  211. #if defined(SPOTLIGHT2) || defined(DIRLIGHT2)
  212. varying vec4 vPositionFromLight2;
  213. uniform sampler2D shadowSampler2;
  214. #else
  215. uniform samplerCube shadowSampler2;
  216. #endif
  217. uniform vec3 shadowsInfo2;
  218. #endif
  219. #ifdef SPOTLIGHT2
  220. uniform vec4 vLightDirection2;
  221. #endif
  222. #ifdef HEMILIGHT2
  223. uniform vec3 vLightGround2;
  224. #endif
  225. #endif
  226. #ifdef LIGHT3
  227. uniform vec4 vLightData3;
  228. uniform vec4 vLightDiffuse3;
  229. #ifdef SPECULARTERM
  230. uniform vec3 vLightSpecular3;
  231. #endif
  232. #ifdef SHADOW3
  233. #if defined(SPOTLIGHT3) || defined(DIRLIGHT3)
  234. varying vec4 vPositionFromLight3;
  235. uniform sampler2D shadowSampler3;
  236. #else
  237. uniform samplerCube shadowSampler3;
  238. #endif
  239. uniform vec3 shadowsInfo3;
  240. #endif
  241. #ifdef SPOTLIGHT3
  242. uniform vec4 vLightDirection3;
  243. #endif
  244. #ifdef HEMILIGHT3
  245. uniform vec3 vLightGround3;
  246. #endif
  247. #endif
  248. // Samplers
  249. #ifdef DIFFUSE
  250. varying vec2 vDiffuseUV;
  251. uniform sampler2D diffuseSampler;
  252. uniform vec2 vDiffuseInfos;
  253. #endif
  254. #ifdef AMBIENT
  255. varying vec2 vAmbientUV;
  256. uniform sampler2D ambientSampler;
  257. uniform vec2 vAmbientInfos;
  258. #endif
  259. #ifdef OPACITY
  260. varying vec2 vOpacityUV;
  261. uniform sampler2D opacitySampler;
  262. uniform vec2 vOpacityInfos;
  263. #endif
  264. #ifdef EMISSIVE
  265. varying vec2 vEmissiveUV;
  266. uniform vec2 vEmissiveInfos;
  267. uniform sampler2D emissiveSampler;
  268. #endif
  269. #ifdef LIGHTMAP
  270. varying vec2 vLightmapUV;
  271. uniform vec2 vLightmapInfos;
  272. uniform sampler2D lightmapSampler;
  273. #endif
  274. #if defined(SPECULAR) && defined(SPECULARTERM)
  275. varying vec2 vSpecularUV;
  276. uniform vec2 vSpecularInfos;
  277. uniform sampler2D specularSampler;
  278. #endif
  279. #ifdef CLIPPLANE
  280. varying float fClipDistance;
  281. #endif
  282. // Light Computing
  283. struct lightingInfo
  284. {
  285. vec3 diffuse;
  286. #ifdef SPECULARTERM
  287. vec3 specular;
  288. #endif
  289. };
  290. lightingInfo computeLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, vec3 specularColor, float range, float roughness, float NdotV) {
  291. lightingInfo result;
  292. vec3 lightVectorW;
  293. float attenuation = 1.0;
  294. if (lightData.w == 0.)
  295. {
  296. vec3 direction = lightData.xyz - vPositionW;
  297. attenuation = max(0., 1.0 - length(direction) / range);
  298. lightVectorW = normalize(direction);
  299. }
  300. else
  301. {
  302. lightVectorW = normalize(-lightData.xyz);
  303. }
  304. // diffuse
  305. vec3 H = normalize(viewDirectionW + lightVectorW);
  306. float NdotL = max(0.00000000001, dot(vNormal, lightVectorW));
  307. float VdotH = clamp(0.00000000001, 1.0, dot(viewDirectionW, H));
  308. float diffuseTerm = computeDiffuseTerm(NdotL, NdotV, VdotH, roughness);
  309. result.diffuse = diffuseTerm * diffuseColor * attenuation;
  310. #ifdef SPECULARTERM
  311. // Specular
  312. float NdotH = max(0.00000000001, dot(vNormal, H));
  313. vec3 specTerm = computeSpecularTerm(NdotH, NdotL, NdotV, VdotH, roughness, specularColor);
  314. result.specular = specTerm * specularColor * attenuation;
  315. #endif
  316. return result;
  317. }
  318. lightingInfo computeSpotLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec4 lightDirection, vec3 diffuseColor, vec3 specularColor, float range, float roughness, float NdotV) {
  319. lightingInfo result;
  320. vec3 direction = lightData.xyz - vPositionW;
  321. vec3 lightVectorW = normalize(direction);
  322. float attenuation = max(0., 1.0 - length(direction) / range);
  323. // diffuse
  324. float cosAngle = max(0.0000001, dot(-lightDirection.xyz, lightVectorW));
  325. float spotAtten = 0.0;
  326. if (cosAngle >= lightDirection.w)
  327. {
  328. cosAngle = max(0., pow(cosAngle, lightData.w));
  329. spotAtten = clamp((cosAngle - lightDirection.w) / (1. - cosAngle), 0.0, 1.0);
  330. // Diffuse
  331. vec3 H = normalize(viewDirectionW - lightDirection.xyz);
  332. float NdotL = max(0.00000000001, dot(vNormal, -lightDirection.xyz));
  333. float VdotH = clamp(dot(viewDirectionW, H), 0.00000000001, 1.0);
  334. float diffuseTerm = computeDiffuseTerm(NdotL, NdotV, VdotH, roughness);
  335. result.diffuse = diffuseTerm * diffuseColor * attenuation * spotAtten;
  336. #ifdef SPECULARTERM
  337. // Specular
  338. float NdotH = max(0.00000000001, dot(vNormal, H));
  339. vec3 specTerm = computeSpecularTerm(NdotH, NdotL, NdotV, VdotH, roughness, specularColor);
  340. result.specular = specTerm * specularColor * attenuation * spotAtten;
  341. #endif
  342. return result;
  343. }
  344. result.diffuse = vec3(0.);
  345. #ifdef SPECULARTERM
  346. result.specular = vec3(0.);
  347. #endif
  348. return result;
  349. }
  350. lightingInfo computeHemisphericLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, vec3 specularColor, vec3 groundColor, float roughness, float NdotV) {
  351. lightingInfo result;
  352. vec3 lightVectorW = normalize(lightData.xyz);
  353. // Diffuse
  354. float ndl = dot(vNormal, lightData.xyz) * 0.5 + 0.5;
  355. result.diffuse = mix(groundColor, diffuseColor, ndl);
  356. #ifdef SPECULARTERM
  357. // Specular
  358. vec3 H = normalize(viewDirectionW + lightVectorW);
  359. float NdotH = max(0.00000000001, dot(vNormal, H));
  360. float NdotL = max(0.00000000001, ndl);
  361. float VdotH = clamp(0.00000000001, 1.0, dot(viewDirectionW, H));
  362. vec3 specTerm = computeSpecularTerm(NdotH, NdotL, NdotV, VdotH, roughness, specularColor);
  363. result.specular = specTerm * specularColor;
  364. #endif
  365. return result;
  366. }
  367. void main(void) {
  368. // Clip plane
  369. #ifdef CLIPPLANE
  370. if (fClipDistance > 0.0)
  371. discard;
  372. #endif
  373. vec3 viewDirectionW = normalize(vEyePosition - vPositionW);
  374. // Base color
  375. vec4 baseColor = vec4(1., 1., 1., 1.);
  376. vec3 diffuseColor = vDiffuseColor.rgb;
  377. // Alpha
  378. float alpha = vDiffuseColor.a;
  379. #ifdef DIFFUSE
  380. baseColor = texture2D(diffuseSampler, vDiffuseUV);
  381. baseColor = vec4(toLinearSpace(baseColor.rgb), baseColor.a);
  382. #ifdef ALPHATEST
  383. if (baseColor.a < 0.4)
  384. discard;
  385. #endif
  386. #ifdef ALPHAFROMDIFFUSE
  387. alpha *= baseColor.a;
  388. #endif
  389. baseColor.rgb *= vDiffuseInfos.y;
  390. #endif
  391. #ifdef VERTEXCOLOR
  392. baseColor.rgb *= vColor.rgb;
  393. #endif
  394. #ifdef OVERLOADEDVALUES
  395. baseColor.rgb = mix(baseColor.rgb, vOverloadedDiffuse, vOverloadedIntensity.y);
  396. diffuseColor.rgb = mix(diffuseColor.rgb, vOverloadedDiffuse, vOverloadedIntensity.y);
  397. #endif
  398. // Bump
  399. #ifdef NORMAL
  400. vec3 normalW = normalize(vNormalW);
  401. #else
  402. vec3 normalW = vec3(1.0, 1.0, 1.0);
  403. #endif
  404. // Ambient color
  405. vec3 baseAmbientColor = vec3(1., 1., 1.);
  406. #ifdef AMBIENT
  407. baseAmbientColor = texture2D(ambientSampler, vAmbientUV).rgb * vAmbientInfos.y;
  408. #ifdef OVERLOADEDVALUES
  409. baseAmbientColor.rgb = mix(baseAmbientColor.rgb, vOverloadedAmbient, vOverloadedIntensity.x);
  410. #endif
  411. #endif
  412. // Specular map
  413. #ifdef SPECULARTERM
  414. float glossiness = vSpecularColor.a;
  415. vec3 specularColor = vSpecularColor.rgb;
  416. #ifdef OVERLOADEDVALUES
  417. specularColor.rgb = mix(specularColor.rgb, vOverloadedSpecular, vOverloadedIntensity.z);
  418. #endif
  419. #ifdef SPECULAR
  420. vec4 specularMapColor = texture2D(specularSampler, vSpecularUV);
  421. specularColor = toLinearSpace(specularMapColor.rgb);
  422. #ifdef OVERLOADEDVALUES
  423. specularColor.rgb = mix(specularColor.rgb, vOverloadedSpecular, vOverloadedIntensity.z);
  424. #endif
  425. #ifdef GLOSSINESSFROMSPECULARMAP
  426. glossiness = specularMapColor.a;
  427. #else
  428. glossiness = computeDefaultGlossiness(glossiness, specularColor);
  429. #endif
  430. #endif
  431. #ifdef OVERLOADEDVALUES
  432. glossiness = mix(glossiness, vOverloadedGlossiness.x, vOverloadedGlossiness.y);
  433. #endif
  434. #else
  435. float glossiness = 0.;
  436. #ifdef OVERLOADEDVALUES
  437. glossiness = mix(glossiness, vOverloadedGlossiness.x, vOverloadedGlossiness.y);
  438. #endif
  439. vec3 specularColor = vec3(0., 0., 0);
  440. #ifdef OVERLOADEDVALUES
  441. specularColor.rgb = mix(specularColor.rgb, vOverloadedSpecular, vOverloadedIntensity.z);
  442. #endif
  443. #endif
  444. // Apply Energy Conservation taking in account the environment level only if the environment is present.
  445. float reflectance = max(max(specularColor.r, specularColor.g), specularColor.b);
  446. baseColor.rgb = (1. - reflectance) * baseColor.rgb;
  447. // Compute Specular Fresnel + Reflectance.
  448. float NdotV = max(0.00000000001, dot(normalW, viewDirectionW));
  449. // Adapt glossiness.
  450. glossiness = clamp(glossiness, 0., 1.) * 0.98;
  451. // Call rough to not conflict with previous one.
  452. float rough = clamp(1. - glossiness, 0.000001, 1.0);
  453. // Lighting
  454. vec3 diffuseBase = vec3(0., 0., 0.);
  455. #ifdef OVERLOADEDSHADOWVALUES
  456. vec3 shadowedOnlyDiffuseBase = vec3(1., 1., 1.);
  457. #endif
  458. #ifdef SPECULARTERM
  459. vec3 specularBase = vec3(0., 0., 0.);
  460. #endif
  461. float shadow = 1.;
  462. #ifdef LIGHT0
  463. #ifndef SPECULARTERM
  464. vec3 vLightSpecular0 = vec3(0.0);
  465. #endif
  466. #ifdef SPOTLIGHT0
  467. lightingInfo info = computeSpotLighting(viewDirectionW, normalW, vLightData0, vLightDirection0, vLightDiffuse0.rgb, vLightSpecular0, vLightDiffuse0.a, rough, NdotV);
  468. #endif
  469. #ifdef HEMILIGHT0
  470. lightingInfo info = computeHemisphericLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightSpecular0, vLightGround0, rough, NdotV);
  471. #endif
  472. #if defined(POINTLIGHT0) || defined(DIRLIGHT0)
  473. lightingInfo info = computeLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightSpecular0, vLightDiffuse0.a, rough, NdotV);
  474. #endif
  475. shadow = 1.;
  476. diffuseBase += info.diffuse * shadow;
  477. #ifdef OVERLOADEDSHADOWVALUES
  478. shadowedOnlyDiffuseBase *= shadow;
  479. #endif
  480. #ifdef SPECULARTERM
  481. specularBase += info.specular * shadow;
  482. #endif
  483. #endif
  484. #ifdef LIGHT1
  485. #ifndef SPECULARTERM
  486. vec3 vLightSpecular1 = vec3(0.0);
  487. #endif
  488. #ifdef SPOTLIGHT1
  489. info = computeSpotLighting(viewDirectionW, normalW, vLightData1, vLightDirection1, vLightDiffuse1.rgb, vLightSpecular1, vLightDiffuse1.a, rough, NdotV);
  490. #endif
  491. #ifdef HEMILIGHT1
  492. info = computeHemisphericLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightSpecular1, vLightGround1, rough, NdotV);
  493. #endif
  494. #if defined(POINTLIGHT1) || defined(DIRLIGHT1)
  495. info = computeLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightSpecular1, vLightDiffuse1.a, rough, NdotV);
  496. #endif
  497. shadow = 1.;
  498. diffuseBase += info.diffuse * shadow;
  499. #ifdef OVERLOADEDSHADOWVALUES
  500. shadowedOnlyDiffuseBase *= shadow;
  501. #endif
  502. #ifdef SPECULARTERM
  503. specularBase += info.specular * shadow;
  504. #endif
  505. #endif
  506. #ifdef LIGHT2
  507. #ifndef SPECULARTERM
  508. vec3 vLightSpecular2 = vec3(0.0);
  509. #endif
  510. #ifdef SPOTLIGHT2
  511. info = computeSpotLighting(viewDirectionW, normalW, vLightData2, vLightDirection2, vLightDiffuse2.rgb, vLightSpecular2, vLightDiffuse2.a, rough, NdotV);
  512. #endif
  513. #ifdef HEMILIGHT2
  514. info = computeHemisphericLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightSpecular2, vLightGround2, rough, NdotV);
  515. #endif
  516. #if defined(POINTLIGHT2) || defined(DIRLIGHT2)
  517. info = computeLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightSpecular2, vLightDiffuse2.a, rough, NdotV);
  518. #endif
  519. shadow = 1.;
  520. diffuseBase += info.diffuse * shadow;
  521. #ifdef OVERLOADEDSHADOWVALUES
  522. shadowedOnlyDiffuseBase *= shadow;
  523. #endif
  524. #ifdef SPECULARTERM
  525. specularBase += info.specular * shadow;
  526. #endif
  527. #endif
  528. #ifdef LIGHT3
  529. #ifndef SPECULARTERM
  530. vec3 vLightSpecular3 = vec3(0.0);
  531. #endif
  532. #ifdef SPOTLIGHT3
  533. info = computeSpotLighting(viewDirectionW, normalW, vLightData3, vLightDirection3, vLightDiffuse3.rgb, vLightSpecular3, vLightDiffuse3.a, rough, NdotV);
  534. #endif
  535. #ifdef HEMILIGHT3
  536. info = computeHemisphericLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightSpecular3, vLightGround3, rough, NdotV);
  537. #endif
  538. #if defined(POINTLIGHT3) || defined(DIRLIGHT3)
  539. info = computeLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightSpecular3, vLightDiffuse3.a, rough, NdotV);
  540. #endif
  541. shadow = 1.;
  542. diffuseBase += info.diffuse * shadow;
  543. #ifdef OVERLOADEDSHADOWVALUES
  544. shadowedOnlyDiffuseBase *= shadow;
  545. #endif
  546. #ifdef SPECULARTERM
  547. specularBase += info.specular * shadow;
  548. #endif
  549. #endif
  550. // Reflection
  551. vec3 reflectionColor = vReflectionColor.rgb;
  552. vec3 ambientReflectionColor = vReflectionColor.rgb;
  553. reflectionColor *= vLightingIntensity.z;
  554. ambientReflectionColor *= vLightingIntensity.z;
  555. // Compute reflection specular fresnel
  556. vec3 specularEnvironmentR0 = specularColor.rgb;
  557. vec3 specularEnvironmentR90 = vec3(1.0, 1.0, 1.0);
  558. vec3 specularEnvironmentReflectanceViewer = FresnelSchlickEnvironmentGGX(clamp(NdotV, 0., 1.), specularEnvironmentR0, specularEnvironmentR90, sqrt(glossiness));
  559. reflectionColor *= specularEnvironmentReflectanceViewer;
  560. #ifdef OVERLOADEDVALUES
  561. ambientReflectionColor = mix(ambientReflectionColor, vOverloadedReflection, vOverloadedGlossiness.z);
  562. reflectionColor = mix(reflectionColor, vOverloadedReflection, vOverloadedGlossiness.z);
  563. #endif
  564. #ifdef OPACITY
  565. vec4 opacityMap = texture2D(opacitySampler, vOpacityUV);
  566. #ifdef OPACITYRGB
  567. opacityMap.rgb = opacityMap.rgb * vec3(0.3, 0.59, 0.11);
  568. alpha *= (opacityMap.x + opacityMap.y + opacityMap.z)* vOpacityInfos.y;
  569. #else
  570. alpha *= opacityMap.a * vOpacityInfos.y;
  571. #endif
  572. #endif
  573. #ifdef VERTEXALPHA
  574. alpha *= vColor.a;
  575. #endif
  576. // Emissive
  577. vec3 emissiveColor = vEmissiveColor;
  578. #ifdef EMISSIVE
  579. vec3 emissiveColorTex = texture2D(emissiveSampler, vEmissiveUV).rgb;
  580. emissiveColor = toLinearSpace(emissiveColorTex.rgb) * emissiveColor * vEmissiveInfos.y;
  581. #endif
  582. #ifdef OVERLOADEDVALUES
  583. emissiveColor = mix(emissiveColor, vOverloadedEmissive, vOverloadedIntensity.w);
  584. #endif
  585. // Composition
  586. #ifdef EMISSIVEASILLUMINATION
  587. vec3 finalDiffuse = max(diffuseBase * diffuseColor + vAmbientColor, 0.0) * baseColor.rgb;
  588. #ifdef OVERLOADEDSHADOWVALUES
  589. shadowedOnlyDiffuseBase = max(shadowedOnlyDiffuseBase * diffuseColor + vAmbientColor, 0.0) * baseColor.rgb;
  590. #endif
  591. #else
  592. #ifdef LINKEMISSIVEWITHDIFFUSE
  593. vec3 finalDiffuse = max((diffuseBase + emissiveColor) * diffuseColor + vAmbientColor, 0.0) * baseColor.rgb;
  594. #ifdef OVERLOADEDSHADOWVALUES
  595. shadowedOnlyDiffuseBase = max((shadowedOnlyDiffuseBase + emissiveColor) * diffuseColor + vAmbientColor, 0.0) * baseColor.rgb;
  596. #endif
  597. #else
  598. vec3 finalDiffuse = max(diffuseBase * diffuseColor + emissiveColor + vAmbientColor, 0.0) * baseColor.rgb;
  599. #ifdef OVERLOADEDSHADOWVALUES
  600. shadowedOnlyDiffuseBase = max(shadowedOnlyDiffuseBase * diffuseColor + emissiveColor + vAmbientColor, 0.0) * baseColor.rgb;
  601. #endif
  602. #endif
  603. #endif
  604. #ifdef OVERLOADEDSHADOWVALUES
  605. finalDiffuse = mix(finalDiffuse, shadowedOnlyDiffuseBase, (1.0 - vOverloadedShadowIntensity.y));
  606. #endif
  607. // diffuse lighting from environment 0.2 replaces Harmonic...
  608. // Ambient Reflection already includes the environment intensity.
  609. finalDiffuse += baseColor.rgb * ambientReflectionColor * 0.2;
  610. #ifdef SPECULARTERM
  611. vec3 finalSpecular = specularBase * specularColor;
  612. #else
  613. vec3 finalSpecular = vec3(0.0);
  614. #endif
  615. #ifdef SPECULAROVERALPHA
  616. alpha = clamp(alpha + dot(finalSpecular, vec3(0.3, 0.59, 0.11)), 0., 1.);
  617. #endif
  618. // Composition
  619. // Reflection already includes the environment intensity.
  620. #ifdef EMISSIVEASILLUMINATION
  621. vec4 color = vec4(finalDiffuse * baseAmbientColor * vLightingIntensity.x + finalSpecular * vLightingIntensity.x + reflectionColor + emissiveColor * vLightingIntensity.y, alpha);
  622. #else
  623. vec4 color = vec4(finalDiffuse * baseAmbientColor * vLightingIntensity.x + finalSpecular * vLightingIntensity.x + reflectionColor, alpha);
  624. #endif
  625. color = max(color, 0.0);
  626. #ifdef CAMERATONEMAP
  627. color.rgb = toneMaps(color.rgb);
  628. #endif
  629. color.rgb = toGammaSpace(color.rgb);
  630. #ifdef CAMERACONTRAST
  631. color = contrasts(color);
  632. #endif
  633. gl_FragColor = color;
  634. }