legacypbr.fragment.fx 20 KB

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