webgl_geometry_extrude_shapes.html 4.2 KB

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