我開發了一個簡單的three.js
應用程序,呈現立方體。我創建了三個文件:index.html
,viewer_style.css
和viewer.js
。three.js正交相機:縮放所有角度的立方體
的index.html
內容如下:
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8>
<title>My first three.js app</title>
<link rel='stylesheet' type='text/css' href='viewer_style.css'>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="js/three.js"></script>
<script src="js/controls/OrbitControls.js"></script>
<script src="js/renderers/Projector.js"></script>
</head>
<body>
<script src="viewer.js"></script>
</body>
</html>
和viewer.js
內容如下:
// SCENE
var scene = new THREE.Scene();
// CAMERA
var frustumHeight;
var aspect = window.innerWidth/window.innerHeight;
var camera = new THREE.OrthographicCamera(- frustumHeight * aspect/2, frustumHeight * aspect/2, frustumHeight/2, - frustumHeight/2, 1, 2000);
camera.position.x = 0;
camera.position.y = 0;
camera.position.z = 100;
// CUBE
var geometry = new THREE.BoxGeometry(1, 1, 1);
var material = new THREE.MeshPhongMaterial({color: 0xbaf5e8, flatShading: true});
var cube = new THREE.Mesh(geometry, material);
cube.receiveShadow = true;
scene.add(cube);
// BOUNDING BOX
var helper_bbox = new THREE.BoxHelper(cube);
helper_bbox.update();
scene.add(helper_bbox);
// AXES
var helper_axes = new THREE.AxisHelper();
scene.add(helper_axes);
// FIT ALL:
var bbox_radius = helper_bbox.geometry.boundingSphere.radius;
if(aspect < 1){
frustumHeight = 2 * bbox_radius;
}
else{
frustumHeight = 2 * bbox_radius/aspect;
}
camera.left = - frustumHeight * aspect/2;
camera.right = frustumHeight * aspect/2;
camera.top = frustumHeight/2;
camera.bottom = - frustumHeight/2;
camera.updateProjectionMatrix();
// RENDERER
var renderer = new THREE.WebGLRenderer({alpha: true});
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFShadowMap;
// LIGHTS
var ambientLight = new THREE.AmbientLight(0x101010, 1.0);
scene.add(ambientLight);
directionalLight = new THREE.DirectionalLight(0xffffff, 1.0);
directionalLight.position.set(1.0, 1.0, 1.0).normalize();
scene.add(directionalLight);
directionalLight_2 = new THREE.DirectionalLight(0xffffff, 1.0);
directionalLight_2.position.set(-1.0, -1.0, 1.0).normalize();
scene.add(directionalLight_2);
// CONTROLS
document.body.appendChild(renderer.domElement);
var controls = new THREE.OrbitControls(camera);
window.addEventListener('resize', onWindowResize, false);
function animate(){
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
function onWindowResize(){
var aspect = window.innerWidth/window.innerHeight;
camera.left = - frustumHeight * aspect/2;
camera.right = frustumHeight * aspect/2;
camera.top = frustumHeight/2;
camera.bottom = - frustumHeight/2;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
camera.lookAt(scene.position);
}
animate();
我打算用一定的餘量,以適應立方現場立方體邊界球的半徑。它似乎正常工作,因爲它可以在下面的圖片中可以看出:
不過,我改變相機位置viewer.js
以下幾點:
camera.position.x = 100;
camera.position.y = 100;
camera.position.z = 100;
在這種情況下,我得到這樣的:
在這種情況下,該立方體沒有安裝到屏幕。我認爲這是由於我正在測量錯誤參考系中邊界球的半徑。但是,我一直無法找到解決這個問題的辦法。
有誰知道我在做什麼錯?我使用正確的方法嗎?提前致謝。
你的代碼看起來不錯。將'aspect <1'改爲'aspect> 1'。爲了清楚起見,將'frustumSize'重命名爲'frustumHeight'。 – WestLangley
謝謝。我編輯了這個問題,將'frustumSize'改爲'frustumHeight'。我也嘗試將aspect <1'更改爲'aspect> 1',但我沒有得到期望的結果。實際上,立方體現在在屏幕上變大了。我沒有在這個問題上添加最後一個變化,因爲我不明白爲什麼你提出了這個變化。 – GLR