cylinders.vert.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /**
  2. * Copyright (c) 2020-2022 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. export const cylinders_vert = `
  7. precision highp float;
  8. precision highp int;
  9. #include common
  10. #include read_from_texture
  11. #include common_vert_params
  12. #include color_vert_params
  13. #include size_vert_params
  14. #include common_clip
  15. uniform mat4 uModelView;
  16. attribute mat4 aTransform;
  17. attribute float aInstance;
  18. attribute float aGroup;
  19. attribute vec3 aMapping;
  20. attribute vec3 aStart;
  21. attribute vec3 aEnd;
  22. attribute float aScale;
  23. attribute float aCap;
  24. varying mat4 vTransform;
  25. varying vec3 vStart;
  26. varying vec3 vEnd;
  27. varying float vSize;
  28. varying float vCap;
  29. uniform float uIsOrtho;
  30. uniform vec3 uCameraDir;
  31. void main() {
  32. #include assign_group
  33. #include assign_color_varying
  34. #include assign_marker_varying
  35. #include assign_clipping_varying
  36. #include assign_size
  37. mat4 modelTransform = uModel * aTransform;
  38. vTransform = aTransform;
  39. vStart = (modelTransform * vec4(aStart, 1.0)).xyz;
  40. vEnd = (modelTransform * vec4(aEnd, 1.0)).xyz;
  41. vSize = size * aScale;
  42. vCap = aCap;
  43. vModelPosition = (vStart + vEnd) * 0.5;
  44. vec3 camDir = -mix(normalize(vModelPosition - uCameraPosition), uCameraDir, uIsOrtho);
  45. vec3 dir = vEnd - vStart;
  46. // ensure cylinder 'dir' is pointing towards the camera
  47. if(dot(camDir, dir) < 0.0) {
  48. dir = -dir;
  49. }
  50. vec3 left = cross(camDir, dir);
  51. vec3 up = cross(left, dir);
  52. left = vSize * normalize(left);
  53. up = vSize * normalize(up);
  54. // move vertex in object-space from center to corner
  55. vModelPosition += aMapping.x * dir + aMapping.y * left + aMapping.z * up;
  56. vec4 mvPosition = uView * vec4(vModelPosition, 1.0);
  57. vViewPosition = mvPosition.xyz;
  58. gl_Position = uProjection * mvPosition;
  59. mvPosition.z -= 2.0 * (length(vEnd - vStart) + vSize); // avoid clipping
  60. gl_Position.z = (uProjection * mvPosition).z;
  61. #include clip_instance
  62. }
  63. `;