Real-time data texture updates in Three.js using JavaScript

Today has been a challenging day as I try to solve what should be a simple problem. I'm working on building an audio visualizer and my goal is to store the audio data array in a texture that updates every frame. Despite following three.js documentation and examples, updating the texture by flagging it as needsUpdate doesn't seem to work as expected. The initialization goes smoothly but it fails to update afterwards.

// Initialization

var simState = new THREE.PlaneBufferGeometry( 300, 300, 45, 45);

noiseSize = 256;

data = generateRandom(noiseSize); 

fftTex = new THREE.DataTexture(data, noiseSize, noiseSize, THREE.RGBAFormat);
fftTex.needsUpdate = true;

simUniforms= {
                fftArray: {type: "t", value: fftTex},
              };


var simShaderMaterial = new THREE.ShaderMaterial({
                    uniforms:       simUniforms,
                    vertexShader:   document.getElementById('basicVertexShader').textContent,
                    fragmentShader: document.getElementById('simFragmentShader').textContent,
                    transparent:    true,


                });

testPlane = new THREE.Mesh(simState, simShaderMaterial);


scene.add(testPlane);

animate();


// Render function

function render() {

  data = generateRandom(256); // Generate random noise of the correct size
  fftTex.needsUpdate = true;
  fftTex.data = data;

  renderer.render(scene, camera);

};


function animate() {

   requestAnimationFrame(animate);

   render();

};

I believed that would be all I needed. Is there something obvious that I am missing? The custom shader material seems to be working fine, though it's quite basic.

<script type="x-shader/x-fragment" id="fragmentShader"> 

#ifdef GL_ES
precision highp float;
#endif


    uniform vec3 color;

    varying float vAlpha;
    varying vec3 vColor;

    varying vec2 vUv;

    void main() {



        gl_FragColor = vec4(vUv.x, vUv.y, 1.0, 1.0);

    }

</script>  

Any advice or suggestions would be greatly appreciated.

Answer №1

Instead of utilizing data = generateRandom(), consider using data.set(generateRandom()) to ensure the data object remains the same reference. This approach proved helpful for another problem discussed at this Stack Overflow thread

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

Unique Soundcloud visualization using webglonDeletegist

After going through the SoundCloud documentation, I have been trying to figure out how to create a visualizer for SoundCloud tracks. The challenge is that in order to achieve this effectively, I need access to the source waveform. I suspect that direct acc ...

jQuery Does Not Iterate Through Arrays

I am encountering an issue where I am trying to populate an array with images and then pass them to a variable called $image. However, when I attempt to iterate over this array, the same item is being alerted three times. Here is the problematic code snip ...

Display a child component in a Vue template

Is there a way to display one of $children in the current vue template? Let's say I have three $children components, is it feasible to render this.$children[1]? The appearance of this.$children[1] is as follows: https://i.sstatic.net/8KzYJ.png ...

Unable to locate Ckeditor toolbar option within Vue (Laravel) framework

Currently, I am utilizing Ckeditor5 for Vue in Laravel. In accordance with the provided documentation, I have gone ahead and installed the necessary modules using the command npm install --save @ckeditor/ckeditor5-vue @ckeditor/ckeditor5-build-classic. Fol ...

What's the best way to display a bootstrap modal window popup without redirecting to a new page?

I am having trouble implementing a modal window that will display validation errors to the user when they submit a form. Currently, the window is opening as a new view instead of overlapping the existing form's view. How can I adjust my code so that t ...

Looking to toggle a button's enable/disable state with a checkbox click?

I am in the process of trying to disable an order button upon page load and enable it when the Terms and Conditions checkbox is checked. Currently, the button is disabled upon page load, but when I click on the checkbox, the button remains disabled. Here i ...

Angular - Enhance ngFor index while filtering

I am currently working with a list that utilizes an *ngFor loop in the template: <li *ngFor="let product of products | filterProducts: selectedFilter; index as productId"> <a [routerLink]="['/product', productId]"> {{produc ...

What is the best way to eliminate excess white space on the right side in WordPress for a mobile perspective?

Is there a quick way to identify which element has shifted beyond the border? It seems like there is excess margin. How can I pinpoint the issue? The link to the broken page on mobile is I applied this style * {border: 2px solid red;} and no elements shif ...

What is the best way to create a scrollable tbody in an HTML table using React?

In my current project, I am using React, Node, Express, and Postgres to fetch data from a database. The issue I'm facing involves creating a scrollable table within a div that spans the entire screen width. Initially, I had to apply display: block to ...

Placing a point on a measurement scale according to a changing factor

I am completely new to html, css, and javascript. My goal is to create a color gradient bar and then place a dot on it based on a variable. I tried to implement a solution I found from various sources, but it is not functioning as expected, and I am seekin ...

The module 'myapp' with the dependency 'chart.js' could not be loaded due to an uncaught error: [$injector:modulerr]

Just starting out with Angular.JS and looking to create a chart using chart.js I've successfully installed chart.js with npm install angular-chart.js --save .state('index.dashboard', { url: "/dashboard", templateUrl ...

Having trouble with the sleep function in nodejs?

Hey there! I'm currently working on a Node.js function that sends requests using array.map. However, I'm facing a challenge with making the function wait for 4 minutes when an error occurs before sending the next request. I've attempted to u ...

What is the best way to track the total time spent on a page over multiple visits?

I am interested in measuring the time spent by a user on a process flow over multiple sessions. While I have come across information regarding single-session tracking, figuring out how to track multiple sessions seems quite complex. For example, let' ...

What is the method to retrieve a randomly generated class from an id?

Is there a way to target elements that have the class "my_class" within the element with the id of "info_id"? It's important to note that these elements may also have another class, which I'm not interested in selecting. <div id="info_id"> ...

Changing multiple tags in JavaScript within an HTML document

Here is the JSON data I have: [ { "menu": { "MenuID":"1", "OmfID":"1", "menu_name":"Coyote Blues Louisiana Menu", "menu_description":"Full menu served all day.", "menu_note":null, "currency_symbol":"$ ...

The JavaScript function does not recognize the global variable when invoked within an onClick event

When a button is clicked in an HTML form, I want to show the user input by calling a JavaScript function. However, I am facing an issue where the function does not recognize the variable unless it is defined inside it. Here is the HTML and JS code snippet ...

The e2e Protractor test is unable to identify the Angular component within a complex Angular router with multiple layers

I am currently working on an Angular application and I need to set up end-to-end testing for this project. Angular Package: 4.6.6 Protractor: 5.3.0 In addition, my project includes a multi-layer router that wraps the router-outlet into another component ...

Debugging the Force-Directed D3 Graph

I stumbled upon a fantastic article that provided a detailed guide on creating a beautiful D3 force layout graph. However, I'm facing some difficulties with the JSON source: The "links" attribute in the author's JSON doesn't seem clear to m ...

PhantomJS fails to trigger function within page.evaluate

My current project involves scraping data from a Facebook page using the PhantomJS node module (https://github.com/sgentle/phantomjs-node). However, I am facing an issue where the function I pass to evaluate the page is not being executed. Interestingly, w ...

What would cause this to result in a blank array?

I have a main component that contains multiple sub-components, which I navigate between by clicking on different elements. These sub-components are all Vue files. What I am trying to achieve is to dynamically highlight the active component when it is bein ...