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.6KB

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