Updating bump map in Three.js not working as expected

I have successfully loaded a model with the corresponding material file (.mtl) into my scene. I am now adding a bump map to it after loading:

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

            // Load obj file
            var objLoader = new THREE.OBJLoader();
            objLoader.setMaterials(materials);
            objLoader.setPath('/models/');
            objLoader.load('model.obj', function (group) {

                var geometry = group.children[0].geometry;
                geometry.center();

                model = new THREE.Mesh(geometry, otherMesh.material.clone());
                model.material.bumpMap = new THREE.ImageUtils.loadTexture('images/bump.png');
                model.name = "obj model";
                scene.add(model);

                render();
                callback();
            });
        });

Everything works smoothly when applying the bumpMap just before adding the model. However, if I try to apply the bump map at a later stage, it doesn't work. For example, using this code snippet will not apply the bump map:

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

            // Load obj file
            var objLoader = new THREE.OBJLoader();
            objLoader.setMaterials(materials);
            objLoader.setPath('/models/');
            objLoader.load('model.obj', function (group) {

                var geometry = group.children[0].geometry;
                geometry.center();

                model = new THREE.Mesh(geometry, otherMesh.material.clone());
                setTimeout(function(){
                    model.material.bumpMap = new THREE.ImageUtils.loadTexture('images/bump.png');
                }, 0);

                model.name = "obj model";
                scene.add(model);

                render();
                callback();
            });
        });

The issue arises when trying to add the bump map using timeout. Even setting

model.bumpMap.needsUpdate = true;
after applying the bump map does not resolve the problem.

Answer №1

Once a material has been rendered at least once, introducing a new texture that was not previously used necessitates the creation of a new shader program. To trigger this process, you must set

mesh.material.needsUpdate = true;

For more information, refer to the documentation on updating things in three.js How to Update Things.

Version: three.js r.85

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

"Error: Unable to Access Map Property of Undefined - Troubleshooting Twitter API Integration in

Hello everyone! I am still new to React, so please be patient with me :0) Currently, I am working on developing a simple React application where users can input a search term and receive a list of Tweets. I am in the initial stages of building this app us ...

Identifying Shared Links in Node.js

Is there a way to determine if a file-system path is a hard link using Node.js? The fs.lstat function provides a stats object that can identify if a path is a hard link by returning true for stats.isDirectory() and stats.isFile() respectively. However, the ...

Discover similar items within an array by utilizing async/await and promise.all

The value of filterdList.length always equals the total number of elements with the code provided below. As a result, this method consistently returns false because there is only one item in the table that matches the given name. async itemExists(name) : ...

Is there a way to display a 404 error in Vue without altering the URL?

Query I'm trying to figure out how to display a 404 error in a dynamic route using Vue.js without changing the URL. Can anyone help? This is part of my route.js file (everything seems fine) { path: '/work/:id', name: 'work& ...

Managing data in an Angular Material table and dialog for creating, reading

Currently, I am struggling with implementing a CRUD method using angular material table and dialog components. I am facing difficulties in understanding how to update records using the dialog and pass data between components. Despite my efforts, the modif ...

Issues arise with matrix multiplication in three.js

Having a Matrix4() named 'transform' with various operations already applied to it, I have successfully chained multiple rotations and translations. However, when attempting to apply one final rotation, I am encountering issues. Despite trying di ...

Creating an optimized dashboard in Next.js: Expert tips for securing pages with specific roles, seamlessly implementing JWT authentication without any distracting "flickering" effect

Given our current setup: We have a backend ready to use with JWT authentication and a custom Role-Based Access Control system There are 4 private pages specifically for unauthenticated users (login, signup, forgot password, reset password) Around 25 priva ...

What is the best method for filtering a dynamic object in Express.js?

Utilizing express for my REST API, I am looking to implement some filtering on my Posts based on req.query. Within the post structure, I have integrated a dynamic Object called options. Here is an example of my post structure: { "category": [ ...

Guide for populating select dropdown from Json file in Angular

As a newcomer to Angular, I am encountering some difficulties when attempting to bind values from a JSON file into a select form. Despite trying various solutions I found online, I am still unable to display anything. I am able to successfully retrieve the ...

Disable the second tab within an Angular md-tabs component

Are there any attributes in angular material md-tabs that can be used to disable a tab, similar to what is available in Bootstrap? $scope.tabs = [{ title: 'Dynamic Title 1', content: 'Dynamic content 1' }, { title: 'Dy ...

How to retrieve document.getElementsByName from a separate webpage using PHP and javascript

Hey there, I've been searching for a solution to this issue but haven't had any luck so far. I'm attempting to retrieve the value of the name test from an external website. <input type="hidden" name="test" value="ThisIsAValue" /> Up ...

Preventing the descent into callback chaos in NodeJS

I've come across this function: /* GET main page */ router.get('/', (req, res, next) => { Account.find({accType: 'Employer'}, (err, col) => { if (err) { console.log(err); } else { ...

Display a still image in cases where the browser does not support 3D images

I need to display a 3D image in my HTML page if the browser supports canvas, otherwise I want to show a static image. The image should be loaded dynamically when the page loads. <!DOCTYPE html> <html class="no-js" lang="en"> <head> ...

Text input not displaying as expected in React Native compared to a text area

I'm having trouble with the Text Area Placeholder not appearing in the center, I would like it to align with the top of the placeholder as shown in the image. Can you please provide suggestions on how to achieve this design? https://i.sstatic.net/Aqhn ...

Sorting in MongoDB v3.6 is a breeze with the powerful capabilities that the

I'm encountering an issue with arranging the most recently added item in a list of objects/items to display at the top. It's similar to a job board where I want the newest jobs to appear at the top rather than at the bottom. I attempted to use Po ...

Variations of a particular software package are necessary

My current project requires Expo, React, and React-Native as dependencies. The configuration in the package.jason file looks like this: "main": "node_modules/expo/AppEntry.js", "private": true, "dependencies": { "expo": "^28.0.0", "expo-three": "^ ...

JavaScript: Constructing an array composed of multiple arrays

I am attempting to create an array containing four arrays. Each of these four arrays will contain three numbers, with two of them being randomly selected from a set of numbers. Although I am not encountering any errors when running the code below, I am al ...

Is there a built-in method in Next.js 13 to secure routes from unauthorized access?

In my project, I have the following pages: /about, /user, and /user/[id]. Unfortunately, I am unable to access req.page, which provides the slug (i.e. /user/[id]), making it difficult for me to implement logic to redirect requests from unauthenticated user ...

Exploring Deeply Nested Routing in Angular

I've been exploring the use of multiple router outlets and encountered an issue. When using the navigateBy function of the router, I am unable to view my child route and encounter an error. However, when I access it via the routerLink in HTML, I get ...

Connecting Angularfire2 with Firestore for advanced querying

Glad you stopped by! Currently, I have two Firestore Collections set up in my Angularfire2 application. One consists of "Clients", while the other contains "Jobs". Each client can have multiple jobs assigned to them, and vice versa. I've been workin ...