webgl_geometry_shapes.html 13 KB

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