cursor.vert.glsl 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #version 450
  2. uniform mat4 VP;
  3. uniform mat4 invVP;
  4. uniform vec2 mouse;
  5. uniform vec2 tex_step;
  6. uniform float radius;
  7. uniform vec3 camera_right;
  8. uniform sampler2D gbufferD;
  9. #ifdef HLSL
  10. in vec4 pos;
  11. in vec2 nor;
  12. #endif
  13. in vec2 tex;
  14. out vec2 tex_coord;
  15. vec3 get_pos(vec2 uv) {
  16. #ifdef HLSL
  17. float keep = pos.x + nor.x;
  18. #endif
  19. #ifdef GLSL
  20. float depth = textureLod(gbufferD, uv, 0.0).r;
  21. #else
  22. float depth = textureLod(gbufferD, vec2(uv.x, 1.0 - uv.y), 0.0).r;
  23. #endif
  24. vec4 wpos = vec4(uv * 2.0 - 1.0, depth * 2.0 - 1.0, 1.0);
  25. wpos = mul(wpos, invVP);
  26. return wpos.xyz / wpos.w;
  27. }
  28. vec3 get_normal(vec3 p0, vec2 uv) {
  29. vec3 p1 = get_pos(uv + vec2(tex_step.x * 4.0, 0.0));
  30. vec3 p2 = get_pos(uv + vec2(0.0, tex_step.y * 4.0));
  31. return normalize(cross(p2 - p0, p1 - p0));
  32. }
  33. void create_basis(vec3 normal, OUT(vec3, tangent), OUT(vec3, binormal)) {
  34. tangent = normalize(camera_right - normal * dot(camera_right, normal));
  35. binormal = cross(tangent, normal);
  36. }
  37. void main() {
  38. tex_coord = tex;
  39. vec3 wpos = get_pos(mouse);
  40. vec2 uv1 = mouse + tex_step * vec2(4.0, 4.0);
  41. vec2 uv2 = mouse - tex_step * vec2(4.0, 4.0);
  42. vec3 wpos1 = get_pos(uv1);
  43. vec3 wpos2 = get_pos(uv2);
  44. vec3 n = normalize(
  45. get_normal(wpos, mouse) +
  46. get_normal(wpos1, uv1) +
  47. get_normal(wpos2, uv2)
  48. );
  49. vec3 n_tan;
  50. vec3 n_bin;
  51. create_basis(n, n_tan, n_bin);
  52. if (gl_VertexID == 0) wpos += normalize(-n_tan - n_bin) * 0.7 * radius;
  53. else if (gl_VertexID == 1) wpos += normalize( n_tan - n_bin) * 0.7 * radius;
  54. else if (gl_VertexID == 2) wpos += normalize( n_tan + n_bin) * 0.7 * radius;
  55. else if (gl_VertexID == 3) wpos += normalize(-n_tan + n_bin) * 0.7 * radius;
  56. gl_Position = mul(vec4(wpos, 1.0), VP);
  57. }