您好,登錄后才能下訂單哦!
前言
本文主要給大家介紹了關(guān)于Three.js利用orbit controls插件(軌道控制)控制模型交互動(dòng)作的相關(guān)內(nèi)容,這個(gè)效果相對(duì)于第八節(jié)的軌跡球插件使用上感覺(jué)要好,雖然軌跡球插件可以來(lái)回的滾動(dòng),但是容易分辨不清楚上下左右的關(guān)系,容易混亂,適合調(diào)試,而軌道控制插件orbit則適合客戶(hù)使用,還不會(huì)產(chǎn)生混亂效果。下面講一下使用。
(1)首先引入插件,文件地址在官方案例的examples/js/controls/OrbitControls.js。
(2)然后實(shí)例化函數(shù),把相機(jī)和渲染器的dom傳入,并設(shè)置相關(guān)設(shè)置。
//用戶(hù)交互插件 鼠標(biāo)左鍵按住旋轉(zhuǎn),右鍵按住平移,滾輪縮放 var controls; function initControls() { controls = new THREE.OrbitControls( camera, renderer.domElement ); // 如果使用animate方法時(shí),將此函數(shù)刪除 //controls.addEventListener( 'change', render ); // 使動(dòng)畫(huà)循環(huán)使用時(shí)阻尼或自轉(zhuǎn) 意思是否有慣性 controls.enableDamping = true; //動(dòng)態(tài)阻尼系數(shù) 就是鼠標(biāo)拖拽旋轉(zhuǎn)靈敏度 //controls.dampingFactor = 0.25; //是否可以縮放 controls.enableZoom = true; //是否自動(dòng)旋轉(zhuǎn) controls.autoRotate = true; //設(shè)置相機(jī)距離原點(diǎn)的最遠(yuǎn)距離 controls.minDistance = 200; //設(shè)置相機(jī)距離原點(diǎn)的最遠(yuǎn)距離 controls.maxDistance = 600; //是否開(kāi)啟右鍵拖拽 controls.enablePan = true; }
(3)最后,在animate函數(shù)內(nèi)調(diào)用orbit的update()
更新。
function animate() { //更新控制器 controls.update(); render(); //更新性能插件 stats.update(); requestAnimationFrame(animate); }
就實(shí)現(xiàn)了相關(guān)效果。
下面是全部案例代碼:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style type="text/css"> html, body { margin: 0; height: 100%; } canvas { display: block; } </style> </head> <body onload="draw();"> </body> <script src="build/three.js"></script> <script src="examples/js/controls/OrbitControls.js"></script> <script src="examples/js/libs/stats.min.js"></script> <script> var renderer; function initRender() { renderer = new THREE.WebGLRenderer({antialias:true}); renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement); } var camera; function initCamera() { camera = new THREE.PerspectiveCamera(45, window.innerWidth/window.innerHeight, 1, 10000); camera.position.set(0, 0, 400); } var scene; function initScene() { scene = new THREE.Scene(); } var light; function initLight() { scene.add(new THREE.AmbientLight(0x404040)); light = new THREE.DirectionalLight(0xffffff); light.position.set(1,1,1); scene.add(light); } function initModel() { var map = new THREE.TextureLoader().load("examples/textures/UV_Grid_Sm.jpg"); var material = new THREE.MeshLambertMaterial({map:map}); var cube = new THREE.Mesh(new THREE.BoxGeometry(100, 200, 100, 1, 1, 1), material); scene.add(cube); } //初始化性能插件 var stats; function initStats() { stats = new Stats(); document.body.appendChild(stats.dom); } //用戶(hù)交互插件 鼠標(biāo)左鍵按住旋轉(zhuǎn),右鍵按住平移,滾輪縮放 var controls; function initControls() { controls = new THREE.OrbitControls( camera, renderer.domElement ); // 如果使用animate方法時(shí),將此函數(shù)刪除 //controls.addEventListener( 'change', render ); // 使動(dòng)畫(huà)循環(huán)使用時(shí)阻尼或自轉(zhuǎn) 意思是否有慣性 controls.enableDamping = true; //動(dòng)態(tài)阻尼系數(shù) 就是鼠標(biāo)拖拽旋轉(zhuǎn)靈敏度 //controls.dampingFactor = 0.25; //是否可以縮放 controls.enableZoom = true; //是否自動(dòng)旋轉(zhuǎn) controls.autoRotate = true; //設(shè)置相機(jī)距離原點(diǎn)的最遠(yuǎn)距離 controls.minDistance = 200; //設(shè)置相機(jī)距離原點(diǎn)的最遠(yuǎn)距離 controls.maxDistance = 600; //是否開(kāi)啟右鍵拖拽 controls.enablePan = true; } function render() { renderer.render( scene, camera ); } //窗口變動(dòng)觸發(fā)的函數(shù) function onWindowResize() { camera.aspect = window.innerWidth / window.innerHeight; camera.updateProjectionMatrix(); render(); renderer.setSize( window.innerWidth, window.innerHeight ); } function animate() { //更新控制器 controls.update(); render(); //更新性能插件 stats.update(); requestAnimationFrame(animate); } function draw() { initRender(); initScene(); initCamera(); initLight(); initModel(); initControls(); initStats(); animate(); window.onresize = onWindowResize; } </script> </html>
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。