2
0

defaultShader.vert 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. #version 430 core
  2. layout(location = 0) in int in_faceOrientation; //up down left etc
  3. layout(location = 1) in int in_textureIndex; //dirt grass stone etc
  4. layout(location = 2) in ivec3 in_facePosition; // int x y z
  5. //y component:
  6. //0x FF FF FF FF
  7. // -flags----light----position--
  8. ivec3 facePosition;
  9. uniform mat4 u_viewProjection;
  10. uniform ivec3 u_positionInt;
  11. uniform vec3 u_positionFloat;
  12. uniform float u_time;
  13. uniform int u_skyLightIntensity;
  14. uniform mat4 u_lightSpaceMatrix;
  15. uniform ivec3 u_lightPos;
  16. out flat ivec3 v_blockPos;
  17. //todo remove? or stop using after a size
  18. float vertexColor[] = float[](
  19. //front
  20. 0.95,
  21. //back
  22. 0.85,
  23. //top
  24. 1.0,
  25. //bottom
  26. 0.8,
  27. //left
  28. 0.85,
  29. //right
  30. 0.95,
  31. //grass
  32. 0.95,
  33. 0.95,
  34. 0.95,
  35. 0.95,
  36. // leaves
  37. //front
  38. 0.95,
  39. //back
  40. 0.85,
  41. //top
  42. 1.0,
  43. //bottom
  44. 0.8,
  45. //left
  46. 0.85,
  47. //right
  48. 0.95
  49. );
  50. //geometry
  51. readonly restrict layout(std430) buffer u_vertexData
  52. {
  53. float vertexData[];
  54. };
  55. readonly restrict layout(std430) buffer u_vertexUV
  56. {
  57. float vertexUV[];
  58. };
  59. //todo change and send directly the texture's id
  60. readonly restrict layout(std430) buffer u_textureSamplerers
  61. {
  62. uvec2 textureSamplerers[];
  63. };
  64. out vec2 v_uv;
  65. out float v_ambient;
  66. out float v_ambientMultiplier; //for face darkening or under water darkening
  67. out flat int v_ambientInt;
  68. out flat uvec2 v_textureSampler;
  69. out flat uvec2 v_normalSampler;
  70. out flat uvec2 v_materialSampler;
  71. out flat uvec2 v_paralaxSampler;
  72. //in world space
  73. out flat ivec3 fragmentPositionI;
  74. out vec3 fragmentPositionF;
  75. out flat int v_skyLight;
  76. out flat int v_normalLight;
  77. out flat int v_skyLightUnchanged;
  78. out flat vec3 v_normal;
  79. //in view space
  80. out vec4 v_fragPos;
  81. out vec4 v_fragPosLightSpace;
  82. out flat int v_flags;
  83. out flat int v_colors;
  84. vec2 calculateUVs(int vertexId)
  85. {
  86. vec2 uvs;
  87. uvs.x = vertexUV[(in_faceOrientation) * 2 * 4 + vertexId * 2 + 0];
  88. uvs.y = vertexUV[(in_faceOrientation) * 2 * 4 + vertexId * 2 + 1];
  89. return uvs;
  90. }
  91. uniform float u_timeGrass;
  92. uint grassMask[] =
  93. {
  94. //grass
  95. 1,
  96. 1,
  97. 0,
  98. 0,
  99. 0,
  100. 1,
  101. 1,
  102. 0,
  103. 1,
  104. 1,
  105. 0,
  106. 0,
  107. 0,
  108. 0,
  109. 1,
  110. 1,
  111. };
  112. int waterMask[] =
  113. {
  114. 1,1,0,0, 0,1,1,0, 1,1,1,1, 0,0,0,0, 0,1,1,0, 1,1,0,0,
  115. //1,1,0,0, 0,1,1,0, 0,1,1,0, 1,1,0,0,
  116. // 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
  117. //1,1,-1,-1, -1,1,1,-1, -1,1,1,-1, 1,1,-1,-1,
  118. // 0,0,-1,-1, -1,0,0,-1, -1,0,0,-1, 0,0,-1,-1,
  119. 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1,
  120. 0,0,1,1, 1,0,0,1, 1,0,0,1, 0,0,1,1,
  121. };
  122. float biasUp(float a)
  123. {
  124. return (a + 0.75)/1.75;
  125. }
  126. float biasUp2(float a)
  127. {
  128. return ((a + 1)/2.f)/2.0 + 0.5;
  129. }
  130. vec3 calculateVertexPos(int vertexId)
  131. {
  132. vec3 pos = vec3(0);
  133. vec3 vertexShape = vec3(0);
  134. if(in_faceOrientation >= 22 && in_faceOrientation < 36) //water
  135. {
  136. int mask = waterMask[(in_faceOrientation-22)*4+vertexId];
  137. vertexShape.x += vertexData[(in_faceOrientation) * 3 * 4 + vertexId * 3 + 0];
  138. vertexShape.y += vertexData[(in_faceOrientation) * 3 * 4 + vertexId * 3 + 1];
  139. vertexShape.z += vertexData[(in_faceOrientation) * 3 * 4 + vertexId * 3 + 2];
  140. float SPEED = 3.1f;
  141. float FREQUENCY = 0.5f;
  142. float AMPLITUDE = 0.034f;
  143. float SPEED2 = 1.92f;
  144. float FREQUENCY2 = 0.04f;
  145. float AMPLITUDE2 = 0.008f;
  146. float offset = biasUp(cos((facePosition.x + vertexShape.x + vertexShape.z +
  147. facePosition.z - u_timeGrass * SPEED) * FREQUENCY)) * AMPLITUDE;
  148. float offset2 = (sin((1 + facePosition.x + vertexShape.x + (vertexShape.z +
  149. facePosition.z) * 2 - u_timeGrass * SPEED) * FREQUENCY)) * AMPLITUDE;
  150. vertexShape.y += mask * (offset + offset2 - 0.08);
  151. }else
  152. if(in_faceOrientation >= 10 && in_faceOrientation < 16) //animated trees
  153. {
  154. vertexShape.x += vertexData[(in_faceOrientation-10) * 3 * 4 + vertexId * 3 + 0];
  155. vertexShape.y += vertexData[(in_faceOrientation-10) * 3 * 4 + vertexId * 3 + 1];
  156. vertexShape.z += vertexData[(in_faceOrientation-10) * 3 * 4 + vertexId * 3 + 2];
  157. vec3 offset = vec3(0);
  158. offset.y = 0.01*sin((u_timeGrass/0.2 )+vertexShape.x+facePosition.x);
  159. offset.y += 0.04*sin((u_timeGrass/1.0 )+vertexShape.z+facePosition.z);
  160. offset.xz = 0.05*sin((u_timeGrass/2.0 )+vertexShape.xz+facePosition.xz);
  161. offset.y += 0.010*cos((u_timeGrass/1.0 )+vertexShape.x+facePosition.x);
  162. offset.y += 0.02*cos((u_timeGrass/2.0 )+vertexShape.z+facePosition.z);
  163. offset.xz += 0.02*cos((u_timeGrass/3.0 )+vertexShape.xz+facePosition.xz);
  164. vertexShape += offset;
  165. }else if(in_faceOrientation >= 6 && in_faceOrientation < 16)
  166. {
  167. uint mask = grassMask[(in_faceOrientation-6)*4+vertexId];
  168. //grass
  169. vec2 dir = normalize(vec2(0.5,1));
  170. float SPEED = 2.f;
  171. float FREQUENCY = 2.5f;
  172. float AMPLITUDE = 0.15f;
  173. float SPEED2 = 1.f;
  174. float FREQUENCY2 = 2.0f;
  175. float AMPLITUDE2 = 0.9f;
  176. vec2 dir2 = normalize(vec2(0.9,1));
  177. float offset = biasUp(cos((facePosition.x * dir.x +
  178. facePosition.z * dir.y - u_timeGrass * SPEED) * FREQUENCY)) * AMPLITUDE;
  179. float offset2 = biasUp2(sin((facePosition.x * dir2.x +
  180. facePosition.z * dir2.y - u_timeGrass * SPEED) * FREQUENCY)) * AMPLITUDE;
  181. vertexShape.x = vertexData[in_faceOrientation * 3 * 4 + vertexId * 3 + 0];
  182. vertexShape.y = vertexData[in_faceOrientation * 3 * 4 + vertexId * 3 + 1];
  183. vertexShape.z = vertexData[in_faceOrientation * 3 * 4 + vertexId * 3 + 2];
  184. vertexShape.x += mask * (offset * dir.x + offset2 * dir.x);
  185. vertexShape.z += mask * (offset * dir.y + offset2 * dir.y);
  186. }else
  187. {
  188. //todo optimize move up
  189. vertexShape.x = vertexData[in_faceOrientation * 3 * 4 + vertexId * 3 + 0];
  190. vertexShape.y = vertexData[in_faceOrientation * 3 * 4 + vertexId * 3 + 1];
  191. vertexShape.z = vertexData[in_faceOrientation * 3 * 4 + vertexId * 3 + 2];
  192. }
  193. pos.xyz += vertexShape;
  194. return pos;
  195. }
  196. out vec3 v_semiViewSpacePos;
  197. vec3 normals[10] =
  198. {vec3(0,0,1),vec3(0,0,-1),vec3(0,1,0),vec3(0,-1,0),vec3(-1,0,0),vec3(1,0,0),
  199. vec3(0,1,0),vec3(0,1,0),vec3(0,1,0),vec3(0,1,0) //grass
  200. };
  201. out flat int v_isSkyLightMain;
  202. void main()
  203. {
  204. facePosition = in_facePosition;
  205. facePosition.y &= 0x0000FFFF;
  206. int in_skyAndNormalLights = in_facePosition.y >> 16;
  207. in_skyAndNormalLights &= 0xFF;
  208. int in_flags = in_facePosition.y >> 24;
  209. in_flags &= 0xFF;
  210. bool isWater = ((in_flags & 1) != 0);
  211. v_skyLight = (in_skyAndNormalLights & 0xf0) >> 4;
  212. v_normalLight = (in_skyAndNormalLights & 0xf);
  213. v_skyLightUnchanged = v_skyLight;
  214. v_skyLight = max(v_skyLight - (15 - u_skyLightIntensity), 0);
  215. v_ambientInt = max(v_skyLight, v_normalLight);
  216. v_isSkyLightMain = 0;
  217. if(v_skyLight > v_normalLight)
  218. {
  219. v_isSkyLightMain = 1;
  220. }
  221. vec3 diffI = facePosition - u_positionInt;
  222. vec3 diffF = diffI - u_positionFloat;
  223. v_flags = in_flags;
  224. v_blockPos = facePosition;
  225. fragmentPositionF = calculateVertexPos(gl_VertexID);
  226. fragmentPositionI = facePosition;
  227. vec4 posView = vec4(fragmentPositionF + diffF,1);
  228. v_uv = calculateUVs(gl_VertexID);
  229. //todo don't do animations at far distances.
  230. //calculate normals
  231. if(in_faceOrientation < 6)
  232. {
  233. v_normal = normals[in_faceOrientation];
  234. }
  235. else
  236. if(in_faceOrientation >= 22 && in_faceOrientation < 28) //water
  237. {
  238. v_normal = normals[in_faceOrientation-22];
  239. }
  240. else
  241. {
  242. vec3 pos1 = vec3(0);
  243. vec3 pos2 = vec3(0);
  244. vec3 pos3 = vec3(0);
  245. pos1 = posView.xyz;
  246. pos2 = calculateVertexPos((gl_VertexID + 1)%4) + diffF;
  247. pos3 = calculateVertexPos((gl_VertexID + 2)%4) + diffF;
  248. vec3 a = (pos3-pos2);
  249. vec3 b = (pos1-pos2);
  250. v_normal = normalize(cross(a, b));
  251. }
  252. //apply curvature
  253. if(false)
  254. {
  255. float fragmentDist = length(posView.xyz);
  256. float curved = posView.y - 0.001f * fragmentDist * fragmentDist;
  257. posView.y = curved;
  258. }
  259. v_semiViewSpacePos = posView.xyz;
  260. vec4 posProjection = u_viewProjection * posView;
  261. gl_Position = posProjection;
  262. v_fragPos = posProjection;
  263. v_fragPosLightSpace = u_lightSpaceMatrix * vec4((fragmentPositionI - u_lightPos) + fragmentPositionF, 1);
  264. //v_fragPosLightSpace.xyz -= u_lightPos;
  265. if(in_faceOrientation<16 && !isWater)
  266. {
  267. v_ambient = (vertexColor[in_faceOrientation] * (v_ambientInt/15.f));
  268. v_ambientMultiplier = vertexColor[in_faceOrientation];
  269. //v_ambient = (v_ambientInt/15.f);
  270. }else
  271. {
  272. v_ambient = (v_ambientInt/15.f);
  273. v_ambientMultiplier = 1.f;
  274. }
  275. //v_color = vertexColor[in_faceOrientation];
  276. //0b1111'1000'0000'0000
  277. //0b0000'0111'1111'1111
  278. v_colors = (in_textureIndex & 0xF800) >> 11;
  279. int textureIndex = in_textureIndex & 0x07FF;
  280. v_textureSampler = textureSamplerers[textureIndex];
  281. v_normalSampler = textureSamplerers[textureIndex+1];
  282. v_materialSampler = textureSamplerers[textureIndex+2];
  283. v_paralaxSampler = textureSamplerers[textureIndex+3];
  284. }