Switch up the position of the vertex shader in Three.js/webgl

I've been working on a particle system using three.js that involves using regular JavaScript loops to change particle positions, but I've found that this method is quite slow.

Due to this performance issue, I've decided to delve into transferring the calculations to the GPU via shaders.

However, I've encountered an issue where I'm unable to get the positions to update properly. Each particle seems to remain at its initial position.

var vertexShader = [
        'void main() {',
        '    vec3 newPosition = position.xyz;',
        '    newPosition.y += 10.0;',
        '    gl_PointSize = 1.00;',
        '    gl_Position = projectionMatrix * modelViewMatrix * vec4( newPosition, 1.0 );',
        '}',
    ].join('\n');

My question is, why does

'    newPosition.y += 10.0;',

not update the particle positions as expected?

Do I need to introduce some sort of time variable into the shader code, as I've noticed others doing this in their implementations? Any advice would be greatly appreciated.

Answer №1

I figured it out, the position is considered as an Attribute. To manipulate it, I discovered that passing a uniform to the Shader is necessary. This uniform can then be utilized to increment the position Attribute.

Here is an example of a Vertex shader:

let vertexShader = [
        'uniform float time;',
        'void main() {',
        '   vec3 updatedPos = position;',
        '   updatedPos.x += time;',
        '   gl_PointSize = 1.00;',
        '   gl_Position = projectionMatrix * modelViewMatrix * vec4( updatedPos, 1.0 );',
        '}',
    ].join('\n');

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

Retrieving selected values from an ngx dropdown list

I am having trouble implementing ngx dropdown list in this way: <ngx-dropdown-list [items]="categoryItems" id="categoriesofdata" [multiSelection]="true" [placeHolder]="'Select categories'"></ngx-dropdown-list> ...

After toggling the class, Jquery will no longer select the button

I am having an issue with my jQuery code where I have a button that, when clicked, should switch classes from #testButton to .first or .second. The image toggle shows that the first click works fine and toggles the image, but the second click does not seem ...

Display a pop-up when hovering over a layer with react-leaflet

I am attempting to display a popup when hovering over a layer in react leaflet. Utilizing GeoJson to render all layers on the map and onEachFeature() to trigger the popup on hover, I encountered an issue where the popup only appeared upon click, not hover. ...

IE Troubles: Timer Function Fails in Asp.Net MVC

I implemented the following code snippet: @Using Ajax.BeginForm("Index", New AjaxOptions() With { _ .UpdateTargetId = "AnswerSN", .HttpMethod = ...

Ways to clearly establish the concept of "a"

module.exports.getData = function (id) { const userData = require("./data/Users.json"); if (userData.find(user => user.uid === id)) { return user.name; } else return "User"; } I'm trying to display the name of a user, but the consol ...

Retrieving checkbox value upon form submission

Imagine having a form containing several checkboxes. Upon submitting the form, you aim to display all values of the selected checkboxes. <form> <input type="checkbox" id="product1" name="product1" value="12"> <input type="checkbox" id="prod ...

Unable to successfully download zip files using Ajax in the MVC framework

I need to trigger the download of a zip file that contains .pdf files using an ajax call. The issue I'm encountering is that everything seems to be working fine except for the actual downloading of the zip file. public FileResult DownloadZip(string[] ...

To trigger the opening of one offcanvas menu from another offcanvas menu

I am facing a small issue with opening an offcanvas from a previous canvas. Typically, I have a canvas set up like this using Bootstrap 5: <button class="btn btn-primary" type="button" data-bs-toggle="offcanvas" data-bs-tar ...

show image with the help of jquery and ajax

To showcase company information along with an image, I have a controller set up as follows: public JsonResult DisplayCompanyDetails() { CompanyModel cm = new CompanyModel(); string query = "select CompanyName,Address,Conta ...

Select value not updating as expected

I have a select box that is linked to a switch statement in PHP, which changes the order of the results returned from the database based on the value selected in the select box. However, I am facing an issue with my JavaScript not working correctly. Can an ...

When it comes to Redux, is it considered an anti-pattern to pass an event from a presentational component to a container component

As a newcomer to Redux, I am challenging myself to rebuild an old React app using this technology in order to gain proficiency. However, I am facing a significant challenge regarding where to place the logic within the application. My understanding is tha ...

Looking for assistance in using JavaScript to determine the percentage of a DIV's width in pixels

Using PHP, I am generating boxes whose width is set to 100% of a container in CSS. Now, I want to determine the equivalent pixel value of that box... HTML <div class="masonry" > <? while($row = $stmt->fetch()){ ?> <div class="i ...

jQuery - Enhancing User Experience with Dynamic Screen Updates

Is there a way to update the screen height when resizing or zooming the screen? Whenever I zoom the screen, the arrows break. I'm also curious if the method I'm using to display images on the screen is effective. It's supposed to be a paral ...

The function res.send is unavailable and errors are not being properly managed

I encountered a peculiar error while using my function to create users in my mongoose database. Despite the user being successfully created, I am facing an issue where res.send is not functioning as expected, resulting in no response and instead, an error. ...

Tips on generating a sample Mongoose Model with all null values

Imagine you are working with the following schema: var userSchema = new Schema({ name: String, schools: [{ level: String, name: String, timeInterval: { start: {type:Date,default:Date.now}, ...

Generating a safe POST connection with express.js

Is there a simple method to generate a link for submitting a POST request using Express.js or a plugin? This approach can also be employed to enhance security for important actions like user deletion, including CSRF protection. In some PHP frameworks lik ...

What steps should be taken to incorporate a user input space similar to the one found in the Wordpress new post section

I am looking to incorporate a user input section on my website similar to the one found in WordPress for creating new posts. I would like this area to have all of the same tools available, such as adding hyperlinks, bolding text, and uploading images. Ca ...

Is it possible to apply action.payload to multiple keys within an object?

My plan for updating tasks in my Todo app is as follows: addTask: (state, action) => { const newTask = { id: uniqueId(), text: action.payload, completed: false, date: action.payload, }; state.push(newTas ...

Next.js encountered an error while trying to locate the flowbite.min.js file for Tailwindcss and Flowbite, resulting in a

I'm having an issue with integrating the flowbite package with TailwindCSS in my Next.js application. Despite configuring everything correctly, I am encountering an error when adding the flowbite.min.js script: GET http://localhost:3000/node_modules/f ...

Angular displays error ERR_UNKNOWN_URL_SCHEME when attempting to retrieve an image saved in a blob

As I transition my app from Electron to Angular, one of my main objectives is to display an image uploaded by a user. Here's how I attempted to achieve this: page.component.ts uploadImageFile(){ fileDialog({}, files =>{ //Utilizing the fileDi ...