ParametricGeometries.js 5.6 KB

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