What makes the obj loader incompatible with the mtl loader in Three.js?

I am encountering an issue while loading models using three.js. I have an obj file with mtl and a glb model of the same object. When I load the model with the obj and the mtl loader, the object is not displayed in the browser. If I only load it with the obj loader, the model appears but without any textures. However, when I use the gltf loader, there are no issues and the model is shown with the correct texture.

Additionally, I am facing a problem with the scale. The size of the object loaded with the gltf loader differs from the one imported with the obj loader. With the obj loader, the object appears much larger. The data size is also four times bigger, so I suspect that the exporting process could be causing the issue.

My goal is to successfully load the Object with the obj and the mtl loader!

Here is the code snippet for loading the model with the obj loader:

const objLoader = new THREE.OBJLoader();
objLoader.load( "model/test.obj", function( object ) {

    scene.add( object );
    object.scale.set(0.001, 0.001, 0.001);
    object.rotateX(-Math.PI/4);
    console.log( object );

});

And here is the code snippet for loading the model with both the obj and the mtl loader:

var mtlLoader = new THREE.MTLLoader();
mtlLoader.load( "model/test.mtl", function( materials ) {

    materials.preload();

    var objLoader = new THREE.OBJLoader();
    objLoader.setMaterials( materials );
    objLoader.load( "model/test.obj", function( object ) {
        
        scene.add( object );

    });

});

To load the model using the gltf loader, you can use the following code:

const gltfLoader = new THREE.GLTFLoader( manager );
gltfLoader.load( "model/test.glb", function( gltf ) {

    model = gltf.scene;
    model.rotateX( -Math.PI/2 );
    scene.add( model );
    console.log( model );

});

This document contains additional code for initializing the scene and rendering the models using different loaders. It includes settings for lights, cameras, and controls along with the necessary imports and scripts. You can find the full code and CSS snippets in this section as well.

If you need access to my folder structure or the OBJ and MAT files, check out the relevant links provided.

Answer №1

To resolve the problem, adjust the settings of MTLLoader in this way:

new THREE.MTLLoader()
    .setMaterialOptions( { invertTrProperty: true } )

Your MTL file includes Tr 1.000000 values that determine material opacity. Unfortunately, the interpretation of the Tr value varies between exporters and importers. In three.js, a value of 1 indicates complete transparency, making your asset invisible.

The configuration given above reverses the value, resulting in a fully opaque object.

It is important to note that consistency issues like this can be avoided by using the well-defined glTF format, which should be preferred for 3D projects.

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

Can someone explain why my button icons are not aligning to the center properly?

I have a small issue with the icons I pulled from react-icons - they appear slightly raised within the buttons. How can I position them in the middle? That's my only query, but I can't post it alone due to mostly having code in my post. What an ...

The functionality of $state.go within $stateChangeStart in the app.run is not functioning properly in AngularJS

