2
0

ParametricGeometries.js 6.0 KB

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