spheres.vert.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * Copyright (c) 2019-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. export const spheres_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. uniform mat4 uInvProjection;
  17. attribute vec3 aPosition;
  18. attribute vec2 aMapping;
  19. attribute mat4 aTransform;
  20. attribute float aInstance;
  21. attribute float aGroup;
  22. varying float vRadius;
  23. varying float vRadiusSq;
  24. varying vec3 vPoint;
  25. varying vec3 vPointViewPosition;
  26. #include matrix_scale
  27. const mat4 D = mat4(
  28. 1.0, 0.0, 0.0, 0.0,
  29. 0.0, 1.0, 0.0, 0.0,
  30. 0.0, 0.0, 1.0, 0.0,
  31. 0.0, 0.0, 0.0, -1.0
  32. );
  33. /**
  34. * Compute point size and center using the technique described in:
  35. * "GPU-Based Ray-Casting of Quadratic Surfaces" http://dl.acm.org/citation.cfm?id=2386396
  36. * by Christian Sigg, Tim Weyrich, Mario Botsch, Markus Gross.
  37. */
  38. void quadraticProjection(const in float radius, const in vec3 position){
  39. vec2 xbc, ybc;
  40. mat4 T = mat4(
  41. radius, 0.0, 0.0, 0.0,
  42. 0.0, radius, 0.0, 0.0,
  43. 0.0, 0.0, radius, 0.0,
  44. position.x, position.y, position.z, 1.0
  45. );
  46. mat4 R = transpose4(uProjection * uModelView * aTransform * T);
  47. float A = dot(R[3], D * R[3]);
  48. float B = -2.0 * dot(R[0], D * R[3]);
  49. float C = dot(R[0], D * R[0]);
  50. xbc[0] = (-B - sqrt(B * B - 4.0 * A * C)) / (2.0 * A);
  51. xbc[1] = (-B + sqrt(B * B - 4.0 * A * C)) / (2.0 * A);
  52. float sx = abs(xbc[0] - xbc[1]) * 0.5;
  53. A = dot(R[3], D * R[3]);
  54. B = -2.0 * dot(R[1], D * R[3]);
  55. C = dot(R[1], D * R[1]);
  56. ybc[0] = (-B - sqrt(B * B - 4.0 * A * C)) / (2.0 * A);
  57. ybc[1] = (-B + sqrt(B * B - 4.0 * A * C)) / (2.0 * A);
  58. float sy = abs(ybc[0] - ybc[1]) * 0.5;
  59. gl_Position.xy = vec2(0.5 * (xbc.x + xbc.y), 0.5 * (ybc.x + ybc.y));
  60. gl_Position.xy -= aMapping * vec2(sx, sy);
  61. gl_Position.xy *= gl_Position.w;
  62. }
  63. void main(void){
  64. #include assign_group
  65. #include assign_color_varying
  66. #include assign_marker_varying
  67. #include assign_clipping_varying
  68. #include assign_size
  69. vRadius = size * matrixScale(uModelView);
  70. vec4 position4 = vec4(aPosition, 1.0);
  71. vec4 mvPosition = uModelView * aTransform * position4;
  72. mvPosition.z -= vRadius; // avoid clipping, added again in fragment shader
  73. gl_Position = uProjection * vec4(mvPosition.xyz, 1.0);
  74. quadraticProjection(size, aPosition);
  75. vRadiusSq = vRadius * vRadius;
  76. vec4 vPoint4 = uInvProjection * gl_Position;
  77. vPoint = vPoint4.xyz / vPoint4.w;
  78. vPointViewPosition = -mvPosition.xyz / mvPosition.w;
  79. vModelPosition = (uModel * aTransform * position4).xyz; // for clipping in frag shader
  80. #include clip_instance
  81. }
  82. `;