2015-07-10 99 views
0

我想在3D空間中看多邊形的中心,然後將其點轉換爲2D屏幕座標。這是不起作用的代碼。任何幫助,將不勝感激。三JS投影問題

//calculate the center 
var center = new THREE.Vector3(); 
for (var i = 0; i < positions.length; i++) { 
    center.add(positions[i]); 
} 
center.multiplyScalar(1/positions.length); 
//look at the center 
var camera = new THREE.PerspectiveCamera(75, width/height, 0.1, 1000); 
camera.lookAt(center); 
//calculate the screen coordinates 
for (var i = 0; i < positions.length; i++) { 
    screenPositions.push(toScreenXY(positions[i], camera, width, height)); 
} 

function toScreenXY(position, camera, width, height) { 
    var pos = position.clone(); 
    pos.project(camera); 
    var halfWidth = width/2, 
     halfHeight = height/2; 
    return { 
     x: (pos.x + 1) * halfWidth + halfWidth, 
     y: (-pos.y + 1) * halfHeight + halfHeight 
    }; 

} 

就好像照相機變換對投影

回答

1

沒有影響,因爲你改變了相機的旋轉,你需要更新相機的世界矩陣,因爲該方法project()引用它。

camera.position.set(x, y, z); 
camera.lookAt(center); 

camera.updateMatrixWorld(); // add this 

渲染調用renderer.render()camera.updateMatrixWorld()你,但在這種情況下,你必須自己調用它。

three.js r.71