ParametricGeometries.js 6.4 KB

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