webgl_geometry_shapes.html 15 KB

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