我正在尝试在three.js中创建一个10 x 10网格的椅子obj文件。我有一个功能,将添加一个椅子到场景。但是,当我尝试将对象返回到循环函数以创建更多它们时,我被告知“undefined不是一个对象。它似乎没有保存它应该返回的对象。这是什么我有。
function getChair(){
var loader = new THREE.OBJLoader();
var textureLoader = new THREE.TextureLoader();
loader.load(‘assets/models/theatreChair.obj’, function (object) {
var colorMap = textureLoader.load(‘assets/textures/chairDiffuseColor.png’);
var bumpMap = textureLoader.load(‘assets/textures/chairBumpTextures.png’);
var faceMaterial = new THREE.MeshStandardMaterial(‘rgb(255,255,255)’);
object.traverse(function(child) {
child.material = faceMaterial;
faceMaterial.roughness = 1;
faceMaterial.map = colorMap;
faceMaterial.bumpMap = bumpMap;
faceMaterial.roughnessMap = bumpMap;
faceMaterial.metalness = 0;
faceMaterial.bumpScale = 0.1;
});
// scene.add(object) -- This works. It adds a chair at 0,0,0. Use scroll wheel to see it.
return object; // Should return the chair object, right?
});
}
function getChairGrid(amount, seperationMultiplier){
var group = new THREE.Group();
for (var i=0; i<amount; i++) {
var obj = getChair();
obj.position.x = i * seperationMultiplier; // Fails here. “undefined is not an object (evaluating ‘obj.position’)”
obj.position.y = obj.geometry.parameters.height/2;
group.add(obj);
for (var j = 1; j<amount; j++){
var obj = getChair();
obj.position.x = i * seperationMultiplier;
obj.position.y = obj.geometry.parameters.height/2;
obj.position.z = j * seperationMultiplier;
group.add(obj);
}
}
group.position.x = -(seperationMultiplier * (amount-1))/2;
group.position.z = -(seperationMultiplier * (amount-1))/2;
return group;
}