有很多回答这个问题,但我不知道他们与XTK所有的工作,如在three.js所看到的这种多个答案,但当然XTK和three.js所不具有相同的API明显。 使用光线和Matrix
显得很类似于其他框架的许多其他解决方案,但我还没有在这里抓一个可能的解决方案。 现在只是寻找坐标X,Y和Z并将其记录到Console.Log
是好的,以后我希望能创造一个标题/工具提示中显示的信息,但有其他的方法也显示它。 但至少有人告诉我,如果这是可以使用光线与物体发生碰撞? 我不知道碰撞XTK如何与网格或任何其他文件。 任何提示,现在将是伟大的!
Answer 1:
这里是我的功能XTK到unproject。 请告诉我,如果你看到的错误。 现在与所得到的点和摄像机的位置,我应该能够找到我的交叉点。 为了使计算以下更快一步,我会打电话给它出挑选事件,所以我只需要尝试用定对象的交叉点。 如果我有时间,我还会尝试与测试边框。
诺塔好处:最后一行不是必需的,我可以在光线而不是点工作。
X.camera3D.prototype.unproject = function (x,y) {
// get the 4x4 model-view matrix
var mvMatrix = this._view;
// create the 4x4 projection matrix from the flatten gl version
var pMatrix = new X.matrix(4,4);
for (var i=0 ; i<16 ; i++) {
pMatrix.setValueAt(i - 4*Math.floor(i/4), Math.floor(i/4), this._perspective[i]);
}
// compute the product and inverse it
var mvpMatrxix = pMatrix.multiply(mwMatrix); /** Edit : wrong product corrected **/
var inverse_mvpMatrix = mvpMatrxix.getInverse();
if (!goog.isDefAndNotNull(inverse_mvpMatrix)) throw new Error("Could not inverse the transformation matrix.");
// check if x & y are map in [-1,1] interval (required for the computations)
if (x<-1 || x>1 || y<-1 || y>1) throw new Error("Invalid x or y coordinate, it must be between -1 and 1");
// fill the 4x1 normalized (in [-1,1]⁴) vector of the point of the screen in word camera world's basis
var point4f = new X.matrix(4,1);
point4f.setValueAt(0, 0, x);
point4f.setValueAt(1, 0, y);
point4f.setValueAt(2, 0, -1.0); // 2*?-1, with ?=0 for near plan and ?=1 for far plan
point4f.setValueAt(3, 0, 1.0); // homogeneous coordinate arbitrary set at 1
// compute the picked ray in the world's basis in homogeneous coordinates
var ray4f = inverse_mvpMatrix.multiply(point4f);
if (ray4f.getValueAt(3,0)==0) throw new Error("Ray is not valid.");
// return in not-homogeneous coordinates to compute the 3D direction vector
var point3f = new X.matrix(3,1);
point3f.setValueAt(0, 0, ray4f.getValueAt(0, 0) / ray4f.getValueAt(3, 0) );
point3f.setValueAt(1, 0, ray4f.getValueAt(1, 0) / ray4f.getValueAt(3, 0) );
point3f.setValueAt(2, 0, ray4f.getValueAt(2, 0) / ray4f.getValueAt(3, 0) );
return point3f;
};
编辑
在这里 ,在我的回购,你可以找到在camera3D.js功能和renderer3D.js在XTK高效的3D采摘。
Answer 2:
现在,这是不容易成为可能。 我想你可以抢摄像机的观察矩阵来计算位置。 如果这样做,这将是巨大的,使其恢复到XTK内置的功能!
目前,仅采摘的对象可能是这样的(r是X.renderer3D):
/**
* Picks an object at a position defined by display coordinates. If
* X.renderer3D.config['PICKING_ENABLED'] is FALSE, this function always returns
* -1.
*
* @param {!number} x The X-value of the display coordinates.
* @param {!number} y The Y-value of the display coordinates.
* @return {number} The ID of the found X.object or -1 if no X.object was found.
*/
var pick = r.pick($X, $Y);
文章来源: Finding world coordinates from screen coordinates