webgl_geometry_shapes.html 14 KB

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