threejs加載obj文件后如何獲取其下的mesh的相對位置,解決位置為{0,0,0}的問題,three 導入的OBJ根據自身坐標中心的自轉


我在網上查了很久沒有詳細代碼。整理了一下

object.children[0].geometry.computeBoundingBox();

var centroid = new THREE.Vector3();
centroid.addVectors( object.children[0].geometry.boundingBox.min, object.children[0].geometry.boundingBox.max );
centroid.multiplyScalar( 0.5 );
centroid.applyMatrix4( object.children[0].matrixWorld );



object.children[0].geometry.center(centroid.x*0.05, centroid.y*0.05, centroid.z*0.05)
object.position.set(centroid.x*0.05, centroid.y*0.05, centroid.z*0.05)//

導入OBJ時
         var mtlLoader1 = new THREE.MTLLoader();
mtlLoader1.setPath('精簡風扇/');
mtlLoader1.load('shanye_020.mtl', function(materials) {
mtlLoader1.receiveShadow = true;
materials.preload();

var objLoader1 = new THREE.OBJLoader();
objLoader1.setMaterials(materials);
objLoader1.setPath('精簡風扇/');
objLoader1.load('shanye_020.obj', function(object) {
console.log(object)
// object.children[0].geometry.computeBoundingBox()
object.children[0].geometry.computeBoundingBox();
var centroid = new THREE.Vector3();
centroid.addVectors( object.children[0].geometry.boundingBox.min, object.children[0].geometry.boundingBox.max );
centroid.multiplyScalar( 0.5 );
centroid.applyMatrix4( object.children[0].matrixWorld );
mesh = object;
object.traverse(function(child)
{

console.log(child)
objects.push( child );
});
for(k in object.children){

object.children[k].castShadow = true;
object.children[k].receiveShadow = true;
}
object.scale.set(0.05, 0.05, 0.05);
object.children[0].geometry.center(centroid.x*0.05, centroid.y*0.05, centroid.z*0.05)
object.position.set(centroid.x*0.05, centroid.y*0.05, centroid.z*0.05)//
helper = new THREE.BoundingBoxHelper(object, 0xff0000);
helper.update();
scene.add(helper);
object.enabled = true
object.castShadow = true
object.receiveShadow = true;
// object.children[0].geometry.center()
scene.add(mesh);
objects.push( object)

}, onProgress, onError);


})

 

划重點

 

 

 

 

 我是綜合了兩個帖子實現的OBJ按自身坐標中心旋轉;

參考

https://blog.csdn.net/yuanben_wuxin/article/details/79274808;

https://blog.csdn.net/qq_35750405/article/details/52076553

需要源碼的可以私信


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM