ParametricGeometries.js 6.4 KB

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