Refresh outfit designs or retrieve information from fragment shader post-processing

While working on image processing in the fragment shader (specifically thresholding), I find myself wanting to access the processed result from JavaScript so that I can save the modified image using standard JavaScript methods.

I send the original texture to be segmented to the fragment shader as a uniform, where the thresholding happens. The result renders correctly on screen. But now my goal is to extract this thresholded image for saving purposes.

Considering the fact that the fragment shader runs over pixels rendered on the screen, I'm not even sure if it makes sense to try and access the processed image.

Despite the constraints, I am curious if there is an effective technique to execute complex image processing tasks within the fragment shaders and then somehow retrieve the resulting data for further use.

Another scenario I have in mind involves flood filling based on the mouse cursor position, among other things.

JavaScript's speed is insufficient for my needs, and while web workers may offer some improvement, I was hoping to capitalize on GPU acceleration for my image processing requirements.

(Just for context: I am currently using THREEJS along with custom shaders - though this might not be relevant information)

Warm regards, Nicolas

Answer №1

If I have grasped your issue correctly, there are a couple of ways to tackle it. Imagine we have a canvas at our disposal. We can display the processed image on this canvas and proceed to save it either as a JPEG or PNG blob:

canvas.toBlob(
    (blob) => {
        /* Handle the blob, such as sending it to a server */
    },
    'image/jpeg',
    0.95
);

Alternatively, you can save it as a data URL:

const imageAsDataUrl = canvas.toDataURL('image/png');

In case you need access to the pixel values of the image, you can retrieve them using a WebGL call (assuming that the gl context is available):

const pixels = new Uint8Array(4 * canvas.width * canvas.height);
gl.readPixels(0, 0, canvas.width, canvas.height, gl.RGBA, gl.UNSIGNED_BYTE, pixels);

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

Oops! An issue occurred on the server: ReferenceError - 'window' is not defined. This error occurred during

I'm looking to integrate a Microsoft Login Button into a Next Js application using Material UI. However, after adding the necessary package from NPM and refreshing the page, I encountered the error mentioned above. When I tried setting ClientId="a973 ...

Creating an Android application with a masking feature

Currently, I am working on a project where I want users to be able to mask out specific parts of an image. My approach involves painting over the base image using canvas, but unfortunately, I have not been successful yet (you can see my other questions on ...

Changing the size of the logo as you scroll through the page

I have implemented the following code to resize the logo as I scroll down the page. $(document).on('scroll', function() { if ($(document).scrollTop() >= 10) { $('.logo img').css('width', '50px'); } else ...

Having trouble with Vue and Vuex not returning the correct value in HTML?

I've implemented a navbar that displays specific menu items based on the user's role. Below is the simplified HTML code for the navbar: <v-speed-dial class="speed-dial-container contextual-text-menu" v-if="user && user. ...

What's the best way to implement image size and type validation, specifically for .jpg and .png files, using Multer?

When using multer to receive files from the FrontEnd, I need to validate the image size to ensure it's less than 1MB. Additionally, I want to restrict the accepted file types to .jpg, .jpeg, and .png only. const multer = require("multer"); c ...

Whenever I attempt to include state in a React class that is declared as a constant, I consistently encounter the error message: "TypeError:

Apologies for the redundancy, but as a newcomer to React, I previously inquired about a similar issue and have since modified my code. My current challenge involves trying to access a state value within a const React class. Below is the excerpt from my Ar ...

The button fails to appear when navigating

Hello everyone, I have a code snippet for a functional back button in my Angular application, but I'm facing an issue with adding it to an existing constructor. I already have a constructor for navigating between pages, but it seems the back button co ...

What is the proper way to reference a property's value within another property of the same JavaScript Object?

Within a Gulp.js file (or any Javascript file), I have defined paths in a Javascript object: var paths = { devDir : 'builds/development/', devDirGlobs : this.devDir+'*.html' } I am attempting to reference the pro ...

Easily transforming data between JSON and CSV formats in JavaScript using user-defined headers

Can someone provide guidance on creating import/export functionality for CSV format without using external libraries? It would be a huge help! I have a CSV string labeled as "A": "First Name,Last Name,Phone Number\r\nJohn,Doe,0102030405&bso ...

Unable to locate the Chart object within the chartjs-plugin-labels.js file

Hello there, I am currently working on an Angular project where I want to incorporate a chart plugin. To achieve this, I executed the following commands: npm install angular2-chartjs npm install chartjs-plugin-labels Following that, I imported it into my ...

Disable the button until all input fields contain text in ASP

Curious if anyone knows how to disable a button until all text boxes have input in ASP.NET and C#. Here is an image showing the scenario I'm referring to - wanting to gray out the commit button. Thanks, Chris! ...

Is using multiple meshes with the same material in Three.js beneficial for performance?

Being new to three.js, I have a question regarding combining multiple mesh materials into one mesh. If the mesh is made with the same material, will it result in additional detail? Essentially, does adding extra code like mesh = new THREE.Mesh( geometry, ...

Error message: The module '@project-serum/anchor' does not export the object 'AnchorProvider' as intended

I encountered an issue while attempting to run my react application. The issue: Attempted import error: 'AnchorProvider' is not exported from '@project-serum/anchor'. The import declaration in my code: import idl from './idl.json ...

Activate Jquery to display the submenu when clicked and conceal any other open submenus at the same time

I'm trying to create a responsive menu with menus and submenus using JQuery. However, as a newbie to JQuery, I'm struggling to hide a previous submenu when displaying another one. Here's the CodePen link Below is the HTML code: <nav cl ...

Angular - Issue with setting default value in a reusable FormGroup select component

My Angular reusable select component allows for the input of formControlName. This input is then used to render the select component, and the options are passed as child components and rendered inside <ng-content>. select.component.ts import {Compon ...

Guide on using Ajax to transmit data to Struts2 action class

In my Struts2 web application, I have the following files: member.jsp: <script type="text/javascript"> String str1 = "aaa"; String str2 = "bbb"; xmlhttp.open("GET", "http://localhost:8080/project/editprofile.action", true); xml ...

Tell webpack to exclude a specific import

Currently, I am in the process of developing a desktop application using ElectronJS and ReactJS. To bundle the renderer process that utilizes JSX, I have opted to use webpack. An issue arises when attempting to import anything from electron into the rend ...

What is the best way to retrieve JSON data using JavaScript within a loop?

I'm struggling to retrieve data from a JSON object. Here's the JSON structure I'm working with: var data = [ {"mes":{ "January":{ "Inversion":"1000","Fans":"1020"} ...

Resolving a Tricky Challenge with jQuery's

I am facing an issue with a function that animates images through crossfading. This function is responsible for rotating banners on a website. By calling the function as animateSlideshow("play"), it starts animating and sets up a timeout. On the other hand ...

What method would you recommend for modifying HTML text that has already been loaded using JSP?

Is there a way to update text on an HTML document without reloading the entire page? I'm looking to create a simple "cart" functionality with 5 links on a page. When a link is clicked, I want it to increment the "items in cart" counter displayed on th ...