10-THREE.JS perspective透视摄像机和orthographic正交摄像机区别
<!DOCTYPE html> <html> <head> <title></title> <script src="https://cdn.bootcss.com/three.js/r67/three.js"></script> <script src="https://cdn.bootcss.com/stats.js/r10/Stats.min.js"></script> <script type="text/javascript" src="https://cdn.bootcss.com/dat-gui/0.7.3/dat.gui.js"></script> <style> body { margin: 0; overflow: hidden; } </style> </head> <body> <div id="Stats-output"> </div> <div id="WebGL-output"> </div> <script type="text/javascript"> // 初始化 function init() { var stats = initStats(); // 创建一个场景 var scene = new THREE.Scene(); // 创建一个相机 var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000); camera.position.x = 120; camera.position.y = 60; camera.position.z = 180; // 创建渲染器 var renderer = new THREE.WebGLRenderer(); renderer.setClearColor(new THREE.Color(0xEEEEEE, 1.0)); renderer.setSize(window.innerWidth, window.innerHeight); // 创建地面 var planeGeometry = new THREE.PlaneGeometry(180, 180); var planeMaterial = new THREE.MeshLambertMaterial({color: 0xffffff}); var plane = new THREE.Mesh(planeGeometry, planeMaterial); // 地面保持水平 plane.rotation.x = -0.5 * Math.PI; plane.position.x = 0; plane.position.y = 0; plane.position.z = 0; // 地面添加到场景中去 scene.add(plane); var cubeGeometry = new THREE.BoxGeometry(4, 4, 4); for (var j = 0; j < (planeGeometry.parameters.height / 5); j++) { for (var i = 0; i < planeGeometry.parameters.width / 5; i++) { var rnd = Math.random() * 0.75 + 0.25; var cubeMaterial = new THREE.MeshLambertMaterial(); cubeMaterial.color = new THREE.Color(rnd, 0, 0); var cube = new THREE.Mesh(cubeGeometry, cubeMaterial); cube.position.z = -((planeGeometry.parameters.height) / 2) + 2 + (j * 5); cube.position.x = -((planeGeometry.parameters.width) / 2) + 2 + (i * 5); cube.position.y = 2; scene.add(cube); } } var directionalLight = new THREE.DirectionalLight(0xffffff, 0.7); directionalLight.position.set(-20, 40, 60); scene.add(directionalLight); // 添加自然光 var ambientLight = new THREE.AmbientLight(0x292929); scene.add(ambientLight); // 添加场景到DOM元素中去 document.getElementById("WebGL-output").appendChild(renderer.domElement); var step = 0; //perspective透视摄像机 和 orthographic正交摄像机 替换函数 var controls = new function () { this.perspective = "Perspective"; this.switchCamera = function () { if (camera instanceof THREE.PerspectiveCamera) { camera = new THREE.OrthographicCamera(window.innerWidth / -16, window.innerWidth / 16, window.innerHeight / 16, window.innerHeight / -16, -200, 500); camera.position.x = 120; camera.position.y = 60; camera.position.z = 180; camera.lookAt(scene.position); this.perspective = "Orthographic"; } else { camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000); camera.position.x = 120; camera.position.y = 60; camera.position.z = 180; camera.lookAt(scene.position); this.perspective = "Perspective"; } }; }; var gui = new dat.GUI(); gui.add(controls, 'switchCamera'); gui.add(controls, 'perspective').listen(); //相机看向场景 camera.lookAt(scene.position); render(); function render() { stats.update(); requestAnimationFrame(render); renderer.render(scene, camera); } function initStats() { var stats = new Stats(); stats.setMode(0); // 0: fps, 1: ms stats.domElement.style.position = 'absolute'; stats.domElement.style.left = '0px'; stats.domElement.style.top = '0px'; document.getElementById("Stats-output").appendChild(stats.domElement); return stats; } } window.onload = init </script> </body> </html>
对于这两个相机官方有一个示例更容易让人理解
https://threejs.org/examples/#webgl_camera
按O和P两个按钮切换相机