webgl_geometry_shapes.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. var light = new THREE.DirectionalLight( 0xffffff );
  49. light.position.set( 0, 0, 1 );
  50. light.position.normalize();
  51. scene.addLight( light );
  52. parent = new THREE.Object3D();
  53. parent.position.y = 50;
  54. scene.addChild( parent );
  55. function addGeometry( geometry, color, x, y, z, rx, ry, rz, s ) {
  56. var mesh = new THREE.Mesh( geometry, [ new THREE.MeshLambertMaterial( { color: color } ), new THREE.MeshBasicMaterial( { color: 0x000000, wireframe: true } ) ] );
  57. mesh.position.set( x, y, z );
  58. mesh.rotation.set( rx, ry, rz );
  59. mesh.scale.set( s, s, s );
  60. parent.addChild( mesh );
  61. }
  62. var extrudeSettings = { amount: 20, bevelEnabled: true, bevelSegments: 2, steps: 2 }; //amount: 40, steps: 2
  63. // California
  64. var californiaPts = [];
  65. californiaPts.push( new THREE.Vector2 ( 610, 320 ) );
  66. californiaPts.push( new THREE.Vector2 ( 450, 300 ) );
  67. californiaPts.push( new THREE.Vector2 ( 392, 392 ) );
  68. californiaPts.push( new THREE.Vector2 ( 266, 438 ) );
  69. californiaPts.push( new THREE.Vector2 ( 190, 570 ) );
  70. californiaPts.push( new THREE.Vector2 ( 190, 600 ) );
  71. californiaPts.push( new THREE.Vector2 ( 160, 620 ) );
  72. californiaPts.push( new THREE.Vector2 ( 160, 650 ) );
  73. californiaPts.push( new THREE.Vector2 ( 180, 640 ) );
  74. californiaPts.push( new THREE.Vector2 ( 165, 680 ) );
  75. californiaPts.push( new THREE.Vector2 ( 150, 670 ) );
  76. californiaPts.push( new THREE.Vector2 ( 90, 737 ) );
  77. californiaPts.push( new THREE.Vector2 ( 80, 795 ) );
  78. californiaPts.push( new THREE.Vector2 ( 50, 835 ) );
  79. californiaPts.push( new THREE.Vector2 ( 64, 870 ) );
  80. californiaPts.push( new THREE.Vector2 ( 60, 945 ) );
  81. californiaPts.push( new THREE.Vector2 ( 300, 945 ) );
  82. californiaPts.push( new THREE.Vector2 ( 300, 743 ) );
  83. californiaPts.push( new THREE.Vector2 ( 600, 473 ) );
  84. californiaPts.push( new THREE.Vector2 ( 626, 425 ) );
  85. californiaPts.push( new THREE.Vector2 ( 600, 370 ) );
  86. californiaPts.push( new THREE.Vector2 ( 610, 320 ) );
  87. var californiaShape = new THREE.Shape( californiaPts );
  88. var california3d = new THREE.ExtrudeGeometry( californiaShape, { amount: 20 } );
  89. // Triangle
  90. var triangleShape = new THREE.Shape();
  91. triangleShape.moveTo( 80, 20 );
  92. triangleShape.lineTo( 40, 80 );
  93. triangleShape.lineTo( 120, 80 );
  94. triangleShape.lineTo( 80, 20 ); // close path
  95. var triangle3d = triangleShape.extrude( extrudeSettings );
  96. // Heart
  97. var x = 0, y = 0;
  98. var heartShape = new THREE.Shape(); // From http://blog.burlock.org/html5/130-paths
  99. heartShape.moveTo( x + 25, y + 25 );
  100. heartShape.bezierCurveTo( x + 25, y + 25, x + 20, y, x, y );
  101. heartShape.bezierCurveTo( x - 30, y, x - 30, y + 35,x - 30,y + 35 );
  102. heartShape.bezierCurveTo( x - 30, y + 55, x - 10, y + 77, x + 25, y + 95 );
  103. heartShape.bezierCurveTo( x + 60, y + 77, x + 80, y + 55, x + 80, y + 35 );
  104. heartShape.bezierCurveTo( x + 80, y + 35, x + 80, y, x + 50, y );
  105. heartShape.bezierCurveTo( x + 35, y, x + 25, y + 25, x + 25, y + 25 );
  106. var heart3d = heartShape.extrude( extrudeSettings );
  107. //heartShape.debug( document.getElementById("debug") );
  108. // Square
  109. var sqLength = 80;
  110. var squareShape = new THREE.Shape();
  111. squareShape.moveTo( 0,0 );
  112. squareShape.lineTo( 0, sqLength );
  113. squareShape.lineTo( sqLength, sqLength );
  114. squareShape.lineTo( sqLength, 0 );
  115. squareShape.lineTo( 0, 0 );
  116. var square3d = squareShape.extrude( extrudeSettings );
  117. // Rectangle
  118. var rectLength = 120, rectWidth = 40;
  119. var rectShape = new THREE.Shape();
  120. rectShape.moveTo( 0,0 );
  121. rectShape.lineTo( 0, rectWidth );
  122. rectShape.lineTo( rectLength, rectWidth );
  123. rectShape.lineTo( rectLength, 0 );
  124. rectShape.lineTo( 0, 0 );
  125. var rect3d = rectShape.extrude( extrudeSettings );
  126. // Rounded rectangle
  127. var roundedRectShape = new THREE.Shape();
  128. roundedRect( roundedRectShape, 0, 0, 50, 50, 20 );
  129. var roundedRect3d = roundedRectShape.extrude( extrudeSettings );
  130. function roundedRect( ctx, x, y, width, height, radius ){
  131. ctx.moveTo( x, y + radius );
  132. ctx.lineTo( x, y + height - radius );
  133. ctx.quadraticCurveTo( x, y + height, x + radius, y + height );
  134. ctx.lineTo( x + width - radius, y + height) ;
  135. ctx.quadraticCurveTo( x + width, y + height, x + width, y + height - radius );
  136. ctx.lineTo( x + width, y + radius );
  137. ctx.quadraticCurveTo( x + width, y, x + width - radius, y );
  138. ctx.lineTo( x + radius, y );
  139. ctx.quadraticCurveTo( x, y, x, y + radius );
  140. }
  141. // Circle
  142. var circleRadius = 40;
  143. var circleShape = new THREE.Shape();
  144. circleShape.moveTo( 0, circleRadius );
  145. circleShape.quadraticCurveTo( circleRadius, circleRadius, circleRadius, 0 );
  146. circleShape.quadraticCurveTo( circleRadius, -circleRadius, 0, -circleRadius );
  147. circleShape.quadraticCurveTo( -circleRadius, -circleRadius, -circleRadius, 0 );
  148. circleShape.quadraticCurveTo( -circleRadius, circleRadius, 0, circleRadius );
  149. var circle3d = circleShape.extrude( extrudeSettings );
  150. // Fish
  151. x = y = 0;
  152. var fishShape = new THREE.Shape();
  153. fishShape.moveTo( x, y );
  154. fishShape.quadraticCurveTo( x + 50, y + 80, x + 90, y + 10 );
  155. fishShape.quadraticCurveTo( x + 100, y + 10, x + 115, y + 40 );
  156. fishShape.quadraticCurveTo( x + 100, y, x + 115, y - 40 );
  157. fishShape.quadraticCurveTo( x + 100, y + 10, x + 90, y - 10 );
  158. fishShape.quadraticCurveTo( x + 50, y - 80, x, y );
  159. var fish3d = fishShape.extrude( extrudeSettings );
  160. // Arc Circle
  161. var arcShape = new THREE.Shape();
  162. arcShape.moveTo( 0, 0);
  163. arcShape.arc( 10, 10, 40, 0, Math.PI*2, false );
  164. var holePath = new THREE.Path();
  165. holePath.moveTo( 0, 0 );
  166. holePath.arc( 10, 10, 10, 0, Math.PI*2, true );
  167. arcShape.holes.push( holePath );
  168. var arc3d = arcShape.extrude( extrudeSettings );
  169. // Spline shape + path extrusion
  170. var splinepts = [];
  171. splinepts.push( new THREE.Vector2 ( 350, 100 ) );
  172. splinepts.push( new THREE.Vector2 ( 400, 450 ) );
  173. splinepts.push( new THREE.Vector2 ( -140, 350 ) );
  174. splinepts.push( new THREE.Vector2 ( 0, 0 ) );
  175. var splineShape = new THREE.Shape( );
  176. splineShape.moveTo( 0, 0 );
  177. splineShape.splineThru( splinepts );
  178. //splineShape.debug( document.getElementById("debug") );
  179. var extrudePath = new THREE.Path();
  180. extrudePath.moveTo( 0, 0 );
  181. extrudePath.lineTo( 10, 10 );
  182. extrudePath.quadraticCurveTo( 80, 60, 160, 10 );
  183. extrudePath.quadraticCurveTo( 240, -40, 320, 10 );
  184. extrudeSettings.path = extrudePath;
  185. extrudeSettings.bevelEnabled = false;
  186. var splineShape3d = splineShape.extrude( extrudeSettings );
  187. addGeometry( california3d, 0xffaa00, -300, -100, 0, 0, 0, 0, 0.25 );
  188. addGeometry( triangle3d, 0xffee00, -180, 0, 0, 0, 0, 0, 1 );
  189. addGeometry( roundedRect3d, 0x005500, -150, 150, 0, 0, 0, 0, 1 );
  190. addGeometry( square3d, 0x0055ff, 150, 100, 0, 0, 0, 0, 1 );
  191. addGeometry( heart3d, 0xff1100, 0, 100, 0, 3.14, 0, 0, 1 );
  192. addGeometry( circle3d, 0x00ff11, 120, 250, 0, 0, 0, 0, 1 );
  193. addGeometry( fish3d, 0x222222, -50, 200, 0, 0, 0, 0, 1 );
  194. addGeometry( splineShape3d, 0x888888, -50, -100, -50, 0, 0, 0, 0.2 );
  195. addGeometry( arc3d, 0xbb4422, 150, 0, 0, 0, 0, 0, 1 );
  196. renderer = new THREE.WebGLRenderer( { antialias: true } );
  197. renderer.setSize( window.innerWidth, window.innerHeight );
  198. container.appendChild( renderer.domElement );
  199. stats = new Stats();
  200. stats.domElement.style.position = 'absolute';
  201. stats.domElement.style.top = '0px';
  202. container.appendChild( stats.domElement );
  203. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  204. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  205. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  206. }
  207. //
  208. function onDocumentMouseDown( event ) {
  209. event.preventDefault();
  210. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  211. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  212. document.addEventListener( 'mouseout', onDocumentMouseOut, false );
  213. mouseXOnMouseDown = event.clientX - windowHalfX;
  214. targetRotationOnMouseDown = targetRotation;
  215. }
  216. function onDocumentMouseMove( event ) {
  217. mouseX = event.clientX - windowHalfX;
  218. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
  219. }
  220. function onDocumentMouseUp( event ) {
  221. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  222. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  223. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  224. }
  225. function onDocumentMouseOut( event ) {
  226. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  227. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  228. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  229. }
  230. function onDocumentTouchStart( event ) {
  231. if ( event.touches.length == 1 ) {
  232. event.preventDefault();
  233. mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
  234. targetRotationOnMouseDown = targetRotation;
  235. }
  236. }
  237. function onDocumentTouchMove( event ) {
  238. if ( event.touches.length == 1 ) {
  239. event.preventDefault();
  240. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  241. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
  242. }
  243. }
  244. //
  245. function animate() {
  246. requestAnimationFrame( animate );
  247. render();
  248. stats.update();
  249. }
  250. function render() {
  251. parent.rotation.y += ( targetRotation - parent.rotation.y ) * 0.05;
  252. renderer.render( scene, camera );
  253. }
  254. </script>
  255. </body>
  256. </html>