camera_distance.shader 2.3 KB

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