webgl_geometry_extrude_shapes.html 9.7 KB

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