ParametricGeometries.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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, optionalTarget ) {
  9. var result = optionalTarget || new THREE.Vector3();
  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. return result.set( x, y, z );
  23. },
  24. plane: function ( width, height ) {
  25. return function( u, v, optionalTarget ) {
  26. var result = optionalTarget || new THREE.Vector3();
  27. var x = u * width;
  28. var y = 0;
  29. var z = v * height;
  30. return result.set( x, y, z );
  31. };
  32. },
  33. mobius: function( u, t, optionalTarget ) {
  34. var result = optionalTarget || new THREE.Vector3();
  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. return result.set( x, y, z );
  45. },
  46. mobius3d: function( u, t, optionalTarget ) {
  47. var result = optionalTarget || new THREE.Vector3();
  48. // volumetric mobius strip
  49. u *= Math.PI;
  50. t *= 2 * Math.PI;
  51. u = u * 2;
  52. var phi = u / 2;
  53. var major = 2.25, a = 0.125, b = 0.65;
  54. var x, y, z;
  55. x = a * Math.cos( t ) * Math.cos( phi ) - b * Math.sin( t ) * Math.sin( phi );
  56. z = a * Math.cos( t ) * Math.sin( phi ) + b * Math.sin( t ) * Math.cos( phi );
  57. y = ( major + x ) * Math.sin( u );
  58. x = ( major + x ) * Math.cos( u );
  59. return result.set( x, y, z );
  60. }
  61. };
  62. /*********************************************
  63. *
  64. * Parametric Replacement for TubeGeometry
  65. *
  66. *********************************************/
  67. THREE.ParametricGeometries.TubeGeometry = function( path, segments, radius, segmentsRadius, closed, debug ) {
  68. this.path = path;
  69. this.segments = segments || 64;
  70. this.radius = radius || 1;
  71. this.segmentsRadius = segmentsRadius || 8;
  72. this.closed = closed || false;
  73. if ( debug ) this.debug = new THREE.Object3D();
  74. var scope = this,
  75. tangent, normal, binormal,
  76. numpoints = this.segments + 1,
  77. x, y, z, tx, ty, tz, u, v,
  78. cx, cy, pos,
  79. i, j, ip, jp, a, b, c, d, uva, uvb, uvc, uvd;
  80. var frames = path.computeFrenetFrames( segments, closed ),
  81. tangents = frames.tangents,
  82. normals = frames.normals,
  83. binormals = frames.binormals;
  84. // proxy internals
  85. this.tangents = tangents;
  86. this.normals = normals;
  87. this.binormals = binormals;
  88. var ParametricTube = function( u, v, optionalTarget ) {
  89. var result = optionalTarget || new THREE.Vector3();
  90. v *= 2 * Math.PI;
  91. i = u * ( numpoints - 1 );
  92. i = Math.floor( i );
  93. pos = path.getPointAt( u );
  94. tangent = tangents[ i ];
  95. normal = normals[ i ];
  96. binormal = binormals[ i ];
  97. if ( scope.debug ) {
  98. scope.debug.add( new THREE.ArrowHelper( tangent, pos, radius, 0x0000ff ) );
  99. scope.debug.add( new THREE.ArrowHelper( normal, pos, radius, 0xff0000 ) );
  100. scope.debug.add( new THREE.ArrowHelper( binormal, pos, radius, 0x00ff00 ) );
  101. }
  102. cx = - scope.radius * Math.cos( v ); // TODO: Hack: Negating it so it faces outside.
  103. cy = scope.radius * Math.sin( v );
  104. pos.x += cx * normal.x + cy * binormal.x;
  105. pos.y += cx * normal.y + cy * binormal.y;
  106. pos.z += cx * normal.z + cy * binormal.z;
  107. return result.copy( pos );
  108. };
  109. THREE.ParametricGeometry.call( this, ParametricTube, segments, segmentsRadius );
  110. };
  111. THREE.ParametricGeometries.TubeGeometry.prototype = Object.create( THREE.Geometry.prototype );
  112. THREE.ParametricGeometries.TubeGeometry.prototype.constructor = THREE.ParametricGeometries.TubeGeometry;
  113. /*********************************************
  114. *
  115. * Parametric Replacement for TorusKnotGeometry
  116. *
  117. *********************************************/
  118. THREE.ParametricGeometries.TorusKnotGeometry = function ( radius, tube, segmentsT, segmentsR, p, q ) {
  119. var scope = this;
  120. this.radius = radius || 200;
  121. this.tube = tube || 40;
  122. this.segmentsT = segmentsT || 64;
  123. this.segmentsR = segmentsR || 8;
  124. this.p = p || 2;
  125. this.q = q || 3;
  126. function TorusKnotCurve() {
  127. THREE.Curve.call( this );
  128. }
  129. TorusKnotCurve.prototype = Object.create( THREE.Curve.prototype );
  130. TorusKnotCurve.prototype.constructor = TorusKnotCurve;
  131. TorusKnotCurve.prototype.getPoint = function( t ){
  132. t *= Math.PI * 2;
  133. var r = 0.5;
  134. var x = ( 1 + r * Math.cos( q * t ) ) * Math.cos( p * t );
  135. var y = ( 1 + r * Math.cos( q * t ) ) * Math.sin( p * t );
  136. var z = r * Math.sin( q * t );
  137. return new THREE.Vector3( x, y, z ).multiplyScalar( radius );
  138. };
  139. var segments = segmentsT;
  140. var radiusSegments = segmentsR;
  141. var extrudePath = new TorusKnotCurve();
  142. THREE.ParametricGeometries.TubeGeometry.call( this, extrudePath, segments, tube, radiusSegments, true, false );
  143. };
  144. THREE.ParametricGeometries.TorusKnotGeometry.prototype = Object.create( THREE.Geometry.prototype );
  145. THREE.ParametricGeometries.TorusKnotGeometry.prototype.constructor = THREE.ParametricGeometries.TorusKnotGeometry;
  146. /*********************************************
  147. *
  148. * Parametric Replacement for SphereGeometry
  149. *
  150. *********************************************/
  151. THREE.ParametricGeometries.SphereGeometry = function( size, u, v ) {
  152. function sphere( u, v, optionalTarget ) {
  153. var result = optionalTarget || new THREE.Vector3();
  154. u *= Math.PI;
  155. v *= 2 * Math.PI;
  156. var x = size * Math.sin( u ) * Math.cos( v );
  157. var y = size * Math.sin( u ) * Math.sin( v );
  158. var z = size * Math.cos( u );
  159. return result.set( x, y, z );
  160. }
  161. THREE.ParametricGeometry.call( this, sphere, u, v, ! true );
  162. };
  163. THREE.ParametricGeometries.SphereGeometry.prototype = Object.create( THREE.Geometry.prototype );
  164. THREE.ParametricGeometries.SphereGeometry.prototype.constructor = THREE.ParametricGeometries.SphereGeometry;
  165. /*********************************************
  166. *
  167. * Parametric Replacement for PlaneGeometry
  168. *
  169. *********************************************/
  170. THREE.ParametricGeometries.PlaneGeometry = function( width, depth, segmentsWidth, segmentsDepth ) {
  171. function plane( u, v, optionalTarget ) {
  172. var result = optionalTarget || new THREE.Vector3();
  173. var x = u * width;
  174. var y = 0;
  175. var z = v * depth;
  176. return result.set( x, y, z );
  177. }
  178. THREE.ParametricGeometry.call( this, plane, segmentsWidth, segmentsDepth );
  179. };
  180. THREE.ParametricGeometries.PlaneGeometry.prototype = Object.create( THREE.Geometry.prototype );
  181. THREE.ParametricGeometries.PlaneGeometry.prototype.constructor = THREE.ParametricGeometries.PlaneGeometry;