webgl_geometry_extrude_shapes.html 4.7 KB

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