camera_distance.shader 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. [shaders]
  2. vertex =
  3. uniform highp mat4 u_modelMatrix;
  4. uniform highp mat4 u_viewProjectionMatrix;
  5. attribute highp vec4 a_vertex;
  6. varying highp vec3 v_vertex;
  7. void main()
  8. {
  9. vec4 world_space_vert = u_modelMatrix * a_vertex;
  10. gl_Position = u_viewProjectionMatrix * world_space_vert;
  11. v_vertex = world_space_vert.xyz;
  12. }
  13. fragment =
  14. uniform highp vec3 u_viewPosition;
  15. varying highp vec3 v_vertex;
  16. void main()
  17. {
  18. highp float distance_to_camera = distance(v_vertex, u_viewPosition) * 1000.; // distance in micron
  19. vec3 encoded; // encode float into 3 8-bit channels; this gives a precision of a micron at a range of up to ~16 meter
  20. encoded.b = floor(distance_to_camera / 65536.0);
  21. encoded.g = floor((distance_to_camera - encoded.b * 65536.0) / 256.0);
  22. encoded.r = floor(distance_to_camera - encoded.b * 65536.0 - encoded.g * 256.0);
  23. gl_FragColor.rgb = encoded / 255.;
  24. gl_FragColor.a = 1.0;
  25. }
  26. vertex41core =
  27. #version 410
  28. uniform highp mat4 u_modelMatrix;
  29. uniform highp mat4 u_viewProjectionMatrix;
  30. in highp vec4 a_vertex;
  31. out highp vec3 v_vertex;
  32. void main()
  33. {
  34. vec4 world_space_vert = u_modelMatrix * a_vertex;
  35. gl_Position = u_viewProjectionMatrix * world_space_vert;
  36. v_vertex = world_space_vert.xyz;
  37. }
  38. fragment41core =
  39. #version 410
  40. uniform highp vec3 u_viewPosition;
  41. in highp vec3 v_vertex;
  42. out vec4 frag_color;
  43. void main()
  44. {
  45. highp float distance_to_camera = distance(v_vertex, u_viewPosition) * 1000.; // distance in micron
  46. vec3 encoded; // encode float into 3 8-bit channels; this gives a precision of a micron at a range of up to ~16 meter
  47. encoded.r = floor(distance_to_camera / 65536.0);
  48. encoded.g = floor((distance_to_camera - encoded.r * 65536.0) / 256.0);
  49. encoded.b = floor(distance_to_camera - encoded.r * 65536.0 - encoded.g * 256.0);
  50. frag_color.rgb = encoded / 255.;
  51. frag_color.a = 1.0;
  52. }
  53. [defaults]
  54. [bindings]
  55. u_modelMatrix = model_matrix
  56. u_viewProjectionMatrix = view_projection_matrix
  57. u_normalMatrix = normal_matrix
  58. u_viewPosition = view_position
  59. [attributes]
  60. a_vertex = vertex