webgl_geometry_extrude_shapes.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - geometry - extrude splines</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="../src/extras/geometries/ExtrudeGeometry.js"></script>
  22. <script src="js/Stats.js"></script>
  23. <script>
  24. var container, stats;
  25. var camera, scene, renderer;
  26. var text, plane;
  27. var targetRotation = 0;
  28. var targetRotationOnMouseDown = 0;
  29. var mouseX = 0;
  30. var mouseXOnMouseDown = 0;
  31. var windowHalfX = window.innerWidth / 2;
  32. var windowHalfY = window.innerHeight / 2;
  33. init();
  34. animate();
  35. function init() {
  36. container = document.createElement( 'div' );
  37. document.body.appendChild( container );
  38. var info = document.createElement( 'div' );
  39. info.style.position = 'absolute';
  40. info.style.top = '10px';
  41. info.style.width = '100%';
  42. info.style.textAlign = 'center';
  43. info.innerHTML = 'Simple procedurally generated 3D shapes example by <a href="http://www.lab4games.net/zz85/blog">zz85</a><br/>Drag to spin';
  44. container.appendChild( info );
  45. scene = new THREE.Scene();
  46. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
  47. camera.position.set( 0, 150, 500 );
  48. scene.add( camera );
  49. var light = new THREE.DirectionalLight( 0xffffff );
  50. light.position.set( 0, 0, 1 );
  51. scene.add( light );
  52. parent = new THREE.Object3D();
  53. parent.position.y = 50;
  54. scene.add( parent );
  55. function addGeometry( geometry, color, x, y, z, rx, ry, rz, s ) {
  56. // 3d shape
  57. var mesh = THREE.SceneUtils.createMultiMaterialObject( geometry, [ new THREE.MeshLambertMaterial( { color: color, opacity: 0.2, transparent: true } ), new THREE.MeshBasicMaterial( { color: 0x000000, wireframe: true, opacity: 0.3 } ) ] );
  58. mesh.position.set( x, y, z - 75 );
  59. // mesh.rotation.set( rx, ry, rz );
  60. mesh.scale.set( s, s, s );
  61. if (geometry.debug) mesh.add(geometry.debug);
  62. parent.add( mesh );
  63. }
  64. var extrudeSettings = { amount: 200, bevelEnabled: true, bevelSegments: 2, steps: 150 }; // bevelSegments: 2, steps: 2 , bevelSegments: 5, bevelSize: 8, bevelThickness:5,
  65. // var extrudePath = new THREE.Path();
  66. // extrudePath.moveTo( 0, 0 );
  67. // extrudePath.lineTo( 10, 10 );
  68. // extrudePath.quadraticCurveTo( 80, 60, 160, 10 );
  69. // extrudePath.quadraticCurveTo( 240, -40, 320, 10 );
  70. extrudeSettings.bevelEnabled = false;
  71. var extrudeBend = new THREE.SplineCurve3( //Closed
  72. [
  73. new THREE.Vector3( 30, 12, 83),
  74. new THREE.Vector3( 40, 20, 67),
  75. new THREE.Vector3( 60, 40, 99),
  76. new THREE.Vector3( 10, 60, 49),
  77. new THREE.Vector3( 25, 80, 40)
  78. // new THREE.Vector3( 0, 12, 83),
  79. // new THREE.Vector3( 0, 20, 67),
  80. // new THREE.Vector3( 0, 40, 99),
  81. // new THREE.Vector3( 0, 60, 49),
  82. // new THREE.Vector3( 0, 80, 40)
  83. // new THREE.Vector3( 12, 83, 0 ),
  84. // new THREE.Vector3( 20, 67, 0 ),
  85. // new THREE.Vector3( 40, 99, 0 ),
  86. // new THREE.Vector3( 60, 49, 0 ),
  87. // new THREE.Vector3( 80, 40, 0 )
  88. ]);
  89. var pipeSpline = new THREE.SplineCurve3([
  90. 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)]);
  91. var sampleClosedSpline = new THREE.ClosedSplineCurve3([
  92. new THREE.Vector3(0, -40, -40),
  93. new THREE.Vector3(0, 40, -40),
  94. new THREE.Vector3(0, 140, -40),
  95. new THREE.Vector3(0, 40, 40),
  96. new THREE.Vector3(0, -40, 40),
  97. ]);
  98. var randomPoints = [];
  99. for (var i=0; i<10;i++) {
  100. randomPoints.push(
  101. new THREE.Vector3(Math.random() * 200,Math.random() * 200,Math.random() * 200 )
  102. );
  103. }
  104. var randomSpline = new THREE.SplineCurve3(randomPoints);
  105. extrudeSettings.extrudePath = randomSpline; // extrudeBend sampleClosedSpline pipeSpline randomSpline
  106. // Circle
  107. var circleRadius = 4;
  108. var circleShape = new THREE.Shape();
  109. circleShape.moveTo( 0, circleRadius );
  110. circleShape.quadraticCurveTo( circleRadius, circleRadius, circleRadius, 0 );
  111. circleShape.quadraticCurveTo( circleRadius, -circleRadius, 0, -circleRadius );
  112. circleShape.quadraticCurveTo( -circleRadius, -circleRadius, -circleRadius, 0 );
  113. circleShape.quadraticCurveTo( -circleRadius, circleRadius, 0, circleRadius);
  114. var rectLength = 12, rectWidth = 4;
  115. var rectShape = new THREE.Shape();
  116. // rectShape.moveTo( 0,0 );
  117. // rectShape.lineTo( 0, rectWidth );
  118. // rectShape.lineTo( rectLength, rectWidth );
  119. // rectShape.lineTo( rectLength, 0 );
  120. // rectShape.lineTo( 0, 0 );
  121. rectShape.moveTo( -rectLength/2, -rectWidth/2 );
  122. rectShape.lineTo( -rectLength/2, rectWidth/2 );
  123. rectShape.lineTo( rectLength/2, rectWidth/2 );
  124. rectShape.lineTo( rectLength/2, -rectLength/2 );
  125. rectShape.lineTo( -rectLength/2, -rectLength/2 );
  126. // Smiley
  127. var smileyShape = new THREE.Shape();
  128. smileyShape.moveTo( 80, 40 );
  129. smileyShape.arc( 40, 40, 40, 0, Math.PI*2, false );
  130. var smileyEye1Path = new THREE.Path();
  131. smileyEye1Path.moveTo( 35, 20 );
  132. smileyEye1Path.arc( 25, 20, 10, 0, Math.PI*2, true );
  133. smileyShape.holes.push( smileyEye1Path );
  134. var smileyEye2Path = new THREE.Path();
  135. smileyEye2Path.moveTo( 65, 20 );
  136. smileyEye2Path.arc( 55, 20, 10, 0, Math.PI*2, true );
  137. smileyShape.holes.push( smileyEye2Path );
  138. var smileyMouthPath = new THREE.Path();
  139. // ugly box mouth
  140. // smileyMouthPath.moveTo( 20, 40 );
  141. // smileyMouthPath.lineTo( 60, 40 );
  142. // smileyMouthPath.lineTo( 60, 60 );
  143. // smileyMouthPath.lineTo( 20, 60 );
  144. // smileyMouthPath.lineTo( 20, 40 );
  145. smileyMouthPath.moveTo( 20, 40 );
  146. smileyMouthPath.quadraticCurveTo( 40, 60, 60, 40 );
  147. smileyMouthPath.bezierCurveTo( 70, 45, 70, 50, 60, 60 );
  148. smileyMouthPath.quadraticCurveTo( 40, 80, 20, 60 );
  149. smileyMouthPath.quadraticCurveTo( 5, 50, 20, 40 );
  150. smileyShape.holes.push( smileyMouthPath );
  151. var circle3d = rectShape.extrude( extrudeSettings ); //circleShape rectShape smileyShape
  152. // var circle3d = new THREE.ExtrudeGeometry(circleShape, extrudeBend, extrudeSettings );
  153. var tube = new THREE.TubeGeometry(extrudeSettings.extrudePath, 150, 4, 5, false, true);
  154. // new THREE.TubeGeometry(extrudePath, segments, 2, radiusSegments, closed2, debug);
  155. addGeometry( circle3d, 0xff1111, -100, 0, 0, 0, 0, 0, 1 );
  156. addGeometry( tube, 0x00ff11, 0, 0, 0, 0, 0, 0, 1 );
  157. console.log(tube);
  158. //
  159. renderer = new THREE.WebGLRenderer( { antialias: true } );
  160. renderer.setSize( window.innerWidth, window.innerHeight );
  161. container.appendChild( renderer.domElement );
  162. stats = new Stats();
  163. stats.domElement.style.position = 'absolute';
  164. stats.domElement.style.top = '0px';
  165. container.appendChild( stats.domElement );
  166. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  167. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  168. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  169. }
  170. //
  171. function onDocumentMouseDown( event ) {
  172. event.preventDefault();
  173. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  174. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  175. document.addEventListener( 'mouseout', onDocumentMouseOut, false );
  176. mouseXOnMouseDown = event.clientX - windowHalfX;
  177. targetRotationOnMouseDown = targetRotation;
  178. }
  179. function onDocumentMouseMove( event ) {
  180. mouseX = event.clientX - windowHalfX;
  181. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
  182. }
  183. function onDocumentMouseUp( event ) {
  184. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  185. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  186. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  187. }
  188. function onDocumentMouseOut( event ) {
  189. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  190. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  191. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  192. }
  193. function onDocumentTouchStart( event ) {
  194. if ( event.touches.length == 1 ) {
  195. event.preventDefault();
  196. mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
  197. targetRotationOnMouseDown = targetRotation;
  198. }
  199. }
  200. function onDocumentTouchMove( event ) {
  201. if ( event.touches.length == 1 ) {
  202. event.preventDefault();
  203. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  204. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
  205. }
  206. }
  207. //
  208. function animate() {
  209. requestAnimationFrame( animate );
  210. render();
  211. stats.update();
  212. }
  213. function render() {
  214. parent.rotation.y += ( targetRotation - parent.rotation.y ) * 0.05;
  215. renderer.render( scene, camera );
  216. }
  217. </script>
  218. </body>
  219. </html>