导入的模型文件是个组,里面有多个模型,需要合并为一个,方便操作
参考文章
https://blog.csdn.net/weixin_52125363/article/details/122454894
https://blog.csdn.net/u012483043/article/details/106465465/
https://zhuanlan.zhihu.com/p/467005893
const objLoader = new OBJLoader()
objLoader.load(
"./YUN.obj",
function(obj){
// objObject = obj
// console.log(objObject)
// objObject.children[0].material.color.set(0xCB672E)
// objObject.scale.set(0.1,0.1,0.1)
// objObject.position.set(1.3,0.3,1.3)
// scene.add(objObject)
obj.position.set(1.3,0.3,1.3)
scene.add(merge(obj))
},
function ( xhr ) {
console.log( 'obj-' + ( xhr.loaded / xhr.total * 100 ) + '% loaded' );
},
// called when loading has errors
function ( error ) {
console.log( 'obj加载失败',error );
}
)
function merge(object) {
const geometries = [];
const matArr = [];
var meshs = new THREE.BufferGeometry();
object.traverse((obj) => {
const child = obj;
if (child.isMesh) {
const geo = child.geometry.clone();
if (Array.isArray(child.material)) {
child.material = child.material[0];
}
matArr.push(child.material);
geo.index = null;
child.updateWorldMatrix(true, true);
geo.applyMatrix4(child.matrixWorld);
geometries.push(geo);
}
});
// for(var i=0; i<geometries.length; i++){
// meshs.merge(new THREE.BufferGeometry().fromBufferGeometry( geometries[i] ), new THREE.BufferGeometry().fromBufferGeometry( geometries[i] ).matrix, i);
// }
// var buffermeshs = new THREE.BufferGeometry().fromGeometry(meshs);
// const result = new THREE.Mesh(buffermeshs,matArr);
// console.log(result)
//合并几何体
const mergedGeometries = BufferGeometryUtils.mergeBufferGeometries(geometries, true);
//创建网格
const singleMergeMesh = new THREE.Mesh(mergedGeometries, matArr);
return singleMergeMesh;
}
标签:material,obj,objObject,three,js,导入,child,set,geo
From: https://blog.51cto.com/u_15668841/6388844