How can I combine multiple textures from mtlLoader and objLoader in three.js?

I have the files .mtl, .obj, and several .jpg textures. I have been attempting to use different textures in the export loader OBJ, and while I can see my object on the scene, it appears as a black color. Can anyone spot what might be incorrect or missing in my code?

var mtlLoader = new THREE.MTLLoader();
mtlLoader.setPath('models/LivingRoom/Sample/');
mtlLoader.load( 'small plant.mtl', function( materials ) {
  materials.preload();

  var objLoader = new THREE.OBJLoader();
  objLoader.setMaterials(materials);
  objLoader.setPath('models/LivingRoom/Sample/');
  objLoader.load( 'small plant.obj', function ( object ) {

      var geometry = object.children[ 0 ].geometry;
      var materials = [];
      var mat1 = new THREE.MeshLambertMaterial( { map : THREE.ImageUtils.loadTexture('models/LivingRoom/Sample/Listik-2.jpg')});
      var mat2 = new THREE.MeshLambertMaterial({ map : THREE.ImageUtils.loadTexture('models/LivingRoom/Sample/22_zemlya_oboi_1920x1080.jpg')});
      materials.push(mat1);
      materials.push(mat2);
      mesh = THREE.SceneUtils.createMultiMaterialObject(geometry, materials);
      mesh = THREE.SceneUtils.createMultiMaterialObject(geometry, threeDTexture);

      object.traverse(function (child) {
        if (child instanceof THREE.Mesh) {
          child.materials = materials;

        }
      });

    },
    function ( xhr ) {
      returnValue = ( xhr.loaded / xhr.total * 100 ) + '% loaded';
      console.log(returnValue);
    },
    function ( error ) {
      console.log( 'An error happened' );
    }
  );
});     

Answer №1

Make sure to double-check the paths for textures in your .mtl file. If the paths are incorrect, consider using a 3D object editor such as Blender to make necessary corrections.

Blender is capable of importing obj files with textures smoothly when using the Blender Internal renderer. However, importing obj files with textures while Cycles is the active renderer is not currently supported. In such cases, you will need to create a new material with your texture manually. Therefore, it's crucial to verify which renderer you're using when attempting to import the mesh.

It's important to note that the Obj file format consists of two files: the *.obj file containing mesh data and the *.mtl file containing phong materials with associated texture paths. Fortunately, the Obj file format is straightforward and can be edited using a text editor for customization.

If the *.mtl file is missing, Blender won't be able to import any materials or textures. Similarly, if the texture paths in the *.mtl file are absolute and the texture is located elsewhere, Blender will fail to import the textures. To resolve this, ensure that the *.mtl file exists and that the texture paths are either relative or adjusted accordingly.

For example, changing an absolute texture path like: map_Kd C:\UserX\myfolder\mytextures\mytexture.png to a relative path like: map_Kd mytexture.png can help ensure that the texture is loaded correctly, especially when using Blender Internal renderer.

For further guidance on exporting obj format files with textures in Blender, you can refer to this Stack Overflow thread.

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

Steps to display images within a div from a specific directory

Recently, I have encountered a challenge that involves retrieving images from a specific directory, regardless of the number of images present, and then displaying them in a div using an unordered list. I attempted the following code snippet, but unfortuna ...

Creating a promise in an AngularJS factory function to perform an operation

