webgl_geometry_extrude_shapes.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - geometry - extrude 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: #222;
  11. }
  12. a {
  13. color: #f80;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <!-- Import maps polyfill -->
  19. <!-- Remove this when import maps will be widely supported -->
  20. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  21. <script type="importmap">
  22. {
  23. "imports": {
  24. "three": "../build/three.module.js",
  25. "three/addons/": "./jsm/"
  26. }
  27. }
  28. </script>
  29. <script type="module">
  30. import * as THREE from 'three';
  31. import { TrackballControls } from 'three/addons/controls/TrackballControls.js';
  32. let camera, scene, renderer, controls;
  33. init();
  34. animate();
  35. function init() {
  36. const info = document.createElement( 'div' );
  37. info.style.position = 'absolute';
  38. info.style.top = '10px';
  39. info.style.width = '100%';
  40. info.style.textAlign = 'center';
  41. info.style.color = '#fff';
  42. info.style.link = '#f80';
  43. info.innerHTML = '<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - geometry extrude shapes';
  44. document.body.appendChild( info );
  45. renderer = new THREE.WebGLRenderer( { antialias: true } );
  46. renderer.setPixelRatio( window.devicePixelRatio );
  47. renderer.setSize( window.innerWidth, window.innerHeight );
  48. renderer.useLegacyLights = false;
  49. document.body.appendChild( renderer.domElement );
  50. scene = new THREE.Scene();
  51. scene.background = new THREE.Color( 0x222222 );
  52. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
  53. camera.position.set( 0, 0, 500 );
  54. controls = new TrackballControls( camera, renderer.domElement );
  55. controls.minDistance = 200;
  56. controls.maxDistance = 500;
  57. scene.add( new THREE.AmbientLight( 0x666666 ) );
  58. const light = new THREE.PointLight( 0xffffff, 3, 0, 0 );
  59. light.position.copy( camera.position );
  60. scene.add( light );
  61. //
  62. const closedSpline = new THREE.CatmullRomCurve3( [
  63. new THREE.Vector3( - 60, - 100, 60 ),
  64. new THREE.Vector3( - 60, 20, 60 ),
  65. new THREE.Vector3( - 60, 120, 60 ),
  66. new THREE.Vector3( 60, 20, - 60 ),
  67. new THREE.Vector3( 60, - 100, - 60 )
  68. ] );
  69. closedSpline.curveType = 'catmullrom';
  70. closedSpline.closed = true;
  71. const extrudeSettings1 = {
  72. steps: 100,
  73. bevelEnabled: false,
  74. extrudePath: closedSpline
  75. };
  76. const pts1 = [], count = 3;
  77. for ( let i = 0; i < count; i ++ ) {
  78. const l = 20;
  79. const a = 2 * i / count * Math.PI;
  80. pts1.push( new THREE.Vector2( Math.cos( a ) * l, Math.sin( a ) * l ) );
  81. }
  82. const shape1 = new THREE.Shape( pts1 );
  83. const geometry1 = new THREE.ExtrudeGeometry( shape1, extrudeSettings1 );
  84. const material1 = new THREE.MeshLambertMaterial( { color: 0xb00000, wireframe: false } );
  85. const mesh1 = new THREE.Mesh( geometry1, material1 );
  86. scene.add( mesh1 );
  87. //
  88. const randomPoints = [];
  89. for ( let i = 0; i < 10; i ++ ) {
  90. randomPoints.push( new THREE.Vector3( ( i - 4.5 ) * 50, THREE.MathUtils.randFloat( - 50, 50 ), THREE.MathUtils.randFloat( - 50, 50 ) ) );
  91. }
  92. const randomSpline = new THREE.CatmullRomCurve3( randomPoints );
  93. //
  94. const extrudeSettings2 = {
  95. steps: 200,
  96. bevelEnabled: false,
  97. extrudePath: randomSpline
  98. };
  99. const pts2 = [], numPts = 5;
  100. for ( let i = 0; i < numPts * 2; i ++ ) {
  101. const l = i % 2 == 1 ? 10 : 20;
  102. const a = i / numPts * Math.PI;
  103. pts2.push( new THREE.Vector2( Math.cos( a ) * l, Math.sin( a ) * l ) );
  104. }
  105. const shape2 = new THREE.Shape( pts2 );
  106. const geometry2 = new THREE.ExtrudeGeometry( shape2, extrudeSettings2 );
  107. const material2 = new THREE.MeshLambertMaterial( { color: 0xff8000, wireframe: false } );
  108. const mesh2 = new THREE.Mesh( geometry2, material2 );
  109. scene.add( mesh2 );
  110. //
  111. const materials = [ material1, material2 ];
  112. const extrudeSettings3 = {
  113. depth: 20,
  114. steps: 1,
  115. bevelEnabled: true,
  116. bevelThickness: 2,
  117. bevelSize: 4,
  118. bevelSegments: 1
  119. };
  120. const geometry3 = new THREE.ExtrudeGeometry( shape2, extrudeSettings3 );
  121. const mesh3 = new THREE.Mesh( geometry3, materials );
  122. mesh3.position.set( 50, 100, 50 );
  123. scene.add( mesh3 );
  124. }
  125. function animate() {
  126. requestAnimationFrame( animate );
  127. controls.update();
  128. renderer.render( scene, camera );
  129. }
  130. </script>
  131. </body>
  132. </html>