ParametricGeometries.js 6.1 KB

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