Creating both Uniform and Varying drawings on a single webGL canvas

My goal is to create this specific illustration.

https://i.sstatic.net/5AfdW.png

This project requires the usage of TypeScript.

The Code:

The code is organized across multiple files. Within the scenegraph file, there's a function that visits a group node and executes a Depth-First Search (DFS) method as shown below:

visitGroupNode(node: GroupNode, lvl: number = 0) {
    // The DFS logic goes here...
}

The crucial part involves the visitBoxNode and visitSphereNode functions.

For rendering a sphere, the visitSphereNode function calls a render function defined in the Sphere.ts file like so:

render(shader: Shader) {
    // Rendering process for spheres...
}

Full code available on pastebin: HERE

Shader file link on pastebin: HERE

The shader file includes both vertex and fragment shaders, with the vertex shader structured as:

// Vertex shader logic...
void main() {
    // Transformation calculations...
}

And the fragment shader designed as:

// Fragment shader logic...
void main(void) {
    // Color computations...
}

Similar code for boxes can be found on pastebin: HERE.

Issue: The colors are displaying inconsistently.

https://i.sstatic.net/JyMZK.png

Question:

It's evident that the sphere should utilize a Uniform color while the box should have a Varying color. How can I achieve their simultaneous display on the canvas using the provided GLSL shader file(s)?

Answer №1

By default, the attributes and uniforms in this scenario have specific values assigned: 0 for the RGB channels and 1 for the alpha channel. Even when a vertex attribute is not defined or turned off, these defaults apply. It's possible to explicitly set the uniform variable to 0 if needed. You can blend colors from both vertex attributes and a uniform variable by combining the RGB channels and multiplying the alpha channels. If the mesh is uniformly colored, you can disable the color attribute using 'disableVertexAttribArray'. But if there is a color attribute present, set the uniform variable to (0, 0, 0, 1).

precision mediump float;

varying lowp vec4 v_color;
uniform vec4 u_color;

void main(void) {
    gl_FragColor = vec4(v_color.rgb + u_color.rgb, v_color.a * u_color.a);
}

Alternatively, another approach is to mix the colors based on the alpha channel of the uniform color:

precision mediump float;

varying lowp vec4 v_color;
uniform vec4 u_color;

void main(void) {
    gl_FragColor = vec4(mix(v_color.rgb, u_color.rgb, u_color.a), 1.0);
}

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

summing 3 numbers to a total of 100 percent

I am currently trying to calculate the percentages of different statuses based on 3 count values. Let's assume I have 3 statuses: 1) Passed 2) Failed 3) Skipped When dealing with only two cases, I was able to use a combination of the Floor and Ceil ...

Having trouble running `npm start` on my NodeJs project

Hi everyone, I could really use some help with the npm start command. I am currently working on a Node.js project with TypeScript on Windows 7-64, but I'm encountering errors when trying to start it. If you can assist, please check out the following ...

What is the proper way to indicate that a function parameter corresponds to one of an Interface's keys?

When working with TypeScript, I am looking for a way to validate that the argument passed to myFunction matches one of the keys defined in MyInterface. Essentially, I want to enforce type checking on the arg parameter as shown below. export interface MyInt ...

New substitute for extending the extent in OpenLayers 4

I am currently in the process of migrating my code from OpenLayers 3 to OpenLayers 4 using TypeScript. Previously, I had a code snippet that extended the extent of my map so that all vector shapes I drew would be visible upon loading... angular.forEach(w ...

The issue of the Angular service being consistently undefined arises when it is invoked within an

I have already researched numerous other SO questions, but none of the solutions worked for me. My goal is to implement an async validator that checks if a entered username already exists. However, every time I type a letter into the input field, I encoun ...

Utilize Ngrx to keep an eye on specific items within the store

If we consider an interface called INotification: export interface INotification { id: number; DateReceived: number; Title: string; Message: string; Tipology: string; isRead: number; } and a reducer system. In the component, it&ap ...

Nested forwardRef in React is a powerful feature that allows

Within my React application, specifically utilizing typescript, I have implemented a form using react-hook-form to handle all the necessary logic. Afterwards, I proceeded to customize the select element with various CSS and additional features. To simplif ...

Is it possible to retrieve the signature for every method within a class?

Let's consider a scenario where we have a class defined as follows: class Actions { static FooAction = 'foo' as const; someAction1() { return { type: Actions.FooAction, payload: {a: 1, b:2} }} static BarAction = &apos ...

Customizing the placeholder text for each mat input within a formArray

I have a specific scenario in my mat-table where I need to display three rows with different placeholder text in each row's column. For example, test1, test2, and test3. What would be the most efficient way to achieve this? Code Example: <div form ...

Is it possible to swap out the Firestore module `doc` with the `document` module

I enjoy using the Firebase version 9 modules, however, I find that doc is not to my liking. It would be better if it were document, similar to how collection is not shortened to col. The following code does not function as expected: import { doc, collecti ...

Does combineLatest detach this from an angular service function?

Check out this test service on Stackblitz! It utilizes the combineLatest method inside the constructor to invoke a service method: constructor() { console.log("TEST SERVICE CONSTRUCTED") this.setParameters.bind(this) this.assignFixedParamete ...

Express displays html instead of json when error handling occurs

I recently followed a tutorial on Express.js to create a simple error handler. function clientErrorHandler(err, req, res, next) { if (req.xhr) { console.log('clienterrorhandler', err); res.status(500).send({ error: 'Something faile ...

Problem encountered while implementing callbacks in redux-saga

I am facing a scenario in which I have a function called onGetCameras that takes a callback function named getCamerasSuccess. The idea is to invoke the external function onGetCameras, which makes an AJAX call and then calls getCamerasSuccess upon completio ...

How can I test for equality with an array item using v-if in Vue.js?

Currently, I am facing a challenge in my Vue.js project where I need to determine if a number is equal to an element within an array. Here is the code snippet that I am working with: <div v-if="someValue != arrayElement"> // </div> I am st ...

Issue: the module '@raruto/leaflet-elevation' does not include the expected export 'control' as imported under the alias 'L' . This results in an error message indicating the absence of exports within the module

Looking for guidance on adding a custom Leaflet package to my Angular application called "leaflet-elevation". The package can be found at: https://github.com/Raruto/leaflet-elevation I have attempted to integrate it by running the command: npm i @raruto/ ...

Developing Angular PWAs with a focus on microfrontends

I have set up multiple microfrontends using an "app-shell" type of application for the domain root, with each microfrontend on the first path element. Each app is constructed as a standalone angular application utilizing shared libraries to reuse common co ...

Is there a way to update the parent state from a child component in React when using Switch Route?

I have a page that features a control panel for modifying the content on display through Switch-Route. The code structure is as follows: <div className="controls"> some controls here </div> <Switch> <Route exact path=&apo ...

Is it possible that I am making a mistake when using the multi-mixin helper, which is causing an unexpected compiler error that I cannot

As I work on creating a multi-mixin helper function that takes in a map of constructors and returns an extended map with new interfaces, I come across some challenges. Let's first look at the basic base classes: class Alpha { alpha: string = &ap ...

I am experiencing an issue with applying responsiveFontSize() to the new variants in Material UI Typography

I am looking to enhance the subtitles in MUI Typography by adding new variants using Typescript, as outlined in the documentation here. I have defined these new variants in a file named global.d.ts, alongside other customizations: // global.d.ts import * a ...

Enhancing Angular Material forms with custom background colors

I'm new to Angular and Angular material, still learning the ropes. I have been trying to create a form and needed to change the background color to Red. However, when I attempted to do so, the red color ended up covering the entire form instead of ju ...