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

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