Postprocessing with a basic shader in Three.js

I've been attempting to create a basic shader for adding noise, but I'm encountering difficulty retrieving the uv coordinates with my current setup.

Fragment Shader:

uniform float seed;
uniform sampler2D pass;

varying vec2 vUv;

void main (){
    //noise
    vec2 pos = gl_FragCoord.xy;
    pos.x *= seed;
    pos.y *= seed;
    float lum=fract(sin(dot(pos ,vec2(12.9898,78.233))) * 434658.5453116487577816842168767168087910388737310);

    vec4 tx = texture2D(pass, vUv);

    gl_FragColor = vec4(tx.r*lum,tx.g*lum,tx.b*lum,1.0);

}

Vertex Shader:

varying vec2 vUv;

void main (){
    vUv = uv;
    gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.0);
}

Rendering:

OBJECT.material = OBJECT.mat.flat; // THREE MeshPhongMaterial ({color: 0xE40D59,shading:THREE.FlatShading});

RENDERER.render(SCENE,CAMERA,BEAUTY_PASS,false);

OBJECT.material = OBJECT.mat.noise; // THREE ShaderMaterial

RENDERER.render(SCENE,CAMERA);

An error message is displayed:

Error: WebGL: DrawElements: bound vertex attribute buffers do not have sufficient size for given indices from the bound element array @

After some testing, I found that it works if I use the same coordinate for all pixels:

vec4 tx = texture2D(pass, vec2(0.5,0.5));

This displays my object with a reddish noisy color. However, the issue arises when trying to retrieve the uv coordinate on the second render pass (

RENDERER.render(SCENE,CAMERA,BEAUTY_PASS,False)
). It seems to work fine without this initial rendering pass.

Why am I unable to fetch the uv coordinate during the second render? Based on various examples, it should be possible to render using the same scene and camera configuration as shown in this example.

Answer №1

If the geometry doesn't already have a texture applied, it won't have the required WebGL UV buffers.

There are multiple ways to solve this issue, but the simplest might be to ensure that the mesh has a texture applied during its initial rendering. Even a basic white texture will suffice.

Version: three.js r.58

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

Ways to combine all similar key values into an array

Apologies if the question seems unclear. Essentially, I am dealing with an object structured as follows: [{"6":6.5},{"4":4.2},{"6":6.3}] My goal is to eliminate duplicate keys while preserving their values, and merge them into a single unique key, formin ...

Determine whether a value contains a minimum of two words or more using JavaScript

When users input their names into a field and submit it, I want to make sure that I receive both their first and last names. In order to do this, I need to check if the value contains at least two words. However, the current code I am using does not seem ...

Angular.js loads, processes, and presents a dynamic template that is fetched through an $resource from a REST

My Angular application requires customizable reporting functionality. The goal is to permit users to select from a variety of available reports, with a backend REST API providing both the template and data in JSON format for user customization. The app wi ...

Having trouble with understanding the usage of "this" in nodejs/js when using it after a callback function within setTimeout

It's quite peculiar. Here is the code snippet that I am having trouble with: var client = { init: function () { this.connect(); return this; }, connect: function () { var clientObj = this; this.socket = ...

Utilizing db.system.js Function within the $where Clause

Here is an illustration of a basic function that I have saved: db.system.js.save({_id: "sum", value: function (x, y) { return x + y; }}); However, when attempting to call it within the $where clause, a Reference not exist error occurs. <?php $collec ...

Tips for detecting when the MDC Snackbar has been closed using JavaScript

I'm currently working with Material Design's Snackbar in combination with VueJS. My goal is to detect when the snackbar has finished closing. The Snackbar object does have a property called isOpen, which allows me to check if the snackbar is ope ...

Step-by-step guide on removing all elements except the last one using pure JavaScript, without relying on jQuery and utilizing document.querySelectorAll(".someClass"):

Here's my question: Is there a way to remove all elements except the last one with the same class name using vanilla JavaScript without jQuery? In jQuery, I can achieve this in one line like so: $('.someClass').not(':last(2)').re ...

Scroll bar in Highstock does not completely cover the entire length when dealing with multiple series

Having trouble with the scrollbar on a multi-series line chart where the x-axis represents dates. It seems that the maximum length of the scrollbar is determined by the length of the first, older series. When clicking the 'All' button in the ran ...

Menu changes when hovering

I want to create an effect where hovering over the .hoverarea class will toggle the visibility of .sociallink1, .sociallink2, and so on, with a drover effect. However, my code isn't working as expected. Additionally, an extra margin is automatically ...

Evaluating text presence with Nightwatch and Selenium by checking for single quotes in an element

I need to verify if an element includes text with an apostrophe. I attempted: 'PROGRAMMA\'S' or "PROGRAMMA'S", such as .assert.containsText('element', 'PROGRAMMA\'S') However, neither method seems t ...

Using asynchronous import statements in Vue allows for more efficient loading of components

I am using Vue and have a method in my file called load.js. async function loadTask(x) { return await x; // Some async code } export { loadTask }; In one of my Vue components, I call the method but encounter an issue where the use of await prevents the ...

Using Mongoose schema with a reference to an undefined 'ObjectID' data type

I am currently working on establishing relationships between my schemas, and I have encountered some issues with my solution. Here is how my device schema looks like: var deviceSchema = schema({ name : String, type : String, room: {type: mongo ...

Every time you attempt to download in Vue, you are met with the familiar sight

Encountering a peculiar issue while attempting to download an apk file using a Vue.js website: Referring to How to download a locally stored file in VueJS, I am trying to download a local file with this command: <a href="../../app-debug.apk" download= ...

Encountering an issue while fetching information from a JSON file using JavaScript

I am encountering an Uncaught SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data let mydata = JSON.parse("file.json"); console.log(myJSON) Here is a sample of the JSON file's data: [[1,1,0,1,1,0,0,0,1,1,1,1,1, ...

Ever since I switched to a different monitor, my Javascript has suddenly stopped functioning

I'm encountering an issue where my JS stops working every time I switch displays within a single HTML file. I've attempted to replace the HTML onclick call with a JavaScript function, but the problem persists. Update: Apologies for the unclear e ...

Ways to display a box following the submission of a value within a form

There's a pop-up window where the user has to enter their name. After clicking the submit button, this window closes. You can try it out here: (Click the 21 button, enter any value, and see what happens) The function used for the pop-up box is: fu ...

Setting up authorization levels for roles in Discord.js

Hi everyone, I came across this script that deals with users posting invite links. How can I whitelist specific channels to prevent the bot from banning or kicking users for posting invite links? Any help would be greatly appreciated. Thank you. adminCli ...

Tips for updating checked checkboxes in a php mysql database

When submitting an HTML form with a selected checkbox, update the values of the selected checkboxes in a MySQL database. For example: update enquires set status = '2' where id in (selected checkbox values) View the screenshot of the checkbox Vi ...

Add a border to the navigation bar item to indicate the current page being displayed

This section shows the current navigation bar item https://i.sstatic.net/6RAGJ.png This is the desired outcome when clicking on the tab https://i.sstatic.net/8nFeB.png Uncertain about the best approach for achieving this. I have attempted to submit a val ...

Utilizing One-to-Many Microphone Streaming Technology

I'm looking to create a unique one-to-many microphone streaming system where a user can record from their microphone and others can listen in. I also need to be able to record the microphone session. Would it be better to use WebRTC for client commun ...