Currently, I am delving into WebGL concepts by utilizing JavaScript and the Three.js library. However, I have encountered a hurdle while attempting to load a .obj file using classes with the OBJLoader. Below is the code snippet that I am struggling with:
Model.prototype.loadModel = function () {
var self = this;
loader = new THREE.OBJLoader();
loader.load( this.modelPath, function ( object ) {
object.traverse( function ( child ) {
if ( child instanceof THREE.Mesh ) {
child.material.map = self.modelTexture;
}
});
self.modelObj = object;
console.log(self.modelObj); // Returns [Object object]
});
console.log(self.modelObj); // Returns undefined
this.modelObj = self.modelObj;
this.modelObj.position.x = this.x;
this.modelObj.position.y = this.y;
this.modelObj.position.z = this.z;
}
I seem to be unable to access the object as this.modelObj
outside of the anonymous function, leading me to believe it's a scope-related issue. When trying to include this.modelObj
in the function parameters, I get an error stating "missing formal parameter", and attempting to use this.
inside the function implies it is within the function's scope or the loader itself.