I am facing a challenge in storing a loaded JSON model in Three.js using classes. Even though I can add it to the scene, I am unable to use it afterwards. Here is an overview of my code:
class AScene extens THREE.Scene{
constructor(renderer){
super();
this.ground = null;
this.model = this.createModel(); // It creates and returns a model that encapsulates all models in this scene.
this.soldier = this.createSoldier(); // I aim to store the loaded object here.
this.model.add(this.soldier); // This line always results in 'null' or 'undefined'.
this.add(this.model);
}
createModel(){
var model = new THREE.OBJECT3D();
this.ground = new Ground (300, 300, new THREE.MeshPhongMaterial ({map: texture}), 4);
model.add(this.ground);
return model; // This part functions as expected.
}
createSoldier(){
var obj = null;
var loader2 = new THREE.JSONLoader();
loader2.load('models/untitled.json', function(geometry, materials){
var material = new THREE.MeshBasicMaterial( { color: 0x0000ff } );
obj = new THREE.Mesh(geometry, material);
});
return obj;
}
}
Despite numerous attempts, I have been unsuccessful in successfully storing it in the 'this.soldier' class variable. The object loading process itself works well.