canvas_geometry_shape.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js canvas - geometry - shapes</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/>
  7. <style type="text/css">
  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 type="text/javascript" src="../build/Three.js"></script>
  19. <script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
  20. <script type="text/javascript" src="js/Stats.js"></script>
  21. <script type="text/javascript">
  22. var container, stats;
  23. var camera, scene, renderer;
  24. var text, plane;
  25. var targetRotation = 0;
  26. var targetRotationOnMouseDown = 0;
  27. var mouseX = 0;
  28. var mouseXOnMouseDown = 0;
  29. var windowHalfX = window.innerWidth / 2;
  30. var windowHalfY = window.innerHeight / 2;
  31. init();
  32. animate();
  33. function init() {
  34. container = document.createElement( 'div' );
  35. document.body.appendChild( container );
  36. var info = document.createElement( 'div' );
  37. info.style.position = 'absolute';
  38. info.style.top = '10px';
  39. info.style.width = '100%';
  40. info.style.textAlign = 'center';
  41. info.innerHTML = 'Simple procedurally generated 3D shapes example by <a href="http://www.lab4games.net/zz85/blog">zz85</a><br/>Drag to spin';
  42. container.appendChild( info );
  43. camera = new THREE.Camera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
  44. camera.position.y = 150;
  45. camera.position.z = 500;
  46. camera.target.position.y = 150;
  47. scene = new THREE.Scene();
  48. parent = new THREE.Object3D();
  49. parent.position.y = 50;
  50. scene.addChild( parent );
  51. function addGeometry( geometry, color, x, y, z, rx, ry, rz, s ) {
  52. var mesh = new THREE.Mesh( geometry, [ new THREE.MeshBasicMaterial( { color: color } ), new THREE.MeshBasicMaterial( { color: 0x000000, wireframe: true } ) ] );
  53. mesh.position.set( x, y, z );
  54. mesh.rotation.set( rx, ry, rz );
  55. mesh.scale.set( s, s, s );
  56. parent.addChild( mesh );
  57. }
  58. var extrudeSettings = { amount: 20, bevelEnabled: true, bevelSegments: 2, steps: 2 }; //amount: 40, steps: 2
  59. // California
  60. var californiaPts = [];
  61. californiaPts.push( new THREE.Vector2 ( 610, 320 ) );
  62. californiaPts.push( new THREE.Vector2 ( 450, 300 ) );
  63. californiaPts.push( new THREE.Vector2 ( 392, 392 ) );
  64. californiaPts.push( new THREE.Vector2 ( 266, 438 ) );
  65. californiaPts.push( new THREE.Vector2 ( 190, 570 ) );
  66. californiaPts.push( new THREE.Vector2 ( 190, 600 ) );
  67. californiaPts.push( new THREE.Vector2 ( 160, 620 ) );
  68. californiaPts.push( new THREE.Vector2 ( 160, 650 ) );
  69. californiaPts.push( new THREE.Vector2 ( 180, 640 ) );
  70. californiaPts.push( new THREE.Vector2 ( 165, 680 ) );
  71. californiaPts.push( new THREE.Vector2 ( 150, 670 ) );
  72. californiaPts.push( new THREE.Vector2 ( 90, 737 ) );
  73. californiaPts.push( new THREE.Vector2 ( 80, 795 ) );
  74. californiaPts.push( new THREE.Vector2 ( 50, 835 ) );
  75. californiaPts.push( new THREE.Vector2 ( 64, 870 ) );
  76. californiaPts.push( new THREE.Vector2 ( 60, 945 ) );
  77. californiaPts.push( new THREE.Vector2 ( 300, 945 ) );
  78. californiaPts.push( new THREE.Vector2 ( 300, 743 ) );
  79. californiaPts.push( new THREE.Vector2 ( 600, 473 ) );
  80. californiaPts.push( new THREE.Vector2 ( 626, 425 ) );
  81. californiaPts.push( new THREE.Vector2 ( 600, 370 ) );
  82. californiaPts.push( new THREE.Vector2 ( 610, 320 ) );
  83. var californiaShape = new THREE.Shape( californiaPts );
  84. var california3d = new THREE.ExtrudeGeometry( californiaShape, { amount: 20 } );
  85. // Triangle
  86. var triangleShape = new THREE.Shape();
  87. triangleShape.moveTo( 80, 20 );
  88. triangleShape.lineTo( 40, 80 );
  89. triangleShape.lineTo( 120, 80 );
  90. triangleShape.lineTo( 80, 20 ); // close path
  91. var triangle3d = triangleShape.extrude( extrudeSettings );
  92. // Heart
  93. var x = 0, y = 0;
  94. var heartShape = new THREE.Shape(); // From http://blog.burlock.org/html5/130-paths
  95. heartShape.moveTo( x + 25, y + 25 );
  96. heartShape.bezierCurveTo( x + 25, y + 25, x + 20, y, x, y );
  97. heartShape.bezierCurveTo( x - 30, y, x - 30, y + 35,x - 30,y + 35 );
  98. heartShape.bezierCurveTo( x - 30, y + 55, x - 10, y + 77, x + 25, y + 95 );
  99. heartShape.bezierCurveTo( x + 60, y + 77, x + 80, y + 55, x + 80, y + 35 );
  100. heartShape.bezierCurveTo( x + 80, y + 35, x + 80, y, x + 50, y );
  101. heartShape.bezierCurveTo( x + 35, y, x + 25, y + 25, x + 25, y + 25 );
  102. var heart3d = heartShape.extrude( extrudeSettings );
  103. //heartShape.debug( document.getElementById("debug") );
  104. // Square
  105. var sqLength = 80;
  106. var squareShape = new THREE.Shape();
  107. squareShape.moveTo( 0,0 );
  108. squareShape.lineTo( 0, sqLength );
  109. squareShape.lineTo( sqLength, sqLength );
  110. squareShape.lineTo( sqLength, 0 );
  111. squareShape.lineTo( 0, 0 );
  112. var square3d = squareShape.extrude( extrudeSettings );
  113. // Rectangle
  114. var rectLength = 120, rectWidth = 40;
  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. var rect3d = rectShape.extrude( extrudeSettings );
  122. // Rounded rectangle
  123. var roundedRectShape = new THREE.Shape();
  124. roundedRect( roundedRectShape, 0, 0, 50, 50, 20 );
  125. var roundedRect3d = roundedRectShape.extrude( extrudeSettings );
  126. function roundedRect( ctx, x, y, width, height, radius ){
  127. ctx.moveTo( x, y + radius );
  128. ctx.lineTo( x, y + height - radius );
  129. ctx.quadraticCurveTo( x, y + height, x + radius, y + height );
  130. ctx.lineTo( x + width - radius, y + height) ;
  131. ctx.quadraticCurveTo( x + width, y + height, x + width, y + height - radius );
  132. ctx.lineTo( x + width, y + radius );
  133. ctx.quadraticCurveTo( x + width, y, x + width - radius, y );
  134. ctx.lineTo( x + radius, y );
  135. ctx.quadraticCurveTo( x, y, x, y + radius );
  136. }
  137. // Circle
  138. var circleRadius = 40;
  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 circle3d = circleShape.extrude( extrudeSettings );
  146. // Fish
  147. x = y = 0;
  148. var fishShape = new THREE.Shape();
  149. fishShape.moveTo( x, y );
  150. fishShape.quadraticCurveTo( x + 50, y + 80, x + 90, y + 10 );
  151. fishShape.quadraticCurveTo( x + 100, y + 10, x + 115, y + 40 );
  152. fishShape.quadraticCurveTo( x + 100, y, x + 115, y - 40 );
  153. fishShape.quadraticCurveTo( x + 100, y + 10, x + 90, y - 10 );
  154. fishShape.quadraticCurveTo( x + 50, y - 80, x, y );
  155. var fish3d = fishShape.extrude( extrudeSettings );
  156. // Arc Circle
  157. var arcShape = new THREE.Shape();
  158. arcShape.moveTo( 0, 0);
  159. arcShape.arc(10,10, 40, 0, Math.PI*2,false);
  160. var holePath = new THREE.Path();
  161. holePath.moveTo( 0, 0);
  162. holePath.arc(10, 10, 10, 0, Math.PI*2, true);
  163. arcShape.holes.push(holePath);
  164. var arc3d = arcShape.extrude( extrudeSettings );
  165. // Spline shape + Path Extrusion
  166. var splinepts = [];
  167. splinepts.push( new THREE.Vector2 ( 350, 100 ) );
  168. splinepts.push( new THREE.Vector2 ( 400, 450 ) );
  169. splinepts.push( new THREE.Vector2 ( -140, 350 ) );
  170. splinepts.push( new THREE.Vector2 ( 0, 0 ) );
  171. var splineShape = new THREE.Shape( );
  172. splineShape.moveTo( 0, 0 );
  173. splineShape.splineThru( splinepts );
  174. //splineShape.debug( document.getElementById("debug") );
  175. var extrudePath = new THREE.Path();
  176. extrudePath.moveTo(0,0);
  177. extrudePath.lineTo( 10, 10 );
  178. extrudePath.quadraticCurveTo( 80, 60, 160, 10 );
  179. extrudePath.quadraticCurveTo( 240, -40, 320, 10 );
  180. extrudeSettings.path = extrudePath;
  181. extrudeSettings.bevelEnabled = false;
  182. var splineShape3d = splineShape.extrude( extrudeSettings );
  183. addGeometry( california3d, 0xffaa00, -300, -100, 0, 0, 0, 0, 0.25 );
  184. addGeometry( triangle3d, 0xffee00, -180, 0, 0, 0, 0, 0, 1 );
  185. addGeometry( roundedRect3d, 0x005500, -150, 150, 0, 0, 0, 0, 1 );
  186. addGeometry( square3d, 0x0055ff, 150, 100, 0, 0, 0, 0, 1 );
  187. addGeometry( heart3d, 0xff1100, 0, 100, 0, 3.14, 0, 0, 1 );
  188. addGeometry( circle3d, 0x00ff11, 120, 250, 0, 0, 0, 0, 1 );
  189. addGeometry( fish3d, 0x222222, -50, 200, 0, 0, 0, 0, 1 );
  190. addGeometry( splineShape3d, 0x888888, -50, -100, -50, 0, 0, 0, 0.2 );
  191. addGeometry( arc3d, 0xbb4422, 150, 0, 0, 0, 0, 0, 1 );
  192. renderer = new THREE.CanvasRenderer();
  193. renderer.setSize( window.innerWidth, window.innerHeight );
  194. container.appendChild( renderer.domElement );
  195. stats = new Stats();
  196. stats.domElement.style.position = 'absolute';
  197. stats.domElement.style.top = '0px';
  198. container.appendChild( stats.domElement );
  199. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  200. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  201. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  202. }
  203. //
  204. function onDocumentMouseDown( event ) {
  205. event.preventDefault();
  206. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  207. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  208. document.addEventListener( 'mouseout', onDocumentMouseOut, false );
  209. mouseXOnMouseDown = event.clientX - windowHalfX;
  210. targetRotationOnMouseDown = targetRotation;
  211. }
  212. function onDocumentMouseMove( event ) {
  213. mouseX = event.clientX - windowHalfX;
  214. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
  215. }
  216. function onDocumentMouseUp( event ) {
  217. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  218. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  219. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  220. }
  221. function onDocumentMouseOut( event ) {
  222. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  223. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  224. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  225. }
  226. function onDocumentTouchStart( event ) {
  227. if ( event.touches.length == 1 ) {
  228. event.preventDefault();
  229. mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
  230. targetRotationOnMouseDown = targetRotation;
  231. }
  232. }
  233. function onDocumentTouchMove( event ) {
  234. if ( event.touches.length == 1 ) {
  235. event.preventDefault();
  236. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  237. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
  238. }
  239. }
  240. //
  241. function animate() {
  242. requestAnimationFrame( animate );
  243. render();
  244. stats.update();
  245. }
  246. function render() {
  247. parent.rotation.y += ( targetRotation - parent.rotation.y ) * 0.05;
  248. renderer.render( scene, camera );
  249. }
  250. </script>
  251. </body>
  252. </html>