line.glslv 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #version GLSL_VERSION
  2. /*==============================================================================
  3. INCLUDES
  4. ==============================================================================*/
  5. #include <std.glsl>
  6. #include <math.glslv>
  7. #include <to_world.glslv>
  8. /*==============================================================================
  9. SHADER INTERFACE
  10. ==============================================================================*/
  11. GLSL_IN vec3 a_position;
  12. GLSL_IN vec3 a_direction;
  13. //------------------------------------------------------------------------------
  14. /*==============================================================================
  15. UNIFORMS
  16. ==============================================================================*/
  17. uniform mat3 u_model_tsr;
  18. uniform mat3 u_view_tsr;
  19. uniform mat4 u_proj_matrix;
  20. uniform float u_height;
  21. uniform float u_line_width;
  22. /*==============================================================================
  23. MAIN
  24. ==============================================================================*/
  25. void main() {
  26. mat3 view_tsr = u_view_tsr;
  27. mat3 model_tsr = u_model_tsr;
  28. vertex world = to_world(a_position, vec3(0.0), vec3(0.0), vec3(0.0),
  29. vec3(0.0), normalize(a_direction), model_tsr);
  30. vec4 pos_cam = u_proj_matrix * vec4(tsr9_transform(view_tsr, world.position), 1.0);
  31. pos_cam.xyz /= pos_cam.w;
  32. vec4 pos_side_cam = u_proj_matrix * vec4(tsr9_transform(view_tsr,
  33. world.position + world.normal), 1.0);
  34. pos_side_cam.xyz /= pos_side_cam.w;
  35. float aspect = u_proj_matrix[1][1] / u_proj_matrix[0][0];
  36. vec2 dir_cam = (pos_side_cam - pos_cam).xy;
  37. dir_cam.x *= aspect;
  38. float angle = M_PI / 2.0;
  39. mat2 rot_z = mat2(cos(angle), sin(angle), -sin(angle), cos(angle));
  40. dir_cam = rot_z * dir_cam;
  41. dir_cam = normalize(dir_cam);
  42. // width - inverse aspect
  43. vec2 line_width = (u_line_width / u_height) * vec2(1.0 / aspect, 1.0);
  44. gl_Position = vec4(pos_cam.xy + dir_cam.xy * line_width / 2.0, pos_cam.z, 1.0);
  45. gl_Position *= pos_cam.w;
  46. }