ParametricGeometries.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * @author zz85
  3. *
  4. * Experimenting of primitive geometry creation using Surface Parametric equations
  5. */
  6. var sin = Math.sin, cos = Math.cos, pi = Math.PI;
  7. THREE.ParametricGeometries = {
  8. klein: function (v, u) {
  9. u *= pi;
  10. v *= 2 * pi;
  11. u = u * 2;
  12. var x, y, z;
  13. if (u < pi) {
  14. x = 3 * cos(u) * (1 + sin(u)) + (2 * (1 - cos(u) / 2)) * cos(u) * cos(v);
  15. z = -8 * sin(u) - 2 * (1 - cos(u) / 2) * sin(u) * cos(v);
  16. } else {
  17. x = 3 * cos(u) * (1 + sin(u)) + (2 * (1 - cos(u) / 2)) * cos(v + pi);
  18. z = -8 * sin(u);
  19. }
  20. y = -2 * (1 - cos(u) / 2) * sin(v);
  21. return new THREE.Vector3(x, y, z);
  22. },
  23. plane: function (width, height) {
  24. return function(u, v) {
  25. var x = u * width;
  26. var y = 0;
  27. var z = v * height;
  28. console.log(x, y, z);
  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. THREE.TubeGeometry2 = function(path, segments, radius, segmentsRadius, closed, debug) {
  60. this.path = path;
  61. this.segments = segments || 64;
  62. this.radius = radius || 1;
  63. this.segmentsRadius = segmentsRadius || 8;
  64. this.closed = closed || false;
  65. if (debug) this.debug = new THREE.Object3D();
  66. var scope = this,
  67. tangent, normal, binormal,
  68. numpoints = this.segments + 1,
  69. x, y, z, tx, ty, tz, u, v,
  70. cx, cy, pos, pos2 = new THREE.Vector3(),
  71. i, j, ip, jp, a, b, c, d, uva, uvb, uvc, uvd;
  72. var frames = new THREE.TubeGeometry.FrenetFrames(path, segments, closed),
  73. tangents = frames.tangents,
  74. normals = frames.normals,
  75. binormals = frames.binormals;
  76. // proxy internals
  77. this.tangents = tangents;
  78. this.normals = normals;
  79. this.binormals = binormals;
  80. var ParametricTube = function(u, v) {
  81. v *= 2 * pi;
  82. i = u * (numpoints - 1);
  83. i = Math.floor(i);
  84. pos = path.getPointAt(u);
  85. tangent = tangents[i];
  86. normal = normals[i];
  87. binormal = binormals[i];
  88. if (scope.debug) {
  89. scope.debug.add(new THREE.ArrowHelper(tangent, pos, radius, 0x0000ff));
  90. scope.debug.add(new THREE.ArrowHelper(normal, pos, radius, 0xff0000));
  91. scope.debug.add(new THREE.ArrowHelper(binormal, pos, radius, 0x00ff00));
  92. }
  93. cx = -scope.radius * Math.cos(v); // TODO: Hack: Negating it so it faces outside.
  94. cy = scope.radius * Math.sin(v);
  95. pos2.copy(pos);
  96. pos2.x += cx * normal.x + cy * binormal.x;
  97. pos2.y += cx * normal.y + cy * binormal.y;
  98. pos2.z += cx * normal.z + cy * binormal.z;
  99. return pos2.clone();
  100. };
  101. THREE.ParametricGeometry.call(this, ParametricTube, segments, segmentsRadius);
  102. };
  103. THREE.TubeGeometry2.prototype = new THREE.Geometry();
  104. THREE.TubeGeometry2.prototype.constructor = THREE.TubeGeometry2;
  105. // Replacement for TorusKnotGeometry?
  106. THREE.TorusKnotGeometry2 = function ( radius, tube, segmentsR, segmentsT, p, q, heightScale ) {
  107. var scope = this;
  108. this.radius = radius || 200;
  109. this.tube = tube || 40;
  110. this.segmentsR = segmentsR || 64;
  111. this.segmentsT = segmentsT || 8;
  112. this.p = p || 2;
  113. this.q = q || 3;
  114. this.heightScale = heightScale || 1;
  115. var TorusKnotCurve = THREE.Curve.create(
  116. function() {
  117. },
  118. function(t) {
  119. t *= Math.PI * 2;
  120. var r = 0.5;
  121. var tx = (1 + r * Math.cos(q * t)) * Math.cos(p * t),
  122. ty = (1 + r * Math.cos(q * t)) * Math.sin(p * t),
  123. tz = r * Math.sin(q * t);
  124. return new THREE.Vector3(tx, ty * heightScale, tz).multiplyScalar(radius);
  125. }
  126. );
  127. var segments = segmentsR;
  128. var radiusSegments = segmentsT;
  129. var extrudePath = new TorusKnotCurve();
  130. THREE.TubeGeometry2.call( this, extrudePath, segments, tube, radiusSegments, true, false );
  131. };
  132. THREE.TorusKnotGeometry2.prototype = new THREE.Geometry();
  133. THREE.TorusKnotGeometry2.prototype.constructor = THREE.TorusKnotGeometry2;
  134. THREE.SphereGeometry2 = function(size, x, y) {
  135. function sphere(u, v) {
  136. u *= pi;
  137. v *= 2 * pi;
  138. var x = sin(u) * cos(v);
  139. var y = cos(u);
  140. var z = -sin(u) * sin(v);
  141. return new THREE.Vector3(x, y, z).multiplyScalar(size);
  142. }
  143. THREE.ParametricGeometry.call(this, sphere, y, x);
  144. };
  145. THREE.SphereGeometry2.prototype = new THREE.Geometry();
  146. THREE.SphereGeometry2.prototype.constructor = THREE.SphereGeometry2;
  147. THREE.PlaneGeometry2 = function(width, depth, segmentsWidth, segmentsDepth) {
  148. function plane(u, v) {
  149. var x = u * width;
  150. var y = 0;
  151. var z = v * depth;
  152. return new THREE.Vector3(x, y, z);
  153. }
  154. THREE.ParametricGeometry.call(this, plane, segmentsWidth, segmentsDepth);
  155. };
  156. THREE.PlaneGeometry2.prototype = new THREE.Geometry();
  157. THREE.PlaneGeometry2.prototype.constructor = THREE.PlaneGeometry2;