Ways to implement shader on a THREE.Object3D that has been loaded using OBJMTLLoader

After loading a model with THREE.OBJMTLLoader, I want to add a vertex and fragment shader to it.

var loader = new THREE.OBJMTLLoader();
loader.addEventListener('load', function(event) {
    var mesh = event.content;
    scene.add(mesh);
});
loader.load('model/machine.obj', 'model/machine.mtl');

What is the process for applying a vertex and fragment shader to the loaded model?

Answer №1

Building on Gero3's response, implementing the suggested code will ensure that all meshes within the content have the correct material applied:

let customMaterial = new THREE.ShaderMaterial({
    uniforms: myShader.uniforms,
    fragmentShader: myShader.fragmentShader,
    vertexShader: myShader.vertexShader
});

let selectedObject = event.content;
selectedObject.traverse(function(child) {
    if (child instanceof THREE.Mesh) {
        child.material = customMaterial;
    }
});
scene.add(selectedObject);

Answer №2

To incorporate both a vertex and fragment shader, you must utilize a shadermaterial.

            var material = new THREE.ShaderMaterial( {

                fragmentShader: shader.fragmentShader,
                vertexShader: shader.vertexShader,
                uniforms: shader.uniforms

            } ),

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

Is it possible for me to transfer a class attribute to a directive template in AngularJS?

I recently came across this directive in AngularJS: productApp.directive('notification', function($timeout) { return { restrict : 'E', replace : true, scope : { type: "@", message: "@ ...

MUI-Datatable rows that can be expanded

I'm attempting to implement nested tables where each row in the main table expands to display a sub-table with specific data when clicked. I've been following the official documentation, but so far without success. Below is a code snippet that I& ...

Failure to load a picture does not trigger onError in the onLoad function

Is there a way to ensure that an image always loads, even if the initial load fails? I have tried using the following code snippet: <img class="small" src="VTVFile4.jpg" onload="this.onload=null; this.src='VTVFile4.jpg?' + new Date();" alt=" ...

"Can you send multiple variables using an Ajax post by using a loop

I'm trying to figure out how to post multiple variables from dynamically generated "username" ids like "username1", "username2", and so on, all in one ajax post request. The issue I'm facing is mainly with the data parameter. var numOfInputs = $ ...

Extracting user input from an iframe and transferring it to another frame in HTML format

Can someone advise me on how to extract values from iframe1 and transmit them to iframe2 as HTML? I am open to either JavaScript or jQuery - no preference. As a beginner in javascript, I stumbled upon some code that seems relevant. <!DOCTYPE html> ...

ChartJS has compatibility issues on Windows 10, regardless of the browser being used

Recently, I performed a fresh installation of Windows 10 on my laptop. However, after this process, I encountered an unusual issue with ChartJS on multiple pages of my site. Despite trying various browsers like IE11, Edge, Chrome, and Firefox, the charts s ...

How can I set up flat-pickr for date and time input with Vue.js?

I'm currently working with flat-pickr. Let's dive into the configuration part of it. I have a start date field and an end date field. My goal is to make it so that when a time is selected in the start date field, it defaults to 12h AM, and when a ...

Node server quickly sends a response to an asynchronous client request

Apologies for my lack of writing skills when I first wake up, let me make some revisions. I am utilizing expressjs with passportjs (local strategy) to handle my server and connect-busboy for file uploads. I believe passport will not have a role in this pr ...

Retrieving components from Ajax response data

While I have a good grasp of PHP, diving into AJAX and dealing with JSON is proving to be quite challenging for me. My PHP script simply delivers a straightforward JSON string like this: {"bindings": [ {"ircEvent": "PRIVMSG", "method": "newURI", "regex": ...

Error: The componentwillmount function has encountered an issue. Actions must be in the form of plain objects. For asynchronous actions, consider

Currently, I am setting up a feature to retrieve all images based on their type using redux-saga. There are two types available: kristik and motif. While implementing the kristik type, everything works smoothly and I receive successful responses. However, ...

Exporting all components using require: A complete guide

Currently, I am working on a Vue js application where I am tasked with exporting all the files and components from a specific directory. These files/components need to be imported into a separate file named paths.js. If I use the require function to impor ...

When using Angular2, I have found that I am unable to extract the body data from JSONP responses. However, I have discovered that this issue

Initially, I developed the SERVER using spring-boot framework. The code for this looks like: public class App { @RequestMapping("/") @ResponseBody String home(HttpServletRequest request) { String aa=request.getParameter("callback"); System.out.pri ...

Maintain the markup created by jQuery inside the AJAX function called by setInterval

In the code snippet below, real-time markup is generated and automatically updates using the setInterval function. The markup includes buttons and hidden content within div elements. Each button reveals its corresponding div, but the div reverts to a hid ...

Show the identical Javascript code in Bootstrap columns with a size of col-sm6

I have a javascript code with a function called fuctionone() that generates a graph. I include this code within a p tag so that when 'HERE' is clicked, the graph is displayed below each section header. <svg width="480" height="250"> <di ...

Tips for updating information when a button is chosen

Hello everyone, I need some help with a form that has three select buttons. The button labeled "Distribute" is already selected when the page loads, and it contains information about full name, password, and location. How can I use JavaScript to create a c ...

Even though I am aware that the variable AJAX is attempting to return is not empty, it is still returning 'undefined'

I wrote a basic PHP script that retrieves information from a database and stores it in a multidimensional array: <?php //PHP code to fetch data from DB error_reporting(E_ALL); $db = new mysqli("localhost","root","pass", "Media") ...

The appearance of Recaptcha buttons is unattractive and seemingly impossible to

Let me start by saying that I have already looked into the issue of "Recaptcha is broken" where adjusting the line-height was suggested as a solution. Unfortunately, that did not work for me. After implementing Google's impressive Recaptcha on my web ...

Instructions on how to trigger an http request upon clicking a link, alter the href, and proceed to navigate to the new destination

Here is a piece of code that demonstrates how to modify an a element's href before the user navigates to the new link: <a href="/initial-link" onclick="modifyHref($event)" /> <script> function modifyHref(event) ...

Issues arise with the functionality of custom jQuery sliders

I'm attempting to create a bootstrap panel slider using my own jQuery code, but it's not functioning as anticipated. My Objective: I have two links for "previous" and "next", and three panels with distinct headings. When the "previous" link is ...

`Unable to retrieve HTTP headers from PHP when using AngularJS`

My challenge lies in extracting a header from an http call in AngularJS sourced from a PHP website. The website admin assures me that CORS is enabled and the server allows JavaScript access to cookies. Here is a snippet of the code: $http({ me ...