camera_distance.shader 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. uniform lowp vec4 u_color;
  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. gl_FragColor = u_color;
  27. }
  28. vertex41core =
  29. #version 410
  30. uniform highp mat4 u_modelMatrix;
  31. uniform highp mat4 u_viewProjectionMatrix;
  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_viewProjectionMatrix * world_space_vert;
  38. v_vertex = world_space_vert.xyz;
  39. }
  40. fragment41core =
  41. #version 410
  42. uniform highp vec3 u_viewPosition;
  43. uniform lowp vec4 u_color;
  44. in highp vec3 v_vertex;
  45. out vec4 frag_color;
  46. void main()
  47. {
  48. highp float distance_to_camera = distance(v_vertex, u_viewPosition) * 1000.; // distance in micron
  49. vec3 encoded; // encode float into 3 8-bit channels; this gives a precision of a micron at a range of up to ~16 meter
  50. encoded.r = floor(distance_to_camera / 65536.0);
  51. encoded.g = floor((distance_to_camera - encoded.r * 65536.0) / 256.0);
  52. encoded.b = floor(distance_to_camera - encoded.r * 65536.0 - encoded.g * 256.0);
  53. //frag_color.rgb = encoded / 255.;
  54. //frag_color.a = 1.0;
  55. frag_color = u_color;
  56. }
  57. [defaults]
  58. [bindings]
  59. u_modelMatrix = model_matrix
  60. u_viewProjectionMatrix = view_projection_matrix
  61. u_normalMatrix = normal_matrix
  62. u_viewPosition = view_position
  63. u_color = selection_color
  64. [attributes]
  65. a_vertex = vertex