Is it possible to dynamically add custom shaders to objects in three.js?

Is it possible to dynamically apply custom shaders to a MeshBasicMaterial with VideoTexture in Three.js? I have successfully applied shaders to the entire scene using THREE.EffectComposer, but how can I apply custom filters to a specific element within the scene? I am interested in testing simple filters like sepia and hue-saturation, and I also need to be able to switch between these without reloading the project.

I chose not to use JavaScript libraries like seriously.js or glfx.js because they may cause issues later on when working with Three.js objects that do not have canvas/img/video as maps.

videoTexture = new THREE.VideoTexture(video);
videoTexture.minFilter = THREE.LinearFilter;
videoTexture.magFilter = THREE.LinearFilter;
material = new THREE.MeshBasicMaterial({
    map: videoTexture,
    side: THREE.DoubleSide,
    transparent: true,
    depthTest: false,
    depthWrite: false
});

composer = new THREE.EffectComposer( renderer );
composer.addPass( new THREE.RenderPass( scene, camera ) );
var effect = new THREE.ShaderPass( THREE.SepiaShader );
effect.renderToScreen = true;
composer.addPass( effect );
composer.render();

Edit 1: Using CSS filter won't work either as it only changes visual media and cannot manipulate internal object textures. See more here: https://developer.mozilla.org/en-US/docs/Web/CSS/filter.

Answer №1

If you're looking to reset the EffectComposer, simply utilize the .reset function. Load your desired filters into an array and then apply them by resetting and adding passes from that array.

UPDATED It seems I misunderstood your query about changing shaders within the composer. To alter shaders on an object, consider creating multiple shadermaterials with different shaders while sharing the same texture. Then, interchange these materials on the object for various effects such as sepia tones or black and white conversions.

You can easily switch between multiple materials in real-time. If writing shaders poses a challenge, feel free to seek help on specific problems through another thread. For beginners in glsl, I recommend checking out the book of shaders for a quick crash course in glsl programming.

The key solution is to either create multiple shadermaterials or develop one versatile material using uniforms to toggle between varied effects seamlessly.

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

Managing and retrieving data in bulk from an indexed database as JSON format

I am currently developing an application that utilizes IndexexDB for local data storage. I am looking for a way to extract and insert large amounts of data from an indexed DB in JSON format. Below is the code snippet illustrating what I have accomplished s ...

What is the best way to integrate a Svelte component into a vanilla JS project without including Svelte as a dependency in the main project?

Is there a way to import a Svelte component into a project that does not have Svelte as a dependency? Imagine creating an npm package with a Svelte component and installing it in different projects without needing to add Svelte as a dependency. The package ...

Implementing a JavaScript Date object within a Kendo Grid

As a newcomer to web development, I recently came across an issue while testing some basic functionalities. In my database, I have data stored in SQL datetime format in UTC GTC timezone: Link to Database To display this data in a Kendo Grid and append "U ...

arrangement of columns within the <select> element

I have been attempting to create a column layout inside of <select> using CSS, but so far I have been unsuccessful. https://i.sstatic.net/ltIDl.png This is the HTML code I have tried: <select id="select-bill-period"> <option>Select ...

0 situation where using an empty value with PHP SESSION

Good Morning! I'm encountering an issue with a variable named "var" which can be 0. I'm trying to check if the variable is null, and if it's not, I save it in a PHP session and call another page. When I check the session on the new page, it ...

Chrome experiencing problems with placeholder text fallback event

My text field has a placeholder text that says "First Name". When the user clicks on the field, the text disappears and allows them to type in their own text. HTML <input class="fname" type="text" placeholder="First Name"/> JS function handlePlac ...

Can the Three.js Orbit Controls be customized to modify their appearance?

While I appreciate the orbit controls that come with Three.js, I am wondering if there is a way to customize them to follow the path of an ellipse (or technically an ellipsoid) instead of a circle. My main goal is to move the camera in an ellipse using th ...

Hide the search popup when the user clicks on the "find" button within the jqgrid

What is the solution to closing the search popup when clicking on the Find button? When I search for data in jqgrid and click on the Find button, the Search popup remains open even though the data has been filtered. How can I close the popup after searchin ...

Executing Node.js Function from an External File Dynamically

Is it possible to run a Node function from an external file that may be subject to change? main.js function read_external(){ var external = require('./external.js'); var result = external.result(); console.log(result); } setInterva ...

Combining two numbers retrieved from Firebase using React

Hello, I am new to React and finding it challenging to perform mathematical calculations with React. I have been attempting to add two values retrieved from a Firebase database, but they keep displaying as strings without adding the actual values together. ...

The <br/> tag is not functioning properly when retrieving text from a MySQL query

Currently, I am involved in a project that involves an A.I pushing text onto a MySQL database. Our goal is to display the ongoing writing process on a webpage. We are still actively working on this. We are retrieving the data and regularly checking the da ...

What is the process of choosing a language (English/French) within a pop-up?

<body style="text-align:center"> <h2>Popup</h2> <div class="popup" onclick="myFunction()">Interact with me to show/hide the popup! <span class="popuptext" id="myPopup">A Simple Popup!</span> </div> <script& ...

Continue executing code until the Button is clicked once more

I was learning some new concepts and had a question that I couldn't find an answer to online. So, here's the button I have: <button onClick={toggle} className="common-btn">{buttonText}</button> I want this button to contr ...

Passing parameters in a callback function using $.getJSON in Javascript

I am currently using a $.getJSON call that is working perfectly fine, as demonstrated below. var jsonUrl = "http://www.somesite.co.uk/jsonusv.php?callback=?"; $.getJSON(jsonUrl,function(zippy){ ...some code } However, I want to pass a ...

Submitting data to an external PHP file using the JavaScript function $.post

Having issues with the PHP page function ajaxFunction(){ var ajaxRequest; try{ ajaxRequest = new XMLHttpRequest(); } catch (e){ try{ ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try{ ajaxRequ ...

Encountering sporadic EACCES issues while executing selenium tests in the AVA framework

Encountering errors on Windows 10 1809 while using Chrome for testing purposes. Error message 1 Frequency: Occurs approximately every 50th driver instantiation. [...]project\node_modules\selenium-webdriver\net\portprober.js:159 ...

What is the best way to combine a string with a $scope variable in AngularJS?

Is there a way to add a variable to the $scope model in order to concat it? I'm attempting this code: for(var i=0; i<=response.length-1; i++) { $scope.formData.jobId+i = response[i].jobId; } How can I combine the variable i with $scope.formDat ...

Storing the created .wav file on the server using PHP

Is there another way to handle this? How can I manage streaming of a WAV file? I'm currently working on a platform that allows users to create their music compositions and the system outputs a .wav file for each creation. While I can play the mus ...

Building a contact form in Angular and sending emails with Nodemailer

Currently, I am in the process of setting up a contact form for my website. Since I am utilizing a MEAN stack, it made sense to incorporate the nodemailer module for sending emails. In order to handle this functionality, I have established an endpoint &ap ...

Adjust the node_modules path tailored to your project

My current project structure looks like this: |-app |-... |-libs |- package.json |- index.html I am interested in organizing my project such that the 'node_modules' folder is inside the 'libs' folder. The desired structure is as fo ...