ParametricFunctions.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * @module ParametricFunctions
  3. * @three_import import * as ParametricFunctions from 'three/addons/geometries/ParametricFunctions.js';
  4. */
  5. /**
  6. * A parametric function representing the Klein bottle.
  7. *
  8. * @param {number} v - The `v` coordinate on the surface in the range `[0,1]`.
  9. * @param {number} u - The `u` coordinate on the surface in the range `[0,1]`.
  10. * @param {Vector3} target - The target vector that is used to store the method's result.
  11. */
  12. function klein( v, u, target ) {
  13. u *= Math.PI;
  14. v *= 2 * Math.PI;
  15. u = u * 2;
  16. let x, z;
  17. if ( u < Math.PI ) {
  18. x = 3 * Math.cos( u ) * ( 1 + Math.sin( u ) ) + ( 2 * ( 1 - Math.cos( u ) / 2 ) ) * Math.cos( u ) * Math.cos( v );
  19. z = - 8 * Math.sin( u ) - 2 * ( 1 - Math.cos( u ) / 2 ) * Math.sin( u ) * Math.cos( v );
  20. } else {
  21. x = 3 * Math.cos( u ) * ( 1 + Math.sin( u ) ) + ( 2 * ( 1 - Math.cos( u ) / 2 ) ) * Math.cos( v + Math.PI );
  22. z = - 8 * Math.sin( u );
  23. }
  24. const y = - 2 * ( 1 - Math.cos( u ) / 2 ) * Math.sin( v );
  25. target.set( x, y, z );
  26. }
  27. /**
  28. * A parametric function representing a flat plane.
  29. *
  30. * @param {number} u - The `u` coordinate on the surface in the range `[0,1]`.
  31. * @param {number} v - The `v` coordinate on the surface in the range `[0,1]`.
  32. * @param {Vector3} target - The target vector that is used to store the method's result.
  33. */
  34. function plane( u, v, target ) {
  35. target.set( u, 0, v );
  36. }
  37. /**
  38. * A parametric function representing a flat mobius strip.
  39. *
  40. * @param {number} u - The `u` coordinate on the surface in the range `[0,1]`.
  41. * @param {number} t - The `v` coordinate on the surface in the range `[0,1]`.
  42. * @param {Vector3} target - The target vector that is used to store the method's result.
  43. */
  44. function mobius( u, t, target ) {
  45. // http://www.wolframalpha.com/input/?i=M%C3%B6bius+strip+parametric+equations&lk=1&a=ClashPrefs_*Surface.MoebiusStrip.SurfaceProperty.ParametricEquations-
  46. u = u - 0.5;
  47. const v = 2 * Math.PI * t;
  48. const a = 2;
  49. const x = Math.cos( v ) * ( a + u * Math.cos( v / 2 ) );
  50. const y = Math.sin( v ) * ( a + u * Math.cos( v / 2 ) );
  51. const z = u * Math.sin( v / 2 );
  52. target.set( x, y, z );
  53. }
  54. /**
  55. * A parametric function representing a volumetric mobius strip.
  56. *
  57. * @param {number} u - The `u` coordinate on the surface in the range `[0,1]`.
  58. * @param {number} t - The `v` coordinate on the surface in the range `[0,1]`.
  59. * @param {Vector3} target - The target vector that is used to store the method's result.
  60. */
  61. function mobius3d( u, t, target ) {
  62. u *= Math.PI;
  63. t *= 2 * Math.PI;
  64. u = u * 2;
  65. const phi = u / 2;
  66. const major = 2.25, a = 0.125, b = 0.65;
  67. let x = a * Math.cos( t ) * Math.cos( phi ) - b * Math.sin( t ) * Math.sin( phi );
  68. const z = a * Math.cos( t ) * Math.sin( phi ) + b * Math.sin( t ) * Math.cos( phi );
  69. const y = ( major + x ) * Math.sin( u );
  70. x = ( major + x ) * Math.cos( u );
  71. target.set( x, y, z );
  72. }
  73. export { klein, plane, mobius, mobius3d };