Although this question may appear to be a duplicate, as it is similar to this one and others, none of the solutions I have found have helped me resolve the issue. Therefore, I have decided to post it here in the hopes of finding a solution, as it is driving me crazy.
Currently, I am working on an Angular2 project generated with Angular CLI, with only HTML, CSS, and JS files (all up to date and latest versions). I have imported Three.js and three-obj-loader via npm and declared it in my component like this:
import * as THREE from 'three';
declare var require: any;
const OBJLoader = require('three-obj-loader')(THREE);
I am able to create shapes, use lighting and shading, but I am unable to load meshes from external .obj files. I have tried various approaches like the one below:
const manager = new THREE.LoadingManager();
const loader = new THREE.OBJLoader(manager);
loader.load('./working/path/to/file.obj', function (object) {
object.position.x = 0;
object.position.y = 0;
object.scale.x = 10;
object.scale.y = 10;
object.scale.z = 10;
const texture = THREE.TextureLoader('./working/path/to/file.jpg');
const material = new THREE.MeshLambertMaterial({ map: texture });
const mesh = new THREE.Mesh(object, material);
scene.add(mesh);
});
However, when I try to load the .obj file, I get a console error:
TypeError: undefined is not a constructor (evaluating 'new THREE.FileLoader(scope.manager)')
The error points to line 49 of the "three-obj-loader" module that I installed from here. The relevant part of the third-party code mentioned is:
THREE.OBJLoader.prototype = {
constructor: THREE.OBJLoader,
load: function load(url, onLoad, onProgress, onError) {
var scope = this;
var loader = new THREE.FileLoader(scope.manager);
loader.setPath(this.path);
loader.load(url, function (text) {
onLoad(scope.parse(text));
}, onProgress, onError);
},
I am not sure if this issue is related to the fact that I have not installed or declared any special types for these modules, and I am using plain JS source files. I have also not installed any file loader.
Additionally, I attempted to implement OBJLoader2 from here but encountered the same result.
Any advice would be greatly appreciated.
Best, k