ParametricGeometries.js 6.3 KB

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