webgl_geometry_shapes.html 13 KB

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