webgl_geometry_shapes.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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.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. init();
  29. animate();
  30. function init() {
  31. container = document.createElement( 'div' );
  32. document.body.appendChild( container );
  33. var info = document.createElement( 'div' );
  34. info.style.position = 'absolute';
  35. info.style.top = '10px';
  36. info.style.width = '100%';
  37. info.style.textAlign = 'center';
  38. info.innerHTML = 'Simple procedurally-generated shapes<br/>Drag to spin';
  39. container.appendChild( info );
  40. scene = new THREE.Scene();
  41. scene.background = new THREE.Color( 0xf0f0f0 );
  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. var loader = new THREE.TextureLoader();
  51. var texture = loader.load( "textures/UV_Grid_Sm.jpg" );
  52. // it's necessary to apply these settings in order to correctly display the texture on a shape geometry
  53. texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
  54. texture.repeat.set( 0.008, 0.008 );
  55. function addShape( shape, extrudeSettings, color, x, y, z, rx, ry, rz, s ) {
  56. // flat shape with texture
  57. // note: default UVs generated by ShapeBufferGeometry are simply the x- and y-coordinates of the vertices
  58. var geometry = new THREE.ShapeBufferGeometry( shape );
  59. var mesh = new THREE.Mesh( geometry, new THREE.MeshPhongMaterial( { side: THREE.DoubleSide, map: texture } ) );
  60. mesh.position.set( x, y, z - 175 );
  61. mesh.rotation.set( rx, ry, rz );
  62. mesh.scale.set( s, s, s );
  63. group.add( mesh );
  64. // flat shape
  65. var geometry = new THREE.ShapeBufferGeometry( shape );
  66. var mesh = new THREE.Mesh( geometry, new THREE.MeshPhongMaterial( { color: color, side: THREE.DoubleSide } ) );
  67. mesh.position.set( x, y, z - 125 );
  68. mesh.rotation.set( rx, ry, rz );
  69. mesh.scale.set( s, s, s );
  70. group.add( mesh );
  71. // extruded shape
  72. var geometry = new THREE.ExtrudeGeometry( shape, extrudeSettings );
  73. var mesh = new THREE.Mesh( geometry, new THREE.MeshPhongMaterial( { color: color } ) );
  74. mesh.position.set( x, y, z - 75 );
  75. mesh.rotation.set( rx, ry, rz );
  76. mesh.scale.set( s, s, s );
  77. group.add( mesh );
  78. addLineShape( shape, color, x, y, z, rx, ry, rz, s );
  79. }
  80. function addLineShape( shape, color, x, y, z, rx, ry, rz, s ) {
  81. var geometryPoints = new THREE.BufferGeometry();
  82. var geometrySpacedPoints = new THREE.BufferGeometry();
  83. // lines
  84. shape.autoClose = true;
  85. var points = shape.getPoints();
  86. var spacedPoints = shape.getSpacedPoints( 50 );
  87. // points
  88. var position = [];
  89. for ( var i = 0, l = points.length; i < l; i ++ ) {
  90. var point = points[ i ];
  91. position.push( point.x, point.y, 0 );
  92. }
  93. geometryPoints.addAttribute( 'position', new THREE.Float32BufferAttribute( position, 3 ) );
  94. // spaced points
  95. position = [];
  96. for ( var i = 0, l = spacedPoints.length; i < l; i ++ ) {
  97. var point = spacedPoints[ i ];
  98. position.push( point.x, point.y, 0 );
  99. }
  100. geometrySpacedPoints.addAttribute( 'position', new THREE.Float32BufferAttribute( position, 3 ) );
  101. // solid line
  102. var line = new THREE.Line( geometryPoints, new THREE.LineBasicMaterial( { color: color, linewidth: 3 } ) );
  103. line.position.set( x, y, z - 25 );
  104. line.rotation.set( rx, ry, rz );
  105. line.scale.set( s, s, s );
  106. group.add( line );
  107. // line from equidistance sampled points
  108. var line = new THREE.Line( geometrySpacedPoints, new THREE.LineBasicMaterial( { color: color, linewidth: 3 } ) );
  109. line.position.set( x, y, z + 25 );
  110. line.rotation.set( rx, ry, rz );
  111. line.scale.set( s, s, s );
  112. group.add( line );
  113. // vertices from real points
  114. var particles = new THREE.Points( geometryPoints, new THREE.PointsMaterial( { color: color, size: 4 } ) );
  115. particles.position.set( x, y, z + 75 );
  116. particles.rotation.set( rx, ry, rz );
  117. particles.scale.set( s, s, s );
  118. group.add( particles );
  119. // equidistance sampled points
  120. var particles = new THREE.Points( geometrySpacedPoints, new THREE.PointsMaterial( { color: color, size: 4 } ) );
  121. particles.position.set( x, y, z + 125 );
  122. particles.rotation.set( rx, ry, rz );
  123. particles.scale.set( s, s, s );
  124. group.add( particles );
  125. }
  126. // California
  127. var californiaPts = [];
  128. californiaPts.push( new THREE.Vector2( 610, 320 ) );
  129. californiaPts.push( new THREE.Vector2( 450, 300 ) );
  130. californiaPts.push( new THREE.Vector2( 392, 392 ) );
  131. californiaPts.push( new THREE.Vector2( 266, 438 ) );
  132. californiaPts.push( new THREE.Vector2( 190, 570 ) );
  133. californiaPts.push( new THREE.Vector2( 190, 600 ) );
  134. californiaPts.push( new THREE.Vector2( 160, 620 ) );
  135. californiaPts.push( new THREE.Vector2( 160, 650 ) );
  136. californiaPts.push( new THREE.Vector2( 180, 640 ) );
  137. californiaPts.push( new THREE.Vector2( 165, 680 ) );
  138. californiaPts.push( new THREE.Vector2( 150, 670 ) );
  139. californiaPts.push( new THREE.Vector2( 90, 737 ) );
  140. californiaPts.push( new THREE.Vector2( 80, 795 ) );
  141. californiaPts.push( new THREE.Vector2( 50, 835 ) );
  142. californiaPts.push( new THREE.Vector2( 64, 870 ) );
  143. californiaPts.push( new THREE.Vector2( 60, 945 ) );
  144. californiaPts.push( new THREE.Vector2( 300, 945 ) );
  145. californiaPts.push( new THREE.Vector2( 300, 743 ) );
  146. californiaPts.push( new THREE.Vector2( 600, 473 ) );
  147. californiaPts.push( new THREE.Vector2( 626, 425 ) );
  148. californiaPts.push( new THREE.Vector2( 600, 370 ) );
  149. californiaPts.push( new THREE.Vector2( 610, 320 ) );
  150. for( var i = 0; i < californiaPts.length; i ++ ) californiaPts[ i ].multiplyScalar( 0.25 );
  151. var californiaShape = new THREE.Shape( californiaPts );
  152. // Triangle
  153. var triangleShape = new THREE.Shape();
  154. triangleShape.moveTo( 80, 20 );
  155. triangleShape.lineTo( 40, 80 );
  156. triangleShape.lineTo( 120, 80 );
  157. triangleShape.lineTo( 80, 20 ); // close path
  158. // Heart
  159. var x = 0, y = 0;
  160. var heartShape = new THREE.Shape(); // From http://blog.burlock.org/html5/130-paths
  161. heartShape.moveTo( x + 25, y + 25 );
  162. heartShape.bezierCurveTo( x + 25, y + 25, x + 20, y, x, y );
  163. heartShape.bezierCurveTo( x - 30, y, x - 30, y + 35, x - 30, y + 35 );
  164. heartShape.bezierCurveTo( x - 30, y + 55, x - 10, y + 77, x + 25, y + 95 );
  165. heartShape.bezierCurveTo( x + 60, y + 77, x + 80, y + 55, x + 80, y + 35 );
  166. heartShape.bezierCurveTo( x + 80, y + 35, x + 80, y, x + 50, y );
  167. heartShape.bezierCurveTo( x + 35, y, x + 25, y + 25, x + 25, y + 25 );
  168. // Square
  169. var sqLength = 80;
  170. var squareShape = new THREE.Shape();
  171. squareShape.moveTo( 0, 0 );
  172. squareShape.lineTo( 0, sqLength );
  173. squareShape.lineTo( sqLength, sqLength );
  174. squareShape.lineTo( sqLength, 0 );
  175. squareShape.lineTo( 0, 0 );
  176. // Rectangle
  177. var rectLength = 120, rectWidth = 40;
  178. var rectShape = new THREE.Shape();
  179. rectShape.moveTo( 0, 0 );
  180. rectShape.lineTo( 0, rectWidth );
  181. rectShape.lineTo( rectLength, rectWidth );
  182. rectShape.lineTo( rectLength, 0 );
  183. rectShape.lineTo( 0, 0 );
  184. // Rounded rectangle
  185. var roundedRectShape = new THREE.Shape();
  186. ( function roundedRect( ctx, x, y, width, height, radius ) {
  187. ctx.moveTo( x, y + radius );
  188. ctx.lineTo( x, y + height - radius );
  189. ctx.quadraticCurveTo( x, y + height, x + radius, y + height );
  190. ctx.lineTo( x + width - radius, y + height );
  191. ctx.quadraticCurveTo( x + width, y + height, x + width, y + height - radius );
  192. ctx.lineTo( x + width, y + radius );
  193. ctx.quadraticCurveTo( x + width, y, x + width - radius, y );
  194. ctx.lineTo( x + radius, y );
  195. ctx.quadraticCurveTo( x, y, x, y + radius );
  196. } )( roundedRectShape, 0, 0, 50, 50, 20 );
  197. // Track
  198. var trackShape = new THREE.Shape();
  199. trackShape.moveTo( 40, 40 );
  200. trackShape.lineTo( 40, 160 );
  201. trackShape.absarc( 60, 160, 20, Math.PI, 0, true );
  202. trackShape.lineTo( 80, 40 );
  203. trackShape.absarc( 60, 40, 20, 2 * Math.PI, Math.PI, true );
  204. // Circle
  205. var circleRadius = 40;
  206. var circleShape = new THREE.Shape();
  207. circleShape.moveTo( 0, circleRadius );
  208. circleShape.quadraticCurveTo( circleRadius, circleRadius, circleRadius, 0 );
  209. circleShape.quadraticCurveTo( circleRadius, - circleRadius, 0, - circleRadius );
  210. circleShape.quadraticCurveTo( - circleRadius, - circleRadius, - circleRadius, 0 );
  211. circleShape.quadraticCurveTo( - circleRadius, circleRadius, 0, circleRadius );
  212. // Fish
  213. var x = y = 0;
  214. var fishShape = new THREE.Shape();
  215. fishShape.moveTo( x, y );
  216. fishShape.quadraticCurveTo( x + 50, y - 80, x + 90, y - 10 );
  217. fishShape.quadraticCurveTo( x + 100, y - 10, x + 115, y - 40 );
  218. fishShape.quadraticCurveTo( x + 115, y, x + 115, y + 40 );
  219. fishShape.quadraticCurveTo( x + 100, y + 10, x + 90, y + 10 );
  220. fishShape.quadraticCurveTo( x + 50, y + 80, x, y );
  221. // Arc circle
  222. var arcShape = new THREE.Shape();
  223. arcShape.moveTo( 50, 10 );
  224. arcShape.absarc( 10, 10, 40, 0, Math.PI * 2, false );
  225. var holePath = new THREE.Path();
  226. holePath.moveTo( 20, 10 );
  227. holePath.absarc( 10, 10, 10, 0, Math.PI * 2, true );
  228. arcShape.holes.push( holePath );
  229. // Smiley
  230. var smileyShape = new THREE.Shape();
  231. smileyShape.moveTo( 80, 40 );
  232. smileyShape.absarc( 40, 40, 40, 0, Math.PI * 2, false );
  233. var smileyEye1Path = new THREE.Path();
  234. smileyEye1Path.moveTo( 35, 20 );
  235. smileyEye1Path.absellipse( 25, 20, 10, 10, 0, Math.PI * 2, true );
  236. smileyShape.holes.push( smileyEye1Path );
  237. var smileyEye2Path = new THREE.Path();
  238. smileyEye2Path.moveTo( 65, 20 );
  239. smileyEye2Path.absarc( 55, 20, 10, 0, Math.PI * 2, true );
  240. smileyShape.holes.push( smileyEye2Path );
  241. var smileyMouthPath = new THREE.Path();
  242. smileyMouthPath.moveTo( 20, 40 );
  243. smileyMouthPath.quadraticCurveTo( 40, 60, 60, 40 );
  244. smileyMouthPath.bezierCurveTo( 70, 45, 70, 50, 60, 60 );
  245. smileyMouthPath.quadraticCurveTo( 40, 80, 20, 60 );
  246. smileyMouthPath.quadraticCurveTo( 5, 50, 20, 40 );
  247. smileyShape.holes.push( smileyMouthPath );
  248. // Spline shape
  249. var splinepts = [];
  250. splinepts.push( new THREE.Vector2( 70, 20 ) );
  251. splinepts.push( new THREE.Vector2( 80, 90 ) );
  252. splinepts.push( new THREE.Vector2( - 30, 70 ) );
  253. splinepts.push( new THREE.Vector2( 0, 0 ) );
  254. var splineShape = new THREE.Shape();
  255. splineShape.moveTo( 0, 0 );
  256. splineShape.splineThru( splinepts );
  257. var extrudeSettings = { amount: 8, bevelEnabled: true, bevelSegments: 2, steps: 2, bevelSize: 1, bevelThickness: 1 };
  258. // addShape( shape, color, x, y, z, rx, ry,rz, s );
  259. addShape( californiaShape, extrudeSettings, 0xf08000, -300, -100, 0, 0, 0, 0, 1 );
  260. addShape( triangleShape, extrudeSettings, 0x8080f0, -180, 0, 0, 0, 0, 0, 1 );
  261. addShape( roundedRectShape, extrudeSettings, 0x008000, -150, 150, 0, 0, 0, 0, 1 );
  262. addShape( trackShape, extrudeSettings, 0x008080, 200, -100, 0, 0, 0, 0, 1 );
  263. addShape( squareShape, extrudeSettings, 0x0040f0, 150, 100, 0, 0, 0, 0, 1 );
  264. addShape( heartShape, extrudeSettings, 0xf00000, 60, 100, 0, 0, 0, Math.PI, 1 );
  265. addShape( circleShape, extrudeSettings, 0x00f000, 120, 250, 0, 0, 0, 0, 1 );
  266. addShape( fishShape, extrudeSettings, 0x404040, - 60, 200, 0, 0, 0, 0, 1 );
  267. addShape( smileyShape, extrudeSettings, 0xf000f0, -200, 250, 0, 0, 0, Math.PI, 1 );
  268. addShape( arcShape, extrudeSettings, 0x804000, 150, 0, 0, 0, 0, 0, 1 );
  269. addShape( splineShape, extrudeSettings, 0x808080, - 50, -100, 0, 0, 0, 0, 1 );
  270. addLineShape( arcShape.holes[ 0 ], 0x804000, 150, 0, 0, 0, 0, 0, 1 );
  271. for ( var i = 0; i < smileyShape.holes.length; i += 1 ) {
  272. addLineShape( smileyShape.holes[ i ], 0xf000f0, - 200, 250, 0, 0, 0, Math.PI, 1 );
  273. }
  274. //
  275. renderer = new THREE.WebGLRenderer( { antialias: true } );
  276. renderer.setPixelRatio( window.devicePixelRatio );
  277. renderer.setSize( window.innerWidth, window.innerHeight );
  278. container.appendChild( renderer.domElement );
  279. stats = new Stats();
  280. container.appendChild( stats.dom );
  281. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  282. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  283. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  284. //
  285. window.addEventListener( 'resize', onWindowResize, false );
  286. }
  287. function onWindowResize() {
  288. camera.aspect = window.innerWidth / window.innerHeight;
  289. camera.updateProjectionMatrix();
  290. renderer.setSize( window.innerWidth, window.innerHeight );
  291. }
  292. //
  293. function onDocumentMouseDown( event ) {
  294. event.preventDefault();
  295. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  296. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  297. document.addEventListener( 'mouseout', onDocumentMouseOut, false );
  298. mouseXOnMouseDown = event.clientX - windowHalfX;
  299. targetRotationOnMouseDown = targetRotation;
  300. }
  301. function onDocumentMouseMove( event ) {
  302. mouseX = event.clientX - windowHalfX;
  303. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
  304. }
  305. function onDocumentMouseUp( event ) {
  306. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  307. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  308. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  309. }
  310. function onDocumentMouseOut( event ) {
  311. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  312. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  313. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  314. }
  315. function onDocumentTouchStart( event ) {
  316. if ( event.touches.length == 1 ) {
  317. event.preventDefault();
  318. mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
  319. targetRotationOnMouseDown = targetRotation;
  320. }
  321. }
  322. function onDocumentTouchMove( event ) {
  323. if ( event.touches.length == 1 ) {
  324. event.preventDefault();
  325. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  326. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
  327. }
  328. }
  329. //
  330. function animate() {
  331. requestAnimationFrame( animate );
  332. render();
  333. stats.update();
  334. }
  335. function render() {
  336. group.rotation.y += ( targetRotation - group.rotation.y ) * 0.05;
  337. renderer.render( scene, camera );
  338. }
  339. </script>
  340. </body>
  341. </html>