Is there a way to displace a 2D triangle within a webgl shader?

I am facing a challenging situation while trying to implement conservative depth rendering, as described in CPU gems. My primary obstacle is enlarging the triangle, and it seems like I might have encountered the "stupid" phase.

To illustrate the current state of affairs, please refer to this link: (In this visualization, the black triangle represents the input, the red dots indicate the enlarged vertices, and the squares represent the sampling grid)

Utilizing webgl with three.js, I've included the 2 companion vertices as attributes for each vertex. My goal is to enlarge the triangles within the vertex shader. However, due to my camera setup (an orthographic view with certain constraints), directly applying the code from CPU gems designed for a perspective camera is proving to be a challenge.

My attempted solution involves generating normals for adjacent edges based on the considered vertex. Despite my efforts, achieving consistent results has been elusive. Implementing various strategies to determine the optimal direction for vertex displacement has yielded mixed outcomes, with some points clearly deviating from the intended location.

If you'd like to explore the complete code and delve deeper into the intricacies of this issue, please visit: https://github.com/nraynaud/webgcode/blob/01550668fdedba5ab61e2fbf5ed9917ca06b5e75/test_3D_conservative_rendering.html

In summary, my attempts so far have resulted in only one vertex moving correctly, while discrepancies remain for the other two. How can I successfully offset a triangle without falling victim to geometric challenges?

Answer №1

An alternative approach to calculating the normal direction without iterating through all candidate combinations involves utilizing the angle bisector of the current vertex and the sum of direction vectors p1 and p2 from the other two triangle vertices.

A visual representation reveals that the offset distance from the original position is directly related to the sine of half the angle between the two direction vectors.

float pixelSize = 10.0; // or any other value

void main() { 
    vec3 a = normalize(position - prev);
    vec3 b = normalize(position - next);

    vec3 c = normalize(a + b); 

    float halfsine = sqrt((1.0 - dot(a, b)) / 2.0);

    gl_Position = vec4(position + pixelSize * c / halfsine, 1);
}

This concept can be illustrated with a napkin diagram: where vectors A and B represent the normalized directions from the other vertices, and pixel signifies the offset size. The congruent right triangles demonstrate that both old and new vertex positions are equidistant from the two edge lines.

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

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

Incorporate a fresh label for a function utilizing AngularJS

I want to insert a new HTML tag with an event attached to it. Here is an example of what I am trying to achieve: <html ng-app="module"> <head> <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script&g ...

Retrieving the output of an asynchronous function within another function in JavaScript

I'm facing an issue with the Chrome API functions being asynchronous, making it difficult for me to get their return value. Here's a snippet of code that demonstrates my problem. I'm working with AngularJS. $scope.storageGet = function( ...

Determine if a dropdown menu includes a certain string within a designated group option

I have a specific condition in my code that states: "Add a new option if the select box does not already contain an identical option." Currently, a 'Hogwarts' option will only be added if there is no school with the same name already present. No ...

Adjust particle quantity

Hello, I am looking to update the number of particles in my project with a simple click event. Currently, I have only managed to modify the camera settings. I have read through the documentation for three.js on how to update elements, but I could use some ...

"Disabling Click Event on Sidebar Menu in Angular: A Step-by-Step Guide

I am working on an Angular project with a sidebar that I want to modify in order to disable click events on the menu items. My goal is to guide users through a specific flow without allowing them to navigate freely. However, simply disabling the [routerLin ...

Having trouble deleting a Repeatable Job from the Bull queue in Node.js

Upon attempting to utilize the removeRepeatableByKey method, I encountered an error stating that removeRepeatableByKey is not a function. Specifically, it mentioned that queue_1.taskQueue.removeRepeatableByKey is not a function. Furthermore, I am facing d ...

Leveraging the keyword 'this' within an object method in node's module.exports

My custom module: module.exports = { name: '', email: '', id: '', provider: '', logged_in: false, checkIfLoggedIn: function(req, res, next){ console.log(this); } }; I inclu ...

Unable to execute functions using an AJAX request

Currently, I am developing a React component that is bound to various actions. During this process, I found the need to call an API from within the component and handle the response accordingly. To achieve this, here is my current approach: In a successfu ...

Omit any items from an array that do not have any child elements

Upon receiving data from the server in the format of a flat tree, I proceed to transfer this data to the JsTree library for tree building. Before sending the data to JsTree, I filter out any empty elements of type "folder" that do not have children. Below ...

What should you do when the server is taking a while to respond?

I am in the process of creating a webpage that involves interactions between users, and I could use some guidance. Let's consider this hypothetical scenario: Client A visits a 'public' webpage and clicks a button. Client A then waits for a ...

Ensure the latest item is stored in the browser's local storage

I'm currently working on developing a flipbook-type application using HTML canvas in React. One issue I've encountered is that when I save an object (drawing) from the canvas to local storage, it saves not only the current project but also the pr ...

How can TypeScript and MobX work together using Set()?

I'm encountering errors while trying to implement Set() in my Grocery Shopping app using MobX with TypeScript. I have a simple setup with defined types in types.ts: types.ts export type Item = { name: string; instock: number; price: number; }; ...

Steps for integrating Stanford's NLP into a web application

I have successfully developed a project utilizing Stanford's NLP API and models. Now, I am looking to integrate this Java-based project onto the web. After seeing the demo provided by Stanford-NLP, I am curious about the process they use to achieve th ...

Highlighting JavaScript code with React Syntax Highlighter exclusively

I've been struggling to highlight Ruby code, but it seems like no matter what I do, it keeps highlighting JavaScript instead. It's frustrating because I can't seem to get it right. import React from "react"; import atom from "node_module ...

What is the optimal strategy for collecting a comprehensive array of candidates in WebRTC minus the use of ice

Currently, I am attempting to establish a video call between two computers without using ice trickle. At times, I succeed in making the video call, but other times (for unknown reasons), I am unable to gather all the ice candidates. The iceGatheringState r ...

What are some ways to streamline and improve the readability of my if/else if statement?

I've created a Rock Paper Scissors game that runs in the console. It currently uses multiple if/else if statements to compare user input against the computer's selection and determine a winner. The code is quite lengthy and repetitive, so I' ...

Using Jquery Functions within Codeigniter

I am facing an issue with calling a function containing jQuery script within an if-else statement. Below is the codeignitor view code: Code if($osoption == "windows") { ?> <script> windows(); </script> <? ...

ng-view or controller appears to be malfunctioning

I'm currently working on a small web application using AngularJS. I have an index.html file in the main directory and three other HTML pages within the html subdirectory. login.html list.html detail.html Initially, the index.html should load the ...

JavaScript: Accessing the selectedIndex of a dropdown list in RadGrid

Currently facing an issue with utilizing the RadGrid control from Telerik. I am attempting to access and manipulate the value of a GridDropDowncolumn within RadGrid using JavaScript (while in edit mode). Does anyone have any suggestions for the correct Ja ...