CurveExtras.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. console.warn( "THREE.CurveExtras: As part of the transition to ES6 Modules, the files in 'examples/js' were deprecated in May 2020 (r117) and will be deleted in December 2020 (r124). You can find more information about developing using ES6 Modules in https://threejs.org/docs/index.html#manual/en/introduction/Import-via-modules." );
  2. /**
  3. * A bunch of parametric curves
  4. * @author zz85
  5. *
  6. * Formulas collected from various sources
  7. * http://mathworld.wolfram.com/HeartCurve.html
  8. * http://mathdl.maa.org/images/upload_library/23/stemkoski/knots/page6.html
  9. * http://en.wikipedia.org/wiki/Viviani%27s_curve
  10. * http://mathdl.maa.org/images/upload_library/23/stemkoski/knots/page4.html
  11. * http://www.mi.sanu.ac.rs/vismath/taylorapril2011/Taylor.pdf
  12. * https://prideout.net/blog/old/blog/index.html@p=44.html
  13. */
  14. THREE.Curves = ( function () {
  15. // GrannyKnot
  16. function GrannyKnot() {
  17. THREE.Curve.call( this );
  18. }
  19. GrannyKnot.prototype = Object.create( THREE.Curve.prototype );
  20. GrannyKnot.prototype.constructor = GrannyKnot;
  21. GrannyKnot.prototype.getPoint = function ( t, optionalTarget ) {
  22. var point = optionalTarget || new THREE.Vector3();
  23. t = 2 * Math.PI * t;
  24. var x = - 0.22 * Math.cos( t ) - 1.28 * Math.sin( t ) - 0.44 * Math.cos( 3 * t ) - 0.78 * Math.sin( 3 * t );
  25. var y = - 0.1 * Math.cos( 2 * t ) - 0.27 * Math.sin( 2 * t ) + 0.38 * Math.cos( 4 * t ) + 0.46 * Math.sin( 4 * t );
  26. var z = 0.7 * Math.cos( 3 * t ) - 0.4 * Math.sin( 3 * t );
  27. return point.set( x, y, z ).multiplyScalar( 20 );
  28. };
  29. // HeartCurve
  30. function HeartCurve( scale ) {
  31. THREE.Curve.call( this );
  32. this.scale = ( scale === undefined ) ? 5 : scale;
  33. }
  34. HeartCurve.prototype = Object.create( THREE.Curve.prototype );
  35. HeartCurve.prototype.constructor = HeartCurve;
  36. HeartCurve.prototype.getPoint = function ( t, optionalTarget ) {
  37. var point = optionalTarget || new THREE.Vector3();
  38. t *= 2 * Math.PI;
  39. var x = 16 * Math.pow( Math.sin( t ), 3 );
  40. var y = 13 * Math.cos( t ) - 5 * Math.cos( 2 * t ) - 2 * Math.cos( 3 * t ) - Math.cos( 4 * t );
  41. var z = 0;
  42. return point.set( x, y, z ).multiplyScalar( this.scale );
  43. };
  44. // Viviani's Curve
  45. function VivianiCurve( scale ) {
  46. THREE.Curve.call( this );
  47. this.scale = ( scale === undefined ) ? 70 : scale;
  48. }
  49. VivianiCurve.prototype = Object.create( THREE.Curve.prototype );
  50. VivianiCurve.prototype.constructor = VivianiCurve;
  51. VivianiCurve.prototype.getPoint = function ( t, optionalTarget ) {
  52. var point = optionalTarget || new THREE.Vector3();
  53. t = t * 4 * Math.PI; // normalized to 0..1
  54. var a = this.scale / 2;
  55. var x = a * ( 1 + Math.cos( t ) );
  56. var y = a * Math.sin( t );
  57. var z = 2 * a * Math.sin( t / 2 );
  58. return point.set( x, y, z );
  59. };
  60. // KnotCurve
  61. function KnotCurve() {
  62. THREE.Curve.call( this );
  63. }
  64. KnotCurve.prototype = Object.create( THREE.Curve.prototype );
  65. KnotCurve.prototype.constructor = KnotCurve;
  66. KnotCurve.prototype.getPoint = function ( t, optionalTarget ) {
  67. var point = optionalTarget || new THREE.Vector3();
  68. t *= 2 * Math.PI;
  69. var R = 10;
  70. var s = 50;
  71. var x = s * Math.sin( t );
  72. var y = Math.cos( t ) * ( R + s * Math.cos( t ) );
  73. var z = Math.sin( t ) * ( R + s * Math.cos( t ) );
  74. return point.set( x, y, z );
  75. };
  76. // HelixCurve
  77. function HelixCurve() {
  78. THREE.Curve.call( this );
  79. }
  80. HelixCurve.prototype = Object.create( THREE.Curve.prototype );
  81. HelixCurve.prototype.constructor = HelixCurve;
  82. HelixCurve.prototype.getPoint = function ( t, optionalTarget ) {
  83. var point = optionalTarget || new THREE.Vector3();
  84. var a = 30; // radius
  85. var b = 150; // height
  86. var t2 = 2 * Math.PI * t * b / 30;
  87. var x = Math.cos( t2 ) * a;
  88. var y = Math.sin( t2 ) * a;
  89. var z = b * t;
  90. return point.set( x, y, z );
  91. };
  92. // TrefoilKnot
  93. function TrefoilKnot( scale ) {
  94. THREE.Curve.call( this );
  95. this.scale = ( scale === undefined ) ? 10 : scale;
  96. }
  97. TrefoilKnot.prototype = Object.create( THREE.Curve.prototype );
  98. TrefoilKnot.prototype.constructor = TrefoilKnot;
  99. TrefoilKnot.prototype.getPoint = function ( t, optionalTarget ) {
  100. var point = optionalTarget || new THREE.Vector3();
  101. t *= Math.PI * 2;
  102. var x = ( 2 + Math.cos( 3 * t ) ) * Math.cos( 2 * t );
  103. var y = ( 2 + Math.cos( 3 * t ) ) * Math.sin( 2 * t );
  104. var z = Math.sin( 3 * t );
  105. return point.set( x, y, z ).multiplyScalar( this.scale );
  106. };
  107. // TorusKnot
  108. function TorusKnot( scale ) {
  109. THREE.Curve.call( this );
  110. this.scale = ( scale === undefined ) ? 10 : scale;
  111. }
  112. TorusKnot.prototype = Object.create( THREE.Curve.prototype );
  113. TorusKnot.prototype.constructor = TorusKnot;
  114. TorusKnot.prototype.getPoint = function ( t, optionalTarget ) {
  115. var point = optionalTarget || new THREE.Vector3();
  116. var p = 3;
  117. var q = 4;
  118. t *= Math.PI * 2;
  119. var x = ( 2 + Math.cos( q * t ) ) * Math.cos( p * t );
  120. var y = ( 2 + Math.cos( q * t ) ) * Math.sin( p * t );
  121. var z = Math.sin( q * t );
  122. return point.set( x, y, z ).multiplyScalar( this.scale );
  123. };
  124. // CinquefoilKnot
  125. function CinquefoilKnot( scale ) {
  126. THREE.Curve.call( this );
  127. this.scale = ( scale === undefined ) ? 10 : scale;
  128. }
  129. CinquefoilKnot.prototype = Object.create( THREE.Curve.prototype );
  130. CinquefoilKnot.prototype.constructor = CinquefoilKnot;
  131. CinquefoilKnot.prototype.getPoint = function ( t, optionalTarget ) {
  132. var point = optionalTarget || new THREE.Vector3();
  133. var p = 2;
  134. var q = 5;
  135. t *= Math.PI * 2;
  136. var x = ( 2 + Math.cos( q * t ) ) * Math.cos( p * t );
  137. var y = ( 2 + Math.cos( q * t ) ) * Math.sin( p * t );
  138. var z = Math.sin( q * t );
  139. return point.set( x, y, z ).multiplyScalar( this.scale );
  140. };
  141. // TrefoilPolynomialKnot
  142. function TrefoilPolynomialKnot( scale ) {
  143. THREE.Curve.call( this );
  144. this.scale = ( scale === undefined ) ? 10 : scale;
  145. }
  146. TrefoilPolynomialKnot.prototype = Object.create( THREE.Curve.prototype );
  147. TrefoilPolynomialKnot.prototype.constructor = TrefoilPolynomialKnot;
  148. TrefoilPolynomialKnot.prototype.getPoint = function ( t, optionalTarget ) {
  149. var point = optionalTarget || new THREE.Vector3();
  150. t = t * 4 - 2;
  151. var x = Math.pow( t, 3 ) - 3 * t;
  152. var y = Math.pow( t, 4 ) - 4 * t * t;
  153. var z = 1 / 5 * Math.pow( t, 5 ) - 2 * t;
  154. return point.set( x, y, z ).multiplyScalar( this.scale );
  155. };
  156. var scaleTo = function ( x, y, t ) {
  157. var r = y - x;
  158. return t * r + x;
  159. };
  160. // FigureEightPolynomialKnot
  161. function FigureEightPolynomialKnot( scale ) {
  162. THREE.Curve.call( this );
  163. this.scale = ( scale === undefined ) ? 1 : scale;
  164. }
  165. FigureEightPolynomialKnot.prototype = Object.create( THREE.Curve.prototype );
  166. FigureEightPolynomialKnot.prototype.constructor = FigureEightPolynomialKnot;
  167. FigureEightPolynomialKnot.prototype.getPoint = function ( t, optionalTarget ) {
  168. var point = optionalTarget || new THREE.Vector3();
  169. t = scaleTo( - 4, 4, t );
  170. var x = 2 / 5 * t * ( t * t - 7 ) * ( t * t - 10 );
  171. var y = Math.pow( t, 4 ) - 13 * t * t;
  172. var z = 1 / 10 * t * ( t * t - 4 ) * ( t * t - 9 ) * ( t * t - 12 );
  173. return point.set( x, y, z ).multiplyScalar( this.scale );
  174. };
  175. // DecoratedTorusKnot4a
  176. function DecoratedTorusKnot4a( scale ) {
  177. THREE.Curve.call( this );
  178. this.scale = ( scale === undefined ) ? 40 : scale;
  179. }
  180. DecoratedTorusKnot4a.prototype = Object.create( THREE.Curve.prototype );
  181. DecoratedTorusKnot4a.prototype.constructor = DecoratedTorusKnot4a;
  182. DecoratedTorusKnot4a.prototype.getPoint = function ( t, optionalTarget ) {
  183. var point = optionalTarget || new THREE.Vector3();
  184. t *= Math.PI * 2;
  185. var x = Math.cos( 2 * t ) * ( 1 + 0.6 * ( Math.cos( 5 * t ) + 0.75 * Math.cos( 10 * t ) ) );
  186. var y = Math.sin( 2 * t ) * ( 1 + 0.6 * ( Math.cos( 5 * t ) + 0.75 * Math.cos( 10 * t ) ) );
  187. var z = 0.35 * Math.sin( 5 * t );
  188. return point.set( x, y, z ).multiplyScalar( this.scale );
  189. };
  190. // DecoratedTorusKnot4b
  191. function DecoratedTorusKnot4b( scale ) {
  192. THREE.Curve.call( this );
  193. this.scale = ( scale === undefined ) ? 40 : scale;
  194. }
  195. DecoratedTorusKnot4b.prototype = Object.create( THREE.Curve.prototype );
  196. DecoratedTorusKnot4b.prototype.constructor = DecoratedTorusKnot4b;
  197. DecoratedTorusKnot4b.prototype.getPoint = function ( t, optionalTarget ) {
  198. var point = optionalTarget || new THREE.Vector3();
  199. var fi = t * Math.PI * 2;
  200. var x = Math.cos( 2 * fi ) * ( 1 + 0.45 * Math.cos( 3 * fi ) + 0.4 * Math.cos( 9 * fi ) );
  201. var y = Math.sin( 2 * fi ) * ( 1 + 0.45 * Math.cos( 3 * fi ) + 0.4 * Math.cos( 9 * fi ) );
  202. var z = 0.2 * Math.sin( 9 * fi );
  203. return point.set( x, y, z ).multiplyScalar( this.scale );
  204. };
  205. // DecoratedTorusKnot5a
  206. function DecoratedTorusKnot5a( scale ) {
  207. THREE.Curve.call( this );
  208. this.scale = ( scale === undefined ) ? 40 : scale;
  209. }
  210. DecoratedTorusKnot5a.prototype = Object.create( THREE.Curve.prototype );
  211. DecoratedTorusKnot5a.prototype.constructor = DecoratedTorusKnot5a;
  212. DecoratedTorusKnot5a.prototype.getPoint = function ( t, optionalTarget ) {
  213. var point = optionalTarget || new THREE.Vector3();
  214. var fi = t * Math.PI * 2;
  215. var x = Math.cos( 3 * fi ) * ( 1 + 0.3 * Math.cos( 5 * fi ) + 0.5 * Math.cos( 10 * fi ) );
  216. var y = Math.sin( 3 * fi ) * ( 1 + 0.3 * Math.cos( 5 * fi ) + 0.5 * Math.cos( 10 * fi ) );
  217. var z = 0.2 * Math.sin( 20 * fi );
  218. return point.set( x, y, z ).multiplyScalar( this.scale );
  219. };
  220. // DecoratedTorusKnot5c
  221. function DecoratedTorusKnot5c( scale ) {
  222. THREE.Curve.call( this );
  223. this.scale = ( scale === undefined ) ? 40 : scale;
  224. }
  225. DecoratedTorusKnot5c.prototype = Object.create( THREE.Curve.prototype );
  226. DecoratedTorusKnot5c.prototype.constructor = DecoratedTorusKnot5c;
  227. DecoratedTorusKnot5c.prototype.getPoint = function ( t, optionalTarget ) {
  228. var point = optionalTarget || new THREE.Vector3();
  229. var fi = t * Math.PI * 2;
  230. var x = Math.cos( 4 * fi ) * ( 1 + 0.5 * ( Math.cos( 5 * fi ) + 0.4 * Math.cos( 20 * fi ) ) );
  231. var y = Math.sin( 4 * fi ) * ( 1 + 0.5 * ( Math.cos( 5 * fi ) + 0.4 * Math.cos( 20 * fi ) ) );
  232. var z = 0.35 * Math.sin( 15 * fi );
  233. return point.set( x, y, z ).multiplyScalar( this.scale );
  234. };
  235. return {
  236. GrannyKnot: GrannyKnot,
  237. HeartCurve: HeartCurve,
  238. VivianiCurve: VivianiCurve,
  239. KnotCurve: KnotCurve,
  240. HelixCurve: HelixCurve,
  241. TrefoilKnot: TrefoilKnot,
  242. TorusKnot: TorusKnot,
  243. CinquefoilKnot: CinquefoilKnot,
  244. TrefoilPolynomialKnot: TrefoilPolynomialKnot,
  245. FigureEightPolynomialKnot: FigureEightPolynomialKnot,
  246. DecoratedTorusKnot4a: DecoratedTorusKnot4a,
  247. DecoratedTorusKnot4b: DecoratedTorusKnot4b,
  248. DecoratedTorusKnot5a: DecoratedTorusKnot5a,
  249. DecoratedTorusKnot5c: DecoratedTorusKnot5c
  250. };
  251. } )();