pbr.fragment.fx 38 KB

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