ParametricGeometries.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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, numpoints = this.segments + 1;
  75. var frames = path.computeFrenetFrames( segments, closed ),
  76. tangents = frames.tangents,
  77. normals = frames.normals,
  78. binormals = frames.binormals;
  79. // proxy internals
  80. this.tangents = tangents;
  81. this.normals = normals;
  82. this.binormals = binormals;
  83. var ParametricTube = function ( u, v, optionalTarget ) {
  84. var result = optionalTarget || new THREE.Vector3();
  85. v *= 2 * Math.PI;
  86. var i = u * ( numpoints - 1 );
  87. i = Math.floor( i );
  88. var pos = path.getPointAt( u );
  89. var tangent = tangents[ i ];
  90. var normal = normals[ i ];
  91. var binormal = binormals[ i ];
  92. if ( scope.debug ) {
  93. scope.debug.add( new THREE.ArrowHelper( tangent, pos, radius, 0x0000ff ) );
  94. scope.debug.add( new THREE.ArrowHelper( normal, pos, radius, 0xff0000 ) );
  95. scope.debug.add( new THREE.ArrowHelper( binormal, pos, radius, 0x00ff00 ) );
  96. }
  97. var cx = - scope.radius * Math.cos( v ); // TODO: Hack: Negating it so it faces outside.
  98. var cy = scope.radius * Math.sin( v );
  99. pos.x += cx * normal.x + cy * binormal.x;
  100. pos.y += cx * normal.y + cy * binormal.y;
  101. pos.z += cx * normal.z + cy * binormal.z;
  102. return result.copy( pos );
  103. };
  104. THREE.ParametricGeometry.call( this, ParametricTube, segments, segmentsRadius );
  105. };
  106. THREE.ParametricGeometries.TubeGeometry.prototype = Object.create( THREE.Geometry.prototype );
  107. THREE.ParametricGeometries.TubeGeometry.prototype.constructor = THREE.ParametricGeometries.TubeGeometry;
  108. /*********************************************
  109. *
  110. * Parametric Replacement for TorusKnotGeometry
  111. *
  112. *********************************************/
  113. THREE.ParametricGeometries.TorusKnotGeometry = function ( radius, tube, segmentsT, segmentsR, p, q ) {
  114. this.radius = radius || 200;
  115. this.tube = tube || 40;
  116. this.segmentsT = segmentsT || 64;
  117. this.segmentsR = segmentsR || 8;
  118. this.p = p || 2;
  119. this.q = q || 3;
  120. function TorusKnotCurve() {
  121. THREE.Curve.call( this );
  122. }
  123. TorusKnotCurve.prototype = Object.create( THREE.Curve.prototype );
  124. TorusKnotCurve.prototype.constructor = TorusKnotCurve;
  125. TorusKnotCurve.prototype.getPoint = function ( t, optionalTarget ) {
  126. var point = optionalTarget || new THREE.Vector3();
  127. t *= Math.PI * 2;
  128. var r = 0.5;
  129. var x = ( 1 + r * Math.cos( q * t ) ) * Math.cos( p * t );
  130. var y = ( 1 + r * Math.cos( q * t ) ) * Math.sin( p * t );
  131. var z = r * Math.sin( q * t );
  132. return point.set( x, y, z ).multiplyScalar( radius );
  133. };
  134. var segments = segmentsT;
  135. var radiusSegments = segmentsR;
  136. var extrudePath = new TorusKnotCurve();
  137. THREE.ParametricGeometries.TubeGeometry.call( this, extrudePath, segments, tube, radiusSegments, true, false );
  138. };
  139. THREE.ParametricGeometries.TorusKnotGeometry.prototype = Object.create( THREE.Geometry.prototype );
  140. THREE.ParametricGeometries.TorusKnotGeometry.prototype.constructor = THREE.ParametricGeometries.TorusKnotGeometry;
  141. /*********************************************
  142. *
  143. * Parametric Replacement for SphereGeometry
  144. *
  145. *********************************************/
  146. THREE.ParametricGeometries.SphereGeometry = function ( size, u, v ) {
  147. function sphere( u, v, optionalTarget ) {
  148. var result = optionalTarget || new THREE.Vector3();
  149. u *= Math.PI;
  150. v *= 2 * Math.PI;
  151. var x = size * Math.sin( u ) * Math.cos( v );
  152. var y = size * Math.sin( u ) * Math.sin( v );
  153. var z = size * Math.cos( u );
  154. return result.set( x, y, z );
  155. }
  156. THREE.ParametricGeometry.call( this, sphere, u, v );
  157. };
  158. THREE.ParametricGeometries.SphereGeometry.prototype = Object.create( THREE.Geometry.prototype );
  159. THREE.ParametricGeometries.SphereGeometry.prototype.constructor = THREE.ParametricGeometries.SphereGeometry;
  160. /*********************************************
  161. *
  162. * Parametric Replacement for PlaneGeometry
  163. *
  164. *********************************************/
  165. THREE.ParametricGeometries.PlaneGeometry = function ( width, depth, segmentsWidth, segmentsDepth ) {
  166. function plane( u, v, optionalTarget ) {
  167. var result = optionalTarget || new THREE.Vector3();
  168. var x = u * width;
  169. var y = 0;
  170. var z = v * depth;
  171. return result.set( x, y, z );
  172. }
  173. THREE.ParametricGeometry.call( this, plane, segmentsWidth, segmentsDepth );
  174. };
  175. THREE.ParametricGeometries.PlaneGeometry.prototype = Object.create( THREE.Geometry.prototype );
  176. THREE.ParametricGeometries.PlaneGeometry.prototype.constructor = THREE.ParametricGeometries.PlaneGeometry;