Error: material not being loaded by objloader

I recently started working with Three.js and encountered an issue while loading an obj object into my scene. The object loads successfully, but the materials are not provided by the MTLLoader. I'm unsure if my object is broken or if there is an issue with the MTL Files.

Below is the code snippet that adds my spaceship OBJ:

const name = "shipA_OBJ";

loadMesh('shipA_OBJ', function(obj){
obj.position.x = 0;
obj.position.y = 0;
obj.position.z = 450;
obj.rotation.x += 1;
//obj.rotation.y -= 1;
obj.scale.x = .1;
obj.scale.y = .1
obj.scale.z = .1;
addMesh(obj);

}

function addMesh(mesh){
scene.add(mesh);
console.log(mesh.getWorldPosition());

}

function loadMesh(name, callback){
var objLoader = new THREE.OBJLoader();
var matLoader = new THREE.MTLLoader();
matLoader.load('models/shipA_OBJ.mtl', function(materials){
   materials.preload();
    objLoader.setMaterials(materials);
    objLoader.load('models/shipA_OBJ.obj', function(obj){
        callback(obj);
    });
});

This is the content of my Mtl file:

3ds Max Wavefront OBJ Exporter v0.97b - (c)2007 guruware

File Created: 04.05.2010 13:43:14

newmtl shipA_mat Ns 10.0000 Ni 1.5000 d 1.0000 Tr 0.0000 Tf 1.0000 1.0000 1.0000 illum 2 Ka 0.0000 0.0000 0.0000 Kd 0.5880 0.5880 0.5880 Ks 0.0000 0.0000 0.0000 Ke 0.0000 0.0000 0.0000

map_Ka s_1024_C.tga
map_Kd s_1024_C.tga
map_Ke s_1024_I.tga
map_bump s_1024_N.tga

After placing all the tga, mtl, and obj files in the same directory, I experimented by commenting out the MTLLoader. When omitted, my spaceship appeared grey, but using the MTLLoader resulted in a completely black spaceship. Despite having AmbientLight in place, I am certain that lighting is not the cause of the problem.

You can find the obj spaceship download link here:

Answer №1

When using the MTL loader, it is necessary to specify the texture path. This may seem counterintuitive when working with 3D models locally on your computer, but it is essential for web applications. The code must not rely on the file being in the same directory.

Fortunately, the MTLloader class provides a method to handle this issue.

For more information, visit:

Here is an illustration:

function loadMesh(name, callback){
var objLoader = new THREE.OBJLoader();
var matLoader = new THREE.MTLLoader();
matLoader.setTexturePath("models/");
matLoader.load('models/shipA_OBJ.mtl', function(materials){
   materials.preload();
    objLoader.setMaterials(materials);
    objLoader.load('models/shipA_OBJ.obj', function(obj){
        callback(obj);
    });
});

To simplify referencing within the model, you can also utilize setPath.

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

The eBay API is providing users with small thumbnail images with low resolution

Adding &outputSelector=GalleryInfo to the URL was supposed to give me a higher resolution thumbnail, but it doesn't seem to be working. I'm still new to JSON and the tutorial I'm following isn't very clear on the exact syntax needed ...

Transmit information between controllers during pageload in AngularJS without utilizing $rootscope

I currently have 2 controllers set up as follows: app.controller('ParentMenuController', function ($scope,MenuService) { $scope.contentLoaded = false; $scope.showButton = false; $scope.showButton = MenuService ...

Tips on enhancing SauceLabs javascript queries in your selenium testing for faster speeds?

My experience running Selenium tests on the Chrome browser in SauceLabs has been quite frustrating due to the sluggish performance. One of the major issues I have encountered is the significant delay in javascript queries, taking about 200ms to return res ...

Steps to Display a JavaScript Function Two Times on a Single Page

Hey there! I'm currently working on a project where I need to display the output of my function in two separate divs. However, when I try to simply output the same ID, it messes up the whole code and the output becomes null. Here's the code snipp ...

Tips for attaching a "progress" and refresh event to an ajax call while sending a file

I am currently using the FormData API and AJAX to upload files to a server in PHP-CodeIgniter. The file upload works perfectly when triggered by the file select event. However, I would like to display a progress bar next to each file being uploaded with th ...

How can I match the date format of d-M-Y using JavaScript regex?

When it comes to date formatting in PHP, I typically use the format d-M-Y. Recently, I attempted to match these dates using a JavaScript regex: s.match(new RegExp(/^(\d{1,2})(\-)(\w{3})(\-)(\d{4})$/)) I wanted to use this regex w ...

The scroll function remains unchanged when using overflow-y scroll and border radius

I need help with styling a div that will contain a large amount of text on my webpage. I want to use the overflow-y property for scroll, but the issue arises when trying to style the scroll bar with a radius. Check out this jsfiddle link for reference: ht ...

Tips for utilizing identical properties across numerous styled elements:

Utilizing the styled-components library, I have enhanced the header components from the Blueprintjs library. The current template for the H6 component appears as follows: export const DH6 = styled(H6)` color: ${(props) => (props.white ? "white&qu ...

Unable to fetch data in CakePHP 3.x controller using jQuery AJAX POST

I've been searching everywhere and unfortunately, I can't seem to figure out why the data is not being sent to my controller when posting via ajax. Here's the jQuery code snippet: var newDate = {}; newDate['start' ...

At times, Mongoose may return null, while other times it returns data frequently

I have designed a unique mongoose schema for managing contacts, with a custom defined ID. Here is the schema structure: const mongooseSchema = new mongoose.Schema({ _id:{ type:String, unique:true, required:true }, firstN ...

javascript function not being invoked

Currently, I have incorporated the following HTML <html> <head> <Title>EBAY Search</title> </head> <script language="JavaScript" src="ajaxlib.js"></script> <body> Click here & ...

ERESOLVE encountered difficulty resolving the error during the execution of npm install

I've been struggling with resolving dependency conflicts while installing and updating npm packages. The error message in the console is provided below. I have attempted to install legacy dependencies and re-install some modules, but nothing seems to ...

Is there a way to ensure that this ajax code functions seamlessly with all types of HTML files?

Currently, I am facing a challenge with an ajax call that retrieves data from a database, which I need to load into an HTML file. The issue lies in the fact that I have various HTML files and I am unaware of their contents. Despite spending countless hour ...

How to pass a variable or value through the async await API in the Vue.js and Laravel integration?

I'm facing an issue with my API where I want to check if a given email exists in the database, but every time I run it and view the console log, it returns undefined. Can anyone here suggest a better code snippet or approach for this? I specifically w ...

Obtaining JSON values by key name using JQuery

I am trying to extract JSON data using an HTML attribute How can I retrieve JSON values based on the translate attribute and "ed" key? JSON FILE { "open" : [ { "en": "Open", "ed": "Opened ...

A guide to securely retrieving data from the Hono API endpoint using type safety within a Next.js application

Currently, I am using Hono as my API endpoint with Bun. Now, I am working on a new app with Next.js and I want to ensure type safety when fetching data from my API. I believe I can accomplish this through RPC. However, I am unable to locate AppType mention ...

The Jquery Load() function is causing [object object] to be returned instead of the element ID value when retrieving it

I am pulling a value from a MySQL query result in page.php and displaying it in display.html. I am successfully showing the value "VALUE368" in the div ID on display.html. However, I also want to log "VALUE386" in the Firebase Realtime Database. I am attem ...

Manipulating front matter metadata when reading/writing a markdown file in Node.js

I have a large collection of markdown files that I need to update by adding new data to their front matter metadata. Currently, the file structure looks like this: --- title: My title here --- Markdown content here My goal is to include an id property ...

Is it possible to define a state within a map function?

This section of my code retrieves JSON data from the backend API and displays it as a list. Here is an example of my JSON data: (15) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}] 0: {id: 1, t ...

Retrieving JSON data from $routeParams in AngularJS

Currently, I am working on a controller that is responsible for saving address data and changing the location to the next page with the JSON response as a parameter. Here's the code snippet that handles this: $routeProvider.when('/checkout/:chec ...