Utilize texture in shader material incorporated into object loaded with object loader in three.js

I have encountered an issue while trying to incorporate a texture into a shader material used in a Blender object loaded through the object loader THREE.OBJLoader.

The following simple code functions correctly, loading a texture and applying it to the loaded object.

    var manager = new THREE.LoadingManager();
    var texture = new THREE.Texture();
    var imageLoader = new THREE.ImageLoader(manager);
    var objLoader = new THREE.OBJLoader(manager);

    imageLoader.load("texture.png", function (image) {
       texture.image = image;
       texture.needsUpdate = true;
    });
    var material = new THREE.MeshBasicMaterial({ map: texture});
    objLoader.load("blenderObject.obj", function (object) {
       object.traverse(function (child) {
       if (child instanceof THREE.Mesh) {
          child.material = material;
       }    
    });                
    scene.add(object);

However, I wish to utilize my ShaderMaterial instead of a MeshBasicMaterial. This shader material functions properly with other objects in my projects. Below is how I initialize my shader material:

        var uniforms = {
          texture: { type: "t", value: THREE.ImageUtils.loadTexture("texture.png") },                 
        }

        var shader = THREE.ShaderLib["shader"];

        var material = new THREE.ShaderMaterial({
            uniforms: uniforms,
            vertexShader: shader.vertexShader,
            fragmentShader: shader.fragmentShader,
            depthTest: true,    
        });

When I use this material instead of MeshBasicMaterial, there are no errors but the object appears entirely black. I am curious about the correct method to apply textures in shader materials with objects created by THREE.OBJLoader. Applying this shader to other objects yields the expected results. Thank you in advance for any insights.

Answer №1

All textures are equally important in creating objects. The method you choose to create them does not make a difference.

The issue with your current code is that the texture value assigned to the `uniforms` variable will be null because `THREE.ImageUtils.loadTexture()` function is asynchronous. The recommended approach is to use `THREE.TextureLoader()` as explained in the documentation:

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

Reactstrap and React-router v4 are failing to redirect when there is a partial change in the address link

Within the header of my website, <NavItem> <NavLink tag={Link} to="/template/editor">Create New Template</NavLink> </NavItem> On the routing page of my website, <BrowserRouter> <div className="container-fluid"> ...

Best practice for retrieving the $scope object inside the ng-view directive

Check out my plunk. Incorporating ngRoute into my project. I want to increase a value using ng-click, and upon clicking "Show total", the ng-view template should display the updated value. However, it seems like the scope is not being accessed as expecte ...

Issue with symbol not functioning on different device

There seems to be a display issue with the ...

Implementing class using Javascript

I have a JavaScript function that switches grid images when clicked. It also delays the href to the second click, allowing the switched image to be displayed. What I want to achieve is to add a class on the first click, triggering another JavaScript code ...

Sorting alphabetically, either by JAVA, JavaScript, or Idoc script

Currently, I have a task at hand that requires sorting items within categories alphabetically, with the exception of Examples. Special characters and numbers should be prioritized over letters in the sorting order. I've encountered an issue where mos ...

Steps for implementing a Toggle Navigation Bar in CSS

I'm looking to implement a show/hide navigation menu similar to the one showcased in this inspiration source: Code Snippet (HTML) <div id="menus"> <nav id="nav"> <ul> <li><a href="#">HOME</a></li> <li& ...

Using Javascript to dynamically populate dropdown options based on radio button selection

I am in the process of developing a basic form that allows users to input the frequency of a specific report. Initially, users were only able to enter days of the week. However, due to changes in demand for certain reports, options such as workday and day ...

`Next application will have all accordions simultaneously opening`

Is there a way to make the accordion open only one item at a time? Currently, when I click on one accordion item, all of them expand simultaneously. The data is being fetched from a local JavaScript file and consists of a list of objects with questions a ...

Tips for ensuring the CSRF token functions properly on the browser when utilizing Django and React

Apologies in advance if this question seems beginner-friendly, but I have developed an application with Django backend and React frontend. I am currently working on implementing the CSRF token for the post request on the create endpoint using the code snip ...

Tips for effectively organizing and redirecting multiple Express routers within a project

Using Express has presented me with a challenge. I am unable to redirect from one route folder to another within my project structure. To provide more context, both my app.js and routes folder are located in the same directory. The middleware setup in my ...

Arrange the array in chronological order based on the month and year

I'm looking for assistance with sorting an array by month and year to display on a chart in the correct order. Array1: ['Mar19','Apr18','Jun18','Jul18','May18','Jan19'....]; Desired Output: ...

Click on the canvas to fill a specific area with JavaScript

My goal is to implement a JavaScript function that will shade specific areas of canvas drawings when clicked. Below is the code I have that draws a square inside a circle. <!DOCTYPE HTML> <html> <head> </head> <body&g ...

Redirecting to new page after submitting form using Ajax

I'm having some trouble with my HTML form submission using JQuery and AJAX. After successfully submitting the form, I want to display a modal and then redirect to another page based on certain conditions. I've written the logic in my JS file wh ...

Instructions for adding a class when a li is clicked and replacing the previous class on any other li in Vue 2

Even though I successfully used jQuery within a Vue method to achieve this task, I am still searching for a pure Vue solution. My goal is to remove a class from one list item and then add the same class to the clicked list item. Below is the code snippet w ...

Seeking advice on Javascript prototypes - how can I execute a function within a prototype without using "this"?

let Human = function (userName, userAge) { this.userName = userName; this.userAge = userAge; }; Human.prototype.sampleProto = function () { console.log(this.userName + " == " + this.userAge); ...

A simulated movement normal map without changing the camera position or adjusting the light source in Three.js

Is there a way to shift the normal map in Three.js without adjusting the camera or light source? I have a plane positioned perpendicular to the camera's view vector. This plane always remains at the center of the view. Whenever I move the camera, the ...

Employing the new operator in conjunction with a variable

Is there a way to achieve the following scenario: var foo = function(){ this.value = 1; } var bar = "foo"; var baz = new bar(); alert(baz.value) // 1 Specifically, I am looking for a method to instantiate an object using its string name. Any sugge ...

When a <a href> link is clicked, the Struts action should open as a popup

I have a form on my jsp page, <form action="test1_action" name="test" method="post" id="test"> Additionally, I have two distinct links: link1 and link2. When link1 is clicked, the form should be submitted with the action test1_action. $('#l ...

Separate the selected option in the TEXTAREA by commas to make it easier to

Can you assist me with integrating this example? I have the following elements: When adding a textarea, I require an option to be selected and separated by a comma. For instance: Here I will select an option: Subsequently, this chosen option must be ad ...

State not getting updated with array passed to this.setState()

Within an array, I have stored the following credentials: //The Snippet variable is an array that looks like this { details: test, _id: "5d9d0b506ee7d03a54d8b1f6", name: "TEST GREEN", content: "<div style="background-color:green; height:100px; widt ...