Extract data from THREE.js computed textures using GPUComputationRenderer

Experimenting with the GPUComputationRenderer on a customized version of this three.js example, I have been working on adjusting boid interactions using GPU shaders to handle, retrieve, and manipulate boid position and velocity data.

I have reached a point where I can input GPU-computed data (such as predicted collision times) into the texture buffer using the shader. Now, my goal is to access some of that texture data within the main JavaScript animation script in order to identify the earliest collision.

Below is the relevant code snippet from the render function (which gets called during each animation loop):

//... Original THREE.js example GPU calculations
gpuCompute.compute();   //... gpuCompute represents the GPU computation renderer.
birdUniforms.texturePosition.value = gpuCompute.getCurrentRenderTarget( positionVariable ).texture;
birdUniforms.textureVelocity.value = gpuCompute.getCurrentRenderTarget( velocityVariable ).texture;

var xTexture = birdUniforms.texturePosition.value;  //... My custom variable.

//... Source: http://zhangwenli.com/blog/2015/06/20/read-from-shader-texture-with-threejs/
var pixelBuffer = new Uint8Array( WIDTH * WIDTH * 4);   //... Fine for me.

//... Reference: http://stackoverflow.com/questions/13475209/three-js-get-data-from-three-webglrendertarget
gpuCompute.readRenderTargetPixels( xTexture, 0, 0, WIDTH, WIDTH, pixelBuffer ); //... Issue here!

In the provided code, I was hoping for the gpuCompute renderer object to offer functions like .getContext() or readRenderTargetPixels(), but unfortunately, they are not available for gpuCompute.


EDIT:

My troubleshooting involved including the following code snippet:

//... Incorporating WebGLRenderer code from THREE.js build
myWebglRenderer = new THREE.WebGLRenderer();
var myRenderTarget = gpuCompute.getCurrentRenderTarget(positionVariable);
myWebglRenderer.readRenderTargetPixels(
    myRenderTarget, 0, 0, WIDTH, WIDTH, pixelBuffer);

This runs smoothly, however, the pixelBuffer still contains all zeros instead of the intended position coordinate values.


Could someone kindly suggest a method for reading the texture data into a pixel buffer? Ideally, utilizing THREE.js/plain JavaScript since I lack familiarity with WebGL technology.

Answer №1

Take note: This response may be outdated. View the referenced link for updated information

In the realm of WebGL 1.0, extracting pixels from floating point textures utilized by GPUComputationRenderer is not a straightforward task.

To retrieve the data, a workaround involves rendering the GPUComputationRenderer's floating point texture into an 8bit RGBA texture with a conversion process from 32bit floats to 8bit textures. Subsequently, one can access this converted data in JavaScript for further analysis.

For more details, refer to WebGL Read pixels from floating point render target

Answer №2

Apologies for the delay in response. It has been a while since I last logged into Stack Overflow.

In the scenario involving water and tennis balls, you can check out this example at:

The height of the water surrounding the balls is extracted from the GPU.

A 4-component texture is used to convert into a 1-component float texture.

This texture consists of 4x1 pixels where the first pixel represents the height and the next two depict the normal of the water surface (the fourth pixel remains unused).

For each individual tennis ball, this texture is computed and retrieved, following which the ball physics are simulated on the CPU.

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

A Step-by-Step Guide on Importing Components in Vue.js