Having some trouble getting $state.go() function to work. The $on('$stateChangeStart'...); is functioning properly, and I can see the console message when trying to access a protected state without permission. However, the $state.go('toState ...

Encountering an issue with inability to resolve the 'react-navigation-stack' module. Still seeking a solution to this problem

Having trouble with my react navigation in react native. I've already added npm react-navigation-stack and also npm install --save react-native-gesture-handler react-native-reanimated react-native-screens. The error message I'm getting is: Unab ...

When reopening the modal in Bootstrap V5 with jQuery, the close event may not trigger as expected

When trying to implement special functionality in my modal close event using sweetalert2, I encountered an issue where the close event does not trigger again after closing the modal for the first time. Check out a live example here: https://codepen.io/th ...

Is this accessible within a function?

When attempting to pass a reference to a class through to a method in order to access a variable, I encountered a few issues: My initial attempt looked like this: p.id = 1; p._initEvents = function(){ $('#file-form').on('valid.fndtn.ab ...

The condition for "Else" always remains untouched

This snippet is a segment of my partial view content - <div class="form-group row"> <label for="email" class="col-sm-2 form-control-label">Email address</label> <div class="col-sm-9"> <input type="email" class=" ...

Coloring of circular items following the use of 'model.setThemingColor(dbId, color, true)'

My model contains some circular elements that I am trying to colorize using model.setThemingColor(dbId, color, true);. However, this process is causing the shading of the elements to look poor and making the object difficult to interpret. https://i.sstatic ...

Retrieve the Twitter user's profile picture

I am looking for a fast method to retrieve a Twitter profile image using either PHP or Javascript. My goal is to obtain the full image URL, not just the avatar size. Any code example would be greatly appreciated. Thank you! ...

Learn how to implement a captivating animation with JavaScript by utilizing the powerful Raphael Library. Unleash the animation by triggering it with either

My desire is to indicate this movement by triggering a mouse click or drag to the desired location. let myDrawing = Raphael(10,10,400,400); let myCircle = myDrawing.circle(200,200,15); myCircle.attr({fill:'blue', stroke:'red'}); let my ...

Using the code `$("img").one('load'` can trigger only a single load event without repetition

I have implemented the following function: function CheckImage(url, index) { $("<img/>").one('load', function () { $("div.photos").append(this); }).attr('src', url).attr('data-photo', '0' + ind ...

issue with customized select dropdown within a bootstrap modal window

Exploring how to implement a customized select box with unique CSS and search functionality within a Bootstrap modal. Here is the code snippet for the select element: <link rel="stylesheet" href="chosen.css"> <select data-placeholder="Choose ...

What is the best way to retrieve the initial element from a map containing certain data?

I am attempting to retrieve the first image path directory from an API that contains an Image so I can assign the value to the Image source and display the initial image. However, when using fl[0], all values are returned. Below is the code snippet: {useL ...

Utilizing a jQuery click event to modify the placement of a table

I have a request regarding the tables within the #middlebox. I would like to be able to rearrange the table positions by clicking on the list items. For instance, if the current order of the tables is starter -> soup -> seafood, clicking on the #seaf ...

Using jQuery UI to trigger Highlight/Error animations

Apologies if this question seems obvious, but I can't seem to find a clear answer anywhere... Is there a way to add a highlight/error message similar to the ones on the bottom right of this page: http://jqueryui.com/themeroller/ by simply using a jQu ...

Steer clear of downloading images while on your smartphone

As I develop a responsive website, I encounter the challenge of optimizing image loading based on viewport size. While using CSS to hide images not needed for certain viewports can reduce display clutter, it does not prevent those images from being downloa ...

Unable to store a customized class object in Parse database

I have been attempting to create a Customer class object that is linked one-to-one with the User class. However, despite my efforts, the object does not save and no error message appears. Here is the code I am working with: Parse.Cloud.afterSave(Parse.Us ...

Exploring the process of passing an array as a function argument from PHP to JavaScript

I am looking for assistance in passing an array as a function argument from PHP to JS. The values I need are retrieved from a database. while ($rows = pg_fetch_array($qry)) { ?> <option value="<?php echo $rows[&ap ...

``Can anyone provide guidance on extracting data from Xmlhttprequest POST request on my Nodejs express backend?"

Is there a way to properly send the values of candid and candidresults to my backend route /savevote in Express? Any help on how to achieve this would be appreciated. var candid; var candidresults; ...

What is the best way to initiate a re-render after updating state within useEffect()?

I'm currently strategizing the structure of my code using React hooks in the following manner: Implementing a state variable to indicate whether my app is loading results or not The loading state turns to true when useEffect() executes to retrieve da ...

Create a datastring using a JSON object

When receiving data in JSON format from an AJAX call, the following code is used to parse it: var parsed=JSON.parse(data); An example output could look like this: {"confirm_type":"contract_action","job_id":12,"id":7} To generate a dynamic data string f ...