webgl_geometry_extrude_shapes.html 4.4 KB

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