webgl_geometry_extrude_splines.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  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="../src/extras/helpers/CameraHelper.js"></script>
  21. <script src="js/Stats.js"></script>
  22. <script>
  23. // Lets define some curves
  24. THREE.Curves = {};
  25. // Formula from http://mathworld.wolfram.com/HeartCurve.html
  26. THREE.Curves.HeartCurve = THREE.Curve.create(
  27. function ( s ) {
  28. this.scale = (s === undefined) ? 5 : s;
  29. },
  30. function ( t ) {
  31. t *= 2 * Math.PI;
  32. var tx = 16 * Math.pow(Math.sin(t), 3);
  33. ty = 13 * Math.cos(t)
  34. - 5 * Math.cos(2 * t)
  35. - 2 * Math.cos(3 * t)
  36. - Math.cos(4 * t ),
  37. tz = 0;
  38. return new THREE.Vector3(tx, ty, tz).multiplyScalar(this.scale);
  39. }
  40. );
  41. // Viviani's Curve
  42. // http://en.wikipedia.org/wiki/Viviani%27s_curve
  43. THREE.Curves.VivianiCurve = THREE.Curve.create(
  44. function( radius ) {
  45. this.radius = radius;
  46. },
  47. function( t ) {
  48. t = t * 4 * Math.PI; // Normalized to 0..1
  49. var a = this.radius / 2;
  50. var tx = a * (1 + Math.cos(t)),
  51. ty = a * Math.sin(t),
  52. tz = 2 * a * Math.sin(t / 2);
  53. return new THREE.Vector3(tx, ty, tz);
  54. }
  55. );
  56. THREE.Curves.KnotCurve = THREE.Curve.create(
  57. function() {
  58. },
  59. function(t) {
  60. t *= 2 * Math.PI;
  61. var R = 10;
  62. var s = 50;
  63. var tx = s * Math.sin(t),
  64. ty = Math.cos(t) * (R + s * Math.cos(t)),
  65. tz = Math.sin(t) * (R + s * Math.cos(t));
  66. return new THREE.Vector3(tx, ty, tz);
  67. }
  68. );
  69. THREE.Curves.HelixCurve = THREE.Curve.create(
  70. function() {
  71. },
  72. function(t) {
  73. var a = 30; // radius
  74. var b = 150; //height
  75. var t2 = 2 * Math.PI * t * b / 30;
  76. var tx = Math.cos(t2) * a,
  77. ty = Math.sin(t2) * a,
  78. tz = b * t;
  79. return new THREE.Vector3(tx, ty, tz);
  80. }
  81. );
  82. // Replacement for TorusKnotGeometry?
  83. THREE.Curves.TrefoilKnot = THREE.Curve.create(
  84. function(s) {
  85. this.scale = (s === undefined) ? 10 : s;
  86. },
  87. function(t) {
  88. t *= Math.PI * 2;
  89. var tx = (2 + Math.cos(3 * t)) * Math.cos(2 * t),
  90. ty = (2 + Math.cos(3* t)) * Math.sin(2 * t),
  91. tz = Math.sin(3 * t);
  92. return new THREE.Vector3(tx, ty, tz).multiplyScalar(this.scale);
  93. }
  94. );
  95. // Formulas from http://mathdl.maa.org/images/upload_library/23/stemkoski/knots/page6.html
  96. THREE.Curves.TorusKnot = THREE.Curve.create(
  97. function(s) {
  98. this.scale = (s === undefined) ? 10 : s;
  99. },
  100. function(t) {
  101. var p = 3, q = 4;
  102. t *= Math.PI * 2;
  103. var tx = (2 + Math.cos(q * t)) * Math.cos(p * t),
  104. ty = (2 + Math.cos(q* t)) * Math.sin(p * t),
  105. tz = Math.sin(q * t);
  106. return new THREE.Vector3(tx, ty, tz).multiplyScalar(this.scale);
  107. }
  108. );
  109. THREE.Curves.CinquefoilKnot = THREE.Curve.create(
  110. function(s) {
  111. this.scale = (s === undefined) ? 10 : s;
  112. },
  113. function(t) {
  114. var p = 2, q = 5;
  115. t *= Math.PI * 2;
  116. var tx = (2 + Math.cos(q * t)) * Math.cos(p * t),
  117. ty = (2 + Math.cos(q* t)) * Math.sin(p * t),
  118. tz = Math.sin(q * t);
  119. return new THREE.Vector3(tx, ty, tz).multiplyScalar(this.scale);
  120. }
  121. );
  122. THREE.Curves.TrefoilPolynomialKnot = THREE.Curve.create(
  123. function(s) {
  124. this.scale = (s === undefined) ? 10 : s;
  125. },
  126. function(t) {
  127. t = t * 4 - 2;
  128. var tx = Math.pow(t, 3) - 3 * t,
  129. ty = Math.pow(t, 4) - 4 * t * t,
  130. tz = 1/ 5 * Math.pow(t, 5) - 2 * t;
  131. return new THREE.Vector3(tx, ty, tz).multiplyScalar(this.scale);
  132. }
  133. );
  134. var sin = Math.sin, pow = Math.pow, cos = Math.cos;
  135. // var scaleTo = function(x, y) {
  136. // var r = y - x;
  137. // return function(t) {
  138. // t * r + x;
  139. // };
  140. // }
  141. var scaleTo = function(x, y, t) {
  142. var r = y - x;
  143. return t * r + x;
  144. }
  145. THREE.Curves.FigureEightPolynomialKnot = THREE.Curve.create(
  146. function(s) {
  147. this.scale = (s === undefined) ? 1 : s;
  148. },
  149. function(t) {
  150. t = scaleTo(-4,4, t);
  151. var tx = 2 / 5 * t * (t * t - 7) * (t * t - 10),
  152. ty = pow(t, 4) - 13 * t * t,
  153. tz = 1/10 * t * (t * t - 4) * (t * t - 9) * (t * t - 12);
  154. return new THREE.Vector3(tx, ty, tz).multiplyScalar(this.scale);
  155. }
  156. );
  157. // When there's time, try more formulas at http://mathdl.maa.org/images/upload_library/23/stemkoski/knots/page4.html
  158. //http://www.mi.sanu.ac.rs/vismath/taylorapril2011/Taylor.pdf
  159. THREE.Curves.DecoratedTorusKnot4a = THREE.Curve.create(
  160. function(s) {
  161. this.scale = (s === undefined) ? 40 : s;
  162. },
  163. function(t) {
  164. t *= Math.PI * 2;
  165. var
  166. x = cos(2*t) * (1+0.6*(cos(5*t) + 0.75*cos(10*t))),
  167. y = sin(2*t) * (1+0.6*(cos(5*t) + 0.75*cos(10*t))),
  168. z = 0.35*sin(5*t);
  169. return new THREE.Vector3(x, y, z).multiplyScalar(this.scale);
  170. }
  171. );
  172. THREE.Curves.DecoratedTorusKnot4b = THREE.Curve.create(
  173. function(s) {
  174. this.scale = (s === undefined) ? 40 : s;
  175. },
  176. function(t) {
  177. var fi = t * Math.PI * 2;
  178. var x = cos(2*fi) * (1 + 0.45*cos(3*fi) + 0.4*cos(9*fi)),
  179. y = sin(2*fi) * (1 + 0.45*cos(3*fi) + 0.4*cos(9*fi)),
  180. z = 0.2*sin(9*fi);
  181. return new THREE.Vector3(x, y, z).multiplyScalar(this.scale);
  182. }
  183. );
  184. THREE.Curves.DecoratedTorusKnot5a = THREE.Curve.create(
  185. function(s) {
  186. this.scale = (s === undefined) ? 40 : s;
  187. },
  188. function(t) {
  189. var fi = t * Math.PI * 2;
  190. var x = cos(3*fi) * (1 + 0.3*cos(5*fi) + 0.5*cos(10*fi)),
  191. y = sin(3*fi) * (1 + 0.3*cos(5*fi) + 0.5*cos(10*fi)),
  192. z = 0.2*sin(20*fi);
  193. return new THREE.Vector3(x, y, z).multiplyScalar(this.scale);
  194. }
  195. );
  196. THREE.Curves.DecoratedTorusKnot5c = THREE.Curve.create(
  197. function(s) {
  198. this.scale = (s === undefined) ? 40 : s;
  199. },
  200. function(t) {
  201. var fi = t * Math.PI * 2;
  202. var x = cos(4*fi) * (1 + 0.5*(cos(5*fi) + 0.4*cos(20*fi))),
  203. y = sin(4*fi) * (1 + 0.5*(cos(5*fi) + 0.4*cos(20*fi))),
  204. z = 0.35*sin(15*fi);
  205. return new THREE.Vector3(x, y, z).multiplyScalar(this.scale);
  206. }
  207. );
  208. var container, stats;
  209. var camera, scene, renderer, splineCamera, cameraHelper, cameraPos;
  210. var text, plane;
  211. var targetRotation = 0;
  212. var targetRotationOnMouseDown = 0;
  213. var mouseX = 0;
  214. var mouseXOnMouseDown = 0;
  215. var windowHalfX = window.innerWidth / 2;
  216. var windowHalfY = window.innerHeight / 2;
  217. var binormal = new THREE.Vector3();
  218. var normal = new THREE.Vector3();
  219. var pipeSpline = new THREE.SplineCurve3([
  220. 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)]);
  221. var sampleClosedSpline = new THREE.ClosedSplineCurve3([
  222. new THREE.Vector3(0, -40, -40),
  223. new THREE.Vector3(0, 40, -40),
  224. new THREE.Vector3(0, 140, -40),
  225. new THREE.Vector3(0, 40, 40),
  226. new THREE.Vector3(0, -40, 40),
  227. ]);
  228. var splines = {
  229. HeartCurve: new THREE.Curves.HeartCurve(3.5),
  230. VivianiCurve: new THREE.Curves.VivianiCurve(70),
  231. KnotCurve: new THREE.Curves.KnotCurve(),
  232. HelixCurve: new THREE.Curves.HelixCurve(),
  233. TrefoilKnot: new THREE.Curves.TrefoilKnot(),
  234. TorusKnot: new THREE.Curves.TorusKnot(20),
  235. CinquefoilKnot: new THREE.Curves.CinquefoilKnot(20),
  236. TrefoilPolynomialKnot: new THREE.Curves.TrefoilPolynomialKnot(14),
  237. FigureEightPolynomialKnot: new THREE.Curves.FigureEightPolynomialKnot(),
  238. DecoratedTorusKnot4a: new THREE.Curves.DecoratedTorusKnot4a(),
  239. DecoratedTorusKnot4b: new THREE.Curves.DecoratedTorusKnot4b(),
  240. DecoratedTorusKnot5a: new THREE.Curves.DecoratedTorusKnot5a(),
  241. DecoratedTorusKnot5c: new THREE.Curves.DecoratedTorusKnot5c(),
  242. PipeSpline: pipeSpline,
  243. SampleClosedSpline: sampleClosedSpline
  244. };
  245. extrudePath = new THREE.Curves.TrefoilKnot();
  246. var dropdown = '<select id="dropdown" onchange="addTube(this.value)">';
  247. var s;
  248. for ( s in splines ) {
  249. dropdown += '<option value="' + s + '"';
  250. // dropdown += (geometryIndex == i) ? ' selected' : '';
  251. dropdown += '>' + s + '</option>';
  252. }
  253. dropdown += '</select>';
  254. var closed2 = true;
  255. var debug = true;
  256. var parent;
  257. var tube, tubeMesh;
  258. var animation = false, lookAhead = false;
  259. var scale;
  260. function addTube() {
  261. var value = document.getElementById('dropdown').value;
  262. var segments = parseInt(document.getElementById('segments').value);
  263. closed2 = document.getElementById('closed').checked;
  264. debug = document.getElementById('debug').checked;
  265. var radiusSegments = parseInt(document.getElementById('radiusSegments').value);
  266. console.log('adding tube', value, closed2, debug, radiusSegments);
  267. if (tubeMesh) parent.remove(tubeMesh);
  268. extrudePath = splines[value];
  269. tube = new THREE.TubeGeometry(extrudePath, segments, 2, radiusSegments, closed2, debug);
  270. addGeometry(tube, 0xff00ff);
  271. setScale();
  272. }
  273. function setScale() {
  274. scale = parseInt(document.getElementById('scale').value);
  275. tubeMesh.scale.set(scale, scale, scale);
  276. }
  277. function addGeometry(geometry, color) {
  278. // 3d shape
  279. tubeMesh = THREE.SceneUtils.createMultiMaterialObject(geometry, [
  280. new THREE.MeshLambertMaterial({
  281. color: color,
  282. opacity: 0.2,
  283. transparent: true
  284. }),
  285. new THREE.MeshBasicMaterial({
  286. color: 0x000000,
  287. opacity: 0.3,
  288. wireframe: true
  289. })]);
  290. if (geometry.debug) tubeMesh.add(geometry.debug);
  291. // tubeMesh.position.set(x, y, z);
  292. // tubeMesh.rotation.set(rx, ry, rz);
  293. //mesh.children[0].doubleSided = true;
  294. parent.add(tubeMesh);
  295. }
  296. function animateCamera() {
  297. animation = document.getElementById('animation').checked;
  298. lookAhead = document.getElementById('lookAhead').checked;
  299. }
  300. init();
  301. animate();
  302. function init() {
  303. container = document.createElement('div');
  304. document.body.appendChild(container);
  305. var info = document.createElement('div');
  306. info.style.position = 'absolute';
  307. info.style.top = '10px';
  308. info.style.width = '100%';
  309. info.style.textAlign = 'center';
  310. info.innerHTML = 'Spline Extrusion Examples by <a href="http://www.lab4games.net/zz85/blog">zz85</a><br/>Select spline:';
  311. info.innerHTML += dropdown;
  312. 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>';
  313. info.innerHTML += '<br/>Extrusion Segments: <select onchange="addTube()" id="segments"><option>50</option><option selected>100</option><option>200</option><option>400</option></select>';
  314. info.innerHTML += '<br/>Debug: <input id="debug" type="checkbox" onchange="addTube()" checked /> Closed:<input id="closed" onchange="addTube()" type="checkbox" checked />';
  315. 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><option>6</option><option>8</option><option>12</option></select>';
  316. info.innerHTML += '<br/>Camera Spline Animation: <input id="animation" type="checkbox" onchange="animateCamera()" /> Look Ahead <input id="lookAhead" type="checkbox" onchange="animateCamera()" />'; //checkbox button
  317. container.appendChild(info);
  318. scene = new THREE.Scene();
  319. //
  320. camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.01, 1000);
  321. splineCamera = new THREE.PerspectiveCamera(84, window.innerWidth / window.innerHeight, 0.01, 1000);
  322. cameraHelper = new THREE.CameraHelper(splineCamera);
  323. camera.position.set(0, 50, 500);
  324. scene.add(camera);
  325. var light = new THREE.DirectionalLight(0xffffff);
  326. light.position.set(0, 0, 1);
  327. scene.add(light);
  328. parent = new THREE.Object3D();
  329. parent.position.y = 100;
  330. scene.add(parent);
  331. addTube();
  332. // Debug point
  333. cameraPos = new THREE.Mesh(new THREE.SphereGeometry(5), new THREE.MeshBasicMaterial({
  334. color: 0xdddddd
  335. }));
  336. parent.add(cameraPos);
  337. cameraHelper.scale.multiplyScalar(0.1);
  338. splineCamera.add(cameraHelper);
  339. parent.add(splineCamera);
  340. //
  341. renderer = new THREE.WebGLRenderer({
  342. antialias: true
  343. });
  344. renderer.setSize(window.innerWidth, window.innerHeight);
  345. container.appendChild(renderer.domElement);
  346. stats = new Stats();
  347. stats.domElement.style.position = 'absolute';
  348. stats.domElement.style.top = '0px';
  349. container.appendChild(stats.domElement);
  350. renderer.domElement.addEventListener('mousedown', onDocumentMouseDown, false);
  351. renderer.domElement.addEventListener('touchstart', onDocumentTouchStart, false);
  352. renderer.domElement.addEventListener('touchmove', onDocumentTouchMove, false);
  353. }
  354. //
  355. function onDocumentMouseDown(event) {
  356. event.preventDefault();
  357. renderer.domElement.addEventListener('mousemove', onDocumentMouseMove, false);
  358. renderer.domElement.addEventListener('mouseup', onDocumentMouseUp, false);
  359. renderer.domElement.addEventListener('mouseout', onDocumentMouseOut, false);
  360. mouseXOnMouseDown = event.clientX - windowHalfX;
  361. targetRotationOnMouseDown = targetRotation;
  362. }
  363. function onDocumentMouseMove(event) {
  364. mouseX = event.clientX - windowHalfX;
  365. targetRotation = targetRotationOnMouseDown + (mouseX - mouseXOnMouseDown) * 0.02;
  366. }
  367. function onDocumentMouseUp(event) {
  368. renderer.domElement.removeEventListener('mousemove', onDocumentMouseMove, false);
  369. renderer.domElement.removeEventListener('mouseup', onDocumentMouseUp, false);
  370. renderer.domElement.removeEventListener('mouseout', onDocumentMouseOut, false);
  371. }
  372. function onDocumentMouseOut(event) {
  373. renderer.domElement.removeEventListener('mousemove', onDocumentMouseMove, false);
  374. renderer.domElement.removeEventListener('mouseup', onDocumentMouseUp, false);
  375. renderer.domElement.removeEventListener('mouseout', onDocumentMouseOut, false);
  376. }
  377. function onDocumentTouchStart(event) {
  378. if (event.touches.length == 1) {
  379. event.preventDefault();
  380. mouseXOnMouseDown = event.touches[0].pageX - windowHalfX;
  381. targetRotationOnMouseDown = targetRotation;
  382. }
  383. }
  384. function onDocumentTouchMove(event) {
  385. if (event.touches.length == 1) {
  386. event.preventDefault();
  387. mouseX = event.touches[0].pageX - windowHalfX;
  388. targetRotation = targetRotationOnMouseDown + (mouseX - mouseXOnMouseDown) * 0.05;
  389. }
  390. }
  391. //
  392. function animate() {
  393. requestAnimationFrame(animate);
  394. render();
  395. stats.update();
  396. }
  397. function render() {
  398. // Try Animate Camera Along Spline
  399. var time = Date.now();
  400. var looptime = 20 * 1000;
  401. var t = (time % looptime) / looptime;
  402. var pos = tube.path.getPointAt(t);
  403. pos.multiplyScalar(scale);
  404. // interpolation
  405. var segments = tube.tangents.length;
  406. var pickt = t * segments;
  407. var pick = Math.floor(pickt);
  408. var pickNext = (pick + 1) % segments;
  409. binormal.sub(tube.binormals[pickNext], tube.binormals[pick]);
  410. binormal.multiplyScalar(pickt - pick).addSelf(tube.binormals[pick]);
  411. var dir = tube.path.getTangentAt(t);
  412. var offset = 15;
  413. normal.copy(binormal).crossSelf(dir);
  414. // We move on a offset on its binormal
  415. pos.addSelf(normal.clone().multiplyScalar(offset));
  416. splineCamera.position = pos;
  417. cameraPos.position = pos;
  418. // Camera Orientation 1 - default look at
  419. var lookAt = tube.path.getPointAt((t + 0.101) % 1).multiplyScalar(scale);
  420. // splineCamera.lookAt(lookAt);
  421. // Camera Orientation 2 - up orientation via normal
  422. if (!lookAhead)
  423. lookAt.copy(pos).addSelf(dir);
  424. splineCamera.matrix.lookAt(splineCamera.position, lookAt, normal);
  425. splineCamera.rotation.getRotationFromMatrix(splineCamera.matrix);
  426. cameraHelper.update();
  427. parent.rotation.y += (targetRotation - parent.rotation.y) * 0.05;
  428. if (animation) {
  429. renderer.render(scene, splineCamera);
  430. } else {
  431. renderer.render(scene, camera);
  432. }
  433. }
  434. </script>
  435. </body>
  436. </html>