When working within an Angular factory, I am tasked with creating a function that must return a promise. Once the promise is returned, additional functionality will be added. SPApp.factory('processing', ['$http', '$rootScope&a ...

Importing modules from another module in Node.js

I'm currently working on a nodejs app that consists of two files, index.js and ping.js. The main functionality is in index.js which handles routing and other tasks, while ping.js utilizes phantomJS. In my index.js file, I have the following line of co ...

Dynamically loading Ember templates with asynchronous requests

I need a way to dynamically load HTML content from another file using the code below: App.MainView = Ember.View.extend({ template:function(){ $.ajax({ url: 'views/main.html', dataType: 'text', async: false, ...

What is the best way to generate the message dynamically?

I have implemented the react-intl package for translation purposes in my project. Users have the option to choose between Spanish and English languages, with Spanish being the default language. When a user switches to English, all text should be dynamicall ...

How can I use jQuery to set the color of every other row in a

I'm facing an issue where I want to use jQuery to set the color of alternate rows in an HTML table. However, every time I add a new row, the color of the entire table switches. Below is the JavaScript code snippet that I am utilizing: var alternate = ...

Update input field value with data retrieved via ajax call in AngularJS

My current approach involves using AngularJS directives for Bootstrap in order to create an edit form on a Bootstrap modal when the user clicks on the edit button from a list of items. Here is the code I have implemented: HTML: <div class="modal-heade ...

Sending the most recent result to each dynamically created div

In my WordPress project, I have an event panel that displays upcoming event details and shows the remaining time until the next event. The countdown dynamically gets values from the database and calculates the time left based on the user's input in th ...

Utilizing JQuery for responsive screen size detection coupled with the scroll top feature

I've been trying to incorporate a screen size parameter into the function below, but so far I haven't had any success. I seem to be stuck at this point. The code shown here is working correctly... $(document).ready(function(){ $( ...

Convert a relative path to an absolute path using the file:// protocol

When I scrape a website with similar html content, I come across the following code: <a href="/pages/1></a> In addition to this, I have access to the window.location object which contains: origin:"http://www.example.org" This allows me to ...

Mastering the art of resolving a dynamic collection of promises with '$.all'

Imagine having 3 promises and passing them to $q.all. This results in a new promise that resolves when the 3 promises are resolved. But what if I realize before the 3 promises resolve that I also want a 4th promise to be resolved? Can this be achieved? I ...

What is the process for activating namespacing on a VueX module that has been imported?

I am currently utilizing a helper file to import VueX modules: const requireModule = require.context('.', false, /\.store\.js$/) const modules = {} requireModule.keys().forEach(filename => { const moduleName = filename ...

Is my React component being rendered twice?

I have created an app component and a test component. Within both components, I have included a console.log statement in the render method. Upon observation, I noticed that the app component is only rendered once, while the test component renders twice. Ad ...

Tips for resolving the error message: React is not able to identify the `currentSlide` and `slideCount` prop on a DOM element

I recently implemented an image slider using "react-slick" in my next.js project. However, I encountered some warnings in the console related to the 'currentSlide' and 'slideCount' props on a DOM element. Warning: React does not recogni ...

Select the text inside the current cell of a jqGrid table

The issue at hand is that when using jqGrid with cellEdit:true, users are unable to select text in a cell. Once the mouse button is released, the selection resets. This limitation prevents users from copying the selected text within cells. Has anyone enco ...

I've received an array of objects, but I require the objects to be consolidated into a single array using a MongoDB query

Hello, I am a newcomer to mongodb and I am attempting to generate a specific output using the mongodb aggregate function. I am aiming for a unified array of objects rather than multiple arrays of objects. Can someone assist me in achieving this desired out ...

Having issues with templateURL not working in AngularJS routeProvider

I've been experimenting with ngRoute and I'm facing an issue with templateURL not working as expected. The functionality works perfectly except for when I attempt to use templateURL. Here are the 4 files involved: test.html <p>{{test}}&l ...

What is the most effective method for inputting a date/time into a Django view?

Looking to create a feature where users can see what events are happening at a specific time. What is the most efficient method to implement this request? For example, if I want to display all current events, should I submit a post request to /events/2009 ...

What is the best way to swap out an HTML file with another without altering the link?

I'm working on an HTML file, which is basically a webpage. My goal is to create a functionality where clicking a button will dynamically replace the content of the page with another HTML file, complete with its own CSS, JavaScript functions, and other ...

Forcing a property binding update in Angular 2

Take a look at this particular component import {Component} from 'angular2/core' @Component({ selector: 'my-app', providers: [], template: ` <div> <h3>Input with two decimals</h3> <input type ...