123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - geometry - extrude shapes</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- <style>
- body {
- background-color: #222;
- }
- a {
- color: #f80;
- }
- </style>
- </head>
- <body>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import { TrackballControls } from './jsm/controls/TrackballControls.js';
- var camera, scene, renderer, controls;
- init();
- animate();
- function init() {
- var info = document.createElement( 'div' );
- info.style.position = 'absolute';
- info.style.top = '10px';
- info.style.width = '100%';
- info.style.textAlign = 'center';
- info.style.color = '#fff';
- info.style.link = '#f80';
- info.innerHTML = '<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - geometry extrude shapes';
- document.body.appendChild( info );
- renderer = new THREE.WebGLRenderer();
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- document.body.appendChild( renderer.domElement );
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0x222222 );
- camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
- camera.position.set( 0, 0, 500 );
- controls = new TrackballControls( camera, renderer.domElement );
- controls.minDistance = 200;
- controls.maxDistance = 500;
- scene.add( new THREE.AmbientLight( 0x222222 ) );
- var light = new THREE.PointLight( 0xffffff );
- light.position.copy( camera.position );
- scene.add( light );
- //
- var closedSpline = new THREE.CatmullRomCurve3( [
- new THREE.Vector3( - 60, - 100, 60 ),
- new THREE.Vector3( - 60, 20, 60 ),
- new THREE.Vector3( - 60, 120, 60 ),
- new THREE.Vector3( 60, 20, - 60 ),
- new THREE.Vector3( 60, - 100, - 60 )
- ] );
- closedSpline.curveType = 'catmullrom';
- closedSpline.closed = true;
- var extrudeSettings = {
- steps: 100,
- bevelEnabled: false,
- extrudePath: closedSpline
- };
- var pts = [], count = 3;
- for ( var i = 0; i < count; i ++ ) {
- var l = 20;
- var a = 2 * i / count * Math.PI;
- pts.push( new THREE.Vector2( Math.cos( a ) * l, Math.sin( a ) * l ) );
- }
- var shape = new THREE.Shape( pts );
- var geometry = new THREE.ExtrudeBufferGeometry( shape, extrudeSettings );
- var material = new THREE.MeshLambertMaterial( { color: 0xb00000, wireframe: false } );
- var mesh = new THREE.Mesh( geometry, material );
- scene.add( mesh );
- //
- var randomPoints = [];
- for ( var i = 0; i < 10; i ++ ) {
- randomPoints.push( new THREE.Vector3( ( i - 4.5 ) * 50, THREE.MathUtils.randFloat( - 50, 50 ), THREE.MathUtils.randFloat( - 50, 50 ) ) );
- }
- var randomSpline = new THREE.CatmullRomCurve3( randomPoints );
- //
- var extrudeSettings = {
- steps: 200,
- bevelEnabled: false,
- extrudePath: randomSpline
- };
- var pts = [], numPts = 5;
- for ( var i = 0; i < numPts * 2; i ++ ) {
- var l = i % 2 == 1 ? 10 : 20;
- var a = i / numPts * Math.PI;
- pts.push( new THREE.Vector2( Math.cos( a ) * l, Math.sin( a ) * l ) );
- }
- var shape = new THREE.Shape( pts );
- var geometry = new THREE.ExtrudeBufferGeometry( shape, extrudeSettings );
- var material2 = new THREE.MeshLambertMaterial( { color: 0xff8000, wireframe: false } );
- var mesh = new THREE.Mesh( geometry, material2 );
- scene.add( mesh );
- //
- var materials = [ material, material2 ];
- var extrudeSettings = {
- depth: 20,
- steps: 1,
- bevelEnabled: true,
- bevelThickness: 2,
- bevelSize: 4,
- bevelSegments: 1
- };
- var geometry = new THREE.ExtrudeBufferGeometry( shape, extrudeSettings );
- var mesh = new THREE.Mesh( geometry, materials );
- mesh.position.set( 50, 100, 50 );
- scene.add( mesh );
- }
- function animate() {
- requestAnimationFrame( animate );
- controls.update();
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|