ParametricGeometries.js 5.4 KB

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