three.js 小例子分析
阿新 • • 發佈:2019-01-04
這是一個官方的小例子就是分析學習下。
https://threejs.org/examples/#webgl_geometry_extrude_shapes
開頭的引入js沒什麼好說
<script src="../build/three.js"></script>
//這是滑鼠控制的工具
<script src="js/controls/TrackballControls.js"></script>
接著分析程式碼
var camera, scene, renderer, controls;
init();
animate();
開頭簡單例子就幫忙總結了一下three.js 大舞臺搭建
- camera 攝像機
- scene 場景(畫板)
- renderer 渲染器 (畫筆)
- controls 這裡就是場景畫面的控制器
- init() 初始化上面的幾個元素
- animate() 動畫,場景變換
下面開始看看init()到底都做了些什麼
function init() { //初始化畫筆 renderer = new THREE.WebGLRenderer(); //設定裝置畫素比。通常用於避免HiDPI裝置上繪圖模糊 //不懂的PixelRatio的同學可以看下這篇部落格 //https://www.cnblogs.com/timejs/p/5409533.html renderer.setPixelRatio( window.devicePixelRatio ); //將輸出canvas的大小調整為(width, height)並考慮裝置畫素比 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 THREE.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 ); //使用Catmull-Rom演算法從一系列點建立平滑的三維樣條曲線 。 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 //THREE.CurvePath。應該擠出形狀的3D樣條路徑 }; 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.Math.randFloat( - 50, 50 ), THREE.Math.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.ad d( mesh ); }
animate() 方法
function animate() {
requestAnimationFrame( animate );
//控制器變化更新檢視
controls.update();
renderer.render( scene, camera );
}