Error: THREE.MTLLoader cannot be instantiated 2.0

Previously, I posted a question regarding this issue: Uncaught TypeError: THREE.MTLLoader is not a constructor

I was able to resolve it by modifying the three-mtl-loader file. However, since I plan to upload my work to GitHub later, I need to find a solution that doesn't involve altering these files.

Instead, I'm attempting to load the obj and mtl files using loaders from the classic example found here: https://github.com/mrdoob/three.js/blob/master/examples/webgl_loader_obj_mtl.html

Despite my efforts, I still encounter the error 'Uncaught TypeError: THREE.MTLLoader is not a constructor'

I'm a bit uncertain about how to correctly implement these loaders, but my current code snippet looks like this:

import * as THREE from 'three'
import {OBJLoader} from 'three'
import {MTLLoader} from 'three'


var mtlLoader = new THREE.MTLLoader();
mtlLoader.load("http://blabla.obj.mtl", function(materials) {
   materials.preload();
   var objLoader = new THREE.OBJLoader();
   objLoader.setMaterials(materials);
   objLoader.load("http://blabla.obj", function(object) {

       object.scale.x = scale;
       object.scale.y = scale;
       object.scale.z = scale;

       scene.add(object)

  });
});

In the three.js file located in the src folder, I have added:

export { OBJLoader } from './loaders/OBJLoader.js'
export { MTLLoader} from './loaders/MTLLoader.js'

EDIT

After installing via NPM, the initial error disappeared. However, new issues have surfaced - which I have addressed in a separate question: three-mtl-loader error: THREE.MeshPhongMaterial: .shading has been removed -> object not visible

Answer №1

The documentation for Three.js may lead you astray. Despite the MTLLoader being featured prominently, it is not actually a part of Three.js's core library. Instead, it can be found in the examples/ folder.

If you are using ES6 module syntax, there are two possible approaches.

Method 1: Using NPM

Typically, individuals publish utilities like this on NPM because Three.js's creators do not adhere to modern JS development practices. In such cases, there exists a three-mtl-loader npm package.

npm install --save three-mtl-loader

and then

import MTLLoader from 'three-mtl-loader';

Method 2: Manually Adding MTLLoader from Three's Source

You also have the option to copy the MTLLoader.js file into your project. However, it is not recommended to directly copy third-party libraries into your local project when utilizing modern ES6 modules.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"I am looking for a way to incorporate animation into my Angular application when the data changes. Specifically, I am interested in adding animation effects to

Whenever I click on the left or right button, the data should come with animation. However, it did not work for me. I tried adding some void animation in Angular and placed a trigger on my HTML element. The animation worked when the page was refreshed, bu ...

Exploring the world of chained JavaScript Promises for automatic pagination of an API

Dealing with a paged API that requires fetching each page of results automatically has led me to construct a recursive promise chain. Surprisingly, this approach actually gives me the desired output. As I've tried to wrap my head around it, I've ...

Geometry Subtraction in Three.js (or more broadly, 3D development)

Currently, I am working on a small project where I have the task of subtracting a rectangular area from a cube when the user moves their mouse (imagine carving out space for a door on a house). The 'door' would be placed upon clicking, but it sho ...

Why is my sprite not appearing in the correct location in Three.js?

I'm currently utilizing Three.js version r69 for my project. In order to display text on sprites, I have created several sprites with canvas textures. Following the advice from this particular question, I made some adjustments to ensure compatibility ...

Managing errors when dealing with Observables that are being replayed and have a long lifespan

StackBlitz: https://stackblitz.com/edit/angular-ivy-s8mzka I've developed an Angular template that utilizes the `async` pipe to subscribe to an Observable. This Observable is generated by: Subscription to an NgRx Store Selector (which provides sele ...

Enhanced hierarchical organization of trees

I came across this code snippet: class Category { constructor( readonly _title: string, ) { } get title() { return this._title } } const categories = { get pets() { const pets = new Category('Pets') return { ge ...

Different ways to reference a variable in Typescript without relying on the keyword "this" throughout the codebase

Can we eliminate the need to write "this" repeatedly, and find a way to write heroes, myHero, lastone without using "this"? Similar to how it is done in common JavaScript. https://i.stack.imgur.com/TZ4sM.png ...

Issue with undefined arrays in the Angular merge sort visualization tool

I am currently working on developing a visualizer for sorting algorithms using Angular. However, I have encountered some difficulties while implementing merge sort. As a Java programmer, I suspect that there may be an issue with my TypeScript code and the ...

The style fails to load correctly upon the page's initial loading

I am utilizing <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4d26282823603e212429283f0d7b63756378">[email protected]</a> in a <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="026c677a76 ...

What is the process for importing files with nested namespaces in TypeScript?

Currently, I am in the process of transitioning an established Node.js project into a fully TypeScript-based system. In the past, there was a static Sql class which contained sub-objects with MySQL helper functions. For instance, you could access functions ...

Add a service to populate the values in the environment.ts configuration file

My angular service is called clientAppSettings.service.ts. It retrieves configuration values from a json file on the backend named appsettings.json. I need to inject this angular service in order to populate the values in the environment.ts file. Specific ...

JavaScript code that displays values that are not equal

Here is a code snippet that checks whether two arrays of objects are equal or not. How can we enhance this code to log which answer is not matching? The structure of the arrays: arrayA represents user answered questions, while arrayB contains correct answ ...

Issue: Angular 14 - Validators Not Resetting in Nested FormGroup

I am currently working on implementing a nested FormGroup. However, I have encountered an error when attempting to reset the form. Here is the structure of the form: form: UntypedFormGroup; this.form = this.fb.nonNullable.group({ f1: [''], f2: ...

Finding out whether the current date falls between a startDate and endDate within a nested object in mongoose can be done by using a specific method

My data structure includes a nested object as shown: votingPeriod: {startDate: ISOdate(), endDate: ISOdate()}. Despite using the query below, I am getting an empty object back from my MongoDB. const organizations = await this.organizationRepository.find( ...

Mastering Three.JS: Configuring Rotation Axis

After experimenting with Three.JS (not my area of expertise) and following a tutorial, I managed to create something interesting. You can check it out on this JS Fiddle. While I may not be a 3D animation genius yet, I have a question: How can I make the ...

What steps should be taken to trigger an API call once 3 characters have been entered into a field

In my current project, I am dealing with a parent and child component setup. The child component includes an input field that will emit the user-entered value to the parent component using the following syntax: <parent-component (sendInputValue)="g ...

When attempting to import a react component written with TypeScript to npm, receiving an 'undefined' error

I recently encountered an issue when trying to publish my custom React component developed with TypeScript on npm. Although the publishing process was successful, I faced an error upon importing the npm package into a new React project: Error: Element ty ...

Experience the power of TypeScript in a serverless environment as you transform your code from

I have some JavaScript code that needs to be converted to TypeScript. Currently, I have two files: API_Responses.js const Responses = { _200(data = {}) { return { statusCode: 200, body: JSON.stringify(data), }; } }; module.export ...

The declaration file for the 'express' module could not be located

Whenever I run my code to search for a request and response from an express server, I encounter an issue where it cannot find declarations for the 'express' module. The error message transitions from Could not find a declaration file for module & ...

Error: No 'map' property found in 'TwitterserviceService' type

Currently, I am in the process of learning how to develop simple and basic Angular applications. As part of my learning journey, I decided to incorporate my Twitter timeline into one of my projects. To aid me in this endeavor, I referred to various online ...