webgl_geometry_extrude_splines.html 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. function ( t ) {
  58. t *= 2 * Math.PI;
  59. var R = 10;
  60. var s = 50;
  61. var tx = s * Math.sin( t ),
  62. ty = Math.cos( t ) * ( R + s * Math.cos( t ) ),
  63. tz = Math.sin( t ) * ( R + s * Math.cos( t ) );
  64. return new THREE.Vector3( tx, ty, tz );
  65. }
  66. );
  67. THREE.HelixCurve = THREE.Curve.create(
  68. function () {},
  69. function ( t ) {
  70. var a = 30; // radius
  71. var b = 150; //height
  72. var t2 = 2 * Math.PI * t * b / 30;
  73. var tx = Math.cos( t2 ) * a,
  74. ty = Math.sin( t2 ) * a,
  75. tz = b * t;
  76. return new THREE.Vector3( tx, ty, tz );
  77. }
  78. );
  79. var container, stats;
  80. var camera, scene, renderer;
  81. var text, plane;
  82. var targetRotation = 0;
  83. var targetRotationOnMouseDown = 0;
  84. var mouseX = 0;
  85. var mouseXOnMouseDown = 0;
  86. var windowHalfX = window.innerWidth / 2;
  87. var windowHalfY = window.innerHeight / 2;
  88. init();
  89. animate();
  90. function init() {
  91. container = document.createElement( 'div' );
  92. document.body.appendChild( container );
  93. var info = document.createElement('div');
  94. info.style.position = 'absolute';
  95. info.style.top = '10px';
  96. info.style.width = '100%';
  97. info.style.textAlign = 'center';
  98. info.innerHTML = 'Simple procedurally generated 3D shapes example by <a href="http://www.lab4games.net/zz85/blog">zz85</a><br/>Drag to spin';
  99. container.appendChild(info);
  100. scene = new THREE.Scene();
  101. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
  102. camera.position.set( 0, 50, 500 );
  103. scene.add( camera );
  104. var light = new THREE.DirectionalLight( 0xffffff );
  105. light.position.set( 0, 0, 1 );
  106. scene.add( light );
  107. parent = new THREE.Object3D();
  108. parent.position.y = 130;
  109. scene.add( parent );
  110. function addGeometry( geometry, color, x, y, z, rx, ry, rz, s ) {
  111. // 3d shape
  112. var mesh = THREE.SceneUtils.createMultiMaterialObject( geometry, [
  113. new THREE.MeshLambertMaterial( { color: color }),
  114. new THREE.MeshBasicMaterial( { color: 0x000000, wireframe: true, transparent: true } )
  115. ]);
  116. if ( geometry.debug ) mesh.add( geometry.debug );
  117. mesh.position.set( x, y, z - 75 );
  118. mesh.rotation.set( rx, ry, rz );
  119. mesh.scale.set( s, s, s );
  120. parent.add( mesh );
  121. }
  122. // var extrudePath = new THREE.SplineCurve3([
  123. // 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),
  124. // 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)]);
  125. var extrudePath = new THREE.HeartCurve( 3.5 );
  126. // var extrudePath = new THREE.VivianiCurve(70);
  127. // var extrudePath = new THREE.KnotCurve();
  128. // extrudePath = new THREE.HelixCurve();
  129. // var extrudePath = new THREE.ClosedSplineCurve3([
  130. // new THREE.Vector3(0, -40, -40),
  131. // new THREE.Vector3(0, 40, -40),
  132. // new THREE.Vector3(0, 140, -40),
  133. // new THREE.Vector3(0, 40, 40),
  134. // new THREE.Vector3(0, -40, 40),
  135. // ]);
  136. var tube = new THREE.TubeGeometry( 5, 100, 10, extrudePath, true );
  137. addGeometry( tube, 0xff1100, 0, -80, 0, 0, 0, 0, 3 );
  138. //
  139. renderer = new THREE.WebGLRenderer( { antialias: true } );
  140. renderer.setSize( window.innerWidth, window.innerHeight );
  141. container.appendChild( renderer.domElement );
  142. stats = new Stats();
  143. stats.domElement.style.position = 'absolute';
  144. stats.domElement.style.top = '0px';
  145. container.appendChild( stats.domElement );
  146. document.addEventListener('mousedown', onDocumentMouseDown, false);
  147. document.addEventListener('touchstart', onDocumentTouchStart, false);
  148. document.addEventListener('touchmove', onDocumentTouchMove, false);
  149. }
  150. //
  151. function onDocumentMouseDown( event ) {
  152. event.preventDefault();
  153. document.addEventListener('mousemove', onDocumentMouseMove, false);
  154. document.addEventListener('mouseup', onDocumentMouseUp, false);
  155. document.addEventListener('mouseout', onDocumentMouseOut, false);
  156. mouseXOnMouseDown = event.clientX - windowHalfX;
  157. targetRotationOnMouseDown = targetRotation;
  158. }
  159. function onDocumentMouseMove( event ) {
  160. mouseX = event.clientX - windowHalfX;
  161. targetRotation = targetRotationOnMouseDown + (mouseX - mouseXOnMouseDown) * 0.02;
  162. }
  163. function onDocumentMouseUp( event ) {
  164. document.removeEventListener('mousemove', onDocumentMouseMove, false);
  165. document.removeEventListener('mouseup', onDocumentMouseUp, false);
  166. document.removeEventListener('mouseout', onDocumentMouseOut, false);
  167. }
  168. function onDocumentMouseOut( event ) {
  169. document.removeEventListener('mousemove', onDocumentMouseMove, false);
  170. document.removeEventListener('mouseup', onDocumentMouseUp, false);
  171. document.removeEventListener('mouseout', onDocumentMouseOut, false);
  172. }
  173. function onDocumentTouchStart( event ) {
  174. if ( event.touches.length == 1 ) {
  175. event.preventDefault();
  176. mouseXOnMouseDown = event.touches[0].pageX - windowHalfX;
  177. targetRotationOnMouseDown = targetRotation;
  178. }
  179. }
  180. function onDocumentTouchMove( event ) {
  181. if ( event.touches.length == 1 ) {
  182. event.preventDefault();
  183. mouseX = event.touches[0].pageX - windowHalfX;
  184. targetRotation = targetRotationOnMouseDown + (mouseX - mouseXOnMouseDown) * 0.05;
  185. }
  186. }
  187. //
  188. function animate() {
  189. requestAnimationFrame(animate);
  190. render();
  191. stats.update();
  192. }
  193. function render() {
  194. parent.rotation.y += ( targetRotation - parent.rotation.y ) * 0.05;
  195. renderer.render( scene, camera );
  196. }
  197. </script>
  198. </body>
  199. </html>