legacydefault.fragment.fx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. #ifdef GL_ES
  2. precision highp float;
  3. #endif
  4. #define MAP_PROJECTION 4.
  5. // Constants
  6. uniform vec3 vEyePosition;
  7. uniform vec3 vAmbientColor;
  8. uniform vec4 vDiffuseColor;
  9. uniform vec4 vSpecularColor;
  10. uniform vec3 vEmissiveColor;
  11. // Input
  12. varying vec3 vPositionW;
  13. varying vec3 vNormalW;
  14. #ifdef VERTEXCOLOR
  15. varying vec3 vColor;
  16. #endif
  17. // Lights
  18. #ifdef LIGHT0
  19. uniform vec4 vLightData0;
  20. uniform vec4 vLightDiffuse0;
  21. uniform vec3 vLightSpecular0;
  22. #ifdef SHADOW0
  23. varying vec4 vPositionFromLight0;
  24. uniform sampler2D shadowSampler0;
  25. #endif
  26. #ifdef SPOTLIGHT0
  27. uniform vec4 vLightDirection0;
  28. #endif
  29. #ifdef HEMILIGHT0
  30. uniform vec3 vLightGround0;
  31. #endif
  32. #endif
  33. #ifdef LIGHT1
  34. uniform vec4 vLightData1;
  35. uniform vec4 vLightDiffuse1;
  36. uniform vec3 vLightSpecular1;
  37. #ifdef SHADOW1
  38. varying vec4 vPositionFromLight1;
  39. uniform sampler2D shadowSampler1;
  40. #endif
  41. #ifdef SPOTLIGHT1
  42. uniform vec4 vLightDirection1;
  43. #endif
  44. #ifdef HEMILIGHT1
  45. uniform vec3 vLightGround1;
  46. #endif
  47. #endif
  48. #ifdef LIGHT2
  49. uniform vec4 vLightData2;
  50. uniform vec4 vLightDiffuse2;
  51. uniform vec3 vLightSpecular2;
  52. #ifdef SHADOW2
  53. varying vec4 vPositionFromLight2;
  54. uniform sampler2D shadowSampler2;
  55. #endif
  56. #ifdef SPOTLIGHT2
  57. uniform vec4 vLightDirection2;
  58. #endif
  59. #ifdef HEMILIGHT2
  60. uniform vec3 vLightGround2;
  61. #endif
  62. #endif
  63. #ifdef LIGHT3
  64. uniform vec4 vLightData3;
  65. uniform vec4 vLightDiffuse3;
  66. uniform vec3 vLightSpecular3;
  67. #ifdef SHADOW3
  68. varying vec4 vPositionFromLight3;
  69. uniform sampler2D shadowSampler3;
  70. #endif
  71. #ifdef SPOTLIGHT3
  72. uniform vec4 vLightDirection3;
  73. #endif
  74. #ifdef HEMILIGHT3
  75. uniform vec3 vLightGround3;
  76. #endif
  77. #endif
  78. // Samplers
  79. #ifdef DIFFUSE
  80. varying vec2 vDiffuseUV;
  81. uniform sampler2D diffuseSampler;
  82. uniform vec2 vDiffuseInfos;
  83. #endif
  84. #ifdef AMBIENT
  85. varying vec2 vAmbientUV;
  86. uniform sampler2D ambientSampler;
  87. uniform vec2 vAmbientInfos;
  88. #endif
  89. #ifdef OPACITY
  90. varying vec2 vOpacityUV;
  91. uniform sampler2D opacitySampler;
  92. uniform vec2 vOpacityInfos;
  93. #endif
  94. #ifdef REFLECTION
  95. varying vec3 vReflectionUVW;
  96. uniform samplerCube reflectionCubeSampler;
  97. uniform sampler2D reflection2DSampler;
  98. uniform vec3 vReflectionInfos;
  99. #endif
  100. #ifdef EMISSIVE
  101. varying vec2 vEmissiveUV;
  102. uniform vec2 vEmissiveInfos;
  103. uniform sampler2D emissiveSampler;
  104. #endif
  105. #ifdef SPECULAR
  106. varying vec2 vSpecularUV;
  107. uniform vec2 vSpecularInfos;
  108. uniform sampler2D specularSampler;
  109. #endif
  110. // Fresnel
  111. #ifdef FRESNEL
  112. float computeFresnelTerm(vec3 viewDirection, vec3 worldNormal, float bias, float power)
  113. {
  114. float fresnelTerm = pow(bias + abs(dot(viewDirection, worldNormal)), power);
  115. return clamp(fresnelTerm, 0., 1.);
  116. }
  117. #endif
  118. #ifdef DIFFUSEFRESNEL
  119. uniform vec4 diffuseLeftColor;
  120. uniform vec4 diffuseRightColor;
  121. #endif
  122. #ifdef OPACITYFRESNEL
  123. uniform vec4 opacityParts;
  124. #endif
  125. #ifdef REFLECTIONFRESNEL
  126. uniform vec4 reflectionLeftColor;
  127. uniform vec4 reflectionRightColor;
  128. #endif
  129. #ifdef EMISSIVEFRESNEL
  130. uniform vec4 emissiveLeftColor;
  131. uniform vec4 emissiveRightColor;
  132. #endif
  133. // Shadows
  134. #ifdef SHADOWS
  135. float unpack(vec4 color)
  136. {
  137. const vec4 bitShift = vec4(1. / (255. * 255. * 255.), 1. / (255. * 255.), 1. / 255., 1.);
  138. return dot(color, bitShift);
  139. }
  140. float unpackHalf(vec2 color)
  141. {
  142. return color.x + (color.y / 255.0);
  143. }
  144. float computeShadow(vec4 vPositionFromLight, sampler2D shadowSampler)
  145. {
  146. vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
  147. vec2 uv = 0.5 * depth.xy + vec2(0.5, 0.5);
  148. if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0)
  149. {
  150. return 1.0;
  151. }
  152. float shadow = unpack(texture2D(shadowSampler, uv));
  153. if (depth.z > shadow)
  154. {
  155. return 0.;
  156. }
  157. return 1.;
  158. }
  159. // Thanks to http://devmaster.net/
  160. float ChebychevInequality(vec2 moments, float t)
  161. {
  162. if (t <= moments.x)
  163. {
  164. return 1.0;
  165. }
  166. float variance = moments.y - (moments.x * moments.x);
  167. variance = max(variance, 0.);
  168. float d = t - moments.x;
  169. return variance / (variance + d * d);
  170. }
  171. float computeShadowWithVSM(vec4 vPositionFromLight, sampler2D shadowSampler)
  172. {
  173. vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
  174. vec2 uv = 0.5 * depth.xy + vec2(0.5, 0.5);
  175. if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0)
  176. {
  177. return 1.0;
  178. }
  179. vec4 texel = texture2D(shadowSampler, uv);
  180. vec2 moments = vec2(unpackHalf(texel.xy), unpackHalf(texel.zw));
  181. return clamp(1.3 - ChebychevInequality(moments, depth.z), 0., 1.0);
  182. }
  183. #endif
  184. #ifdef CLIPPLANE
  185. varying float fClipDistance;
  186. #endif
  187. // Fog
  188. #ifdef FOG
  189. #define FOGMODE_NONE 0.
  190. #define FOGMODE_EXP 1.
  191. #define FOGMODE_EXP2 2.
  192. #define FOGMODE_LINEAR 3.
  193. #define E 2.71828
  194. uniform vec4 vFogInfos;
  195. uniform vec3 vFogColor;
  196. varying float fFogDistance;
  197. float CalcFogFactor()
  198. {
  199. float fogCoeff = 1.0;
  200. float fogStart = vFogInfos.y;
  201. float fogEnd = vFogInfos.z;
  202. float fogDensity = vFogInfos.w;
  203. if (FOGMODE_LINEAR == vFogInfos.x)
  204. {
  205. fogCoeff = (fogEnd - fFogDistance) / (fogEnd - fogStart);
  206. }
  207. else if (FOGMODE_EXP == vFogInfos.x)
  208. {
  209. fogCoeff = 1.0 / pow(E, fFogDistance * fogDensity);
  210. }
  211. else if (FOGMODE_EXP2 == vFogInfos.x)
  212. {
  213. fogCoeff = 1.0 / pow(E, fFogDistance * fFogDistance * fogDensity * fogDensity);
  214. }
  215. return clamp(fogCoeff, 0.0, 1.0);
  216. }
  217. #endif
  218. // Light Computing
  219. mat3 computeLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec4 diffuseColor, vec3 specularColor) {
  220. mat3 result;
  221. vec3 lightVectorW;
  222. if (lightData.w == 0.)
  223. {
  224. lightVectorW = normalize(lightData.xyz - vPositionW);
  225. }
  226. else
  227. {
  228. lightVectorW = normalize(-lightData.xyz);
  229. }
  230. // diffuse
  231. float ndl = max(0., dot(vNormal, lightVectorW));
  232. // Specular
  233. vec3 angleW = normalize(viewDirectionW + lightVectorW);
  234. float specComp = max(0., dot(vNormal, angleW));
  235. specComp = max(0., pow(specComp, max(1.0, vSpecularColor.a)));
  236. result[0] = ndl * diffuseColor.rgb;
  237. result[1] = specComp * specularColor;
  238. result[2] = vec3(0.);
  239. return result;
  240. }
  241. mat3 computeSpotLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec4 lightDirection, vec4 diffuseColor, vec3 specularColor) {
  242. mat3 result;
  243. vec3 lightVectorW = normalize(lightData.xyz - vPositionW);
  244. // diffuse
  245. float cosAngle = max(0., dot(-lightDirection.xyz, lightVectorW));
  246. float spotAtten = 0.0;
  247. if (cosAngle >= lightDirection.w)
  248. {
  249. cosAngle = max(0., pow(cosAngle, lightData.w));
  250. spotAtten = max(0., (cosAngle - lightDirection.w) / (1. - cosAngle));
  251. // Diffuse
  252. float ndl = max(0., dot(vNormal, -lightDirection.xyz));
  253. // Specular
  254. vec3 angleW = normalize(viewDirectionW - lightDirection.xyz);
  255. float specComp = max(0., dot(vNormal, angleW));
  256. specComp = pow(specComp, vSpecularColor.a);
  257. result[0] = ndl * spotAtten * diffuseColor.rgb;
  258. result[1] = specComp * specularColor * spotAtten;
  259. result[2] = vec3(0.);
  260. return result;
  261. }
  262. result[0] = vec3(0.);
  263. result[1] = vec3(0.);
  264. result[2] = vec3(0.);
  265. return result;
  266. }
  267. mat3 computeHemisphericLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec4 diffuseColor, vec3 specularColor, vec3 groundColor) {
  268. mat3 result;
  269. // Diffuse
  270. float ndl = dot(vNormal, lightData.xyz) * 0.5 + 0.5;
  271. // Specular
  272. vec3 angleW = normalize(viewDirectionW + lightData.xyz);
  273. float specComp = max(0., dot(vNormal, angleW));
  274. specComp = pow(specComp, vSpecularColor.a);
  275. result[0] = mix(groundColor, diffuseColor.rgb, ndl);
  276. result[1] = specComp * specularColor;
  277. result[2] = vec3(0.);
  278. return result;
  279. }
  280. void main(void) {
  281. // Clip plane
  282. #ifdef CLIPPLANE
  283. if (fClipDistance > 0.0)
  284. discard;
  285. #endif
  286. vec3 viewDirectionW = normalize(vEyePosition - vPositionW);
  287. // Base color
  288. vec4 baseColor = vec4(1., 1., 1., 1.);
  289. vec3 diffuseColor = vDiffuseColor.rgb;
  290. #ifdef VERTEXCOLOR
  291. diffuseColor *= vColor;
  292. #endif
  293. #ifdef DIFFUSE
  294. baseColor = texture2D(diffuseSampler, vDiffuseUV);
  295. #ifdef ALPHATEST
  296. if (baseColor.a < 0.4)
  297. discard;
  298. #endif
  299. baseColor.rgb *= vDiffuseInfos.y;
  300. #endif
  301. // Bump
  302. vec3 normalW = normalize(vNormalW);
  303. // Ambient color
  304. vec3 baseAmbientColor = vec3(1., 1., 1.);
  305. #ifdef AMBIENT
  306. baseAmbientColor = texture2D(ambientSampler, vAmbientUV).rgb * vAmbientInfos.y;
  307. #endif
  308. // Lighting
  309. vec3 diffuseBase = vec3(0., 0., 0.);
  310. vec3 specularBase = vec3(0., 0., 0.);
  311. float shadow = 1.;
  312. #ifdef LIGHT0
  313. #ifdef SPOTLIGHT0
  314. mat3 info = computeSpotLighting(viewDirectionW, normalW, vLightData0, vLightDirection0, vLightDiffuse0, vLightSpecular0);
  315. #endif
  316. #ifdef HEMILIGHT0
  317. mat3 info = computeHemisphericLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0, vLightSpecular0, vLightGround0);
  318. #endif
  319. #ifdef POINTDIRLIGHT0
  320. mat3 info = computeLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0, vLightSpecular0);
  321. #endif
  322. #ifdef SHADOW0
  323. #ifdef SHADOWVSM0
  324. shadow = computeShadowWithVSM(vPositionFromLight0, shadowSampler0);
  325. #else
  326. shadow = computeShadow(vPositionFromLight0, shadowSampler0);
  327. #endif
  328. #else
  329. shadow = 1.;
  330. #endif
  331. diffuseBase += info[0] * shadow;
  332. specularBase += info[1] * shadow;
  333. #endif
  334. #ifdef LIGHT1
  335. #ifdef SPOTLIGHT1
  336. info = computeSpotLighting(viewDirectionW, normalW, vLightData1, vLightDirection1, vLightDiffuse1, vLightSpecular1);
  337. #endif
  338. #ifdef HEMILIGHT1
  339. info = computeHemisphericLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1, vLightSpecular1, vLightGround1);
  340. #endif
  341. #ifdef POINTDIRLIGHT1
  342. info = computeLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1, vLightSpecular1);
  343. #endif
  344. #ifdef SHADOW1
  345. #ifdef SHADOWVSM1
  346. shadow = computeShadowWithVSM(vPositionFromLight1, shadowSampler1);
  347. #else
  348. shadow = computeShadow(vPositionFromLight1, shadowSampler1);
  349. #endif
  350. #else
  351. shadow = 1.;
  352. #endif
  353. diffuseBase += info[0] * shadow;
  354. specularBase += info[1] * shadow;
  355. #endif
  356. #ifdef LIGHT2
  357. #ifdef SPOTLIGHT2
  358. info = computeSpotLighting(viewDirectionW, normalW, vLightData2, vLightDirection2, vLightDiffuse2, vLightSpecular2);
  359. #endif
  360. #ifdef HEMILIGHT2
  361. info = computeHemisphericLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2, vLightSpecular2, vLightGround2);
  362. #endif
  363. #ifdef POINTDIRLIGHT2
  364. info = computeLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2, vLightSpecular2);
  365. #endif
  366. #ifdef SHADOW2
  367. #ifdef SHADOWVSM2
  368. shadow = computeShadowWithVSM(vPositionFromLight2, shadowSampler2);
  369. #else
  370. shadow = computeShadow(vPositionFromLight2, shadowSampler2);
  371. #endif
  372. #else
  373. shadow = 1.;
  374. #endif
  375. diffuseBase += info[0] * shadow;
  376. specularBase += info[1] * shadow;
  377. #endif
  378. #ifdef LIGHT3
  379. #ifdef SPOTLIGHT3
  380. info = computeSpotLighting(viewDirectionW, normalW, vLightData3, vLightDirection3, vLightDiffuse3, vLightSpecular3);
  381. #endif
  382. #ifdef HEMILIGHT3
  383. info = computeHemisphericLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3, vLightSpecular3, vLightGround3);
  384. #endif
  385. #ifdef POINTDIRLIGHT3
  386. info = computeLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3, vLightSpecular3);
  387. #endif
  388. #ifdef SHADOW3
  389. #ifdef SHADOWVSM3
  390. shadow = computeShadowWithVSM(vPositionFromLight3, shadowSampler3);
  391. #else
  392. shadow = computeShadow(vPositionFromLight3, shadowSampler3);
  393. #endif
  394. #else
  395. shadow = 1.;
  396. #endif
  397. diffuseBase += info[0] * shadow;
  398. specularBase += info[1] * shadow;
  399. #endif
  400. // Reflection
  401. vec3 reflectionColor = vec3(0., 0., 0.);
  402. #ifdef REFLECTION
  403. if (vReflectionInfos.z != 0.0)
  404. {
  405. reflectionColor = textureCube(reflectionCubeSampler, vReflectionUVW).rgb * vReflectionInfos.y;
  406. }
  407. else
  408. {
  409. vec2 coords = vReflectionUVW.xy;
  410. if (vReflectionInfos.x == MAP_PROJECTION)
  411. {
  412. coords /= vReflectionUVW.z;
  413. }
  414. coords.y = 1.0 - coords.y;
  415. reflectionColor = texture2D(reflection2DSampler, coords).rgb * vReflectionInfos.y;
  416. }
  417. #ifdef REFLECTIONFRESNEL
  418. float reflectionFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, reflectionRightColor.a, reflectionLeftColor.a);
  419. reflectionColor *= reflectionLeftColor.rgb * (1.0 - reflectionFresnelTerm) + reflectionFresnelTerm * reflectionRightColor.rgb;
  420. #endif
  421. #endif
  422. // Alpha
  423. float alpha = vDiffuseColor.a;
  424. #ifdef OPACITY
  425. vec4 opacityMap = texture2D(opacitySampler, vOpacityUV);
  426. #ifdef OPACITYRGB
  427. opacityMap.rgb = opacityMap.rgb * vec3(0.3, 0.59, 0.11);
  428. alpha *= (opacityMap.x + opacityMap.y + opacityMap.z)* vOpacityInfos.y;
  429. #else
  430. alpha *= opacityMap.a * vOpacityInfos.y;
  431. #endif
  432. #endif
  433. #ifdef OPACITYFRESNEL
  434. float opacityFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, opacityParts.z, opacityParts.w);
  435. alpha += opacityParts.x * (1.0 - opacityFresnelTerm) + opacityFresnelTerm * opacityParts.y;
  436. #endif
  437. // Emissive
  438. vec3 emissiveColor = vEmissiveColor;
  439. #ifdef EMISSIVE
  440. emissiveColor += texture2D(emissiveSampler, vEmissiveUV).rgb * vEmissiveInfos.y;
  441. #endif
  442. #ifdef EMISSIVEFRESNEL
  443. float emissiveFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, emissiveRightColor.a, emissiveLeftColor.a);
  444. emissiveColor *= emissiveLeftColor.rgb * (1.0 - emissiveFresnelTerm) + emissiveFresnelTerm * emissiveRightColor.rgb;
  445. #endif
  446. // Specular map
  447. vec3 specularColor = vSpecularColor.rgb;
  448. #ifdef SPECULAR
  449. specularColor = texture2D(specularSampler, vSpecularUV).rgb * vSpecularInfos.y;
  450. #endif
  451. // Fresnel
  452. #ifdef DIFFUSEFRESNEL
  453. float diffuseFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, diffuseRightColor.a, diffuseLeftColor.a);
  454. diffuseBase *= diffuseLeftColor.rgb * (1.0 - diffuseFresnelTerm) + diffuseFresnelTerm * diffuseRightColor.rgb;
  455. #endif
  456. // Composition
  457. vec3 finalDiffuse = clamp(diffuseBase * diffuseColor + emissiveColor + vAmbientColor, 0.0, 1.0) * baseColor.rgb;
  458. vec3 finalSpecular = specularBase * specularColor;
  459. vec4 color = vec4(finalDiffuse * baseAmbientColor + finalSpecular + reflectionColor, alpha);
  460. #ifdef FOG
  461. float fog = CalcFogFactor();
  462. color.rgb = fog * color.rgb + (1.0 - fog) * vFogColor;
  463. #endif
  464. gl_FragColor = color;
  465. }