webgl_geometry_extrudePath.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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, points, spacedPoints, color, x, y, z, rx, ry, rz, s ) {
  56. // 3d shape
  57. var mesh = THREE.SceneUtils.createMultiMaterialObject( geometry, [ new THREE.MeshLambertMaterial( { color: color } ), new THREE.MeshBasicMaterial( { color: 0x000000, wireframe: true, transparent: true } ) ] );
  58. mesh.position.set( x, y, z - 75 );
  59. mesh.rotation.set( rx, ry, rz );
  60. mesh.scale.set( s, s, s );
  61. parent.add( mesh );
  62. // solid line
  63. var line = new THREE.Line( points, new THREE.LineBasicMaterial( { color: color, linewidth: 2 } ) );
  64. line.position.set( x, y, z + 25 );
  65. line.rotation.set( rx, ry, rz );
  66. line.scale.set( s, s, s );
  67. parent.add( line );
  68. // transparent line from real points
  69. var line = new THREE.Line( points, new THREE.LineBasicMaterial( { color: color, opacity: 0.5 } ) );
  70. line.position.set( x, y, z + 75 );
  71. line.rotation.set( rx, ry, rz );
  72. line.scale.set( s, s, s );
  73. parent.add( line );
  74. // vertices from real points
  75. var pgeo = THREE.GeometryUtils.clone( points );
  76. var particles = new THREE.ParticleSystem( pgeo, new THREE.ParticleBasicMaterial( { color: color, size: 2, opacity: 0.75 } ) );
  77. particles.position.set( x, y, z + 75 );
  78. particles.rotation.set( rx, ry, rz );
  79. particles.scale.set( s, s, s );
  80. parent.add( particles );
  81. // transparent line from equidistance sampled points
  82. var line = new THREE.Line( spacedPoints, new THREE.LineBasicMaterial( { color: color, opacity: 0.2 } ) );
  83. line.position.set( x, y, z + 100 );
  84. line.rotation.set( rx, ry, rz );
  85. line.scale.set( s, s, s );
  86. parent.add( line );
  87. // equidistance sampled points
  88. var pgeo = THREE.GeometryUtils.clone( spacedPoints );
  89. var particles2 = new THREE.ParticleSystem( pgeo, new THREE.ParticleBasicMaterial( { color: color, size: 2, opacity: 0.5 } ) );
  90. particles2.position.set( x, y, z + 100 );
  91. particles2.rotation.set( rx, ry, rz );
  92. particles2.scale.set( s, s, s );
  93. parent.add( particles2 );
  94. }
  95. var extrudeSettings = { amount: 200, bevelEnabled: true, bevelSegments: 2, steps: 150 }; // bevelSegments: 2, steps: 2 , bevelSegments: 5, bevelSize: 8, bevelThickness:5,
  96. // var extrudePath = new THREE.Path();
  97. // extrudePath.moveTo( 0, 0 );
  98. // extrudePath.lineTo( 10, 10 );
  99. // extrudePath.quadraticCurveTo( 80, 60, 160, 10 );
  100. // extrudePath.quadraticCurveTo( 240, -40, 320, 10 );
  101. extrudeSettings.bevelEnabled = false;
  102. var extrudeBend = new THREE.SplineCurve3( //Closed
  103. [
  104. new THREE.Vector3( 30, 12, 83),
  105. new THREE.Vector3( 40, 20, 67),
  106. new THREE.Vector3( 60, 40, 99),
  107. new THREE.Vector3( 10, 60, 49),
  108. new THREE.Vector3( 25, 80, 40)
  109. // new THREE.Vector3( 0, 12, 83),
  110. // new THREE.Vector3( 0, 20, 67),
  111. // new THREE.Vector3( 0, 40, 99),
  112. // new THREE.Vector3( 0, 60, 49),
  113. // new THREE.Vector3( 0, 80, 40)
  114. // new THREE.Vector3( 12, 83, 0 ),
  115. // new THREE.Vector3( 20, 67, 0 ),
  116. // new THREE.Vector3( 40, 99, 0 ),
  117. // new THREE.Vector3( 60, 49, 0 ),
  118. // new THREE.Vector3( 80, 40, 0 )
  119. ]);
  120. var pipeSpline = new THREE.SplineCurve3([
  121. 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)]);
  122. var sampleClosedSpline = new THREE.ClosedSplineCurve3([
  123. new THREE.Vector3(0, -40, -40),
  124. new THREE.Vector3(0, 40, -40),
  125. new THREE.Vector3(0, 140, -40),
  126. new THREE.Vector3(0, 40, 40),
  127. new THREE.Vector3(0, -40, 40),
  128. ]);
  129. var randomPoints = [];
  130. for (var i=0; i<10;i++) {
  131. randomPoints.push(
  132. new THREE.Vector3(Math.random() * 200,Math.random() * 200,Math.random() * 200 )
  133. );
  134. }
  135. var randomSpline = new THREE.SplineCurve3(randomPoints);
  136. extrudeSettings.extrudePath = randomSpline; // extrudeBend sampleClosedSpline pipeSpline
  137. // Circle
  138. var circleRadius = 4;
  139. var circleShape = new THREE.Shape();
  140. circleShape.moveTo( 0, circleRadius );
  141. circleShape.quadraticCurveTo( circleRadius, circleRadius, circleRadius, 0 );
  142. circleShape.quadraticCurveTo( circleRadius, -circleRadius, 0, -circleRadius );
  143. circleShape.quadraticCurveTo( -circleRadius, -circleRadius, -circleRadius, 0 );
  144. circleShape.quadraticCurveTo( -circleRadius, circleRadius, 0, circleRadius);
  145. var rectLength = 12, rectWidth = 4;
  146. var rectShape = new THREE.Shape();
  147. // rectShape.moveTo( 0,0 );
  148. // rectShape.lineTo( 0, rectWidth );
  149. // rectShape.lineTo( rectLength, rectWidth );
  150. // rectShape.lineTo( rectLength, 0 );
  151. // rectShape.lineTo( 0, 0 );
  152. rectShape.moveTo( -rectLength/2, -rectWidth/2 );
  153. rectShape.lineTo( -rectLength/2, rectWidth/2 );
  154. rectShape.lineTo( rectLength/2, rectWidth/2 );
  155. rectShape.lineTo( rectLength/2, -rectLength/2 );
  156. rectShape.lineTo( -rectLength/2, -rectLength/2 );
  157. // Smiley
  158. var smileyShape = new THREE.Shape();
  159. smileyShape.moveTo( 80, 40 );
  160. smileyShape.arc( 40, 40, 40, 0, Math.PI*2, false );
  161. var smileyEye1Path = new THREE.Path();
  162. smileyEye1Path.moveTo( 35, 20 );
  163. smileyEye1Path.arc( 25, 20, 10, 0, Math.PI*2, true );
  164. smileyShape.holes.push( smileyEye1Path );
  165. var smileyEye2Path = new THREE.Path();
  166. smileyEye2Path.moveTo( 65, 20 );
  167. smileyEye2Path.arc( 55, 20, 10, 0, Math.PI*2, true );
  168. smileyShape.holes.push( smileyEye2Path );
  169. var smileyMouthPath = new THREE.Path();
  170. // ugly box mouth
  171. // smileyMouthPath.moveTo( 20, 40 );
  172. // smileyMouthPath.lineTo( 60, 40 );
  173. // smileyMouthPath.lineTo( 60, 60 );
  174. // smileyMouthPath.lineTo( 20, 60 );
  175. // smileyMouthPath.lineTo( 20, 40 );
  176. smileyMouthPath.moveTo( 20, 40 );
  177. smileyMouthPath.quadraticCurveTo( 40, 60, 60, 40 );
  178. smileyMouthPath.bezierCurveTo( 70, 45, 70, 50, 60, 60 );
  179. smileyMouthPath.quadraticCurveTo( 40, 80, 20, 60 );
  180. smileyMouthPath.quadraticCurveTo( 5, 50, 20, 40 );
  181. smileyShape.holes.push( smileyMouthPath );
  182. var circle3d = rectShape.extrude( extrudeSettings ); //circleShape rectShape smileyShape
  183. // var circle3d = new THREE.ExtrudeGeometry(circleShape, extrudeBend, extrudeSettings );
  184. var circlePoints = circleShape.createPointsGeometry();
  185. var circleSpacedPoints = circleShape.createSpacedPointsGeometry();
  186. addGeometry( circle3d, circlePoints, circleSpacedPoints, 0x00ff11, 0, 0, 0, 0, 0, 0, 1 );
  187. //
  188. renderer = new THREE.WebGLRenderer( { antialias: true } );
  189. renderer.setSize( window.innerWidth, window.innerHeight );
  190. container.appendChild( renderer.domElement );
  191. stats = new Stats();
  192. stats.domElement.style.position = 'absolute';
  193. stats.domElement.style.top = '0px';
  194. container.appendChild( stats.domElement );
  195. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  196. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  197. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  198. }
  199. //
  200. function onDocumentMouseDown( event ) {
  201. event.preventDefault();
  202. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  203. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  204. document.addEventListener( 'mouseout', onDocumentMouseOut, false );
  205. mouseXOnMouseDown = event.clientX - windowHalfX;
  206. targetRotationOnMouseDown = targetRotation;
  207. }
  208. function onDocumentMouseMove( event ) {
  209. mouseX = event.clientX - windowHalfX;
  210. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
  211. }
  212. function onDocumentMouseUp( event ) {
  213. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  214. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  215. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  216. }
  217. function onDocumentMouseOut( event ) {
  218. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  219. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  220. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  221. }
  222. function onDocumentTouchStart( event ) {
  223. if ( event.touches.length == 1 ) {
  224. event.preventDefault();
  225. mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
  226. targetRotationOnMouseDown = targetRotation;
  227. }
  228. }
  229. function onDocumentTouchMove( event ) {
  230. if ( event.touches.length == 1 ) {
  231. event.preventDefault();
  232. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  233. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
  234. }
  235. }
  236. //
  237. function animate() {
  238. requestAnimationFrame( animate );
  239. render();
  240. stats.update();
  241. }
  242. function render() {
  243. parent.rotation.y += ( targetRotation - parent.rotation.y ) * 0.05;
  244. renderer.render( scene, camera );
  245. }
  246. </script>
  247. </body>
  248. </html>