webgl_geometry_shapes.html 14 KB

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