A 2D tile-based sandbox game.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

shaders.cc 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include "shaders.h"
  2. namespace Cygnet::Shaders {
  3. const char *chunkVx = R"glsl(
  4. precision mediump float;
  5. uniform mat3 camera;
  6. uniform vec2 pos;
  7. attribute vec2 vertex;
  8. varying vec2 v_tileCoord;
  9. void main() {
  10. vec3 pos = camera * vec3(pos + vertex, 1);
  11. gl_Position = vec4(pos.xy, 0, 1);
  12. v_tileCoord = vertex;
  13. }
  14. )glsl";
  15. const char *chunkFr = R"glsl(
  16. precision mediump float;
  17. #define TILE_SIZE 32.0
  18. #define CHUNK_WIDTH 64
  19. #define CHUNK_HEIGHT 64
  20. varying vec2 v_tileCoord;
  21. uniform sampler2D tileAtlas;
  22. uniform vec2 tileAtlasSize;
  23. uniform sampler2D tiles;
  24. void main() {
  25. vec2 tilePos = floor(vec2(v_tileCoord.x, v_tileCoord.y));
  26. vec4 tileColor = texture2D(tiles, tilePos / vec2(CHUNK_WIDTH, CHUNK_HEIGHT));
  27. float tileID = floor((tileColor.r * 256.0 + tileColor.a) * 256.0);
  28. // 1/(TILE_SIZE*16) plays the same role here as in the sprite vertex shader.
  29. vec2 offset = v_tileCoord - tilePos;
  30. vec2 pixoffset = (1.0 - offset * 2.0) / (TILE_SIZE * 16.0);
  31. vec2 atlasPos = vec2(
  32. pixoffset.x + tileID + offset.x,
  33. pixoffset.y + floor(tileID / tileAtlasSize.x) + offset.y);
  34. gl_FragColor = texture2D(tileAtlas, atlasPos / tileAtlasSize);
  35. }
  36. )glsl";
  37. const char *tileVx = R"glsl(
  38. precision mediump float;
  39. uniform mat3 camera;
  40. uniform mat3 transform;
  41. attribute vec2 vertex;
  42. varying vec2 v_tileCoord;
  43. void main() {
  44. vec3 pos = camera * transform * vec3(vertex, 1);
  45. gl_Position = vec4(pos.xy, 0, 1);
  46. v_tileCoord = vertex;
  47. }
  48. )glsl";
  49. const char *tileFr = R"glsl(
  50. precision mediump float;
  51. #define TILE_SIZE 32.0
  52. varying vec2 v_tileCoord;
  53. uniform sampler2D tileAtlas;
  54. uniform vec2 tileAtlasSize;
  55. uniform float tileID;
  56. void main() {
  57. // 1/(TILE_SIZE*16) plays the same role here as in the sprite vertex shader.
  58. vec2 offset = v_tileCoord;
  59. vec2 pixoffset = (1.0 - offset * 2.0) / (TILE_SIZE * 16.0);
  60. vec2 atlasPos = vec2(
  61. pixoffset.x + tileID + offset.x,
  62. pixoffset.y + floor(tileID / tileAtlasSize.x) + offset.y);
  63. gl_FragColor = texture2D(tileAtlas, atlasPos / tileAtlasSize);
  64. }
  65. )glsl";
  66. const char *spriteVx = R"glsl(
  67. precision mediump float;
  68. #define TILE_SIZE 32.0
  69. uniform mat3 camera;
  70. uniform mat3 transform;
  71. uniform vec2 frameSize;
  72. uniform vec2 frameInfo; // frame count, frame index
  73. attribute vec2 vertex;
  74. varying vec2 v_texCoord;
  75. void main() {
  76. // Here, I'm basically treating 1/(TILE_SIZE*16) as half the size of a "pixel".
  77. // It's just an arbitrary small number, but it works as an offset to make sure
  78. // neighbouring parts of the atlas don't bleed into the frame we actually
  79. // want to draw due to (nearest neighbour) interpolation.
  80. float pixoffset = (1.0 - vertex.y * 2.0) / (frameSize.y * TILE_SIZE * 16.0);
  81. v_texCoord = vec2(
  82. vertex.x,
  83. (frameSize.y * frameInfo.y + (frameSize.y * vertex.y)) /
  84. (frameSize.y * frameInfo.x) + pixoffset);
  85. vec3 pos = camera * transform * vec3(vertex * frameSize, 1);
  86. gl_Position = vec4(pos.xy, 0, 1);
  87. }
  88. )glsl";
  89. const char *spriteFr = R"glsl(
  90. precision mediump float;
  91. varying vec2 v_texCoord;
  92. uniform sampler2D tex;
  93. void main() {
  94. gl_FragColor = texture2D(tex, v_texCoord);
  95. }
  96. )glsl";
  97. const char *rectVx = R"glsl(
  98. precision mediump float;
  99. uniform mat3 camera;
  100. uniform vec2 pos;
  101. uniform vec2 size;
  102. attribute vec2 vertex;
  103. varying vec2 v_coord;
  104. void main() {
  105. vec3 pos = camera * vec3(pos + vertex * size, 1);
  106. gl_Position = vec4(pos.xy, 0, 1);
  107. v_coord = vertex * size;
  108. }
  109. )glsl";
  110. const char *rectFr = R"glsl(
  111. precision mediump float;
  112. #define THICKNESS 0.02
  113. varying vec2 v_coord;
  114. uniform vec2 size;
  115. void main() {
  116. vec2 invCoord = size - v_coord;
  117. float minDist = min(v_coord.x, min(v_coord.y, min(invCoord.x, invCoord.y)));
  118. gl_FragColor = vec4(0.6, 0.6, 0.6, 0.8) * float(minDist < THICKNESS);
  119. }
  120. )glsl";
  121. }