webgl_geometry_extrude_shapes.html 9.5 KB

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