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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 *chunkShadowVx = R"glsl(
  38. precision mediump float;
  39. #define CHUNK_WIDTH 64
  40. #define CHUNK_HEIGHT 64
  41. uniform mat3 camera;
  42. uniform vec2 pos;
  43. attribute vec2 vertex;
  44. varying vec2 v_texCoord;
  45. void main() {
  46. vec3 pos = camera * vec3(pos + vertex, 1);
  47. gl_Position = vec4(pos.xy, 0, 1);
  48. v_texCoord = vertex / vec2(CHUNK_WIDTH, CHUNK_HEIGHT);
  49. }
  50. )glsl";
  51. const char *chunkShadowFr = R"glsl(
  52. precision mediump float;
  53. varying vec2 v_texCoord;
  54. uniform sampler2D tex;
  55. void main() {
  56. vec4 color = texture2D(tex, v_texCoord);
  57. gl_FragColor = vec4(0, 0, 0, 1.0 - color.r);
  58. }
  59. )glsl";
  60. const char *tileVx = R"glsl(
  61. precision mediump float;
  62. uniform mat3 camera;
  63. uniform mat3 transform;
  64. attribute vec2 vertex;
  65. varying vec2 v_tileCoord;
  66. void main() {
  67. vec3 pos = camera * transform * vec3(vertex, 1);
  68. gl_Position = vec4(pos.xy, 0, 1);
  69. v_tileCoord = vertex;
  70. }
  71. )glsl";
  72. const char *tileFr = R"glsl(
  73. precision mediump float;
  74. #define TILE_SIZE 32.0
  75. varying vec2 v_tileCoord;
  76. uniform sampler2D tileAtlas;
  77. uniform vec2 tileAtlasSize;
  78. uniform float tileID;
  79. void main() {
  80. // 1/(TILE_SIZE*16) plays the same role here as in the sprite vertex shader.
  81. vec2 offset = v_tileCoord;
  82. vec2 pixoffset = (1.0 - offset * 2.0) / (TILE_SIZE * 16.0);
  83. vec2 atlasPos = vec2(
  84. pixoffset.x + tileID + offset.x,
  85. pixoffset.y + floor(tileID / tileAtlasSize.x) + offset.y);
  86. gl_FragColor = texture2D(tileAtlas, atlasPos / tileAtlasSize);
  87. }
  88. )glsl";
  89. const char *spriteVx = R"glsl(
  90. precision mediump float;
  91. #define TILE_SIZE 32.0
  92. uniform mat3 camera;
  93. uniform mat3 transform;
  94. uniform vec2 frameSize;
  95. uniform vec2 frameInfo; // frame count, frame index
  96. attribute vec2 vertex;
  97. varying vec2 v_texCoord;
  98. void main() {
  99. // Here, I'm basically treating 1/(TILE_SIZE*16) as half the size of a "pixel".
  100. // It's just an arbitrary small number, but it works as an offset to make sure
  101. // neighbouring parts of the atlas don't bleed into the frame we actually
  102. // want to draw due to (nearest neighbour) interpolation.
  103. float pixoffset = (1.0 - vertex.y * 2.0) / (frameSize.y * TILE_SIZE * 16.0);
  104. v_texCoord = vec2(
  105. vertex.x,
  106. (frameSize.y * frameInfo.y + (frameSize.y * vertex.y)) /
  107. (frameSize.y * frameInfo.x) + pixoffset);
  108. vec3 pos = camera * transform * vec3(vertex * frameSize, 1);
  109. gl_Position = vec4(pos.xy, 0, 1);
  110. }
  111. )glsl";
  112. const char *spriteFr = R"glsl(
  113. precision mediump float;
  114. varying vec2 v_texCoord;
  115. uniform sampler2D tex;
  116. void main() {
  117. gl_FragColor = texture2D(tex, v_texCoord);
  118. }
  119. )glsl";
  120. const char *rectVx = R"glsl(
  121. precision mediump float;
  122. uniform mat3 camera;
  123. uniform vec2 pos;
  124. uniform vec2 size;
  125. attribute vec2 vertex;
  126. varying vec2 v_coord;
  127. void main() {
  128. vec3 pos = camera * vec3(pos + vertex * size, 1);
  129. gl_Position = vec4(pos.xy, 0, 1);
  130. v_coord = vertex * size;
  131. }
  132. )glsl";
  133. const char *rectFr = R"glsl(
  134. precision mediump float;
  135. #define THICKNESS 0.02
  136. varying vec2 v_coord;
  137. uniform vec2 size;
  138. void main() {
  139. vec2 invCoord = size - v_coord;
  140. float minDist = min(v_coord.x, min(v_coord.y, min(invCoord.x, invCoord.y)));
  141. gl_FragColor = vec4(0.6, 0.6, 0.6, 0.8) * float(minDist < THICKNESS);
  142. }
  143. )glsl";
  144. const char *blendVx = R"glsl(
  145. precision mediump float;
  146. attribute vec2 vertex;
  147. attribute vec2 texCoord;
  148. varying vec2 v_texCoord;
  149. void main() {
  150. gl_Position = vec4(vertex.xy, 0, 1);
  151. v_texCoord = texCoord;
  152. }
  153. )glsl";
  154. const char *blendFr = R"glsl(
  155. precision mediump float;
  156. varying vec2 v_texCoord;
  157. uniform sampler2D tex;
  158. void main() {
  159. //gl_FragColor = vec4(v_texCoord.x, v_texCoord.y, 0, 1);
  160. gl_FragColor = texture2D(tex, v_texCoord);
  161. }
  162. )glsl";
  163. }