gpt4 book ai didi

javascript - 3d 数学 : screen space to world space

转载 作者:行者123 更新时间:2023-12-03 07:36:40 27 4
gpt4 key购买 nike

在过去的 6 个小时里,我一直在尝试在我的 webgl 应用程序中实现点击,但我找不到任何关于这个主题的足够清晰的内容。

到目前为止,我想出的是伪代码:

screenSpace = mousePosition;
normalizedScreenSpace = (screenSpace.x/screen.width, screenSpace.y/screen.height);

camSpace = invertProjectionMatrix * normalizedScreenSpace;

worldSpace = invertViewMatrix * camSpace;

打印出世界空间坐标,它与场景中的其他对象不对应。我做错了什么?

最佳答案

viewProjection 矩阵将 vec3 从世界空间带到剪辑空间,因此它的逆矩阵将剪辑空间转换为世界空间。缺少的是 GPU 在幕后为您处理的视角鸿沟,因此您也必须考虑到这一点。添加屏幕宽度和高度,您的屏幕就变成了世界:

screenToWorld: function(invViewProjection, screenWidth, screenHeight){
// expects this[2] (z value) to be -1 if want position at zNear and +1 at zFar

var x = 2*this[0]/screenWidth - 1.0;
var y = 1.0 - (2*this[1]/screenHeight); // note: Y axis oriented top -> down in screen space
var z = this[2];
this.setXYZ(x,y,z);
this.applyMat4(invViewProjection);
var m = invViewProjection;
var w = m[3] * x + m[7] * y + m[11] * z + m[15]; // required for perspective divide
if (w !== 0){
var invW = 1.0/w;
this[0] *= invW;
this[1] *= invW;
this[2] *= invW;
}

return this;
},

以及逆向计算:

worldToScreen: function(viewProjectionMatrix, screenWidth, screenHeight){
var m = viewProjectionMatrix;
var w = m[3] * this[0] + m[7] * this[1] + m[11] * this[2] + m[15]; // required for perspective divide
this.applyMat4(viewProjectionMatrix);
if (w!==0){ // do perspective divide and NDC -> screen conversion
var invW = 1.0/w;
this[0] = (this[0]*invW + 1) / 2 * screenWidth;
this[1] = (1-this[1]*invW) / 2 * screenHeight; // screen space Y goes from top to bottom
this[2] *= invW;
}
return this;
},

关于javascript - 3d 数学 : screen space to world space,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35583808/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com