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
};
}
就好像照相機變換對投影