所謂透視投影相機(PerspectiveCamera),基本等同於人的眼睛,意思就是:離的遠的物體會顯得比較小,近處的物體會比較大.
透視投影相機的構造函數:javascript
PerspectiveCamera(fov,aspect,near,far)複製代碼
根據上圖來理解透視投影相機的4個參數:java
var scene, camera, renderer, cube;
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000);
renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
camera.position.set(0, 0, 5);
scene.add(camera);
cube = new THREE.Mesh(new THREE.BoxGeometry(1, 1, 1),
new THREE.MeshBasicMaterial({
color: 0xff0000,
wireframe: true
})
);
scene.add(cube);
document.body.appendChild(renderer.domElement);
renderer.render(scene, camera);複製代碼
獲得下圖的結果:正投影相機的構造函數:OrthographicCamera(left,right,top,bottom,near,far)
canvas
var scene, camera, renderer, cube;
scene = new THREE.Scene();
camera = new THREE.OrthographicCamera(-2, 2, 1.5, -1.5, 1, 10);
camera.position.set(0, 0, 5);
renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
scene.add(camera);
cube = new THREE.Mesh(new THREE.BoxGeometry(1, 1, 1),
new THREE.MeshBasicMaterial({
color: 0xff0000,
wireframe: true
})
);
scene.add(cube);
document.body.appendChild(renderer.domElement);
renderer.render(scene, camera);複製代碼
獲得以下結果:
app
document.addEventListener('keydown', function (event) {
switch (event.keyCode) {
case 37:
camera.position.x -= 0.1;
break;
case 38:
camera.position.y -= 0.1;
break;
case 39:
camera.position.x += 0.1;
break;
case 40:
camera.position.y += 0.1;
break;
}
renderer.render(scene, camera);
});複製代碼