ParametricGeometries.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. console.warn( "THREE.ParametricGeometries: As part of the transition to ES6 Modules, the files in 'examples/js' were deprecated in May 2020 (r117) and will be deleted in December 2020 (r124). You can find more information about developing using ES6 Modules in https://threejs.org/docs/#manual/en/introduction/Installation." );
  2. /**
  3. * Experimenting of primitive geometry creation using Surface Parametric equations
  4. */
  5. THREE.ParametricGeometries = {
  6. klein: function ( v, u, target ) {
  7. u *= Math.PI;
  8. v *= 2 * Math.PI;
  9. u = u * 2;
  10. var x, y, 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. 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. var x = u * width;
  24. var y = 0;
  25. var 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. var v = 2 * Math.PI * t;
  34. var x, y, z;
  35. var a = 2;
  36. x = Math.cos( v ) * ( a + u * Math.cos( v / 2 ) );
  37. y = Math.sin( v ) * ( a + u * Math.cos( v / 2 ) );
  38. z = u * Math.sin( v / 2 );
  39. target.set( x, y, z );
  40. },
  41. mobius3d: function ( u, t, target ) {
  42. // volumetric mobius strip
  43. u *= Math.PI;
  44. t *= 2 * Math.PI;
  45. u = u * 2;
  46. var phi = u / 2;
  47. var major = 2.25, a = 0.125, b = 0.65;
  48. var x, y, z;
  49. x = a * Math.cos( t ) * Math.cos( phi ) - b * Math.sin( t ) * Math.sin( phi );
  50. z = a * Math.cos( t ) * Math.sin( phi ) + b * Math.sin( t ) * Math.cos( phi );
  51. 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. THREE.ParametricGeometries.TubeGeometry = function ( path, segments, radius, segmentsRadius, closed ) {
  62. this.path = path;
  63. this.segments = segments || 64;
  64. this.radius = radius || 1;
  65. this.segmentsRadius = segmentsRadius || 8;
  66. this.closed = closed || false;
  67. var scope = this, numpoints = this.segments + 1;
  68. var frames = path.computeFrenetFrames( segments, closed ),
  69. tangents = frames.tangents,
  70. normals = frames.normals,
  71. binormals = frames.binormals;
  72. // proxy internals
  73. this.tangents = tangents;
  74. this.normals = normals;
  75. this.binormals = binormals;
  76. var position = new THREE.Vector3();
  77. var ParametricTube = function ( u, v, target ) {
  78. v *= 2 * Math.PI;
  79. var i = u * ( numpoints - 1 );
  80. i = Math.floor( i );
  81. path.getPointAt( u, position );
  82. var normal = normals[ i ];
  83. var binormal = binormals[ i ];
  84. var cx = - scope.radius * Math.cos( v ); // TODO: Hack: Negating it so it faces outside.
  85. var cy = scope.radius * Math.sin( v );
  86. position.x += cx * normal.x + cy * binormal.x;
  87. position.y += cx * normal.y + cy * binormal.y;
  88. position.z += cx * normal.z + cy * binormal.z;
  89. target.copy( position );
  90. };
  91. THREE.ParametricGeometry.call( this, ParametricTube, segments, segmentsRadius );
  92. };
  93. THREE.ParametricGeometries.TubeGeometry.prototype = Object.create( THREE.Geometry.prototype );
  94. THREE.ParametricGeometries.TubeGeometry.prototype.constructor = THREE.ParametricGeometries.TubeGeometry;
  95. /*********************************************
  96. *
  97. * Parametric Replacement for TorusKnotGeometry
  98. *
  99. *********************************************/
  100. THREE.ParametricGeometries.TorusKnotGeometry = function ( radius, tube, segmentsT, segmentsR, p, q ) {
  101. this.radius = radius || 200;
  102. this.tube = tube || 40;
  103. this.segmentsT = segmentsT || 64;
  104. this.segmentsR = segmentsR || 8;
  105. this.p = p || 2;
  106. this.q = q || 3;
  107. function TorusKnotCurve() {
  108. THREE.Curve.call( this );
  109. }
  110. TorusKnotCurve.prototype = Object.create( THREE.Curve.prototype );
  111. TorusKnotCurve.prototype.constructor = TorusKnotCurve;
  112. TorusKnotCurve.prototype.getPoint = function ( t, optionalTarget ) {
  113. var point = optionalTarget || new THREE.Vector3();
  114. t *= Math.PI * 2;
  115. var r = 0.5;
  116. var x = ( 1 + r * Math.cos( q * t ) ) * Math.cos( p * t );
  117. var y = ( 1 + r * Math.cos( q * t ) ) * Math.sin( p * t );
  118. var z = r * Math.sin( q * t );
  119. return point.set( x, y, z ).multiplyScalar( radius );
  120. };
  121. var segments = segmentsT;
  122. var radiusSegments = segmentsR;
  123. var extrudePath = new TorusKnotCurve();
  124. THREE.ParametricGeometries.TubeGeometry.call( this, extrudePath, segments, tube, radiusSegments, true, false );
  125. };
  126. THREE.ParametricGeometries.TorusKnotGeometry.prototype = Object.create( THREE.Geometry.prototype );
  127. THREE.ParametricGeometries.TorusKnotGeometry.prototype.constructor = THREE.ParametricGeometries.TorusKnotGeometry;
  128. /*********************************************
  129. *
  130. * Parametric Replacement for SphereGeometry
  131. *
  132. *********************************************/
  133. THREE.ParametricGeometries.SphereGeometry = function ( size, u, v ) {
  134. function sphere( u, v, target ) {
  135. u *= Math.PI;
  136. v *= 2 * Math.PI;
  137. var x = size * Math.sin( u ) * Math.cos( v );
  138. var y = size * Math.sin( u ) * Math.sin( v );
  139. var z = size * Math.cos( u );
  140. target.set( x, y, z );
  141. }
  142. THREE.ParametricGeometry.call( this, sphere, u, v );
  143. };
  144. THREE.ParametricGeometries.SphereGeometry.prototype = Object.create( THREE.Geometry.prototype );
  145. THREE.ParametricGeometries.SphereGeometry.prototype.constructor = THREE.ParametricGeometries.SphereGeometry;
  146. /*********************************************
  147. *
  148. * Parametric Replacement for PlaneGeometry
  149. *
  150. *********************************************/
  151. THREE.ParametricGeometries.PlaneGeometry = function ( width, depth, segmentsWidth, segmentsDepth ) {
  152. function plane( u, v, target ) {
  153. var x = u * width;
  154. var y = 0;
  155. var z = v * depth;
  156. target.set( x, y, z );
  157. }
  158. THREE.ParametricGeometry.call( this, plane, segmentsWidth, segmentsDepth );
  159. };
  160. THREE.ParametricGeometries.PlaneGeometry.prototype = Object.create( THREE.Geometry.prototype );
  161. THREE.ParametricGeometries.PlaneGeometry.prototype.constructor = THREE.ParametricGeometries.PlaneGeometry;