Above is my code, I created a GUI Ellipse(), I want to obtain the coordinates of the circular UI in the scene when clicking on this circular UI. Because I am in a learning team and making a simple bomb, but I always cannot obtain the correct coordinates after using unprojected.
Here is my partial code:
this.clickHandlerObserver = this.circle.onPointerUpObservable.add((evt) => {
const screenX = parseFloat(this.circle.left) + window.innerWidth / 2;
const screenY = parseFloat(this.circle.top) + window.innerHeight / 2;
// 获取相机、投影矩阵和视图矩阵
const camera = this.camera;
if(!this.camera){
console.warn("camera 空");
}
// 构造屏幕空间中的点
const screenVector = new BABYLON.Vector3(screenX, screenY, 0);
console.log("屏幕空间中的点: ", screenVector);
// 强制更新相机的世界矩阵
camera.computeWorldMatrix(true);
const viewMatrix = camera.getViewMatrix();
const projectionMatrix = camera.getProjectionMatrix();
const worldMatrix = camera.getWorldMatrix();
console.log("视图矩阵: ", viewMatrix);
console.log("投影矩阵: ", projectionMatrix);
console.log("世界矩阵: ", worldMatrix);
// 检查矩阵值是否有效
if (viewMatrix.isIdentity() || projectionMatrix.isIdentity() || worldMatrix.isIdentity()) {
console.error("一个或多个矩阵是单位矩阵,这可能是错误的。");
return;
}
// 将屏幕坐标转换为世界坐标
const unprojectedPoint = BABYLON.Vector3.Unproject(
screenVector,
window.innerWidth,
window.innerHeight,
worldMatrix,
viewMatrix,
projectionMatrix
);
console.log("解投影的世界坐标: ", unprojectedPoint);
if (isNaN(unprojectedPoint.x) || isNaN(unprojectedPoint.y) || isNaN(unprojectedPoint.z) ||
!isFinite(unprojectedPoint.x) || !isFinite(unprojectedPoint.y) || !isFinite(unprojectedPoint.z)) {
console.error("解投影的世界坐标包含无效值。");
return;
}
if(!unprojectedPoint){
console.error("unprojectedPoint is null");
}