legacypbr.fragment.fx 19 KB

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