ParametricFunctions.js 2.8 KB

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