この問題には多くの回答がありますが、Three.JS で複数の回答が見られるなど、それらがすべて XTK で機能するかどうかはわかりませんが、もちろん XTK と Three.JS には明らかに同じ API はありません。光線を使用して、Matrix
他のフレームワークの他の多くのソリューションと非常に似ているように見えましたが、ここで可能な解決策をまだ把握していません。今のところ、座標 X、Y、Z を見つけて記録するだけConsole.Log
で問題ありません。後で、情報を表示するためのキャプション/ツールチップを作成したいと考えていましたが、それを表示する方法は他にもあります。しかし、レイを使用してオブジェクトと衝突させることが可能かどうか、誰かが少なくとも教えてもらえますか? XTK でメッシュやその他のファイルとの衝突がどのように機能するかはわかりません。今のヒントは素晴らしいでしょう!!
1006 次
2 に答える
4
これがxtkでアンプロジェクトする私の関数です。間違いがあれば教えてください。結果のポイントとカメラの位置で、交点を見つけることができるはずです。次のステップの計算を高速化するために、pick イベントで呼び出すので、特定のオブジェクトとの交差を試すだけで済みます。時間があれば、境界ボックスもテストしてみます。
注意: 最後の行は必要ありません。ポイントの代わりに光線を操作できます。
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 ピッキングを行うことができます。
于 2012-07-23T10:23:24.420 に答える
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);
于 2012-07-18T13:21:56.133 に答える