three.js正交相机:用透视放大所有立方体

时间:2017-09-01 08:59:03

标签: javascript three.js orthographic projection-matrix

我开发了一个呈现多维数据集的简单three.js应用程序。我创建了三个文件:index.htmlviewer_style.cssviewer.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 个答案:

答案 0 :(得分:1)

如果您按照建议将--- spring: cloud: config: server: git: uri:https://github.com/PraveenKumarMekala/Microservices-With-Spring-Example searchPaths: ConfigData # "native" is used when the native profile is active, for local tests with a classpath repo: native: searchLocations: classpath:offline-repository/ server: port: 8888更改为aspect < 1,您的代码是正确的。这是一个jsfiddle与您的代码,演示了这一点:https://jsfiddle.net/holgerl/kk5h39qa/