A little toolkit for single-quad fragment shader demos
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.

131 lines
2.8 KiB

  1. #version 330 core
  2. #define FOV 24
  3. #define EPSILON 0.01
  4. #define MAX_STEPS 64
  5. #define NEAR_D 0.
  6. #define FAR_D 120.
  7. #define GRAD_EPSILON 0.0001
  8. #define saturate(x) clamp(x, 0, 1)
  9. uniform vec2 u_Resolution;
  10. uniform vec4 u_Mouse;
  11. uniform float u_Time;
  12. out vec4 color;
  13. #include "hg_sdf.glsl"
  14. #include "utils.glsl"
  15. #include "march_prolog.glsl"
  16. #include "colormap_cool.glsl"
  17. #define MAT_NORMALS 1.
  18. vec3 ray_dir(float fov, vec2 uv) {
  19. float z = 1./tan(radians(fov)/2.);
  20. return normalize(vec3(uv, z));
  21. }
  22. SceneResult scene_f(vec3 p) {
  23. SceneResult ball = SceneResult(length(p-vec3(0, 2, 0)) - 2, MAT_NORMALS);
  24. SceneResult plane = SceneResult(p.y, 2.);
  25. // SceneResult box = SceneResult(
  26. // fOpUnionRound(fBox(p, vec3(1)), fSphere(p-vec3(.8), 1.), .2), 0.);
  27. //SceneResult res = SceneResult(
  28. // mix(fBox(p, vec3(1.)), fSphere(p, 1.),
  29. // pow(sin(.5*TAU*u_Time), 0.8)),
  30. // 1.);
  31. // SceneResult res = SceneResult(-p.z, 1.);
  32. // SceneResult res = SceneResult(
  33. // fBox(p, vec3(1)),
  34. // 1.);
  35. SceneResult res = min_sr(ball, plane);
  36. return res;
  37. }
  38. vec3 estimate_scene_normal(vec3 p) {
  39. vec3 dx = vec3(GRAD_EPSILON, 0, 0);
  40. vec3 dy = vec3(0, GRAD_EPSILON, 0);
  41. vec3 dz = vec3(0, 0, GRAD_EPSILON);
  42. return normalize(vec3(
  43. scene_f(p + dx).d - scene_f(p - dx).d,
  44. scene_f(p + dy).d - scene_f(p - dy).d,
  45. scene_f(p + dz).d - scene_f(p - dz).d
  46. ));
  47. }
  48. vec3 raymarch(vec3 o, vec3 d, float start, float end) {
  49. float t = start;
  50. for (int i = 0; i < MAX_STEPS; i++) {
  51. SceneResult sr = scene_f(o + d*t);
  52. if (sr.d < EPSILON || t > end)
  53. return vec3(t, sr.mat_idx, i);
  54. t += sr.d;
  55. }
  56. return vec3(end, N_HIT_FAR_PLANE, MAX_STEPS);
  57. }
  58. vec4 shade_material(vec3 p, vec3 norm, float mat_idx) {
  59. if (mat_idx <= 0.0) {
  60. return vec4(0);
  61. }
  62. if (mat_idx <= 1.0) {
  63. // return colormap((mod(p.x, 3.) + 1)/2.5);
  64. return vec4(norm, 1.0);
  65. }
  66. if (mat_idx <= 2.0) {
  67. return vec4(sign(mod(floor(p.x) + floor(p.z), 2.0)));
  68. }
  69. //return
  70. }
  71. vec4 shade(vec3 p, float mat_idx) {
  72. vec3 norm = estimate_scene_normal(p);
  73. vec4 color_mat = shade_material(p, norm, mat_idx);
  74. return color_mat;
  75. }
  76. void main() {
  77. vec2 mouse_uv = u_Mouse.xy * 2.0 / u_Resolution.xy - 1.0;
  78. vec2 uv = gl_FragCoord.xy * 2.0 / u_Resolution.xy - 1.0;
  79. uv.x *= u_Resolution.x/u_Resolution.y;
  80. float an = 0.3 * u_Time;
  81. float d = 6.;
  82. vec3 eye = vec3(3. * sin(an), 4., 3. * cos(an)) * d;
  83. vec3 target = vec3(0., 0., 0.);
  84. mat3 lookAt = look_mat(eye, target, 0);
  85. vec3 dir = normalize(lookAt * ray_dir(FOV, uv));
  86. color = vec4(0.);
  87. vec3 result = raymarch(eye, dir, NEAR_D, FAR_D);
  88. float depth = result.x;
  89. float mat_idx = result.y;
  90. float iters = result.z;
  91. if (depth >= FAR_D) {
  92. color = vec4(.1, .3, .9, 1.);
  93. return;
  94. }
  95. //color = colormap(iters/MAX_STEPS+0.5);
  96. vec3 p = eye + dir * depth; // recast the ray
  97. color = shade(p, mat_idx);
  98. // gamma
  99. color = vec4(pow(clamp(color.xyz, 0.0, 1.0), vec3(0.4545)), 1.0);
  100. }