簡體   English   中英

如何獲得對象的世界位置(Three.js)

[英]How to get an object's world position (Three.js)

我想將已經放置的對象放置在新位置,但是它是從本地位置而不是全局位置移動的。

this._scene.updateMatrixWorld();
this._scene.add(mesh);
var v1 = new THREE.Vector3();    
v1.setFromMatrixPosition( mesh.matrixWorld );  
mesh.position.set(v1.x +2, v1.y + 2, v1.z + 2);

我為解決問題所做的工作:

每個網格都有一個幾何屬性。 這就是為什么您可以調用mesh.geometry的原因。 從這一點開始,我在網格周圍創建了一個BoundingSphere。

mesh.geometry.computeBoundingSphere();

現在可以獲取我的boundingSphere的世界位置,同時它也是我的網格物體的世界位置。

var vector = mesh.geometry.boundingSphere.center;

恭喜! “向量”在網格的(x,y,z)中獲得中心世界位置。

為了澄清起見,“ mesh”是一個THREE.Mesh對象。

您難道不只是隨即以增量移動對象本身嗎? 我不確定為什么需要涉及矩陣?

mesh.position.set(mesh.position.x +2, mesh.position.y + 2, mesh.position.z + 2);

編輯...如果需要使用matrix,則需要設置matrixWorld。 查看http://threejs.org/docs/#Reference/Core/Object3D.matrixWorld,但使用位置設置器將為您帶來繁重的工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM