webgl_geometry_extrude_splines.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  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. <script src="../build/Three.js"></script>
  18. <script src="../src/extras/core/Curve.js"></script>
  19. <script src="../src/extras/geometries/TubeGeometry.js"></script>
  20. <script src="js/Stats.js"></script>
  21. <script>
  22. // Lets define some curves
  23. THREE.Curves = {};
  24. // Formula from http://mathworld.wolfram.com/HeartCurve.html
  25. THREE.Curves.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.Curves.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.Curves.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.Curves.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.Curves.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.Curves.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.Curves.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.Curves.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 scaleTo = function(x, y, t) {
  141. var r = y - x;
  142. return t * r + x;
  143. }
  144. THREE.Curves.FigureEightPolynomialKnot = THREE.Curve.create(
  145. function(s) {
  146. this.scale = (s === undefined) ? 1 : s;
  147. },
  148. function(t) {
  149. t = scaleTo(-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.Curves.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.Curves.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.Curves.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.Curves.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. var PipeSpline = new THREE.SplineCurve3([
  217. 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), 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)]);
  218. var sampleClosedSpline = new THREE.ClosedSplineCurve3([
  219. new THREE.Vector3(0, -40, -40),
  220. new THREE.Vector3(0, 40, -40),
  221. new THREE.Vector3(0, 140, -40),
  222. new THREE.Vector3(0, 40, 40),
  223. new THREE.Vector3(0, -40, 40),
  224. ]);
  225. var splines = {
  226. HeartCurve: new THREE.Curves.HeartCurve(3.5),
  227. VivianiCurve: new THREE.Curves.VivianiCurve(70),
  228. KnotCurve: new THREE.Curves.KnotCurve(),
  229. HelixCurve: new THREE.Curves.HelixCurve(),
  230. TrefoilKnot: new THREE.Curves.TrefoilKnot(),
  231. TorusKnot: new THREE.Curves.TorusKnot(20),
  232. CinquefoilKnot: new THREE.Curves.CinquefoilKnot(20),
  233. TrefoilPolynomialKnot: new THREE.Curves.TrefoilPolynomialKnot(14),
  234. FigureEightPolynomialKnot: new THREE.Curves.FigureEightPolynomialKnot(),
  235. DecoratedTorusKnot4a: new THREE.Curves.DecoratedTorusKnot4a(),
  236. DecoratedTorusKnot4b: new THREE.Curves.DecoratedTorusKnot4b(),
  237. DecoratedTorusKnot5a: new THREE.Curves.DecoratedTorusKnot5a(),
  238. DecoratedTorusKnot5c: new THREE.Curves.DecoratedTorusKnot5c()
  239. };
  240. extrudePath = new THREE.Curves.TrefoilKnot();
  241. var dropdown = '<select id="dropdown" onchange="addTube(this.value)">';
  242. var s;
  243. for ( s in splines ) {
  244. dropdown += '<option value="' + s + '"';
  245. // dropdown += (geometryIndex == i) ? ' selected' : '';
  246. dropdown += '>' + s + '</option>';
  247. }
  248. dropdown += '</select>';
  249. var closed2 = true;
  250. var debug = true;
  251. var parent;
  252. var tube, tubeMesh;
  253. var animation = false;
  254. var scale;
  255. function addTube() {
  256. var value = document.getElementById('dropdown').value;
  257. var segments = parseInt(document.getElementById('segments').value);
  258. closed2 = document.getElementById('closed').checked;
  259. debug = document.getElementById('debug').checked;
  260. var radiusSegments = parseInt(document.getElementById('radiusSegments').value);
  261. console.log('adding tube', value, closed2, debug, radiusSegments);
  262. if (tubeMesh) parent.remove(tubeMesh);
  263. extrudePath = splines[value];
  264. tube = new THREE.TubeGeometry(extrudePath, segments, 2, radiusSegments, closed2, debug);
  265. addGeometry(tube, 0xff00ff);
  266. setScale();
  267. }
  268. function setScale() {
  269. scale = parseInt(document.getElementById('scale').value);
  270. tubeMesh.scale.set(scale, scale, scale);
  271. }
  272. function addGeometry(geometry, color) {
  273. // 3d shape
  274. tubeMesh = THREE.SceneUtils.createMultiMaterialObject(geometry, [
  275. new THREE.MeshLambertMaterial({
  276. color: color,
  277. opacity: 0.2,
  278. transparent: true
  279. }),
  280. new THREE.MeshBasicMaterial({
  281. color: 0x000000,
  282. opacity: 0.3,
  283. wireframe: true
  284. })]);
  285. if (geometry.debug) tubeMesh.add(geometry.debug);
  286. // tubeMesh.position.set(x, y, z);
  287. // tubeMesh.rotation.set(rx, ry, rz);
  288. //mesh.children[0].doubleSided = true;
  289. parent.add(tubeMesh);
  290. }
  291. function animateCamera() {
  292. animation = document.getElementById('animation').checked;
  293. }
  294. init();
  295. animate();
  296. function init() {
  297. container = document.createElement('div');
  298. document.body.appendChild(container);
  299. var info = document.createElement('div');
  300. info.style.position = 'absolute';
  301. info.style.top = '10px';
  302. info.style.width = '100%';
  303. info.style.textAlign = 'center';
  304. info.innerHTML = 'Spline Extrusion Examples by <a href="http://www.lab4games.net/zz85/blog">zz85</a><br/>Select spline:';
  305. info.innerHTML += dropdown;
  306. info.innerHTML += '<br/>Scale: <select id="scale" onchange="setScale()"><option>1</option><option>2</option><option selected>4</option><option>6</option><option>10</option></select>';
  307. info.innerHTML += '<br/>Extrusion Segments: <select onchange="addTube()" id="segments"><option>50</option><option selected>100</option><option>200</option><option>400</option></select>';
  308. info.innerHTML += '<br/>Debug: <input id="debug" type="checkbox" onchange="addTube()" checked /> Closed:<input id="closed" onchange="addTube()" type="checkbox" checked />';
  309. info.innerHTML += '<br/>Radius Segments: <select id="radiusSegments" onchange="addTube()"><option>1</option><option>2</option><option selected>3</option><option>4</option><option>5</option></select>';
  310. info.innerHTML += '<br/>Camera Spline Animation: <input id="animation" type="checkbox" onchange="animateCamera()" />';
  311. container.appendChild(info);
  312. scene = new THREE.Scene();
  313. // 50
  314. camera = new THREE.PerspectiveCamera(79, window.innerWidth / window.innerHeight, 0.01, 1000);
  315. camera.position.set(0, 50, 500);
  316. scene.add(camera);
  317. var light = new THREE.DirectionalLight(0xffffff);
  318. light.position.set(0, 0, 1);
  319. scene.add(light);
  320. parent = new THREE.Object3D();
  321. parent.position.y = 100;
  322. scene.add(parent);
  323. addTube();
  324. //
  325. renderer = new THREE.WebGLRenderer({
  326. antialias: true
  327. });
  328. renderer.setSize(window.innerWidth, window.innerHeight);
  329. container.appendChild(renderer.domElement);
  330. stats = new Stats();
  331. stats.domElement.style.position = 'absolute';
  332. stats.domElement.style.top = '0px';
  333. container.appendChild(stats.domElement);
  334. renderer.domElement.addEventListener('mousedown', onDocumentMouseDown, false);
  335. renderer.domElement.addEventListener('touchstart', onDocumentTouchStart, false);
  336. renderer.domElement.addEventListener('touchmove', onDocumentTouchMove, false);
  337. }
  338. //
  339. function onDocumentMouseDown(event) {
  340. event.preventDefault();
  341. renderer.domElement.addEventListener('mousemove', onDocumentMouseMove, false);
  342. renderer.domElement.addEventListener('mouseup', onDocumentMouseUp, false);
  343. renderer.domElement.addEventListener('mouseout', onDocumentMouseOut, false);
  344. mouseXOnMouseDown = event.clientX - windowHalfX;
  345. targetRotationOnMouseDown = targetRotation;
  346. }
  347. function onDocumentMouseMove(event) {
  348. mouseX = event.clientX - windowHalfX;
  349. targetRotation = targetRotationOnMouseDown + (mouseX - mouseXOnMouseDown) * 0.02;
  350. }
  351. function onDocumentMouseUp(event) {
  352. renderer.domElement.removeEventListener('mousemove', onDocumentMouseMove, false);
  353. renderer.domElement.removeEventListener('mouseup', onDocumentMouseUp, false);
  354. renderer.domElement.removeEventListener('mouseout', onDocumentMouseOut, false);
  355. }
  356. function onDocumentMouseOut(event) {
  357. renderer.domElement.removeEventListener('mousemove', onDocumentMouseMove, false);
  358. renderer.domElement.removeEventListener('mouseup', onDocumentMouseUp, false);
  359. renderer.domElement.removeEventListener('mouseout', onDocumentMouseOut, false);
  360. }
  361. function onDocumentTouchStart(event) {
  362. if (event.touches.length == 1) {
  363. event.preventDefault();
  364. mouseXOnMouseDown = event.touches[0].pageX - windowHalfX;
  365. targetRotationOnMouseDown = targetRotation;
  366. }
  367. }
  368. function onDocumentTouchMove(event) {
  369. if (event.touches.length == 1) {
  370. event.preventDefault();
  371. mouseX = event.touches[0].pageX - windowHalfX;
  372. targetRotation = targetRotationOnMouseDown + (mouseX - mouseXOnMouseDown) * 0.05;
  373. }
  374. }
  375. //
  376. function animate() {
  377. requestAnimationFrame(animate);
  378. render();
  379. stats.update();
  380. }
  381. function render() {
  382. if (animation) {
  383. // Try Animate Camera Along Spline
  384. var time = Date.now();
  385. var looptime = 20 * 1000;
  386. var t = (time % looptime) / looptime;
  387. var pos = tube.path.getPointAt(t);
  388. pos.multiplyScalar(scale);
  389. // interpolation
  390. var segments = tube.tangents.length;
  391. var pickt = t * segments;
  392. var pick = Math.floor(pickt);
  393. var pickNext = (pick + 1) % segments;
  394. var binormal = new THREE.Vector3();
  395. binormal.sub(tube.binormals[pickNext], tube.binormals[pick]);
  396. binormal.multiplyScalar(pickt-pick).addSelf(tube.binormals[pick]);
  397. var dir = tube.path.getTangentAt(t);
  398. var offset = 50;
  399. // pos.addSelf(binormal.clone().multiplyScalar(offset));
  400. // console.log(t, pos);
  401. camera.position = pos;
  402. var lookAt = tube.path.getPointAt(t + 0.001);
  403. // lookAt.multiplyScalar(scale);
  404. // camera.lookAt(lookAt);
  405. camera.matrix.lookAt( camera.position, lookAt, binormal ); //camera.position.clone().addSelf(dir)
  406. camera.rotation.getRotationFromMatrix( camera.matrix );
  407. // var axis = new THREE.Vector3( 0, 1, 0 ).crossSelf( dir );
  408. // var radians = Math.acos( new THREE.Vector3( 0, 1, 0 ).dot( dir.clone().normalize() ) );
  409. // var matrix = new THREE.Matrix4().setRotationAxis( axis.normalize(), radians );
  410. // camera.rotation.getRotationFromMatrix( matrix, camera.scale );
  411. }
  412. parent.rotation.y += (targetRotation - parent.rotation.y) * 0.05;
  413. renderer.render(scene, camera);
  414. }
  415. </script>
  416. </body>
  417. </html>