webgl_geometry_extrude_shapes.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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();
  46. renderer.setPixelRatio( window.devicePixelRatio );
  47. renderer.setSize( window.innerWidth, window.innerHeight );
  48. document.body.appendChild( renderer.domElement );
  49. scene = new THREE.Scene();
  50. scene.background = new THREE.Color( 0x222222 );
  51. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
  52. camera.position.set( 0, 0, 500 );
  53. controls = new TrackballControls( camera, renderer.domElement );
  54. controls.minDistance = 200;
  55. controls.maxDistance = 500;
  56. scene.add( new THREE.AmbientLight( 0x222222 ) );
  57. const light = new THREE.PointLight( 0xffffff );
  58. light.position.copy( camera.position );
  59. scene.add( light );
  60. //
  61. const closedSpline = new THREE.CatmullRomCurve3( [
  62. new THREE.Vector3( - 60, - 100, 60 ),
  63. new THREE.Vector3( - 60, 20, 60 ),
  64. new THREE.Vector3( - 60, 120, 60 ),
  65. new THREE.Vector3( 60, 20, - 60 ),
  66. new THREE.Vector3( 60, - 100, - 60 )
  67. ] );
  68. closedSpline.curveType = 'catmullrom';
  69. closedSpline.closed = true;
  70. const extrudeSettings1 = {
  71. steps: 100,
  72. bevelEnabled: false,
  73. extrudePath: closedSpline
  74. };
  75. const pts1 = [], count = 3;
  76. for ( let i = 0; i < count; i ++ ) {
  77. const l = 20;
  78. const a = 2 * i / count * Math.PI;
  79. pts1.push( new THREE.Vector2( Math.cos( a ) * l, Math.sin( a ) * l ) );
  80. }
  81. const shape1 = new THREE.Shape( pts1 );
  82. const geometry1 = new THREE.ExtrudeGeometry( shape1, extrudeSettings1 );
  83. const material1 = new THREE.MeshLambertMaterial( { color: 0xb00000, wireframe: false } );
  84. const mesh1 = new THREE.Mesh( geometry1, material1 );
  85. scene.add( mesh1 );
  86. //
  87. const randomPoints = [];
  88. for ( let i = 0; i < 10; i ++ ) {
  89. randomPoints.push( new THREE.Vector3( ( i - 4.5 ) * 50, THREE.MathUtils.randFloat( - 50, 50 ), THREE.MathUtils.randFloat( - 50, 50 ) ) );
  90. }
  91. const randomSpline = new THREE.CatmullRomCurve3( randomPoints );
  92. //
  93. const extrudeSettings2 = {
  94. steps: 200,
  95. bevelEnabled: false,
  96. extrudePath: randomSpline
  97. };
  98. const pts2 = [], numPts = 5;
  99. for ( let i = 0; i < numPts * 2; i ++ ) {
  100. const l = i % 2 == 1 ? 10 : 20;
  101. const a = i / numPts * Math.PI;
  102. pts2.push( new THREE.Vector2( Math.cos( a ) * l, Math.sin( a ) * l ) );
  103. }
  104. const shape2 = new THREE.Shape( pts2 );
  105. const geometry2 = new THREE.ExtrudeGeometry( shape2, extrudeSettings2 );
  106. const material2 = new THREE.MeshLambertMaterial( { color: 0xff8000, wireframe: false } );
  107. const mesh2 = new THREE.Mesh( geometry2, material2 );
  108. scene.add( mesh2 );
  109. //
  110. const materials = [ material1, material2 ];
  111. const extrudeSettings3 = {
  112. depth: 20,
  113. steps: 1,
  114. bevelEnabled: true,
  115. bevelThickness: 2,
  116. bevelSize: 4,
  117. bevelSegments: 1
  118. };
  119. const geometry3 = new THREE.ExtrudeGeometry( shape2, extrudeSettings3 );
  120. const mesh3 = new THREE.Mesh( geometry3, materials );
  121. mesh3.position.set( 50, 100, 50 );
  122. scene.add( mesh3 );
  123. }
  124. function animate() {
  125. requestAnimationFrame( animate );
  126. controls.update();
  127. renderer.render( scene, camera );
  128. }
  129. </script>
  130. </body>
  131. </html>