What I'm trying to do is:
- Get a
JSON
file from server, containing data about my models - Use a PLY loader inside a for loop to add them to the scene
- Add them to an array
Here are my functions:
function getJSON(callback) {
var temp = $.getJSON("data/data.json", function(data) {
//execute the callback, passing it the data
callback(data);
});
}
function loadModels() {
//get our JSON
getJSON(function(data) {
//evaluate data
nodes = data.library[0].model.nodes;
nodesLen = nodes.length;
//Load nodes and add them to scene and array
for (var i = 0; i < nodesLen; i++) {
var url = nodes[i].url;
// PLY loader
var loader = new THREE.PLYLoader();
loader.load(url, function(geometry) {
geometry.computeFaceNormals();
var material = new THREE.MeshPhongMaterial({ color: 0xffffff, vertexColors: THREE.VertexColors, transparent: true, side: THREE.DoubleSide });
var mesh = new THREE.Mesh(geometry, material);
mesh.stepNum = i;
console.log(i);
mesh.position.x = 0;
mesh.position.y = 0;
mesh.position.z = 0;
//Add to scene
scene.add(mesh);
//Push into array
nodesArr.push(mesh);
});
}
});
}
Problem: They are not loaded correctly. When I check the output of "console.log(i)" in the PLY loader it return always the same value. I think while it is loading the model another loop has already started. How do I instruct the loop to wait until all functions complete before another loop starts?