轉 threejs中3D視野的縮放實現

來源:互聯網
上載者:User

標籤:round   監聽   縮小   for   height   參數   com   tor   角度   

Threejs基礎部分學習知道透視相機new THREE.PerspectiveCamera(fov, aspect , near,far)中。

fov視野角(拍攝距離)越大,情境中的物體越小。fov視野角(拍攝距離)越小,情境中的物體越大。

透視相機(近大遠小)  PerspectiveCamera  
//透視照相機參數設定var fov = 45,//拍攝距離  視野角值越大,情境中的物體越小    near = 1,//相機離視體積最近的距離    far = 1000,//相機離視體積最遠的距離    aspect =  window.innerWidth / window.innerHeight; //縱橫比var camera = new THREE.PerspectiveCamera(fov,aspect, near, far);
改變fov的值,並更新這個照相機
//改變fov值,並更新情境的渲染camera.fov = fov;camera.updateProjectionMatrix();renderer.render(scene, camera); //updateinfo();
滑鼠上下滑輪實現放大縮小效果  代碼如下
//監聽滑鼠滾動事件canvas.addEventListener(‘mousewheel‘, mousewheel, false);
//滑鼠滑輪-滑鼠上下滑輪實現放大縮小效果function mousewheel(e) {            e.preventDefault();            //e.stopPropagation();            if (e.wheelDelta) {  //判斷瀏覽器IE,Google滑輪事件                if (e.wheelDelta > 0) { //當滑輪向上滾動時                    fov -= (near < fov ? 1 : 0);                }                if (e.wheelDelta < 0) { //當滑輪向下滾動時                    fov += (fov < far ? 1 : 0);                }            } else if (e.detail) {  //Firefox滑輪事件                if (e.detail > 0) { //當滑輪向上滾動時                    fov -= 1;                }                if (e.detail < 0) { //當滑輪向下滾動時                    fov += 1;                }            }            //改變fov值,並更新情境的渲染            camera.fov = fov;            camera.updateProjectionMatrix();            renderer.render(scene, camera);            //updateinfo();}

實現效果完整代碼  標註具體案例為個人原創

<!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; //畫布高        //照相機配置        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() {  //透視相機                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; //相機朝向--相機上方為y軸                camera.up.z = 0;                camera.lookAt({  //相機的中心點                    x : 0,                    y : 0,                    z : 0                });            }            function initLight(){            // light--這裡使用環境光線            //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時可以忽略方向和角度,只考慮光源的位置            scene.add(light);*/            }            function initObject(){  //初始化對象            //初始化地板            initFloor();            }            function initGrid(){ //輔助網格                var helper = new THREE.GridHelper( 1000, 50 );                helper.setColors( 0x0000ff, 0x808080 );                scene.add( helper );            }            function initFloor(){            //建立一個立方體            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();        //初始透視化相機                initCamera();                //初始化光源                initLight();                //模型對象                initObject();                //初始化網格輔助線                initGrid();                //渲染                renderer.render(scene, camera);                //即時動畫        //animation();        //監聽滑鼠滾動事件        canvas.addEventListener(‘mousewheel‘, mousewheel, false);            }            function animation(){            //相機圍繞y軸旋轉,並且保持情境中的物體一直再相機的視野中            //即時渲染成像            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);            }        //滑鼠滑輪-滑鼠上下滑輪實現放大縮小效果        function mousewheel(e) {            e.preventDefault();            //e.stopPropagation();            if (e.wheelDelta) {  //判斷瀏覽器IE,Google滑輪事件                if (e.wheelDelta > 0) { //當滑輪向上滾動時                    fov -= (near < fov ? 1 : 0);                }                if (e.wheelDelta < 0) { //當滑輪向下滾動時                    fov += (fov < far ? 1 : 0);                }            } else if (e.detail) {  //Firefox滑輪事件                if (e.detail > 0) { //當滑輪向上滾動時                    fov -= 1;                }                if (e.detail < 0) { //當滑輪向下滾動時                    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>

文章縮放來源  http://blog.csdn.net/u_9_5/article/details/50542847

轉 threejs中3D視野的縮放實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.