webgl_geometry_shapes.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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;
  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<br/>Drag to spin';
  40. container.appendChild( info );
  41. scene = new THREE.Scene();
  42. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
  43. camera.position.set( 0, 150, 500 );
  44. scene.add( camera );
  45. var light = new THREE.PointLight( 0xffffff, 0.8 );
  46. camera.add( light );
  47. group = new THREE.Group();
  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( 50 );
  53. // flat shape
  54. var geometry = new THREE.ShapeGeometry( shape );
  55. var mesh = new THREE.Mesh( geometry, new THREE.MeshPhongMaterial( { color: color, side: THREE.DoubleSide } ) );
  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 = new THREE.Mesh( geometry, new THREE.MeshPhongMaterial( { color: color } ) );
  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: 3 } ) );
  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. // vertices from real points
  74. var pgeo = points.clone();
  75. var particles = new THREE.PointCloud( pgeo, new THREE.PointCloudMaterial( { color: color, size: 4 } ) );
  76. particles.position.set( x, y, z + 25 );
  77. particles.rotation.set( rx, ry, rz );
  78. particles.scale.set( s, s, s );
  79. group.add( particles );
  80. // line from equidistance sampled points
  81. var line = new THREE.Line( spacedPoints, new THREE.LineBasicMaterial( { color: color, linewidth: 3 } ) );
  82. line.position.set( x, y, z + 75 );
  83. line.rotation.set( rx, ry, rz );
  84. line.scale.set( s, s, s );
  85. group.add( line );
  86. // equidistance sampled points
  87. var pgeo = spacedPoints.clone();
  88. var particles2 = new THREE.PointCloud( pgeo, new THREE.PointCloudMaterial( { color: color, size: 4 } ) );
  89. particles2.position.set( x, y, z + 125 );
  90. particles2.rotation.set( rx, ry, rz );
  91. particles2.scale.set( s, s, s );
  92. group.add( particles2 );
  93. }
  94. // California
  95. var californiaPts = [];
  96. californiaPts.push( new THREE.Vector2 ( 610, 320 ) );
  97. californiaPts.push( new THREE.Vector2 ( 450, 300 ) );
  98. californiaPts.push( new THREE.Vector2 ( 392, 392 ) );
  99. californiaPts.push( new THREE.Vector2 ( 266, 438 ) );
  100. californiaPts.push( new THREE.Vector2 ( 190, 570 ) );
  101. californiaPts.push( new THREE.Vector2 ( 190, 600 ) );
  102. californiaPts.push( new THREE.Vector2 ( 160, 620 ) );
  103. californiaPts.push( new THREE.Vector2 ( 160, 650 ) );
  104. californiaPts.push( new THREE.Vector2 ( 180, 640 ) );
  105. californiaPts.push( new THREE.Vector2 ( 165, 680 ) );
  106. californiaPts.push( new THREE.Vector2 ( 150, 670 ) );
  107. californiaPts.push( new THREE.Vector2 ( 90, 737 ) );
  108. californiaPts.push( new THREE.Vector2 ( 80, 795 ) );
  109. californiaPts.push( new THREE.Vector2 ( 50, 835 ) );
  110. californiaPts.push( new THREE.Vector2 ( 64, 870 ) );
  111. californiaPts.push( new THREE.Vector2 ( 60, 945 ) );
  112. californiaPts.push( new THREE.Vector2 ( 300, 945 ) );
  113. californiaPts.push( new THREE.Vector2 ( 300, 743 ) );
  114. californiaPts.push( new THREE.Vector2 ( 600, 473 ) );
  115. californiaPts.push( new THREE.Vector2 ( 626, 425 ) );
  116. californiaPts.push( new THREE.Vector2 ( 600, 370 ) );
  117. californiaPts.push( new THREE.Vector2 ( 610, 320 ) );
  118. for( var i = 0; i < californiaPts.length; i ++ ) californiaPts[ i ].multiplyScalar( 0.25 );
  119. var californiaShape = new THREE.Shape( californiaPts );
  120. // Triangle
  121. var triangleShape = new THREE.Shape();
  122. triangleShape.moveTo( 80, 20 );
  123. triangleShape.lineTo( 40, 80 );
  124. triangleShape.lineTo( 120, 80 );
  125. triangleShape.lineTo( 80, 20 ); // close path
  126. // Heart
  127. var x = 0, y = 0;
  128. var heartShape = new THREE.Shape(); // From http://blog.burlock.org/html5/130-paths
  129. heartShape.moveTo( x + 25, y + 25 );
  130. heartShape.bezierCurveTo( x + 25, y + 25, x + 20, y, x, y );
  131. heartShape.bezierCurveTo( x - 30, y, x - 30, y + 35,x - 30,y + 35 );
  132. heartShape.bezierCurveTo( x - 30, y + 55, x - 10, y + 77, x + 25, y + 95 );
  133. heartShape.bezierCurveTo( x + 60, y + 77, x + 80, y + 55, x + 80, y + 35 );
  134. heartShape.bezierCurveTo( x + 80, y + 35, x + 80, y, x + 50, y );
  135. heartShape.bezierCurveTo( x + 35, y, x + 25, y + 25, x + 25, y + 25 );
  136. // Square
  137. var sqLength = 80;
  138. var squareShape = new THREE.Shape();
  139. squareShape.moveTo( 0,0 );
  140. squareShape.lineTo( 0, sqLength );
  141. squareShape.lineTo( sqLength, sqLength );
  142. squareShape.lineTo( sqLength, 0 );
  143. squareShape.lineTo( 0, 0 );
  144. // Rectangle
  145. var rectLength = 120, rectWidth = 40;
  146. var rectShape = new THREE.Shape();
  147. rectShape.moveTo( 0,0 );
  148. rectShape.lineTo( 0, rectWidth );
  149. rectShape.lineTo( rectLength, rectWidth );
  150. rectShape.lineTo( rectLength, 0 );
  151. rectShape.lineTo( 0, 0 );
  152. // Rounded rectangle
  153. var roundedRectShape = new THREE.Shape();
  154. ( function roundedRect( ctx, x, y, width, height, radius ){
  155. ctx.moveTo( x, y + radius );
  156. ctx.lineTo( x, y + height - radius );
  157. ctx.quadraticCurveTo( x, y + height, x + radius, y + height );
  158. ctx.lineTo( x + width - radius, y + height) ;
  159. ctx.quadraticCurveTo( x + width, y + height, x + width, y + height - radius );
  160. ctx.lineTo( x + width, y + radius );
  161. ctx.quadraticCurveTo( x + width, y, x + width - radius, y );
  162. ctx.lineTo( x + radius, y );
  163. ctx.quadraticCurveTo( x, y, x, y + radius );
  164. } )( roundedRectShape, 0, 0, 50, 50, 20 );
  165. // Track
  166. var trackShape = new THREE.Shape();
  167. trackShape.moveTo( 40, 40 );
  168. trackShape.lineTo( 40, 160 );
  169. trackShape.absarc( 60, 160, 20, Math.PI, 0, true );
  170. trackShape.lineTo( 80, 40 );
  171. trackShape.absarc( 60, 40, 20, 2 * Math.PI, Math.PI, true );
  172. // Circle
  173. var circleRadius = 40;
  174. var circleShape = new THREE.Shape();
  175. circleShape.moveTo( 0, circleRadius );
  176. circleShape.quadraticCurveTo( circleRadius, circleRadius, circleRadius, 0 );
  177. circleShape.quadraticCurveTo( circleRadius, -circleRadius, 0, -circleRadius );
  178. circleShape.quadraticCurveTo( -circleRadius, -circleRadius, -circleRadius, 0 );
  179. circleShape.quadraticCurveTo( -circleRadius, circleRadius, 0, circleRadius );
  180. // Fish
  181. var x = y = 0;
  182. var fishShape = new THREE.Shape();
  183. fishShape.moveTo(x,y);
  184. fishShape.quadraticCurveTo(x + 50, y - 80, x + 90, y - 10);
  185. fishShape.quadraticCurveTo(x + 100, y - 10, x + 115, y - 40);
  186. fishShape.quadraticCurveTo(x + 115, y, x + 115, y + 40);
  187. fishShape.quadraticCurveTo(x + 100, y + 10, x + 90, y + 10);
  188. fishShape.quadraticCurveTo(x + 50, y + 80, x, y);
  189. // Arc circle
  190. var arcShape = new THREE.Shape();
  191. arcShape.moveTo( 50, 10 );
  192. arcShape.absarc( 10, 10, 40, 0, Math.PI*2, false );
  193. var holePath = new THREE.Path();
  194. holePath.moveTo( 20, 10 );
  195. holePath.absarc( 10, 10, 10, 0, Math.PI*2, true );
  196. arcShape.holes.push( holePath );
  197. // Smiley
  198. var smileyShape = new THREE.Shape();
  199. smileyShape.moveTo( 80, 40 );
  200. smileyShape.absarc( 40, 40, 40, 0, Math.PI*2, false );
  201. var smileyEye1Path = new THREE.Path();
  202. smileyEye1Path.moveTo( 35, 20 );
  203. smileyEye1Path.absellipse( 25, 20, 10, 10, 0, Math.PI*2, true );
  204. smileyShape.holes.push( smileyEye1Path );
  205. var smileyEye2Path = new THREE.Path();
  206. smileyEye2Path.moveTo( 65, 20 );
  207. smileyEye2Path.absarc( 55, 20, 10, 0, Math.PI*2, true );
  208. smileyShape.holes.push( smileyEye2Path );
  209. var smileyMouthPath = new THREE.Path();
  210. smileyMouthPath.moveTo( 20, 40 );
  211. smileyMouthPath.quadraticCurveTo( 40, 60, 60, 40 );
  212. smileyMouthPath.bezierCurveTo( 70, 45, 70, 50, 60, 60 );
  213. smileyMouthPath.quadraticCurveTo( 40, 80, 20, 60 );
  214. smileyMouthPath.quadraticCurveTo( 5, 50, 20, 40 );
  215. smileyShape.holes.push( smileyMouthPath );
  216. // Spline shape
  217. var splinepts = [];
  218. splinepts.push( new THREE.Vector2 ( 70, 20 ) );
  219. splinepts.push( new THREE.Vector2 ( 80, 90 ) );
  220. splinepts.push( new THREE.Vector2 ( -30, 70 ) );
  221. splinepts.push( new THREE.Vector2 ( 0, 0 ) );
  222. var splineShape = new THREE.Shape();
  223. splineShape.moveTo( 0, 0 );
  224. splineShape.splineThru( splinepts );
  225. var extrudeSettings = { amount: 8, bevelEnabled: true, bevelSegments: 2, steps: 2, bevelSize: 1, bevelThickness: 1 };
  226. // addShape( shape, color, x, y, z, rx, ry,rz, s );
  227. addShape( californiaShape, extrudeSettings, 0xf08000, -300, -100, 0, 0, 0, 0, 1 );
  228. addShape( triangleShape, extrudeSettings, 0x8080f0, -180, 0, 0, 0, 0, 0, 1 );
  229. addShape( roundedRectShape, extrudeSettings, 0x008000, -150, 150, 0, 0, 0, 0, 1 );
  230. addShape( trackShape, extrudeSettings, 0x008080, 200, -100, 0, 0, 0, 0, 1 );
  231. addShape( squareShape, extrudeSettings, 0x0040f0, 150, 100, 0, 0, 0, 0, 1 );
  232. addShape( heartShape, extrudeSettings, 0xf00000, 60, 100, 0, 0, 0, Math.PI, 1 );
  233. addShape( circleShape, extrudeSettings, 0x00f000, 120, 250, 0, 0, 0, 0, 1 );
  234. addShape( fishShape, extrudeSettings, 0x404040, -60, 200, 0, 0, 0, 0, 1 );
  235. addShape( smileyShape, extrudeSettings, 0xf000f0, -200, 250, 0, 0, 0, Math.PI, 1 );
  236. addShape( arcShape, extrudeSettings, 0x804000, 150, 0, 0, 0, 0, 0, 1 );
  237. addShape( splineShape, extrudeSettings, 0x808080, -50, -100, 0, 0, 0, 0, 1 );
  238. //
  239. renderer = new THREE.WebGLRenderer( { antialias: true } );
  240. renderer.setClearColor( 0xf0f0f0 );
  241. renderer.setPixelRatio( window.devicePixelRatio );
  242. renderer.setSize( window.innerWidth, window.innerHeight );
  243. container.appendChild( renderer.domElement );
  244. stats = new Stats();
  245. stats.domElement.style.position = 'absolute';
  246. stats.domElement.style.top = '0px';
  247. container.appendChild( stats.domElement );
  248. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  249. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  250. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  251. //
  252. window.addEventListener( 'resize', onWindowResize, false );
  253. }
  254. function onWindowResize() {
  255. windowHalfX = window.innerWidth / 2;
  256. windowHalfY = window.innerHeight / 2;
  257. camera.aspect = window.innerWidth / window.innerHeight;
  258. camera.updateProjectionMatrix();
  259. renderer.setSize( window.innerWidth, window.innerHeight );
  260. }
  261. //
  262. function onDocumentMouseDown( event ) {
  263. event.preventDefault();
  264. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  265. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  266. document.addEventListener( 'mouseout', onDocumentMouseOut, false );
  267. mouseXOnMouseDown = event.clientX - windowHalfX;
  268. targetRotationOnMouseDown = targetRotation;
  269. }
  270. function onDocumentMouseMove( event ) {
  271. mouseX = event.clientX - windowHalfX;
  272. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
  273. }
  274. function onDocumentMouseUp( event ) {
  275. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  276. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  277. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  278. }
  279. function onDocumentMouseOut( event ) {
  280. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  281. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  282. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  283. }
  284. function onDocumentTouchStart( event ) {
  285. if ( event.touches.length == 1 ) {
  286. event.preventDefault();
  287. mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
  288. targetRotationOnMouseDown = targetRotation;
  289. }
  290. }
  291. function onDocumentTouchMove( event ) {
  292. if ( event.touches.length == 1 ) {
  293. event.preventDefault();
  294. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  295. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
  296. }
  297. }
  298. //
  299. function animate() {
  300. requestAnimationFrame( animate );
  301. render();
  302. stats.update();
  303. }
  304. function render() {
  305. group.rotation.y += ( targetRotation - group.rotation.y ) * 0.05;
  306. renderer.render( scene, camera );
  307. }
  308. </script>
  309. </body>
  310. </html>