ParametricGeometries.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. ( function () {
  2. /**
  3. * Experimenting of primitive geometry creation using Surface Parametric equations
  4. */
  5. const ParametricGeometries = {
  6. klein: function ( v, u, target ) {
  7. u *= Math.PI;
  8. v *= 2 * Math.PI;
  9. u = u * 2;
  10. let x, z;
  11. if ( u < Math.PI ) {
  12. x = 3 * Math.cos( u ) * ( 1 + Math.sin( u ) ) + 2 * ( 1 - Math.cos( u ) / 2 ) * Math.cos( u ) * Math.cos( v );
  13. z = - 8 * Math.sin( u ) - 2 * ( 1 - Math.cos( u ) / 2 ) * Math.sin( u ) * Math.cos( v );
  14. } else {
  15. x = 3 * Math.cos( u ) * ( 1 + Math.sin( u ) ) + 2 * ( 1 - Math.cos( u ) / 2 ) * Math.cos( v + Math.PI );
  16. z = - 8 * Math.sin( u );
  17. }
  18. const y = - 2 * ( 1 - Math.cos( u ) / 2 ) * Math.sin( v );
  19. target.set( x, y, z );
  20. },
  21. plane: function ( width, height ) {
  22. return function ( u, v, target ) {
  23. const x = u * width;
  24. const y = 0;
  25. const z = v * height;
  26. target.set( x, y, z );
  27. };
  28. },
  29. mobius: function ( u, t, target ) {
  30. // flat mobius strip
  31. // http://www.wolframalpha.com/input/?i=M%C3%B6bius+strip+parametric+equations&lk=1&a=ClashPrefs_*Surface.MoebiusStrip.SurfaceProperty.ParametricEquations-
  32. u = u - 0.5;
  33. const v = 2 * Math.PI * t;
  34. const a = 2;
  35. const x = Math.cos( v ) * ( a + u * Math.cos( v / 2 ) );
  36. const y = Math.sin( v ) * ( a + u * Math.cos( v / 2 ) );
  37. const z = u * Math.sin( v / 2 );
  38. target.set( x, y, z );
  39. },
  40. mobius3d: function ( u, t, target ) {
  41. // volumetric mobius strip
  42. u *= Math.PI;
  43. t *= 2 * Math.PI;
  44. u = u * 2;
  45. const phi = u / 2;
  46. const major = 2.25,
  47. a = 0.125,
  48. b = 0.65;
  49. let x = a * Math.cos( t ) * Math.cos( phi ) - b * Math.sin( t ) * Math.sin( phi );
  50. const z = a * Math.cos( t ) * Math.sin( phi ) + b * Math.sin( t ) * Math.cos( phi );
  51. const y = ( major + x ) * Math.sin( u );
  52. x = ( major + x ) * Math.cos( u );
  53. target.set( x, y, z );
  54. }
  55. };
  56. /*********************************************
  57. *
  58. * Parametric Replacement for TubeGeometry
  59. *
  60. *********************************************/
  61. ParametricGeometries.TubeGeometry = class TubeGeometry extends THREE.ParametricGeometry {
  62. constructor( path, segments = 64, radius = 1, segmentsRadius = 8, closed = false ) {
  63. const numpoints = segments + 1;
  64. const frames = path.computeFrenetFrames( segments, closed ),
  65. tangents = frames.tangents,
  66. normals = frames.normals,
  67. binormals = frames.binormals;
  68. const position = new THREE.Vector3();
  69. function ParametricTube( u, v, target ) {
  70. v *= 2 * Math.PI;
  71. const i = Math.floor( u * ( numpoints - 1 ) );
  72. path.getPointAt( u, position );
  73. const normal = normals[ i ];
  74. const binormal = binormals[ i ];
  75. const cx = - radius * Math.cos( v ); // TODO: Hack: Negating it so it faces outside.
  76. const cy = radius * Math.sin( v );
  77. position.x += cx * normal.x + cy * binormal.x;
  78. position.y += cx * normal.y + cy * binormal.y;
  79. position.z += cx * normal.z + cy * binormal.z;
  80. target.copy( position );
  81. }
  82. super( ParametricTube, segments, segmentsRadius ); // proxy internals
  83. this.tangents = tangents;
  84. this.normals = normals;
  85. this.binormals = binormals;
  86. this.path = path;
  87. this.segments = segments;
  88. this.radius = radius;
  89. this.segmentsRadius = segmentsRadius;
  90. this.closed = closed;
  91. }
  92. };
  93. /*********************************************
  94. *
  95. * Parametric Replacement for TorusKnotGeometry
  96. *
  97. *********************************************/
  98. ParametricGeometries.TorusKnotGeometry = class TorusKnotGeometry extends ParametricGeometries.TubeGeometry {
  99. constructor( radius = 200, tube = 40, segmentsT = 64, segmentsR = 8, p = 2, q = 3 ) {
  100. class TorusKnotCurve extends THREE.Curve {
  101. getPoint( t, optionalTarget = new THREE.Vector3() ) {
  102. const point = optionalTarget;
  103. t *= Math.PI * 2;
  104. const r = 0.5;
  105. const x = ( 1 + r * Math.cos( q * t ) ) * Math.cos( p * t );
  106. const y = ( 1 + r * Math.cos( q * t ) ) * Math.sin( p * t );
  107. const z = r * Math.sin( q * t );
  108. return point.set( x, y, z ).multiplyScalar( radius );
  109. }
  110. }
  111. const segments = segmentsT;
  112. const radiusSegments = segmentsR;
  113. const extrudePath = new TorusKnotCurve();
  114. super( extrudePath, segments, tube, radiusSegments, true, false );
  115. this.radius = radius;
  116. this.tube = tube;
  117. this.segmentsT = segmentsT;
  118. this.segmentsR = segmentsR;
  119. this.p = p;
  120. this.q = q;
  121. }
  122. };
  123. /*********************************************
  124. *
  125. * Parametric Replacement for SphereGeometry
  126. *
  127. *********************************************/
  128. ParametricGeometries.SphereGeometry = class SphereGeometry extends THREE.ParametricGeometry {
  129. constructor( size, u, v ) {
  130. function sphere( u, v, target ) {
  131. u *= Math.PI;
  132. v *= 2 * Math.PI;
  133. var x = size * Math.sin( u ) * Math.cos( v );
  134. var y = size * Math.sin( u ) * Math.sin( v );
  135. var z = size * Math.cos( u );
  136. target.set( x, y, z );
  137. }
  138. super( sphere, u, v );
  139. }
  140. };
  141. /*********************************************
  142. *
  143. * Parametric Replacement for PlaneGeometry
  144. *
  145. *********************************************/
  146. ParametricGeometries.PlaneGeometry = class PlaneGeometry extends THREE.ParametricGeometry {
  147. constructor( width, depth, segmentsWidth, segmentsDepth ) {
  148. function plane( u, v, target ) {
  149. const x = u * width;
  150. const y = 0;
  151. const z = v * depth;
  152. target.set( x, y, z );
  153. }
  154. super( plane, segmentsWidth, segmentsDepth );
  155. }
  156. };
  157. THREE.ParametricGeometries = ParametricGeometries;
  158. } )();