我在网上查了很久没有详细代码。整理了一下

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()

//1111

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

需要源码的可以私信

12-22 17:18