main.vert 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #version 300 es
  2. precision mediump float;
  3. uniform float time;
  4. uniform vec2 resolution;
  5. layout(location = 0) in vec4 vertex_position;
  6. layout(location = 1) in vec2 vertex_uv;
  7. layout(location = 2) in vec4 vertex_normal;
  8. out vec2 uv;
  9. out vec4 vertex;
  10. out vec4 normal;
  11. mat4 mat4_translate(vec3 dir)
  12. {
  13. mat4 result = mat4(1.0);
  14. result[3] = vec4(dir, 1.0);
  15. return result;
  16. }
  17. mat4 mat4_scale(vec3 s)
  18. {
  19. mat4 result = mat4(1.0);
  20. result[0][0] = s.x;
  21. result[1][1] = s.y;
  22. result[2][2] = s.z;
  23. return result;
  24. }
  25. mat4 mat4_rotate_y(float angle)
  26. {
  27. mat4 result = mat4(1.0);
  28. result[0][0] = cos(angle);
  29. result[2][0] = sin(angle);
  30. result[0][2] = -sin(angle);
  31. result[2][2] = cos(angle);
  32. return result;
  33. }
  34. mat4 mat4_rotate_z(float angle)
  35. {
  36. mat4 result = mat4(1.0);
  37. result[0][0] = cos(angle);
  38. result[0][1] = sin(angle);
  39. result[1][0] = -sin(angle);
  40. result[1][1] = cos(angle);
  41. return result;
  42. }
  43. mat4 mat4_perspective(float fovy, float aspect, float near, float far)
  44. {
  45. float tan_half_fovy = tan(fovy * 0.5);
  46. mat4 result = mat4(0.0);
  47. result[0][0] = 1.0 / (aspect * tan_half_fovy);
  48. result[1][1] = 1.0 / tan_half_fovy;
  49. result[2][2] = far / (near - far);
  50. result[3][2] = -1.0;
  51. result[2][3] = -(far * near) / (far - near);
  52. return result;
  53. }
  54. void main(void)
  55. {
  56. float aspect = resolution.x / resolution.y;
  57. float fovy = radians(90.0);// + sin(time);
  58. mat4 camera = (
  59. mat4_translate(vec3(0.0, 0.0, -30.0 + 30.0 * sin(time))) *
  60. mat4_rotate_z(time) *
  61. mat4_rotate_y(time) *
  62. mat4_translate(vertex_normal.xyz * 20.0 * ((sin(time) + 1.0) / 2.0)) *
  63. mat4_scale(vec3(25.0, 25.0, 25.0)) *
  64. mat4_translate(vec3(-0.5, -0.5, -0.5)) *
  65. mat4(1.0)
  66. );
  67. vec4 camera_pos = (
  68. camera *
  69. vertex_position
  70. );
  71. gl_Position = (
  72. mat4_perspective(fovy, aspect, 1.0, 500.0) *
  73. camera_pos
  74. );
  75. uv = vertex_uv;
  76. vertex = camera_pos;
  77. normal = (
  78. mat4_rotate_z(time) *
  79. mat4_rotate_y(time) *
  80. vertex_normal
  81. );
  82. }