我创建了一个在Three.js中加载obj文件的函数。我不想将对象直接添加到该函数中的场景,而是将其返回到上层函数。
当前代码:
var loadObjFile = function(modelConfiguration) {
var mtlLoader = new THREE.MTLLoader();
mtlLoader.load(modelConfiguration.mtl, function (materials) {
materials.preload();
var objLoader = new THREE.OBJLoader();
objLoader.setMaterials(materials);
objLoader.load(modelConfiguration.obj, function (object3d) {
object3d.name = modelConfiguration.name;
scene.add(object3d);
});
});
};
我尝试过的事情
以不同方式重写功能。但是无法让它返回object3d。例如:
var loadObjFile = function(modelConfiguration) {
var mtlLoader = new THREE.MTLLoader();
var obj;
mtlLoader.load(modelConfiguration.mtl, function (materials) {
materials.preload();
var objLoader = new THREE.OBJLoader();
objLoader.setMaterials(materials);
objLoader.load(modelConfiguration.obj, function (object3d) {
object3d.name = modelConfiguration.name;
obj = object3d;
});
});
return obj;
};
我没有将object3d添加到场景中,而是将其添加到虚拟组并返回该组。它工作但后来我有很多不必要的团体。我还尝试使用group.children [0]和group.getObjectByName(modelConfiguration.name)从组中提取对象,但它也没有工作。示例:
var loadObjFile = function(modelConfiguration) {
var mtlLoader = new THREE.MTLLoader();
var group;
mtlLoader.load(modelConfiguration.mtl, function (materials) {
materials.preload();
var objLoader = new THREE.OBJLoader();
objLoader.setMaterials(materials);
objLoader.load(modelConfiguration.obj, function (object3d) {
object3d.name = modelConfiguration.name;
group.add(object3d);
});
});
return group; // works, but unneccessary group
//return group.children[0]; // error: undefined object
//return group.getObjectByName(modelConfiguration.name); // error: undefined object
};
提前谢谢!
答案 0 :(得分:0)
使用lambda:
constructor() {
const obj = new MyClass();
document.write(JSON.stringify(obj, Object.keys(obj.constructor.prototype)))
}
使用:
var loadObjFile = function(modelConfiguration, onObj) {
var mtlLoader = new THREE.MTLLoader();
var obj;
mtlLoader.load(modelConfiguration.mtl, function (materials) {
materials.preload();
var objLoader = new THREE.OBJLoader();
objLoader.setMaterials(materials);
objLoader.load(modelConfiguration.obj, function (object3d) {
object3d.name = modelConfiguration.name;
obj = object3d;
onObj(obj);
});
});
};