webgl_geometry_extrude_splines.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - geometry - shapes</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <style>
  8. body {
  9. font-family: Monospace;
  10. background-color: #f0f0f0;
  11. margin: 0px;
  12. overflow: hidden;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <canvas id="debug" style="position:absolute; left:100px"></canvas>
  18. <script src="../build/Three.js"></script>
  19. <script src="../src/extras/core/Curve.js"></script>
  20. <script src="../src/extras/geometries/TubeGeometry.js"></script>
  21. <script src="js/Stats.js"></script>
  22. <script>
  23. // Lets define some curves
  24. // Formula from http://mathworld.wolfram.com/HeartCurve.html
  25. THREE.HeartCurve = THREE.Curve.create(
  26. function ( s ) {
  27. this.scale = (s === undefined) ? 5 : s;
  28. },
  29. function ( t ) {
  30. t *= 2 * Math.PI;
  31. var tx = 16 * Math.pow(Math.sin(t), 3);
  32. ty = 13 * Math.cos(t)
  33. - 5 * Math.cos(2 * t)
  34. - 2 * Math.cos(3 * t)
  35. - Math.cos(4 * t ),
  36. tz = 0;
  37. return new THREE.Vector3(tx, ty, tz).multiplyScalar(this.scale);
  38. }
  39. );
  40. // Viviani's Curve
  41. // http://en.wikipedia.org/wiki/Viviani%27s_curve
  42. THREE.VivianiCurve = THREE.Curve.create(
  43. function( radius ) {
  44. this.radius = radius;
  45. },
  46. function( t ) {
  47. t = t * 4 * Math.PI; // Normalized to 0..1
  48. var a = this.radius / 2;
  49. var tx = a * (1 + Math.cos(t)),
  50. ty = a * Math.sin(t),
  51. tz = 2 * a * Math.sin(t / 2);
  52. return new THREE.Vector3(tx, ty, tz);
  53. }
  54. );
  55. THREE.KnotCurve = THREE.Curve.create(
  56. function() {
  57. },
  58. function(t) {
  59. t *= 2 * Math.PI;
  60. var R = 10;
  61. var s = 50;
  62. var tx = s * Math.sin(t),
  63. ty = Math.cos(t) * (R + s * Math.cos(t)),
  64. tz = Math.sin(t) * (R + s * Math.cos(t));
  65. return new THREE.Vector3(tx, ty, tz);
  66. }
  67. );
  68. THREE.HelixCurve = THREE.Curve.create(
  69. function() {
  70. },
  71. function(t) {
  72. var a = 30; // radius
  73. var b = 150; //height
  74. var t2 = 2 * Math.PI * t * b / 30;
  75. var tx = Math.cos(t2) * a,
  76. ty = Math.sin(t2) * a,
  77. tz = b * t;
  78. return new THREE.Vector3(tx, ty, tz);
  79. }
  80. );
  81. // Replacement for TorusKnotGeometry?
  82. THREE.TrefoilKnot = THREE.Curve.create(
  83. function(s) {
  84. this.scale = (s === undefined) ? 10 : s;
  85. },
  86. function(t) {
  87. t *= Math.PI * 2;
  88. var tx = (2 + Math.cos(3 * t)) * Math.cos(2 * t),
  89. ty = (2 + Math.cos(3* t)) * Math.sin(2 * t),
  90. tz = Math.sin(3 * t);
  91. return new THREE.Vector3(tx, ty, tz).multiplyScalar(this.scale);
  92. }
  93. );
  94. // Formulas from http://mathdl.maa.org/images/upload_library/23/stemkoski/knots/page6.html
  95. THREE.TorusKnot = THREE.Curve.create(
  96. function(s) {
  97. this.scale = (s === undefined) ? 10 : s;
  98. },
  99. function(t) {
  100. var p = 3, q = 4;
  101. t *= Math.PI * 2;
  102. var tx = (2 + Math.cos(q * t)) * Math.cos(p * t),
  103. ty = (2 + Math.cos(q* t)) * Math.sin(p * t),
  104. tz = Math.sin(q * t);
  105. return new THREE.Vector3(tx, ty, tz).multiplyScalar(this.scale);
  106. }
  107. );
  108. THREE.CinquefoilKnot = THREE.Curve.create(
  109. function(s) {
  110. this.scale = (s === undefined) ? 10 : s;
  111. },
  112. function(t) {
  113. var p = 2, q = 5;
  114. t *= Math.PI * 2;
  115. var tx = (2 + Math.cos(q * t)) * Math.cos(p * t),
  116. ty = (2 + Math.cos(q* t)) * Math.sin(p * t),
  117. tz = Math.sin(q * t);
  118. return new THREE.Vector3(tx, ty, tz).multiplyScalar(this.scale);
  119. }
  120. );
  121. THREE.TrefoilPolynomialKnot = THREE.Curve.create(
  122. function(s) {
  123. this.scale = (s === undefined) ? 10 : s;
  124. },
  125. function(t) {
  126. t = t * 4 - 2;
  127. var tx = Math.pow(t, 3) - 3 * t,
  128. ty = Math.pow(t, 4) - 4 * t * t,
  129. tz = 1/ 5 * Math.pow(t, 5) - 2 * t;
  130. return new THREE.Vector3(tx, ty, tz).multiplyScalar(this.scale);
  131. }
  132. );
  133. var sin = Math.sin, pow = Math.pow, cos = Math.cos;
  134. // var scaleTo = function(x, y) {
  135. // var r = y - x;
  136. // return function(t) {
  137. // t * r + x;
  138. // };
  139. // }
  140. var scale = function(x, y, t) {
  141. var r = y - x;
  142. return t * r + x;
  143. }
  144. THREE.FigureEightPolynomialKnot = THREE.Curve.create(
  145. function(s) {
  146. this.scale = (s === undefined) ? 1 : s;
  147. },
  148. function(t) {
  149. t = scale(-4,4, t);
  150. var tx = 2 / 5 * t * (t * t - 7) * (t * t - 10),
  151. ty = pow(t, 4) - 13 * t * t,
  152. tz = 1/10 * t * (t * t - 4) * (t * t - 9) * (t * t - 12);
  153. return new THREE.Vector3(tx, ty, tz).multiplyScalar(this.scale);
  154. }
  155. );
  156. // When there's time, try more formulas at http://mathdl.maa.org/images/upload_library/23/stemkoski/knots/page4.html
  157. //http://www.mi.sanu.ac.rs/vismath/taylorapril2011/Taylor.pdf
  158. THREE.DecoratedTorusKnot4a = THREE.Curve.create(
  159. function(s) {
  160. this.scale = (s === undefined) ? 40 : s;
  161. },
  162. function(t) {
  163. t *= Math.PI * 2;
  164. var
  165. x = cos(2*t) * (1+0.6*(cos(5*t) + 0.75*cos(10*t))),
  166. y = sin(2*t) * (1+0.6*(cos(5*t) + 0.75*cos(10*t))),
  167. z = 0.35*sin(5*t);
  168. return new THREE.Vector3(x, y, z).multiplyScalar(this.scale);
  169. }
  170. );
  171. THREE.DecoratedTorusKnot4b = THREE.Curve.create(
  172. function(s) {
  173. this.scale = (s === undefined) ? 40 : s;
  174. },
  175. function(t) {
  176. var fi = t * Math.PI * 2;
  177. var x = cos(2*fi) * (1 + 0.45*cos(3*fi) + 0.4*cos(9*fi)),
  178. y = sin(2*fi) * (1 + 0.45*cos(3*fi) + 0.4*cos(9*fi)),
  179. z = 0.2*sin(9*fi);
  180. return new THREE.Vector3(x, y, z).multiplyScalar(this.scale);
  181. }
  182. );
  183. THREE.DecoratedTorusKnot5a = THREE.Curve.create(
  184. function(s) {
  185. this.scale = (s === undefined) ? 40 : s;
  186. },
  187. function(t) {
  188. var fi = t * Math.PI * 2;
  189. var x = cos(3*fi) * (1 + 0.3*cos(5*fi) + 0.5*cos(10*fi)),
  190. y = sin(3*fi) * (1 + 0.3*cos(5*fi) + 0.5*cos(10*fi)),
  191. z = 0.2*sin(20*fi);
  192. return new THREE.Vector3(x, y, z).multiplyScalar(this.scale);
  193. }
  194. );
  195. THREE.DecoratedTorusKnot5c = THREE.Curve.create(
  196. function(s) {
  197. this.scale = (s === undefined) ? 40 : s;
  198. },
  199. function(t) {
  200. var fi = t * Math.PI * 2;
  201. var x = cos(4*fi) * (1 + 0.5*(cos(5*fi) + 0.4*cos(20*fi))),
  202. y = sin(4*fi) * (1 + 0.5*(cos(5*fi) + 0.4*cos(20*fi))),
  203. z = 0.35*sin(15*fi);
  204. return new THREE.Vector3(x, y, z).multiplyScalar(this.scale);
  205. }
  206. );
  207. var container, stats;
  208. var camera, scene, renderer;
  209. var text, plane;
  210. var targetRotation = 0;
  211. var targetRotationOnMouseDown = 0;
  212. var mouseX = 0;
  213. var mouseXOnMouseDown = 0;
  214. var windowHalfX = window.innerWidth / 2;
  215. var windowHalfY = window.innerHeight / 2;
  216. init();
  217. animate();
  218. function init() {
  219. container = document.createElement('div');
  220. document.body.appendChild(container);
  221. var info = document.createElement('div');
  222. info.style.position = 'absolute';
  223. info.style.top = '10px';
  224. info.style.width = '100%';
  225. info.style.textAlign = 'center';
  226. info.innerHTML = 'Simple procedurally generated 3D shapes example by <a href="http://www.lab4games.net/zz85/blog">zz85</a><br/>Drag to spin';
  227. container.appendChild(info);
  228. scene = new THREE.Scene();
  229. camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 1000);
  230. camera.position.set(0, 50, 500);
  231. scene.add(camera);
  232. var light = new THREE.DirectionalLight(0xffffff);
  233. light.position.set(0, 0, 1);
  234. scene.add(light);
  235. parent = new THREE.Object3D();
  236. parent.position.y = 100;
  237. scene.add(parent);
  238. function addGeometry(geometry, color, x, y, z, rx, ry, rz, s) {
  239. // 3d shape
  240. var mesh = THREE.SceneUtils.createMultiMaterialObject(geometry, [
  241. new THREE.MeshLambertMaterial({
  242. color: color,
  243. opacity: 0.2
  244. }),
  245. new THREE.MeshBasicMaterial({
  246. color: 0x000000,
  247. wireframe: true,
  248. transparent: true
  249. })]);
  250. if (geometry.debug) mesh.add(geometry.debug);
  251. mesh.position.set(x, y, z - 75);
  252. mesh.rotation.set(rx, ry, rz);
  253. mesh.scale.set(s, s, s);
  254. //mesh.children[0].doubleSided = true;
  255. parent.add(mesh);
  256. }
  257. // var extrudePath = new THREE.SplineCurve3([
  258. // new THREE.Vector3(0, 10, -10), new THREE.Vector3(10, 0, -10), new THREE.Vector3(20, 0, 0), new THREE.Vector3(30, 0, 10), new THREE.Vector3(30, 0, 20), new THREE.Vector3(20, 0, 30), new THREE.Vector3(10, 0, 30), new THREE.Vector3(0, 0, 30), new THREE.Vector3(-10, 10, 30), new THREE.Vector3(-10, 20, 30), new THREE.Vector3(0, 30, 30), new THREE.Vector3(10, 30, 30), new THREE.Vector3(20, 30, 15), new THREE.Vector3(10, 30, 10),
  259. // new THREE.Vector3(0, 30, 10), new THREE.Vector3(-10, 20, 10), new THREE.Vector3(-10, 10, 10), new THREE.Vector3(0, 0, 10), new THREE.Vector3(10, -10, 10), new THREE.Vector3(20, -15, 10), new THREE.Vector3(30, -15, 10), new THREE.Vector3(40, -15, 10), new THREE.Vector3(50, -15, 10), new THREE.Vector3(60, 0, 10), new THREE.Vector3(70, 0, 0), new THREE.Vector3(80, 0, 0), new THREE.Vector3(90, 0, 0), new THREE.Vector3(100, 0, 0)]);
  260. // var extrudePath = new THREE.HeartCurve(3.5);
  261. // var extrudePath = new THREE.VivianiCurve(70);
  262. // var extrudePath = new THREE.KnotCurve();
  263. //extrudePath = new THREE.HelixCurve();
  264. // var extrudePath = new THREE.ClosedSplineCurve3([
  265. // new THREE.Vector3(0, -40, -40),
  266. // new THREE.Vector3(0, 40, -40),
  267. // new THREE.Vector3(0, 140, -40),
  268. // new THREE.Vector3(0, 40, 40),
  269. // new THREE.Vector3(0, -40, 40),
  270. // ]);
  271. extrudePath = new THREE.TrefoilKnot();
  272. // extrudePath = new THREE.TorusKnot(20);
  273. // extrudePath = new THREE.CinquefoilKnot(20);
  274. // extrudePath = new THREE.TrefoilPolynomialKnot(14);
  275. // extrudePath = new THREE.FigureEightPolynomialKnot();
  276. // extrudePath = new THREE.DecoratedTorusKnot4a();
  277. // extrudePath = new THREE.DecoratedTorusKnot4b();
  278. // extrudePath = new THREE.DecoratedTorusKnot5a();
  279. // extrudePath = new THREE.DecoratedTorusKnot5c();
  280. var closed = true;
  281. var debug = true;
  282. var tube = new THREE.TubeGeometry(extrudePath, 100, 2, 3, closed, debug);
  283. addGeometry(tube, 0xff00ff, 0, -80, 0, 0, 0, 0, 6);
  284. //
  285. renderer = new THREE.WebGLRenderer({
  286. antialias: true
  287. });
  288. renderer.setSize(window.innerWidth, window.innerHeight);
  289. container.appendChild(renderer.domElement);
  290. stats = new Stats();
  291. stats.domElement.style.position = 'absolute';
  292. stats.domElement.style.top = '0px';
  293. container.appendChild(stats.domElement);
  294. document.addEventListener('mousedown', onDocumentMouseDown, false);
  295. document.addEventListener('touchstart', onDocumentTouchStart, false);
  296. document.addEventListener('touchmove', onDocumentTouchMove, false);
  297. }
  298. //
  299. function onDocumentMouseDown(event) {
  300. event.preventDefault();
  301. document.addEventListener('mousemove', onDocumentMouseMove, false);
  302. document.addEventListener('mouseup', onDocumentMouseUp, false);
  303. document.addEventListener('mouseout', onDocumentMouseOut, false);
  304. mouseXOnMouseDown = event.clientX - windowHalfX;
  305. targetRotationOnMouseDown = targetRotation;
  306. }
  307. function onDocumentMouseMove(event) {
  308. mouseX = event.clientX - windowHalfX;
  309. targetRotation = targetRotationOnMouseDown + (mouseX - mouseXOnMouseDown) * 0.02;
  310. }
  311. function onDocumentMouseUp(event) {
  312. document.removeEventListener('mousemove', onDocumentMouseMove, false);
  313. document.removeEventListener('mouseup', onDocumentMouseUp, false);
  314. document.removeEventListener('mouseout', onDocumentMouseOut, false);
  315. }
  316. function onDocumentMouseOut(event) {
  317. document.removeEventListener('mousemove', onDocumentMouseMove, false);
  318. document.removeEventListener('mouseup', onDocumentMouseUp, false);
  319. document.removeEventListener('mouseout', onDocumentMouseOut, false);
  320. }
  321. function onDocumentTouchStart(event) {
  322. if (event.touches.length == 1) {
  323. event.preventDefault();
  324. mouseXOnMouseDown = event.touches[0].pageX - windowHalfX;
  325. targetRotationOnMouseDown = targetRotation;
  326. }
  327. }
  328. function onDocumentTouchMove(event) {
  329. if (event.touches.length == 1) {
  330. event.preventDefault();
  331. mouseX = event.touches[0].pageX - windowHalfX;
  332. targetRotation = targetRotationOnMouseDown + (mouseX - mouseXOnMouseDown) * 0.05;
  333. }
  334. }
  335. //
  336. function animate() {
  337. requestAnimationFrame(animate);
  338. render();
  339. stats.update();
  340. }
  341. function render() {
  342. parent.rotation.y += (targetRotation - parent.rotation.y) * 0.05;
  343. renderer.render(scene, camera);
  344. }
  345. </script>
  346. </body>
  347. </html>