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 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 *spriteVx = R"glsl(
  38. precision mediump float;
  39. #define TILE_SIZE 32.0
  40. uniform mat3 camera;
  41. uniform mat3 transform;
  42. uniform vec2 frameSize;
  43. uniform vec2 frameInfo; // frame count, frame index
  44. attribute vec2 vertex;
  45. varying vec2 v_texCoord;
  46. void main() {
  47. // Here, I'm basically treating 1/(TILE_SIZE*16) as half the size of a "pixel".
  48. // It's just an arbitrary small number, but it works as an offset to make sure
  49. // neighbouring parts of the atlas don't bleed into the frame we actually
  50. // want to draw due to (nearest neighbour) interpolation.
  51. float pixoffset = (1.0 - vertex.y * 2.0) / (frameSize.y * TILE_SIZE * 16.0);
  52. v_texCoord = vec2(
  53. vertex.x,
  54. (frameSize.y * frameInfo.y + (frameSize.y * vertex.y)) /
  55. (frameSize.y * frameInfo.x) + pixoffset);
  56. vec3 pos = camera * transform * vec3(vertex * frameSize, 1);
  57. gl_Position = vec4(pos.xy, 0, 1);
  58. }
  59. )glsl";
  60. const char *spriteFr = R"glsl(
  61. precision mediump float;
  62. varying vec2 v_texCoord;
  63. uniform sampler2D tex;
  64. void main() {
  65. gl_FragColor = texture2D(tex, v_texCoord);
  66. }
  67. )glsl";
  68. const char *rectVx = R"glsl(
  69. precision mediump float;
  70. uniform mat3 camera;
  71. uniform vec2 pos;
  72. uniform vec2 size;
  73. attribute vec2 vertex;
  74. varying vec2 v_coord;
  75. void main() {
  76. vec3 pos = camera * vec3(pos + vertex * size, 1);
  77. gl_Position = vec4(pos.xy, 0, 1);
  78. v_coord = vertex * size;
  79. }
  80. )glsl";
  81. const char *rectFr = R"glsl(
  82. precision mediump float;
  83. #define THICKNESS 0.02
  84. varying vec2 v_coord;
  85. uniform vec2 size;
  86. void main() {
  87. vec2 invCoord = size - v_coord;
  88. float minDist = min(v_coord.x, min(v_coord.y, min(invCoord.x, invCoord.y)));
  89. gl_FragColor = vec4(0.6, 0.6, 0.6, 0.8) * float(minDist < THICKNESS);
  90. }
  91. )glsl";
  92. }