get_xyz_to_uv.glsl 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. vec2 xs_on_clean_parabola(vec3 b0, vec3 b1, vec3 b2){
  2. /*
  3. Given three control points for a quadratic bezier,
  4. this returns the two values (x0, x2) such that the
  5. section of the parabola y = x^2 between those values
  6. is isometric to the given quadratic bezier.
  7. Adapated from https://raphlinus.github.io/graphics/curves/2019/12/23/flatten-quadbez.html
  8. */
  9. vec3 dd = 2 * b1 - b0 - b2;
  10. float u0 = dot(b1 - b0, dd);
  11. float u2 = dot(b2 - b1, dd);
  12. float cp = length(cross(b2 - b0, dd));
  13. return vec2(u0 / cp, u2 / cp);
  14. }
  15. mat4 map_triangles(vec3 src0, vec3 src1, vec3 src2, vec3 dst0, vec3 dst1, vec3 dst2){
  16. /*
  17. Return an affine transform which maps the triangle (src0, src1, src2)
  18. onto the triangle (dst0, dst1, dst2)
  19. */
  20. mat4 src_mat = mat4(
  21. src0, 1.0,
  22. src1, 1.0,
  23. src2, 1.0,
  24. vec4(1.0)
  25. );
  26. mat4 dst_mat = mat4(
  27. dst0, 1.0,
  28. dst1, 1.0,
  29. dst2, 1.0,
  30. vec4(1.0)
  31. );
  32. return dst_mat * inverse(src_mat);
  33. }
  34. mat4 rotation(vec3 axis, float cos_angle){
  35. float c = cos_angle;
  36. float s = sqrt(1 - c * c); // Sine of the angle
  37. float oc = 1.0 - c;
  38. float ax = axis.x;
  39. float ay = axis.y;
  40. float az = axis.z;
  41. return mat4(
  42. oc * ax * ax + c, oc * ax * ay + az * s, oc * az * ax - ay * s, 0.0,
  43. oc * ax * ay - az * s, oc * ay * ay + c, oc * ay * az + ax * s, 0.0,
  44. oc * az * ax + ay * s, oc * ay * az - ax * s, oc * az * az + c, 0.0,
  45. 0.0, 0.0, 0.0, 1.0
  46. );
  47. }
  48. mat4 map_onto_x_axis(vec3 src0, vec3 src1){
  49. mat4 shift = mat4(1.0);
  50. shift[3].xyz = -src0;
  51. // Find rotation matrix between unit vectors in each direction
  52. vec3 vect = normalize(src1 - src0);
  53. // No rotation needed
  54. if(vect.x > 1 - 1e-6) return shift;
  55. // Equivalent to cross(vect, vec3(1, 0, 0))
  56. vec3 axis = normalize(vec3(0.0, vect.z, -vect.y));
  57. mat4 rotate = rotation(axis, vect.x);
  58. return rotate * shift;
  59. }
  60. mat4 get_xyz_to_uv(
  61. vec3 b0, vec3 b1, vec3 b2,
  62. float threshold,
  63. out bool exceeds_threshold
  64. ){
  65. /*
  66. Populates the matrix `result` with an affine transformation which maps a set of
  67. quadratic bezier controls points into a new coordinate system such that the bezier
  68. curve coincides with y = x^2.
  69. If the x-range under this part of the curve exceeds `threshold`, this returns false
  70. and populates result a matrix mapping b0 and b2 onto the x-axis
  71. */
  72. vec2 xs = xs_on_clean_parabola(b0, b1, b2);
  73. float x0 = xs[0];
  74. float x1 = 0.5 * (xs[0] + xs[1]);
  75. float x2 = xs[1];
  76. // Portions of the parabola y = x^2 where abs(x) exceeds
  77. // this value are treated as straight lines.
  78. exceeds_threshold = (min(x0, x2) > threshold || max(x0, x2) < -threshold);
  79. if(exceeds_threshold){
  80. return map_onto_x_axis(b0, b2);
  81. }
  82. // This triangle on the xy plane should be isometric
  83. // to (b0, b1, b2), and it should define a quadratic
  84. // bezier segment aligned with y = x^2
  85. vec3 dst0 = vec3(x0, x0 * x0, 0.0);
  86. vec3 dst1 = vec3(x1, x0 * x2, 0.0);
  87. vec3 dst2 = vec3(x2, x2 * x2, 0.0);
  88. return map_triangles(b0, b1, b2, dst0, dst1, dst2);
  89. }