opengl_fragment.glsl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. uniform sampler2D baseTexture;
  2. uniform vec3 dayLight;
  3. uniform vec4 skyBgColor;
  4. uniform float fogDistance;
  5. uniform vec3 eyePosition;
  6. // The cameraOffset is the current center of the visible world.
  7. uniform vec3 cameraOffset;
  8. uniform float animationTimer;
  9. #ifdef ENABLE_DYNAMIC_SHADOWS
  10. // shadow texture
  11. uniform sampler2D ShadowMapSampler;
  12. // shadow uniforms
  13. uniform vec3 v_LightDirection;
  14. uniform float f_textureresolution;
  15. uniform mat4 m_ShadowViewProj;
  16. uniform float f_shadowfar;
  17. uniform float f_shadow_strength;
  18. uniform vec4 CameraPos;
  19. uniform float xyPerspectiveBias0;
  20. uniform float xyPerspectiveBias1;
  21. varying float adj_shadow_strength;
  22. varying float cosLight;
  23. varying float f_normal_length;
  24. varying vec3 shadow_position;
  25. varying float perspective_factor;
  26. #endif
  27. varying vec3 vNormal;
  28. varying vec3 vPosition;
  29. // World position in the visible world (i.e. relative to the cameraOffset.)
  30. // This can be used for many shader effects without loss of precision.
  31. // If the absolute position is required it can be calculated with
  32. // cameraOffset + worldPosition (for large coordinates the limits of float
  33. // precision must be considered).
  34. varying vec3 worldPosition;
  35. varying lowp vec4 varColor;
  36. #ifdef GL_ES
  37. varying mediump vec2 varTexCoord;
  38. #else
  39. centroid varying vec2 varTexCoord;
  40. #endif
  41. varying vec3 eyeVec;
  42. varying float nightRatio;
  43. const float fogStart = FOG_START;
  44. const float fogShadingParameter = 1.0 / ( 1.0 - fogStart);
  45. #ifdef ENABLE_DYNAMIC_SHADOWS
  46. // assuming near is always 1.0
  47. float getLinearDepth()
  48. {
  49. return 2.0 * f_shadowfar / (f_shadowfar + 1.0 - (2.0 * gl_FragCoord.z - 1.0) * (f_shadowfar - 1.0));
  50. }
  51. vec3 getLightSpacePosition()
  52. {
  53. return shadow_position * 0.5 + 0.5;
  54. }
  55. // custom smoothstep implementation because it's not defined in glsl1.2
  56. // https://docs.gl/sl4/smoothstep
  57. float mtsmoothstep(in float edge0, in float edge1, in float x)
  58. {
  59. float t = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);
  60. return t * t * (3.0 - 2.0 * t);
  61. }
  62. #ifdef COLORED_SHADOWS
  63. // c_precision of 128 fits within 7 base-10 digits
  64. const float c_precision = 128.0;
  65. const float c_precisionp1 = c_precision + 1.0;
  66. float packColor(vec3 color)
  67. {
  68. return floor(color.b * c_precision + 0.5)
  69. + floor(color.g * c_precision + 0.5) * c_precisionp1
  70. + floor(color.r * c_precision + 0.5) * c_precisionp1 * c_precisionp1;
  71. }
  72. vec3 unpackColor(float value)
  73. {
  74. vec3 color;
  75. color.b = mod(value, c_precisionp1) / c_precision;
  76. color.g = mod(floor(value / c_precisionp1), c_precisionp1) / c_precision;
  77. color.r = floor(value / (c_precisionp1 * c_precisionp1)) / c_precision;
  78. return color;
  79. }
  80. vec4 getHardShadowColor(sampler2D shadowsampler, vec2 smTexCoord, float realDistance)
  81. {
  82. vec4 texDepth = texture2D(shadowsampler, smTexCoord.xy).rgba;
  83. float visibility = step(0.0, realDistance - texDepth.r);
  84. vec4 result = vec4(visibility, vec3(0.0,0.0,0.0));//unpackColor(texDepth.g));
  85. if (visibility < 0.1) {
  86. visibility = step(0.0, realDistance - texDepth.b);
  87. result = vec4(visibility, unpackColor(texDepth.a));
  88. }
  89. return result;
  90. }
  91. #else
  92. float getHardShadow(sampler2D shadowsampler, vec2 smTexCoord, float realDistance)
  93. {
  94. float texDepth = texture2D(shadowsampler, smTexCoord.xy).r;
  95. float visibility = step(0.0, realDistance - texDepth);
  96. return visibility;
  97. }
  98. #endif
  99. #if SHADOW_FILTER == 2
  100. #define PCFBOUND 2.0 // 5x5
  101. #define PCFSAMPLES 25
  102. #elif SHADOW_FILTER == 1
  103. #define PCFBOUND 1.0 // 3x3
  104. #define PCFSAMPLES 9
  105. #else
  106. #define PCFBOUND 0.0
  107. #define PCFSAMPLES 1
  108. #endif
  109. #ifdef COLORED_SHADOWS
  110. float getHardShadowDepth(sampler2D shadowsampler, vec2 smTexCoord, float realDistance)
  111. {
  112. vec4 texDepth = texture2D(shadowsampler, smTexCoord.xy);
  113. float depth = max(realDistance - texDepth.r, realDistance - texDepth.b);
  114. return depth;
  115. }
  116. #else
  117. float getHardShadowDepth(sampler2D shadowsampler, vec2 smTexCoord, float realDistance)
  118. {
  119. float texDepth = texture2D(shadowsampler, smTexCoord.xy).r;
  120. float depth = realDistance - texDepth;
  121. return depth;
  122. }
  123. #endif
  124. #define BASEFILTERRADIUS 1.0
  125. float getPenumbraRadius(sampler2D shadowsampler, vec2 smTexCoord, float realDistance)
  126. {
  127. // Return fast if sharp shadows are requested
  128. if (PCFBOUND == 0.0 || SOFTSHADOWRADIUS <= 0.0)
  129. return 0.0;
  130. vec2 clampedpos;
  131. float y, x;
  132. float depth = getHardShadowDepth(shadowsampler, smTexCoord.xy, realDistance);
  133. // A factor from 0 to 1 to reduce blurring of short shadows
  134. float sharpness_factor = 1.0;
  135. // conversion factor from shadow depth to blur radius
  136. float depth_to_blur = f_shadowfar / SOFTSHADOWRADIUS / xyPerspectiveBias0;
  137. if (depth > 0.0 && f_normal_length > 0.0)
  138. // 5 is empirical factor that controls how fast shadow loses sharpness
  139. sharpness_factor = clamp(5 * depth * depth_to_blur, 0.0, 1.0);
  140. depth = 0.0;
  141. float world_to_texture = xyPerspectiveBias1 / perspective_factor / perspective_factor
  142. * f_textureresolution / 2.0 / f_shadowfar;
  143. float world_radius = 0.2; // shadow blur radius in world float coordinates, e.g. 0.2 = 0.02 of one node
  144. return max(BASEFILTERRADIUS * f_textureresolution / 4096.0, sharpness_factor * world_radius * world_to_texture * SOFTSHADOWRADIUS);
  145. }
  146. #ifdef POISSON_FILTER
  147. const vec2[64] poissonDisk = vec2[64](
  148. vec2(0.170019, -0.040254),
  149. vec2(-0.299417, 0.791925),
  150. vec2(0.645680, 0.493210),
  151. vec2(-0.651784, 0.717887),
  152. vec2(0.421003, 0.027070),
  153. vec2(-0.817194, -0.271096),
  154. vec2(-0.705374, -0.668203),
  155. vec2(0.977050, -0.108615),
  156. vec2(0.063326, 0.142369),
  157. vec2(0.203528, 0.214331),
  158. vec2(-0.667531, 0.326090),
  159. vec2(-0.098422, -0.295755),
  160. vec2(-0.885922, 0.215369),
  161. vec2(0.566637, 0.605213),
  162. vec2(0.039766, -0.396100),
  163. vec2(0.751946, 0.453352),
  164. vec2(0.078707, -0.715323),
  165. vec2(-0.075838, -0.529344),
  166. vec2(0.724479, -0.580798),
  167. vec2(0.222999, -0.215125),
  168. vec2(-0.467574, -0.405438),
  169. vec2(-0.248268, -0.814753),
  170. vec2(0.354411, -0.887570),
  171. vec2(0.175817, 0.382366),
  172. vec2(0.487472, -0.063082),
  173. vec2(0.355476, 0.025357),
  174. vec2(-0.084078, 0.898312),
  175. vec2(0.488876, -0.783441),
  176. vec2(0.470016, 0.217933),
  177. vec2(-0.696890, -0.549791),
  178. vec2(-0.149693, 0.605762),
  179. vec2(0.034211, 0.979980),
  180. vec2(0.503098, -0.308878),
  181. vec2(-0.016205, -0.872921),
  182. vec2(0.385784, -0.393902),
  183. vec2(-0.146886, -0.859249),
  184. vec2(0.643361, 0.164098),
  185. vec2(0.634388, -0.049471),
  186. vec2(-0.688894, 0.007843),
  187. vec2(0.464034, -0.188818),
  188. vec2(-0.440840, 0.137486),
  189. vec2(0.364483, 0.511704),
  190. vec2(0.034028, 0.325968),
  191. vec2(0.099094, -0.308023),
  192. vec2(0.693960, -0.366253),
  193. vec2(0.678884, -0.204688),
  194. vec2(0.001801, 0.780328),
  195. vec2(0.145177, -0.898984),
  196. vec2(0.062655, -0.611866),
  197. vec2(0.315226, -0.604297),
  198. vec2(-0.780145, 0.486251),
  199. vec2(-0.371868, 0.882138),
  200. vec2(0.200476, 0.494430),
  201. vec2(-0.494552, -0.711051),
  202. vec2(0.612476, 0.705252),
  203. vec2(-0.578845, -0.768792),
  204. vec2(-0.772454, -0.090976),
  205. vec2(0.504440, 0.372295),
  206. vec2(0.155736, 0.065157),
  207. vec2(0.391522, 0.849605),
  208. vec2(-0.620106, -0.328104),
  209. vec2(0.789239, -0.419965),
  210. vec2(-0.545396, 0.538133),
  211. vec2(-0.178564, -0.596057)
  212. );
  213. #ifdef COLORED_SHADOWS
  214. vec4 getShadowColor(sampler2D shadowsampler, vec2 smTexCoord, float realDistance)
  215. {
  216. float radius = getPenumbraRadius(shadowsampler, smTexCoord, realDistance);
  217. if (radius < 0.1) {
  218. // we are in the middle of even brightness, no need for filtering
  219. return getHardShadowColor(shadowsampler, smTexCoord.xy, realDistance);
  220. }
  221. vec2 clampedpos;
  222. vec4 visibility = vec4(0.0);
  223. float scale_factor = radius / f_textureresolution;
  224. int samples = (1 + 1 * int(SOFTSHADOWRADIUS > 1.0)) * PCFSAMPLES; // scale max samples for the soft shadows
  225. samples = int(clamp(pow(4.0 * radius + 1.0, 2.0), 1.0, float(samples)));
  226. int init_offset = int(floor(mod(((smTexCoord.x * 34.0) + 1.0) * smTexCoord.y, 64.0-samples)));
  227. int end_offset = int(samples) + init_offset;
  228. for (int x = init_offset; x < end_offset; x++) {
  229. clampedpos = poissonDisk[x] * scale_factor + smTexCoord.xy;
  230. visibility += getHardShadowColor(shadowsampler, clampedpos.xy, realDistance);
  231. }
  232. return visibility / samples;
  233. }
  234. #else
  235. float getShadow(sampler2D shadowsampler, vec2 smTexCoord, float realDistance)
  236. {
  237. float radius = getPenumbraRadius(shadowsampler, smTexCoord, realDistance);
  238. if (radius < 0.1) {
  239. // we are in the middle of even brightness, no need for filtering
  240. return getHardShadow(shadowsampler, smTexCoord.xy, realDistance);
  241. }
  242. vec2 clampedpos;
  243. float visibility = 0.0;
  244. float scale_factor = radius / f_textureresolution;
  245. int samples = (1 + 1 * int(SOFTSHADOWRADIUS > 1.0)) * PCFSAMPLES; // scale max samples for the soft shadows
  246. samples = int(clamp(pow(4.0 * radius + 1.0, 2.0), 1.0, float(samples)));
  247. int init_offset = int(floor(mod(((smTexCoord.x * 34.0) + 1.0) * smTexCoord.y, 64.0-samples)));
  248. int end_offset = int(samples) + init_offset;
  249. for (int x = init_offset; x < end_offset; x++) {
  250. clampedpos = poissonDisk[x] * scale_factor + smTexCoord.xy;
  251. visibility += getHardShadow(shadowsampler, clampedpos.xy, realDistance);
  252. }
  253. return visibility / samples;
  254. }
  255. #endif
  256. #else
  257. /* poisson filter disabled */
  258. #ifdef COLORED_SHADOWS
  259. vec4 getShadowColor(sampler2D shadowsampler, vec2 smTexCoord, float realDistance)
  260. {
  261. float radius = getPenumbraRadius(shadowsampler, smTexCoord, realDistance);
  262. if (radius < 0.1) {
  263. // we are in the middle of even brightness, no need for filtering
  264. return getHardShadowColor(shadowsampler, smTexCoord.xy, realDistance);
  265. }
  266. vec2 clampedpos;
  267. vec4 visibility = vec4(0.0);
  268. float x, y;
  269. float bound = (1 + 0.5 * int(SOFTSHADOWRADIUS > 1.0)) * PCFBOUND; // scale max bound for soft shadows
  270. bound = clamp(0.5 * (4.0 * radius - 1.0), 0.5, bound);
  271. float scale_factor = radius / bound / f_textureresolution;
  272. float n = 0.0;
  273. // basic PCF filter
  274. for (y = -bound; y <= bound; y += 1.0)
  275. for (x = -bound; x <= bound; x += 1.0) {
  276. clampedpos = vec2(x,y) * scale_factor + smTexCoord.xy;
  277. visibility += getHardShadowColor(shadowsampler, clampedpos.xy, realDistance);
  278. n += 1.0;
  279. }
  280. return visibility / max(n, 1.0);
  281. }
  282. #else
  283. float getShadow(sampler2D shadowsampler, vec2 smTexCoord, float realDistance)
  284. {
  285. float radius = getPenumbraRadius(shadowsampler, smTexCoord, realDistance);
  286. if (radius < 0.1) {
  287. // we are in the middle of even brightness, no need for filtering
  288. return getHardShadow(shadowsampler, smTexCoord.xy, realDistance);
  289. }
  290. vec2 clampedpos;
  291. float visibility = 0.0;
  292. float x, y;
  293. float bound = (1 + 0.5 * int(SOFTSHADOWRADIUS > 1.0)) * PCFBOUND; // scale max bound for soft shadows
  294. bound = clamp(0.5 * (4.0 * radius - 1.0), 0.5, bound);
  295. float scale_factor = radius / bound / f_textureresolution;
  296. float n = 0.0;
  297. // basic PCF filter
  298. for (y = -bound; y <= bound; y += 1.0)
  299. for (x = -bound; x <= bound; x += 1.0) {
  300. clampedpos = vec2(x,y) * scale_factor + smTexCoord.xy;
  301. visibility += getHardShadow(shadowsampler, clampedpos.xy, realDistance);
  302. n += 1.0;
  303. }
  304. return visibility / max(n, 1.0);
  305. }
  306. #endif
  307. #endif
  308. #endif
  309. #if ENABLE_TONE_MAPPING
  310. /* Hable's UC2 Tone mapping parameters
  311. A = 0.22;
  312. B = 0.30;
  313. C = 0.10;
  314. D = 0.20;
  315. E = 0.01;
  316. F = 0.30;
  317. W = 11.2;
  318. equation used: ((x * (A * x + C * B) + D * E) / (x * (A * x + B) + D * F)) - E / F
  319. */
  320. vec3 uncharted2Tonemap(vec3 x)
  321. {
  322. return ((x * (0.22 * x + 0.03) + 0.002) / (x * (0.22 * x + 0.3) + 0.06)) - 0.03333;
  323. }
  324. vec4 applyToneMapping(vec4 color)
  325. {
  326. color = vec4(pow(color.rgb, vec3(2.2)), color.a);
  327. const float gamma = 1.6;
  328. const float exposureBias = 5.5;
  329. color.rgb = uncharted2Tonemap(exposureBias * color.rgb);
  330. // Precalculated white_scale from
  331. //vec3 whiteScale = 1.0 / uncharted2Tonemap(vec3(W));
  332. vec3 whiteScale = vec3(1.036015346);
  333. color.rgb *= whiteScale;
  334. return vec4(pow(color.rgb, vec3(1.0 / gamma)), color.a);
  335. }
  336. #endif
  337. void main(void)
  338. {
  339. vec3 color;
  340. vec2 uv = varTexCoord.st;
  341. vec4 base = texture2D(baseTexture, uv).rgba;
  342. // If alpha is zero, we can just discard the pixel. This fixes transparency
  343. // on GPUs like GC7000L, where GL_ALPHA_TEST is not implemented in mesa,
  344. // and also on GLES 2, where GL_ALPHA_TEST is missing entirely.
  345. #ifdef USE_DISCARD
  346. if (base.a == 0.0)
  347. discard;
  348. #endif
  349. #ifdef USE_DISCARD_REF
  350. if (base.a < 0.5)
  351. discard;
  352. #endif
  353. color = base.rgb;
  354. vec4 col = vec4(color.rgb * varColor.rgb, 1.0);
  355. #ifdef ENABLE_DYNAMIC_SHADOWS
  356. if (f_shadow_strength > 0.0) {
  357. float shadow_int = 0.0;
  358. vec3 shadow_color = vec3(0.0, 0.0, 0.0);
  359. vec3 posLightSpace = getLightSpacePosition();
  360. float distance_rate = (1.0 - pow(clamp(2.0 * length(posLightSpace.xy - 0.5),0.0,1.0), 10.0));
  361. if (max(abs(posLightSpace.x - 0.5), abs(posLightSpace.y - 0.5)) > 0.5)
  362. distance_rate = 0.0;
  363. float f_adj_shadow_strength = max(adj_shadow_strength-mtsmoothstep(0.9,1.1, posLightSpace.z),0.0);
  364. if (distance_rate > 1e-7) {
  365. #ifdef COLORED_SHADOWS
  366. vec4 visibility;
  367. if (cosLight > 0.0 || f_normal_length < 1e-3)
  368. visibility = getShadowColor(ShadowMapSampler, posLightSpace.xy, posLightSpace.z);
  369. else
  370. visibility = vec4(1.0, 0.0, 0.0, 0.0);
  371. shadow_int = visibility.r;
  372. shadow_color = visibility.gba;
  373. #else
  374. if (cosLight > 0.0 || f_normal_length < 1e-3)
  375. shadow_int = getShadow(ShadowMapSampler, posLightSpace.xy, posLightSpace.z);
  376. else
  377. shadow_int = 1.0;
  378. #endif
  379. shadow_int *= distance_rate;
  380. shadow_int = clamp(shadow_int, 0.0, 1.0);
  381. }
  382. // turns out that nightRatio falls off much faster than
  383. // actual brightness of artificial light in relation to natual light.
  384. // Power ratio was measured on torches in MTG (brightness = 14).
  385. float adjusted_night_ratio = pow(max(0.0, nightRatio), 0.6);
  386. // Apply self-shadowing when light falls at a narrow angle to the surface
  387. // Cosine of the cut-off angle.
  388. const float self_shadow_cutoff_cosine = 0.035;
  389. if (f_normal_length != 0 && cosLight < self_shadow_cutoff_cosine) {
  390. shadow_int = max(shadow_int, 1 - clamp(cosLight, 0.0, self_shadow_cutoff_cosine)/self_shadow_cutoff_cosine);
  391. shadow_color = mix(vec3(0.0), shadow_color, min(cosLight, self_shadow_cutoff_cosine)/self_shadow_cutoff_cosine);
  392. }
  393. shadow_int *= f_adj_shadow_strength;
  394. // calculate fragment color from components:
  395. col.rgb =
  396. adjusted_night_ratio * col.rgb + // artificial light
  397. (1.0 - adjusted_night_ratio) * ( // natural light
  398. col.rgb * (1.0 - shadow_int * (1.0 - shadow_color)) + // filtered texture color
  399. dayLight * shadow_color * shadow_int); // reflected filtered sunlight/moonlight
  400. }
  401. #endif
  402. #if ENABLE_TONE_MAPPING
  403. col = applyToneMapping(col);
  404. #endif
  405. // Due to a bug in some (older ?) graphics stacks (possibly in the glsl compiler ?),
  406. // the fog will only be rendered correctly if the last operation before the
  407. // clamp() is an addition. Else, the clamp() seems to be ignored.
  408. // E.g. the following won't work:
  409. // float clarity = clamp(fogShadingParameter
  410. // * (fogDistance - length(eyeVec)) / fogDistance), 0.0, 1.0);
  411. // As additions usually come for free following a multiplication, the new formula
  412. // should be more efficient as well.
  413. // Note: clarity = (1 - fogginess)
  414. float clarity = clamp(fogShadingParameter
  415. - fogShadingParameter * length(eyeVec) / fogDistance, 0.0, 1.0);
  416. col = mix(skyBgColor, col, clarity);
  417. col = vec4(col.rgb, base.a);
  418. gl_FragColor = col;
  419. }