legacypbr.fragment.fx 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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 vAlbedoColor;
  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 vOverloadedAlbedo;
  16. uniform vec3 vOverloadedReflectivity;
  17. uniform vec3 vOverloadedEmissive;
  18. uniform vec3 vOverloadedReflection;
  19. uniform vec3 vOverloadedMicroSurface;
  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 computeDefaultMicroSurface(float microSurface, vec3 reflectivityColor)
  97. {
  98. if (microSurface == 0.)
  99. {
  100. float kReflectivityNoAlphaWorkflow_SmoothnessMax = 0.95;
  101. float reflectivityLuminance = getLuminance(reflectivityColor);
  102. float reflectivityLuma = sqrt(reflectivityLuminance);
  103. microSurface = reflectivityLuma * kReflectivityNoAlphaWorkflow_SmoothnessMax;
  104. }
  105. return microSurface;
  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. uniform vec4 vReflectivityColor;
  148. uniform vec3 vEmissiveColor;
  149. // Input
  150. varying vec3 vPositionW;
  151. #ifdef NORMAL
  152. varying vec3 vNormalW;
  153. #endif
  154. #ifdef VERTEXCOLOR
  155. varying vec4 vColor;
  156. #endif
  157. // Lights
  158. #ifdef LIGHT0
  159. uniform vec4 vLightData0;
  160. uniform vec4 vLightDiffuse0;
  161. #ifdef SPECULARTERM
  162. uniform vec3 vLightSpecular0;
  163. #endif
  164. #ifdef SHADOW0
  165. #if defined(SPOTLIGHT0) || defined(DIRLIGHT0)
  166. varying vec4 vPositionFromLight0;
  167. uniform sampler2D shadowSampler0;
  168. #else
  169. uniform samplerCube shadowSampler0;
  170. #endif
  171. uniform vec3 shadowsInfo0;
  172. #endif
  173. #ifdef SPOTLIGHT0
  174. uniform vec4 vLightDirection0;
  175. #endif
  176. #ifdef HEMILIGHT0
  177. uniform vec3 vLightGround0;
  178. #endif
  179. #endif
  180. #ifdef LIGHT1
  181. uniform vec4 vLightData1;
  182. uniform vec4 vLightDiffuse1;
  183. #ifdef SPECULARTERM
  184. uniform vec3 vLightSpecular1;
  185. #endif
  186. #ifdef SHADOW1
  187. #if defined(SPOTLIGHT1) || defined(DIRLIGHT1)
  188. varying vec4 vPositionFromLight1;
  189. uniform sampler2D shadowSampler1;
  190. #else
  191. uniform samplerCube shadowSampler1;
  192. #endif
  193. uniform vec3 shadowsInfo1;
  194. #endif
  195. #ifdef SPOTLIGHT1
  196. uniform vec4 vLightDirection1;
  197. #endif
  198. #ifdef HEMILIGHT1
  199. uniform vec3 vLightGround1;
  200. #endif
  201. #endif
  202. #ifdef LIGHT2
  203. uniform vec4 vLightData2;
  204. uniform vec4 vLightDiffuse2;
  205. #ifdef SPECULARTERM
  206. uniform vec3 vLightSpecular2;
  207. #endif
  208. #ifdef SHADOW2
  209. #if defined(SPOTLIGHT2) || defined(DIRLIGHT2)
  210. varying vec4 vPositionFromLight2;
  211. uniform sampler2D shadowSampler2;
  212. #else
  213. uniform samplerCube shadowSampler2;
  214. #endif
  215. uniform vec3 shadowsInfo2;
  216. #endif
  217. #ifdef SPOTLIGHT2
  218. uniform vec4 vLightDirection2;
  219. #endif
  220. #ifdef HEMILIGHT2
  221. uniform vec3 vLightGround2;
  222. #endif
  223. #endif
  224. #ifdef LIGHT3
  225. uniform vec4 vLightData3;
  226. uniform vec4 vLightDiffuse3;
  227. #ifdef SPECULARTERM
  228. uniform vec3 vLightSpecular3;
  229. #endif
  230. #ifdef SHADOW3
  231. #if defined(SPOTLIGHT3) || defined(DIRLIGHT3)
  232. varying vec4 vPositionFromLight3;
  233. uniform sampler2D shadowSampler3;
  234. #else
  235. uniform samplerCube shadowSampler3;
  236. #endif
  237. uniform vec3 shadowsInfo3;
  238. #endif
  239. #ifdef SPOTLIGHT3
  240. uniform vec4 vLightDirection3;
  241. #endif
  242. #ifdef HEMILIGHT3
  243. uniform vec3 vLightGround3;
  244. #endif
  245. #endif
  246. // Samplers
  247. #ifdef ALBEDO
  248. varying vec2 vAlbedoUV;
  249. uniform sampler2D albedoSampler;
  250. uniform vec2 vAlbedoInfos;
  251. #endif
  252. #ifdef AMBIENT
  253. varying vec2 vAmbientUV;
  254. uniform sampler2D ambientSampler;
  255. uniform vec2 vAmbientInfos;
  256. #endif
  257. #ifdef OPACITY
  258. varying vec2 vOpacityUV;
  259. uniform sampler2D opacitySampler;
  260. uniform vec2 vOpacityInfos;
  261. #endif
  262. #ifdef EMISSIVE
  263. varying vec2 vEmissiveUV;
  264. uniform vec2 vEmissiveInfos;
  265. uniform sampler2D emissiveSampler;
  266. #endif
  267. #ifdef LIGHTMAP
  268. varying vec2 vLightmapUV;
  269. uniform vec2 vLightmapInfos;
  270. uniform sampler2D lightmapSampler;
  271. #endif
  272. #if defined(REFLECTIVITY)
  273. varying vec2 vReflectivityUV;
  274. uniform vec2 vReflectivityInfos;
  275. uniform sampler2D reflectivitySampler;
  276. #endif
  277. #ifdef CLIPPLANE
  278. varying float fClipDistance;
  279. #endif
  280. // Light Computing
  281. struct lightingInfo
  282. {
  283. vec3 diffuse;
  284. #ifdef SPECULARTERM
  285. vec3 specular;
  286. #endif
  287. };
  288. lightingInfo computeLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, vec3 specularColor, float range, float roughness, float NdotV) {
  289. lightingInfo result;
  290. vec3 lightVectorW;
  291. float attenuation = 1.0;
  292. if (lightData.w == 0.)
  293. {
  294. vec3 direction = lightData.xyz - vPositionW;
  295. attenuation = max(0., 1.0 - length(direction) / range);
  296. lightVectorW = normalize(direction);
  297. }
  298. else
  299. {
  300. lightVectorW = normalize(-lightData.xyz);
  301. }
  302. // diffuse
  303. vec3 H = normalize(viewDirectionW + lightVectorW);
  304. float NdotL = max(0.00000000001, dot(vNormal, lightVectorW));
  305. float VdotH = clamp(0.00000000001, 1.0, dot(viewDirectionW, H));
  306. float diffuseTerm = computeDiffuseTerm(NdotL, NdotV, VdotH, roughness);
  307. result.diffuse = diffuseTerm * diffuseColor * attenuation;
  308. #ifdef SPECULARTERM
  309. // Specular
  310. float NdotH = max(0.00000000001, dot(vNormal, H));
  311. vec3 specTerm = computeSpecularTerm(NdotH, NdotL, NdotV, VdotH, roughness, specularColor);
  312. result.specular = specTerm * specularColor * attenuation;
  313. #endif
  314. return result;
  315. }
  316. lightingInfo computeSpotLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec4 lightDirection, vec3 diffuseColor, vec3 specularColor, float range, float roughness, float NdotV) {
  317. lightingInfo result;
  318. vec3 direction = lightData.xyz - vPositionW;
  319. vec3 lightVectorW = normalize(direction);
  320. float attenuation = max(0., 1.0 - length(direction) / range);
  321. // diffuse
  322. float cosAngle = max(0.0000001, dot(-lightDirection.xyz, lightVectorW));
  323. float spotAtten = 0.0;
  324. if (cosAngle >= lightDirection.w)
  325. {
  326. cosAngle = max(0., pow(cosAngle, lightData.w));
  327. spotAtten = clamp((cosAngle - lightDirection.w) / (1. - cosAngle), 0.0, 1.0);
  328. // Diffuse
  329. vec3 H = normalize(viewDirectionW - lightDirection.xyz);
  330. float NdotL = max(0.00000000001, dot(vNormal, -lightDirection.xyz));
  331. float VdotH = clamp(dot(viewDirectionW, H), 0.00000000001, 1.0);
  332. float diffuseTerm = computeDiffuseTerm(NdotL, NdotV, VdotH, roughness);
  333. result.diffuse = diffuseTerm * diffuseColor * attenuation * spotAtten;
  334. #ifdef SPECULARTERM
  335. // Specular
  336. float NdotH = max(0.00000000001, dot(vNormal, H));
  337. vec3 specTerm = computeSpecularTerm(NdotH, NdotL, NdotV, VdotH, roughness, specularColor);
  338. result.specular = specTerm * specularColor * attenuation * spotAtten;
  339. #endif
  340. return result;
  341. }
  342. result.diffuse = vec3(0.);
  343. #ifdef SPECULARTERM
  344. result.specular = vec3(0.);
  345. #endif
  346. return result;
  347. }
  348. lightingInfo computeHemisphericLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, vec3 specularColor, vec3 groundColor, float roughness, float NdotV) {
  349. lightingInfo result;
  350. vec3 lightVectorW = normalize(lightData.xyz);
  351. // Diffuse
  352. float ndl = dot(vNormal, lightData.xyz) * 0.5 + 0.5;
  353. result.diffuse = mix(groundColor, diffuseColor, ndl);
  354. #ifdef SPECULARTERM
  355. // Specular
  356. vec3 H = normalize(viewDirectionW + lightVectorW);
  357. float NdotH = max(0.00000000001, dot(vNormal, H));
  358. float NdotL = max(0.00000000001, ndl);
  359. float VdotH = clamp(0.00000000001, 1.0, dot(viewDirectionW, H));
  360. vec3 specTerm = computeSpecularTerm(NdotH, NdotL, NdotV, VdotH, roughness, specularColor);
  361. result.specular = specTerm * specularColor;
  362. #endif
  363. return result;
  364. }
  365. void main(void) {
  366. // Clip plane
  367. #ifdef CLIPPLANE
  368. if (fClipDistance > 0.0)
  369. discard;
  370. #endif
  371. vec3 viewDirectionW = normalize(vEyePosition - vPositionW);
  372. // Base color
  373. vec4 baseColor = vec4(1., 1., 1., 1.);
  374. vec3 diffuseColor = vAlbedoColor.rgb;
  375. // Alpha
  376. float alpha = vAlbedoColor.a;
  377. #ifdef ALBEDO
  378. baseColor = texture2D(diffuseSampler, vAlbedoUV);
  379. baseColor = vec4(toLinearSpace(baseColor.rgb), baseColor.a);
  380. #ifdef ALPHATEST
  381. if (baseColor.a < 0.4)
  382. discard;
  383. #endif
  384. #ifdef ALPHAFROMALBEDO
  385. alpha *= baseColor.a;
  386. #endif
  387. baseColor.rgb *= vAlbedoInfos.y;
  388. #endif
  389. #ifdef VERTEXCOLOR
  390. baseColor.rgb *= vColor.rgb;
  391. #endif
  392. #ifdef OVERLOADEDVALUES
  393. baseColor.rgb = mix(baseColor.rgb, vOverloadedAlbedo, vOverloadedIntensity.y);
  394. albedoColor.rgb = mix(albedoColor.rgb, vOverloadedAlbedo, vOverloadedIntensity.y);
  395. #endif
  396. // Bump
  397. #ifdef NORMAL
  398. vec3 normalW = normalize(vNormalW);
  399. #else
  400. vec3 normalW = vec3(1.0, 1.0, 1.0);
  401. #endif
  402. // Ambient color
  403. vec3 baseAmbientColor = vec3(1., 1., 1.);
  404. #ifdef AMBIENT
  405. baseAmbientColor = texture2D(ambientSampler, vAmbientUV).rgb * vAmbientInfos.y;
  406. #ifdef OVERLOADEDVALUES
  407. baseAmbientColor.rgb = mix(baseAmbientColor.rgb, vOverloadedAmbient, vOverloadedIntensity.x);
  408. #endif
  409. #endif
  410. // Reflectivity map
  411. float microSurface = vReflectivityColor.a;
  412. vec3 reflectivityColor = vReflectivityColor.rgb;
  413. #ifdef OVERLOADEDVALUES
  414. reflectivityColor.rgb = mix(reflectivityColor.rgb, vOverloadedReflectivity, vOverloadedIntensity.z);
  415. #endif
  416. #ifdef REFLECTIVITY
  417. vec4 reflectivityMapColor = texture2D(reflectivitySampler, vReflectivityUV);
  418. reflectivityColor = toLinearSpace(reflectivityMapColor.rgb);
  419. #ifdef OVERLOADEDVALUES
  420. reflectivityColor.rgb = mix(reflectivityColor.rgb, vOverloadedReflectivity, vOverloadedIntensity.z);
  421. #endif
  422. #ifdef MICROSURFACEFROMREFLECTIVITYMAP
  423. microSurface = reflectivityMapColor.a;
  424. #else
  425. microSurface = computeDefaultMicroSurface(microSurface, reflectivityColor);
  426. #endif
  427. #endif
  428. #ifdef OVERLOADEDVALUES
  429. microSurface = mix(microSurface, vOverloadedMicroSurface.x, vOverloadedMicroSurface.y);
  430. #endif
  431. // Apply Energy Conservation taking in account the environment level only if the environment is present.
  432. float reflectance = max(max(reflectivityColor.r, reflectivityColor.g), reflectivityColor.b);
  433. baseColor.rgb = (1. - reflectance) * baseColor.rgb;
  434. // Compute Specular Fresnel + Reflectance.
  435. float NdotV = max(0.00000000001, dot(normalW, viewDirectionW));
  436. // Adapt microSurface.
  437. microSurface = clamp(microSurface, 0., 1.) * 0.98;
  438. // Call rough to not conflict with previous one.
  439. float rough = clamp(1. - microSurface, 0.000001, 1.0);
  440. // Lighting
  441. vec3 diffuseBase = vec3(0., 0., 0.);
  442. #ifdef OVERLOADEDSHADOWVALUES
  443. vec3 shadowedOnlyDiffuseBase = vec3(1., 1., 1.);
  444. #endif
  445. #ifdef SPECULARTERM
  446. vec3 specularBase = vec3(0., 0., 0.);
  447. #endif
  448. float shadow = 1.;
  449. #ifdef LIGHT0
  450. #ifndef SPECULARTERM
  451. vec3 vLightSpecular0 = vec3(0.0);
  452. #endif
  453. #ifdef SPOTLIGHT0
  454. lightingInfo info = computeSpotLighting(viewDirectionW, normalW, vLightData0, vLightDirection0, vLightDiffuse0.rgb, vLightSpecular0, vLightDiffuse0.a, rough, NdotV);
  455. #endif
  456. #ifdef HEMILIGHT0
  457. lightingInfo info = computeHemisphericLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightSpecular0, vLightGround0, rough, NdotV);
  458. #endif
  459. #if defined(POINTLIGHT0) || defined(DIRLIGHT0)
  460. lightingInfo info = computeLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightSpecular0, vLightDiffuse0.a, rough, NdotV);
  461. #endif
  462. shadow = 1.;
  463. diffuseBase += info.diffuse * shadow;
  464. #ifdef OVERLOADEDSHADOWVALUES
  465. shadowedOnlyDiffuseBase *= shadow;
  466. #endif
  467. #ifdef SPECULARTERM
  468. specularBase += info.specular * shadow;
  469. #endif
  470. #endif
  471. #ifdef LIGHT1
  472. #ifndef SPECULARTERM
  473. vec3 vLightSpecular1 = vec3(0.0);
  474. #endif
  475. #ifdef SPOTLIGHT1
  476. info = computeSpotLighting(viewDirectionW, normalW, vLightData1, vLightDirection1, vLightDiffuse1.rgb, vLightSpecular1, vLightDiffuse1.a, rough, NdotV);
  477. #endif
  478. #ifdef HEMILIGHT1
  479. info = computeHemisphericLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightSpecular1, vLightGround1, rough, NdotV);
  480. #endif
  481. #if defined(POINTLIGHT1) || defined(DIRLIGHT1)
  482. info = computeLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightSpecular1, vLightDiffuse1.a, rough, NdotV);
  483. #endif
  484. shadow = 1.;
  485. diffuseBase += info.diffuse * shadow;
  486. #ifdef OVERLOADEDSHADOWVALUES
  487. shadowedOnlyDiffuseBase *= shadow;
  488. #endif
  489. #ifdef SPECULARTERM
  490. specularBase += info.specular * shadow;
  491. #endif
  492. #endif
  493. #ifdef LIGHT2
  494. #ifndef SPECULARTERM
  495. vec3 vLightSpecular2 = vec3(0.0);
  496. #endif
  497. #ifdef SPOTLIGHT2
  498. info = computeSpotLighting(viewDirectionW, normalW, vLightData2, vLightDirection2, vLightDiffuse2.rgb, vLightSpecular2, vLightDiffuse2.a, rough, NdotV);
  499. #endif
  500. #ifdef HEMILIGHT2
  501. info = computeHemisphericLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightSpecular2, vLightGround2, rough, NdotV);
  502. #endif
  503. #if defined(POINTLIGHT2) || defined(DIRLIGHT2)
  504. info = computeLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightSpecular2, vLightDiffuse2.a, rough, NdotV);
  505. #endif
  506. shadow = 1.;
  507. diffuseBase += info.diffuse * shadow;
  508. #ifdef OVERLOADEDSHADOWVALUES
  509. shadowedOnlyDiffuseBase *= shadow;
  510. #endif
  511. #ifdef SPECULARTERM
  512. specularBase += info.specular * shadow;
  513. #endif
  514. #endif
  515. #ifdef LIGHT3
  516. #ifndef SPECULARTERM
  517. vec3 vLightSpecular3 = vec3(0.0);
  518. #endif
  519. #ifdef SPOTLIGHT3
  520. info = computeSpotLighting(viewDirectionW, normalW, vLightData3, vLightDirection3, vLightDiffuse3.rgb, vLightSpecular3, vLightDiffuse3.a, rough, NdotV);
  521. #endif
  522. #ifdef HEMILIGHT3
  523. info = computeHemisphericLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightSpecular3, vLightGround3, rough, NdotV);
  524. #endif
  525. #if defined(POINTLIGHT3) || defined(DIRLIGHT3)
  526. info = computeLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightSpecular3, vLightDiffuse3.a, rough, NdotV);
  527. #endif
  528. shadow = 1.;
  529. diffuseBase += info.diffuse * shadow;
  530. #ifdef OVERLOADEDSHADOWVALUES
  531. shadowedOnlyDiffuseBase *= shadow;
  532. #endif
  533. #ifdef SPECULARTERM
  534. specularBase += info.specular * shadow;
  535. #endif
  536. #endif
  537. // Reflection
  538. vec3 reflectionColor = vReflectionColor.rgb;
  539. vec3 ambientReflectionColor = vReflectionColor.rgb;
  540. reflectionColor *= vLightingIntensity.z;
  541. ambientReflectionColor *= vLightingIntensity.z;
  542. // Compute reflection reflectivity fresnel
  543. vec3 reflectivityEnvironmentR0 = reflectivityColor.rgb;
  544. vec3 reflectivityEnvironmentR90 = vec3(1.0, 1.0, 1.0);
  545. vec3 reflectivityEnvironmentReflectanceViewer = FresnelSchlickEnvironmentGGX(clamp(NdotV, 0., 1.), reflectivityEnvironmentR0, reflectivityEnvironmentR90, sqrt(microSurface));
  546. reflectionColor *= reflectivityEnvironmentReflectanceViewer;
  547. #ifdef OVERLOADEDVALUES
  548. ambientReflectionColor = mix(ambientReflectionColor, vOverloadedReflection, vOverloadedMicroSurface.z);
  549. reflectionColor = mix(reflectionColor, vOverloadedReflection, vOverloadedMicroSurface.z);
  550. #endif
  551. #ifdef OPACITY
  552. vec4 opacityMap = texture2D(opacitySampler, vOpacityUV);
  553. #ifdef OPACITYRGB
  554. opacityMap.rgb = opacityMap.rgb * vec3(0.3, 0.59, 0.11);
  555. alpha *= (opacityMap.x + opacityMap.y + opacityMap.z)* vOpacityInfos.y;
  556. #else
  557. alpha *= opacityMap.a * vOpacityInfos.y;
  558. #endif
  559. #endif
  560. #ifdef VERTEXALPHA
  561. alpha *= vColor.a;
  562. #endif
  563. // Emissive
  564. vec3 emissiveColor = vEmissiveColor;
  565. #ifdef EMISSIVE
  566. vec3 emissiveColorTex = texture2D(emissiveSampler, vEmissiveUV).rgb;
  567. emissiveColor = toLinearSpace(emissiveColorTex.rgb) * emissiveColor * vEmissiveInfos.y;
  568. #endif
  569. #ifdef OVERLOADEDVALUES
  570. emissiveColor = mix(emissiveColor, vOverloadedEmissive, vOverloadedIntensity.w);
  571. #endif
  572. // Composition
  573. #ifdef EMISSIVEASILLUMINATION
  574. vec3 finalDiffuse = max(diffuseBase * albedoColor + vAmbientColor, 0.0) * baseColor.rgb;
  575. #ifdef OVERLOADEDSHADOWVALUES
  576. shadowedOnlyDiffuseBase = max(shadowedOnlyDiffuseBase * albedoColor + vAmbientColor, 0.0) * baseColor.rgb;
  577. #endif
  578. #else
  579. #ifdef LINKEMISSIVEWITHALBEDO
  580. vec3 finalDiffuse = max((diffuseBase + emissiveColor) * albedoColor + vAmbientColor, 0.0) * baseColor.rgb;
  581. #ifdef OVERLOADEDSHADOWVALUES
  582. shadowedOnlyDiffuseBase = max((shadowedOnlyDiffuseBase + emissiveColor) * albedoColor + vAmbientColor, 0.0) * baseColor.rgb;
  583. #endif
  584. #else
  585. vec3 finalDiffuse = max(diffuseBase * albedoColor + emissiveColor + vAmbientColor, 0.0) * baseColor.rgb;
  586. #ifdef OVERLOADEDSHADOWVALUES
  587. shadowedOnlyDiffuseBase = max(shadowedOnlyDiffuseBase * albedoColor + emissiveColor + vAmbientColor, 0.0) * baseColor.rgb;
  588. #endif
  589. #endif
  590. #endif
  591. #ifdef OVERLOADEDSHADOWVALUES
  592. finalDiffuse = mix(finalDiffuse, shadowedOnlyDiffuseBase, (1.0 - vOverloadedShadowIntensity.y));
  593. #endif
  594. // diffuse lighting from environment 0.2 replaces Harmonic...
  595. // Ambient Reflection already includes the environment intensity.
  596. finalDiffuse += baseColor.rgb * ambientReflectionColor * 0.2;
  597. #ifdef SPECULARTERM
  598. vec3 finalSpecular = specularBase * reflectivityColor * vLightingIntensity.w;
  599. #else
  600. vec3 finalSpecular = vec3(0.0);
  601. #endif
  602. #ifdef SPECULAROVERALPHA
  603. alpha = clamp(alpha + dot(finalSpecular, vec3(0.3, 0.59, 0.11)), 0., 1.);
  604. #endif
  605. // Composition
  606. // Reflection already includes the environment intensity.
  607. #ifdef EMISSIVEASILLUMINATION
  608. vec4 color = vec4(finalDiffuse * baseAmbientColor * vLightingIntensity.x + finalSpecular * vLightingIntensity.x + reflectionColor + emissiveColor * vLightingIntensity.y, alpha);
  609. #else
  610. vec4 color = vec4(finalDiffuse * baseAmbientColor * vLightingIntensity.x + finalSpecular * vLightingIntensity.x + reflectionColor, alpha);
  611. #endif
  612. color = max(color, 0.0);
  613. #ifdef CAMERATONEMAP
  614. color.rgb = toneMaps(color.rgb);
  615. #endif
  616. color.rgb = toGammaSpace(color.rgb);
  617. #ifdef CAMERACONTRAST
  618. color = contrasts(color);
  619. #endif
  620. gl_FragColor = color;
  621. }