webgl_geometry_shapes.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - geometry - shapes</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="js/Stats.js"></script>
  20. <script>
  21. var container, stats;
  22. var camera, scene, renderer;
  23. var 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 = 'Simple procedurally generated 3D shapes example by <a href="http://www.lab4games.net/zz85/blog">zz85</a><br/>Drag to spin';
  41. container.appendChild( info );
  42. scene = new THREE.Scene();
  43. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
  44. camera.position.set( 0, 150, 500 );
  45. scene.add( camera );
  46. var light = new THREE.DirectionalLight( 0xffffff );
  47. light.position.set( 0, 0, 1 );
  48. scene.add( light );
  49. parent = new THREE.Object3D();
  50. parent.position.y = 50;
  51. scene.add( parent );
  52. function addGeometry( geometry, points, spacedPoints, color, x, y, z, rx, ry, rz, s ) {
  53. // 3d shape
  54. var mesh = THREE.SceneUtils.createMultiMaterialObject( geometry, [ new THREE.MeshLambertMaterial( { color: color } ), new THREE.MeshBasicMaterial( { color: 0x000000, wireframe: true, transparent: true } ) ] );
  55. mesh.position.set( x, y, z - 75 );
  56. mesh.rotation.set( rx, ry, rz );
  57. mesh.scale.set( s, s, s );
  58. parent.add( mesh );
  59. // solid line
  60. var line = new THREE.Line( points, new THREE.LineBasicMaterial( { color: color, linewidth: 2 } ) );
  61. line.position.set( x, y, z + 25 );
  62. line.rotation.set( rx, ry, rz );
  63. line.scale.set( s, s, s );
  64. parent.add( line );
  65. // transparent line from real points
  66. var line = new THREE.Line( points, new THREE.LineBasicMaterial( { color: color, opacity: 0.5 } ) );
  67. line.position.set( x, y, z + 75 );
  68. line.rotation.set( rx, ry, rz );
  69. line.scale.set( s, s, s );
  70. parent.add( line );
  71. // vertices from real points
  72. var pgeo = THREE.GeometryUtils.clone( points );
  73. var particles = new THREE.ParticleSystem( pgeo, new THREE.ParticleBasicMaterial( { color: color, size: 2, opacity: 0.75 } ) );
  74. particles.position.set( x, y, z + 75 );
  75. particles.rotation.set( rx, ry, rz );
  76. particles.scale.set( s, s, s );
  77. parent.add( particles );
  78. // transparent line from equidistance sampled points
  79. var line = new THREE.Line( spacedPoints, new THREE.LineBasicMaterial( { color: color, opacity: 0.2 } ) );
  80. line.position.set( x, y, z + 100 );
  81. line.rotation.set( rx, ry, rz );
  82. line.scale.set( s, s, s );
  83. parent.add( line );
  84. // equidistance sampled points
  85. var pgeo = THREE.GeometryUtils.clone( spacedPoints );
  86. var particles2 = new THREE.ParticleSystem( pgeo, new THREE.ParticleBasicMaterial( { color: color, size: 2, opacity: 0.5 } ) );
  87. particles2.position.set( x, y, z + 100 );
  88. particles2.rotation.set( rx, ry, rz );
  89. particles2.scale.set( s, s, s );
  90. parent.add( particles2 );
  91. }
  92. var extrudeSettings = { amount: 20, bevelEnabled: true, bevelSegments: 2, steps: 2 }; // bevelSegments: 2, steps: 2 , bevelSegments: 5, bevelSize: 8, bevelThickness:5,
  93. // California
  94. var californiaPts = [];
  95. californiaPts.push( new THREE.Vector2 ( 610, 320 ) );
  96. californiaPts.push( new THREE.Vector2 ( 450, 300 ) );
  97. californiaPts.push( new THREE.Vector2 ( 392, 392 ) );
  98. californiaPts.push( new THREE.Vector2 ( 266, 438 ) );
  99. californiaPts.push( new THREE.Vector2 ( 190, 570 ) );
  100. californiaPts.push( new THREE.Vector2 ( 190, 600 ) );
  101. californiaPts.push( new THREE.Vector2 ( 160, 620 ) );
  102. californiaPts.push( new THREE.Vector2 ( 160, 650 ) );
  103. californiaPts.push( new THREE.Vector2 ( 180, 640 ) );
  104. californiaPts.push( new THREE.Vector2 ( 165, 680 ) );
  105. californiaPts.push( new THREE.Vector2 ( 150, 670 ) );
  106. californiaPts.push( new THREE.Vector2 ( 90, 737 ) );
  107. californiaPts.push( new THREE.Vector2 ( 80, 795 ) );
  108. californiaPts.push( new THREE.Vector2 ( 50, 835 ) );
  109. californiaPts.push( new THREE.Vector2 ( 64, 870 ) );
  110. californiaPts.push( new THREE.Vector2 ( 60, 945 ) );
  111. californiaPts.push( new THREE.Vector2 ( 300, 945 ) );
  112. californiaPts.push( new THREE.Vector2 ( 300, 743 ) );
  113. californiaPts.push( new THREE.Vector2 ( 600, 473 ) );
  114. californiaPts.push( new THREE.Vector2 ( 626, 425 ) );
  115. californiaPts.push( new THREE.Vector2 ( 600, 370 ) );
  116. californiaPts.push( new THREE.Vector2 ( 610, 320 ) );
  117. var californiaShape = new THREE.Shape( californiaPts );
  118. var california3d = new THREE.ExtrudeGeometry( californiaShape, { amount: 20 } );
  119. var californiaPoints = californiaShape.createPointsGeometry();
  120. var californiaSpacedPoints = californiaShape.createSpacedPointsGeometry( 100 );
  121. // Triangle
  122. var triangleShape = new THREE.Shape();
  123. triangleShape.moveTo( 80, 20 );
  124. triangleShape.lineTo( 40, 80 );
  125. triangleShape.lineTo( 120, 80 );
  126. triangleShape.lineTo( 80, 20 ); // close path
  127. var triangle3d = triangleShape.extrude( extrudeSettings );
  128. var trianglePoints = triangleShape.createPointsGeometry();
  129. var triangleSpacedPoints = triangleShape.createSpacedPointsGeometry();
  130. // Heart
  131. var x = 0, y = 0;
  132. var heartShape = new THREE.Shape(); // From http://blog.burlock.org/html5/130-paths
  133. heartShape.moveTo( x + 25, y + 25 );
  134. heartShape.bezierCurveTo( x + 25, y + 25, x + 20, y, x, y );
  135. heartShape.bezierCurveTo( x - 30, y, x - 30, y + 35,x - 30,y + 35 );
  136. heartShape.bezierCurveTo( x - 30, y + 55, x - 10, y + 77, x + 25, y + 95 );
  137. heartShape.bezierCurveTo( x + 60, y + 77, x + 80, y + 55, x + 80, y + 35 );
  138. heartShape.bezierCurveTo( x + 80, y + 35, x + 80, y, x + 50, y );
  139. heartShape.bezierCurveTo( x + 35, y, x + 25, y + 25, x + 25, y + 25 );
  140. var heart3d = heartShape.extrude( extrudeSettings );
  141. var heartPoints = heartShape.createPointsGeometry();
  142. var heartSpacedPoints = heartShape.createSpacedPointsGeometry();
  143. //heartShape.debug( document.getElementById("debug") );
  144. // Square
  145. var sqLength = 80;
  146. var squareShape = new THREE.Shape();
  147. squareShape.moveTo( 0,0 );
  148. squareShape.lineTo( 0, sqLength );
  149. squareShape.lineTo( sqLength, sqLength );
  150. squareShape.lineTo( sqLength, 0 );
  151. squareShape.lineTo( 0, 0 );
  152. var square3d = squareShape.extrude( extrudeSettings );
  153. var squarePoints = squareShape.createPointsGeometry();
  154. var squareSpacedPoints = squareShape.createSpacedPointsGeometry();
  155. // Rectangle
  156. var rectLength = 120, rectWidth = 40;
  157. var rectShape = new THREE.Shape();
  158. rectShape.moveTo( 0,0 );
  159. rectShape.lineTo( 0, rectWidth );
  160. rectShape.lineTo( rectLength, rectWidth );
  161. rectShape.lineTo( rectLength, 0 );
  162. rectShape.lineTo( 0, 0 );
  163. var rect3d = rectShape.extrude( extrudeSettings );
  164. var rectPoints = rectShape.createPointsGeometry();
  165. var rectSpacedPoints = rectShape.createSpacedPointsGeometry();
  166. // Rounded rectangle
  167. var roundedRectShape = new THREE.Shape();
  168. roundedRect( roundedRectShape, 0, 0, 50, 50, 20 );
  169. var roundedRect3d = roundedRectShape.extrude( extrudeSettings );
  170. var roundedRectPoints = roundedRectShape.createPointsGeometry();
  171. var roundedRectSpacedPoints = roundedRectShape.createSpacedPointsGeometry();
  172. function roundedRect( ctx, x, y, width, height, radius ){
  173. ctx.moveTo( x, y + radius );
  174. ctx.lineTo( x, y + height - radius );
  175. ctx.quadraticCurveTo( x, y + height, x + radius, y + height );
  176. ctx.lineTo( x + width - radius, y + height) ;
  177. ctx.quadraticCurveTo( x + width, y + height, x + width, y + height - radius );
  178. ctx.lineTo( x + width, y + radius );
  179. ctx.quadraticCurveTo( x + width, y, x + width - radius, y );
  180. ctx.lineTo( x + radius, y );
  181. ctx.quadraticCurveTo( x, y, x, y + radius );
  182. }
  183. // Circle
  184. var circleRadius = 40;
  185. var circleShape = new THREE.Shape();
  186. circleShape.moveTo( 0, circleRadius );
  187. circleShape.quadraticCurveTo( circleRadius, circleRadius, circleRadius, 0 );
  188. circleShape.quadraticCurveTo( circleRadius, -circleRadius, 0, -circleRadius );
  189. circleShape.quadraticCurveTo( -circleRadius, -circleRadius, -circleRadius, 0 );
  190. circleShape.quadraticCurveTo( -circleRadius, circleRadius, 0, circleRadius );
  191. var circle3d = circleShape.extrude( extrudeSettings );
  192. var circlePoints = circleShape.createPointsGeometry();
  193. var circleSpacedPoints = circleShape.createSpacedPointsGeometry();
  194. // Fish
  195. x = y = 0;
  196. var fishShape = new THREE.Shape();
  197. fishShape.moveTo(x,y);
  198. fishShape.quadraticCurveTo(x + 50, y - 80, x + 90, y - 10);
  199. fishShape.quadraticCurveTo(x + 100, y - 10, x + 115, y - 40);
  200. fishShape.quadraticCurveTo(x + 115, y, x + 115, y + 40);
  201. fishShape.quadraticCurveTo(x + 100, y + 10, x + 90, y + 10);
  202. fishShape.quadraticCurveTo(x + 50, y + 80, x, y);
  203. var fish3d = fishShape.extrude( extrudeSettings );
  204. var fishPoints = fishShape.createPointsGeometry();
  205. var fishSpacedPoints = fishShape.createSpacedPointsGeometry();
  206. // Arc circle
  207. var arcShape = new THREE.Shape();
  208. arcShape.moveTo( 0, 0 );
  209. arcShape.arc( 10, 10, 40, 0, Math.PI*2, false );
  210. var holePath = new THREE.Path();
  211. holePath.moveTo( 0, 0 );
  212. holePath.arc( 10, 10, 10, 0, Math.PI*2, true );
  213. arcShape.holes.push( holePath );
  214. var arc3d = arcShape.extrude( extrudeSettings );
  215. var arcPoints = arcShape.createPointsGeometry();
  216. var arcSpacedPoints = arcShape.createSpacedPointsGeometry();
  217. // Smiley
  218. var smileyShape = new THREE.Shape();
  219. smileyShape.moveTo( 0, 0 );
  220. smileyShape.arc( 40, 40, 40, 0, Math.PI*2, false );
  221. var smileyEye1Path = new THREE.Path();
  222. smileyEye1Path.moveTo( 0, 0 );
  223. smileyEye1Path.arc( 25, 20, 10, 0, Math.PI*2, true );
  224. smileyShape.holes.push( smileyEye1Path );
  225. var smileyEye2Path = new THREE.Path();
  226. smileyEye2Path.moveTo( 0, 0 );
  227. smileyEye2Path.arc( 55, 20, 10, 0, Math.PI*2, true );
  228. smileyShape.holes.push( smileyEye2Path );
  229. var smileyMouthPath = new THREE.Path();
  230. // ugly box mouth
  231. // smileyMouthPath.moveTo( 20, 40 );
  232. // smileyMouthPath.lineTo( 60, 40 );
  233. // smileyMouthPath.lineTo( 60, 60 );
  234. // smileyMouthPath.lineTo( 20, 60 );
  235. // smileyMouthPath.lineTo( 20, 40 );
  236. smileyMouthPath.moveTo( 20, 40 );
  237. smileyMouthPath.quadraticCurveTo( 40, 60, 60, 40 );
  238. smileyMouthPath.bezierCurveTo( 70, 45, 70, 50, 60, 60 );
  239. smileyMouthPath.quadraticCurveTo( 40, 80, 20, 60 );
  240. smileyMouthPath.quadraticCurveTo( 5, 50, 20, 40 );
  241. smileyShape.holes.push( smileyMouthPath );
  242. var smiley3d = smileyShape.extrude( extrudeSettings );
  243. var smileyPoints = smileyShape.createPointsGeometry();
  244. var smileySpacedPoints = smileyShape.createSpacedPointsGeometry();
  245. // Spline shape + path extrusion
  246. var splinepts = [];
  247. splinepts.push( new THREE.Vector2 ( 350, 100 ) );
  248. splinepts.push( new THREE.Vector2 ( 400, 450 ) );
  249. splinepts.push( new THREE.Vector2 ( -140, 350 ) );
  250. splinepts.push( new THREE.Vector2 ( 0, 0 ) );
  251. var splineShape = new THREE.Shape( );
  252. splineShape.moveTo( 0, 0 );
  253. splineShape.splineThru( splinepts );
  254. //splineShape.debug( document.getElementById("debug") );
  255. var extrudePath = new THREE.Path();
  256. extrudePath.moveTo( 0, 0 );
  257. extrudePath.lineTo( 10, 10 );
  258. extrudePath.quadraticCurveTo( 80, 60, 160, 10 );
  259. extrudePath.quadraticCurveTo( 240, -40, 320, 10 );
  260. extrudeSettings.extrudePath = extrudePath;
  261. extrudeSettings.bevelEnabled = false;
  262. var splineShape3d = splineShape.extrude( extrudeSettings );
  263. var splinePoints = splineShape.createPointsGeometry( );
  264. var splineSpacedPoints = splineShape.createSpacedPointsGeometry( );
  265. addGeometry( california3d, californiaPoints, californiaSpacedPoints, 0xffaa00, -300, -100, 0, 0, 0, 0, 0.25 );
  266. addGeometry( triangle3d, trianglePoints, triangleSpacedPoints, 0xffee00, -180, 0, 0, 0, 0, 0, 1 );
  267. addGeometry( roundedRect3d, roundedRectPoints, roundedRectSpacedPoints, 0x005500, -150, 150, 0, 0, 0, 0, 1 );
  268. addGeometry( square3d, squarePoints, squareSpacedPoints, 0x0055ff, 150, 100, 0, 0, 0, 0, 1 );
  269. addGeometry( heart3d, heartPoints, heartSpacedPoints, 0xff1100, 0, 100, 0, Math.PI, 0, 0, 1 );
  270. addGeometry( circle3d, circlePoints, circleSpacedPoints, 0x00ff11, 120, 250, 0, 0, 0, 0, 1 );
  271. addGeometry( fish3d, fishPoints, fishSpacedPoints, 0x222222, -60, 200, 0, 0, 0, 0, 1 );
  272. addGeometry( splineShape3d, splinePoints, splineSpacedPoints, 0x888888, -50, -100, -50, 0, 0, 0, 0.2 );
  273. addGeometry( arc3d, arcPoints, arcSpacedPoints, 0xbb4422, 150, 0, 0, 0, 0, 0, 1 );
  274. addGeometry( smiley3d, smileyPoints, smileySpacedPoints, 0xee00ff, -270, 250, 0, Math.PI, 0, 0, 1 );
  275. //
  276. renderer = new THREE.WebGLRenderer( { antialias: true } );
  277. renderer.setSize( window.innerWidth, window.innerHeight );
  278. container.appendChild( renderer.domElement );
  279. stats = new Stats();
  280. stats.domElement.style.position = 'absolute';
  281. stats.domElement.style.top = '0px';
  282. container.appendChild( stats.domElement );
  283. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  284. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  285. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  286. }
  287. //
  288. function onDocumentMouseDown( event ) {
  289. event.preventDefault();
  290. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  291. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  292. document.addEventListener( 'mouseout', onDocumentMouseOut, false );
  293. mouseXOnMouseDown = event.clientX - windowHalfX;
  294. targetRotationOnMouseDown = targetRotation;
  295. }
  296. function onDocumentMouseMove( event ) {
  297. mouseX = event.clientX - windowHalfX;
  298. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
  299. }
  300. function onDocumentMouseUp( event ) {
  301. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  302. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  303. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  304. }
  305. function onDocumentMouseOut( event ) {
  306. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  307. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  308. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  309. }
  310. function onDocumentTouchStart( event ) {
  311. if ( event.touches.length == 1 ) {
  312. event.preventDefault();
  313. mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
  314. targetRotationOnMouseDown = targetRotation;
  315. }
  316. }
  317. function onDocumentTouchMove( event ) {
  318. if ( event.touches.length == 1 ) {
  319. event.preventDefault();
  320. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  321. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
  322. }
  323. }
  324. //
  325. function animate() {
  326. requestAnimationFrame( animate );
  327. render();
  328. stats.update();
  329. }
  330. function render() {
  331. parent.rotation.y += ( targetRotation - parent.rotation.y ) * 0.05;
  332. renderer.render( scene, camera );
  333. }
  334. </script>
  335. </body>
  336. </html>