webgl_geometry_shapes.html 16 KB

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