I have defined the following component as shown below: import Vue from 'vue'; import Datepicker from 'vuejs-datepicker'; Vue.component('DatePicker', { template: ` <datepicker name="date"></datepicker> ` ...

What is the sequence of the middlewares for error handling and handling of 404 errors?

The express.js website has confused me with contradictory statements regarding error-handling middleware. According to one page, you should define error-handling middleware last, after other app.use() and routes calls. However, another page states that you ...

Adjust the image size without losing sharpness

I'm currently working on a web application for Minecraft and I am looking for a way to resize someone's skin without losing quality. I believe javascript might be the solution to this issue. ...

Creating a stylish navigation bar with custom components using Material UI and React

I've been experimenting with the BottomNavigation component from Material UI, but instead of using labels and text, I want to incorporate custom-made ImageButton components. Here's the code snippet from Material UI: import React from 'rea ...

Having trouble accessing a PDF file using gview

Having trouble opening a document I am unable to preview the documents and I'm not sure what I'm missing. I have read through various StackOverflow questions and answers related to this issue, but still no luck. url = http://192.168.0.6:8077/ ...

Is it possible to utilize the WebGL camera in order to create dynamic transitions between various polygons?

Recently, a friend suggested exploring WebGL as an alternative to CSS transitions. I have a collection of polygons that form a 2D board game. https://i.sstatic.net/D0dnc.png In essence, the application moves the player space by space starting at the top ...

Integrate the dateFilter function into a service within an AngularJS application

Is there a way to include filters in AngularJs services? I've been experimenting app.factory('educationService', [function($rootScope, $filter) { // ..... Some code // What I want console.log(dateFilter(new Date(), 'yyyy ...

Error message: When attempting to create dynamic inputs in React, an error occurs stating that instance.render is not

I encountered an issue while attempting to create dynamic inputs in React. The error message 'TypeError: instance.render is not a function' keeps popping up. import React, { Component } from 'react'; import Input from '../../Ui/Inp ...

Error: the function is not defined, therefore the variable is being passed

I'm attempting to pass a variable in the URL in order to automatically check a radio button on a different page depending on which link is clicked. However, I keep encountering an "Uncaught ReferenceError: radio_onload is not defined" error. Could th ...

Display the Angular UI grid containing data from the textboxes once the information has been submitted

I recently started working on an angularjs application and I am facing some challenges in finding the solution. Any advice or suggestions would be greatly appreciated. My goal is to dynamically display an angular UI-grid based on the text selected in the ...

The addition of input fields on keyup creates problems in the initial field of each row

I am currently working with a table and attempting to calculate the sums as follows: td(1) + td(2) + td(3) = td(4), td(5) + td(6) + td(7) = td(8), td(9) + td(10) + td(11) = td(12). This is the code I have implemented: $(document).ready(function () { ...

Disable the setTimeout() function in order to prevent the countdown from refreshing

I have a JavaScript countdown function that is working well, but I am unsure how to stop and refresh the timer to extend the time. When I call the function again before it times out, it behaves strangely by showing two countdown timers because the updateTi ...

Setting the Node environment as an environment variable within the application: a step-by-step guide

Struggling with setting process.env.NODE_ENV to TEST in my code. Check out the relevant snippets below: test.js import server from "../../src/index.js"; process.env.NODE_ENV = "test"; console.log(process.env.NODE_ENV); // outputs &qu ...

What is the proper way to address the issue of nesting ternary expressions when it comes to conditionally rendering components?

When using the code below, eslint detects errors: {authModal.mode === 'login' ? <Login /> : authModal.mode === 'register' ? <SignUp /> : <ForgotPassword />} Error: Avoid nesting ternary expressions. eslint(no-nested-t ...

Currently, my goal is to create PDFs using Angular

<button class="print" (click)="generatePDF()">Generate PDF</button> Code to Generate PDF generatePDF(): void { const element = document.getElementById('mainPrint') as HTMLElement; const imgWidth = 210; ...

Arrow function utilized in string rendered component

Hello everyone, I could use some help with the following code snippet: rowRenderer = (e) => { debugger; return e.data?.fileName ? '<a class="documentLink" onclick={() => \'' + console.log('\ ...

Can CSS be used to create drop down menus?

Is it possible to create a dropdown menu using only CSS, similar to what you would find in most applications? I have a simple menu bar at the top of my screen and I want to be able to hover over an item like "Item1" and see sub-items appear below it. I en ...

Ways to keep selected values in the Select box without unchecking them again?

Hello there, I am relatively new to ReactJS and currently facing an issue with Selects. Specifically, I have a Select component that displays a list of names using MenuItem. My goal is to have certain names pre-selected in the Select dropdown upon initial ...

Utilizing JavaScript event handlers on dynamically appended elements after the document has finished loading

One issue I am facing is with a javasript function that needs to change the value of an element appended after the document has loaded. The problem arises when trying to intercept actions on a newly appended DIV, such as: <div class="new-div"></d ...

Enhance hover effects with JQuery through dynamic mouse movements

$(document).ready(function() { $(".hoverimage").hover( function(e) { updateCoords(this,e); openQuicktip(this); }, function() { closeQuicktip(); } ); $("area").hover( function(e) { updateCoords(this,e); openQuicktip(this); }, function ...