2017-09-01 323 views
0

我开发了一个简单的three.js应用程序,呈现立方体。我创建了三个文件:index.html,viewer_style.cssviewer.jsthree.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(); 

我打算用一定的余量,以适应​​立方现场立方体边界球的半径。它似乎正常工作,因为它可以在下面的图片中可以看出:

enter image description here

不过,我改变相机位置viewer.js以下几点:

camera.position.x = 100; 
camera.position.y = 100; 
camera.position.z = 100; 

在这种情况下,我得到这样的:

enter image description here

在这种情况下,该立方体没有安装到屏幕。我认为这是由于我正在测量错误参考系中边界球的半径。但是,我一直无法找到解决这个问题的办法。

有谁知道我在做什么错?我使用正确的方法吗?提前致谢。

+1

你的代码看起来不错。将'aspect <1'改为'aspect> 1'。为了清楚起见,将'frustumSize'重命名为'frustumHeight'。 – WestLangley

+0

谢谢。我编辑了这个问题,将'frustumSize'改为'frustumHeight'。我也尝试将aspect <1'更改为'aspect> 1',但我没有得到期望的结果。实际上,立方体现在在屏幕上变大了。我没有在这个问题上添加最后一个变化,因为我不明白为什么你提出了这个变化。 – GLR

回答

1

如果您按照建议将aspect < 1更改为aspect > 1,那么您的代码是正确的。这里是你的代码jsfiddle演示这个:https://jsfiddle.net/holgerl/kk5h39qa/

+0

谢谢,现在它完美的工作(愚蠢的错误)。但是,现在我有另一个问题。我已经实现了“缩放全部”功能的按钮。除了相机的位置及其宽度和高度,我是否应该从控件中更改某些内容?当我平移对象然后单击提到的按钮时,它会一直做奇怪的事情。 – GLR