webgl_geometry_shapes.html 13 KB

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