three.js中3D視野的縮放實(shí)現(xiàn)代碼
通過Threejs基礎(chǔ)學(xué)習(xí)——修改版知道創(chuàng)建一個(gè)相機(jī)的相關(guān)知識點(diǎn)
var camera = new THREE.PerspectiveCamera( fov, aspect , near,far );
視野角:fov 這里視野角(有的地方叫拍攝距離)越大,場景中的物體越小,視野角越小,場景中的物體越大
縱橫比:aspect (3d物體的寬/高比例)
相機(jī)離視體積最近的距離:near
相機(jī)離視體積最遠(yuǎn)的距離:far
其中fov視野角(拍攝距離)越大,場景中的物體越小。fov視野角(拍攝距離)越小,場景中的物體越大。
透視相機(jī)(近大遠(yuǎn)小) PerspectiveCamera
//透視照相機(jī)參數(shù)設(shè)置 var fov = 45,//拍攝距離 視野角值越大,場景中的物體越小 near = 1,//相機(jī)離視體積最近的距離 far = 1000,//相機(jī)離視體積最遠(yuǎn)的距離 aspect = window.innerWidth / window.innerHeight; //縱橫比 var camera = new THREE.PerspectiveCamera(fov,aspect, near, far);
改變fov的值,并更新這個(gè)照相機(jī)
//改變fov值,并更新場景的渲染 camera.fov = fov; camera.updateProjectionMatrix(); renderer.render(scene, camera); //updateinfo();
鼠標(biāo)上下滑輪實(shí)現(xiàn)放大縮小效果 代碼如下
//監(jiān)聽鼠標(biāo)滾動(dòng)事件 canvas.addEventListener('mousewheel', mousewheel, false);
//鼠標(biāo)滑輪-鼠標(biāo)上下滑輪實(shí)現(xiàn)放大縮小效果 function mousewheel(e) { e.preventDefault(); //e.stopPropagation(); if (e.wheelDelta) { //判斷瀏覽器IE,谷歌滑輪事件 if (e.wheelDelta > 0) { //當(dāng)滑輪向上滾動(dòng)時(shí) fov -= (near < fov ? 1 : 0); } if (e.wheelDelta < 0) { //當(dāng)滑輪向下滾動(dòng)時(shí) fov += (fov < far ? 1 : 0); } } else if (e.detail) { //Firefox滑輪事件 if (e.detail > 0) { //當(dāng)滑輪向上滾動(dòng)時(shí) fov -= 1; } if (e.detail < 0) { //當(dāng)滑輪向下滾動(dòng)時(shí) fov += 1; } } //改變fov值,并更新場景的渲染 camera.fov = fov; camera.updateProjectionMatrix(); renderer.render(scene, camera); //updateinfo(); }
實(shí)現(xiàn)效果完整代碼 標(biāo)注具體案例為個(gè)人原創(chuàng)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>threejs中3D視野的縮放</title> <style> #canvas-frame { width: 100%; height: 600px; } </style> </head> <body onload="threeStart()"> <div id="canvas-frame" ></div> </body> <script type="text/javascript" src="./lib/three.js" ></script> <script type="text/javascript"> var renderer, //渲染器 width = document.getElementById('canvas-frame').clientWidth, //畫布寬 height = document.getElementById('canvas-frame').clientHeight; //畫布高 //照相機(jī)配置 var fov = 45,//拍攝距離 視野角值越大,場景中的物體越小 near = 1,//最小范圍 far = 1000;//最大范圍 //DOM對象 var canvas = null; //初始化DOM對象 function initDOM(){ canvas = document.getElementById("canvas-frame"); } //初始化渲染器 function initThree(){ renderer = new THREE.WebGLRenderer({ antialias : true //canvas: document.getElementById('canvas-frame') }); renderer.setSize(width, height); renderer.setClearColor(0xFFFFFF, 1.0); document.getElementById('canvas-frame').appendChild(renderer.domElement); renderer.setClearColor(0xFFFFFF, 1.0); } //初始化場景 var scene; function initScene(){ scene = new THREE.Scene(); } var camera; function initCamera() { //透視相機(jī) camera = new THREE.PerspectiveCamera(fov, width/height , near, far); camera.position.x = 150; camera.position.y = 150; camera.position.z =250; camera.up.x = 0; camera.up.y = 1; //相機(jī)朝向--相機(jī)上方為y軸 camera.up.z = 0; camera.lookAt({ //相機(jī)的中心點(diǎn) x : 0, y : 0, z : 0 }); } function initLight(){ // light--這里使用環(huán)境光 //var light = new THREE.DirectionalLight(0xffffff); /*方向性光源*/ //light.position.set(600, 1000, 800); /* var light = new THREE.AmbientLight(0xffffff); //模擬漫反射光源 light.position.set(600, 1000, 800); //使用Ambient Light時(shí)可以忽略方向和角度,只考慮光源的位置 scene.add(light);*/ } function initObject(){ //初始化對象 //初始化地板 initFloor(); } function initGrid(){ //輔助網(wǎng)格 var helper = new THREE.GridHelper( 1000, 50 ); helper.setColors( 0x0000ff, 0x808080 ); scene.add( helper ); } function initFloor(){ //創(chuàng)建一個(gè)立方體 var geometry = new THREE.BoxGeometry(80, 20, 80); for ( var i = 0; i < geometry.faces.length; i += 2 ) { var hex = Math.random() * 0xffffff; geometry.faces[ i ].color.setHex( hex ); geometry.faces[ i + 1 ].color.setHex( hex ); } var material = new THREE.MeshBasicMaterial( { vertexColors: THREE.FaceColors} ); //將material材料添加到幾何體geometry var mesh = new THREE.Mesh(geometry, material); mesh.position = new THREE.Vector3(0,0,0); scene.add(mesh); } //初始化頁面加載 function threeStart(){ //初始化DOM對象 initDOM(); //初始化渲染器 initThree(); //初始化場景 initScene(); //初始透視化相機(jī) initCamera(); //初始化光源 initLight(); //模型對象 initObject(); //初始化網(wǎng)格輔助線 initGrid(); //渲染 renderer.render(scene, camera); //實(shí)時(shí)動(dòng)畫 //animation(); //監(jiān)聽鼠標(biāo)滾動(dòng)事件 canvas.addEventListener('mousewheel', mousewheel, false); } function animation(){ //相機(jī)圍繞y軸旋轉(zhuǎn),并且保持場景中的物體一直再相機(jī)的視野中 //實(shí)時(shí)渲染成像 var timer = Date.now()*0.0001; camera.position.x = Math.cos(timer)*100; camera.position.z = Math.sin(timer)*100; camera.lookAt(scene.position); renderer.render(scene, camera); requestAnimationFrame(animation); } //鼠標(biāo)滑輪-鼠標(biāo)上下滑輪實(shí)現(xiàn)放大縮小效果 function mousewheel(e) { e.preventDefault(); //e.stopPropagation(); if (e.wheelDelta) { //判斷瀏覽器IE,谷歌滑輪事件 if (e.wheelDelta > 0) { //當(dāng)滑輪向上滾動(dòng)時(shí) fov -= (near < fov ? 1 : 0); } if (e.wheelDelta < 0) { //當(dāng)滑輪向下滾動(dòng)時(shí) fov += (fov < far ? 1 : 0); } } else if (e.detail) { //Firefox滑輪事件 if (e.detail > 0) { //當(dāng)滑輪向上滾動(dòng)時(shí) fov -= 1; } if (e.detail < 0) { //當(dāng)滑輪向下滾動(dòng)時(shí) fov += 1; } } console.info('camera.fov:'+camera.fov); console.info('camera.x:'+camera.position.x); console.info('camera.y:'+camera.position.y); console.info('camera.z:'+camera.position.z); //改變fov值,并更新場景的渲染 camera.fov = fov; camera.updateProjectionMatrix(); renderer.render(scene, camera); //updateinfo(); } </script> </html>
文章縮放來源:three.js實(shí)現(xiàn)3D視野縮放效果
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
javascript中的變量作用域以及變量提升詳細(xì)介紹
在javascript中, 理解變量的作用域以及變量提升是非常有必要的。這個(gè)看起來是否很簡單,但其實(shí)并不是你想的那樣,還要一些重要的細(xì)節(jié)你需要理解2013-10-10javascript 密碼強(qiáng)度驗(yàn)證規(guī)則、打分、驗(yàn)證(給出前端代碼,后端代碼可根據(jù)強(qiáng)度規(guī)則翻譯)
密碼強(qiáng)度是一個(gè)很普遍的功能,比較簡單,主要是怎么制定這個(gè)強(qiáng)度規(guī)則。2010-05-05JavaScript實(shí)現(xiàn)隨機(jī)點(diǎn)名器
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)隨機(jī)點(diǎn)名器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03javascript使用smipleChart實(shí)現(xiàn)簡單圖表
這篇文章主要介紹了javascript使用smipleChart實(shí)現(xiàn)簡單圖表的方法及示例分享,需要的朋友可以參考下2015-01-01JS+CSS模擬可以無刷新顯示內(nèi)容的留言板實(shí)例
這篇文章主要介紹了JS+CSS模擬可以無刷新顯示內(nèi)容的留言板,涉及javascript操作dom元素、鼠標(biāo)事件及css樣式的技巧,需要的朋友可以參考下2015-03-03