Setting solid color values to a mesh in Three.js without interpolation

I am interested in creating a unique surface that showcases a square meshed colormap with distinct colors assigned to each face. Currently, I've managed to develop the geometry by drawing inspiration from @LeeStemkoski but I'm facing an issue where the colors are being interpolated between faces. Is there a method to make the colors align specifically between integer values xx and xx+1, yy and yy+1? Alternatively, are there more effective ways to generate this mesh other than using THREE.ParametricGeometry

for ( var i = 0; i < graphGeometry.faces.length; i++ ) 
{
    face = graphGeometry.faces[ i ];
    numberOfSides = ( face instanceof THREE.Face3 ) ? 3 : 4;
    for( var j = 0; j < numberOfSides; j++ ) 
    {
        vertexIndex = face[ faceIndices[ j ] ];
        //face.vertexColors[ j ] = graphGeometry.colors[ vertexIndex ];
            point = graphGeometry.vertices[ vertexIndex ];
                    color = new THREE.Color( 0x0000ff );
                    xx = Math.round(point.x+nx/2);
                    yy = Math.round(point.y+ny/2);
                    ii = nx*yy+xx;

                color.setHSL( 0.7 * (max - frame[ii]) / (max-min), 1, 0.5 );
                    face.vertexColors[ j ] = color;
    }
}

Answer №1

A solution is to assign colors per vertices in a specific function, instead of faces. By calculating the average color value for each face, you can achieve the desired result. Since the faces are now all Face3 objects in threejs, you need to calculate it for 2 faces at a time. Here is the modified code:

for ( var i = 0; i < graphGeometry.faces.length; i+=2 ) 
{
    face1 = graphGeometry.faces[ i ];
    face2 = graphGeometry.faces[ i +1 ];
    numberOfSides = 3;
    var averageFaceZPosition=0;
    for( var j = 0; j < numberOfSides; j++ )averageFaceZPosition+= graphGeometry.vertices[face1[ faceIndices[ j ] ]].z;
    for( var j = 0; j < numberOfSides; j++ )averageFaceZPosition+= graphGeometry.vertices[face2[ faceIndices[ j ] ]].z;
    averageFaceZPosition/=numberOfSides*2;
    color=new THREE.Color(0x0000ff);
    color.setHSL( 0.7 * (zMax - averageFaceZPosition) / zRange, 1, 0.5 );
    for(var j=0;j<numberOfSides;j++){
        face1.vertexColors[j]=color;
        face2.vertexColors[j]=color;
    }
}

The link below shows the expected result after implementing this change:

https://i.sstatic.net/p0e7h.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

Issue with componentDidUpdate not triggering consistently on subsequent mounts in React

One of the functions in my codebase is responsible for switching windows (components) on a single page when a button is clicked. This particular function handles the window swapping logic: getProfileContent = () => { var html = []; if (this.st ...

What could be causing the jQuery .load() function to trigger twice?

While using jQuery 1.4 along with jQuery History, I noticed that Firebug/Web Inspector are displaying 2 XHR GET requests on each page load (which doubles when visiting the homepage (/ or /#). For example, if you visit this or any other page with Firebug e ...

Generate a main array containing nested arrays made up of individual values

Challenge: I am facing an issue where I have a series of numerical values separated by commas that I need to convert into an array. Each pair of values should be nested within the main array to represent drawing vertices. How can I tackle this challenge ...

Eliminate incorrect or invalid state when resetting a dropdown in an Angular ng-select component

I have integrated the ng-select plugin into my Angular project for handling dropdowns. One specific requirement I have is to reset the second dropdown when the first dropdown is changed. Below is a snippet of the code: <ng-select [items]="branchMo ...

Highlighting Search Results in Kendo TreeView

I am currently working on a KendoTreeview project with spriteclass. My goal is to highlight both the root and child nodes with a specific search term. I have successfully implemented the search functionality, however, there is an issue that arises after th ...

What is the best way to create clickable <tr> elements that function as links rather than cells?

Although it seems simple, I am struggling to achieve this task. I have a list of rows with 6 columns that display data from MySQL. I want to make each row clickable instead of just a cell, which will open a new URL in a new tab. Below is the structure I ...

HeaderView in Angular Framework

When exploring the best practices for organizing an AngularJS structure, I came across the recommendation to implement partial views as directives. Following this advice, I created a directive for my app header. In my specific header design, I included a ...

Even as I create fresh references, my JavaScript array object continues to overwrite previous objects

Coming from a background in c# and c++, I am facing some confusion with a particular situation. Within the scope of my function, I am creating a new object before pushing it into an 'array'. Strangely, when I create the new object, it appears to ...

Show two line graphs with data that commence at separate intervals

Is it possible to create a line graph using two data sets with shared labels, where one dataset is smaller than the other? For example: label1: ['8-20', '8-21', '8-22', '8-23'] data1: [100, 120, 150, 170, 150] lab ...

Guide to implementing 301 redirection from HTTP to HTTPS in Next.js

What is the process for implementing a 301 redirection from HTTP to HTTPS in Next.js? One example of this would be redirecting from "http://stackoverflow.com/" to "https://stackoverflow.com/". ...

struggling to access the value of a hidden field by using the parent class name

My coding experience so far looks like this- <tr class="chosen"> <td id="uniqueID">ABCDE5678</td> <input type="hidden" value="000005678" id="taxCode"> <td id="fullName">Z, Y</td> </tr> In this scenario, I need to ...

Is it detrimental to my search engine ranking if I utilize CSS display:none?

Imagine having valuable content that is initially hidden with CSS, and then using javascript to reveal it only when the user clicks on certain links. Even users without javascript enabled can access this content by following the links to a new page where i ...

Using map-reduce to insert embedded documents from other collections into MongoDB's vast collections

When I receive these files, each will contain at least a million rows, up to a maximum of 1.5 billion. The data is initially normalized when received, and I am looking for a way to store it all in one document. The format of the data may vary, it could be ...

"Is it possible to calculate a sum using JavaScript when data is in a while()

Here is the code snippet I am working with: <script> function select(val, item) { var prize = $(".prize"+item).val(); var prize = prize*val; $(".dollar"+item).html(prize); //----- ISSUE WITH THIS CODE --------// $sub_total=$(".dollar1").val()+$(".d ...

Struggling to trigger updates on a ReactJS component

My current challenge involves updating a section on my webpage once I have made a fetch() call and received an error message from it. The fetch() call is triggered when the form button is submitted. After the completion of the fetch(), I tried using Error ...

Using Regex to Retrieve Text Between Two Specific Characters in Javascript

Is there a way to extract specific text from a string using Javascript? For example, consider the following string: "start-extractThis-234" The numbers at the end can vary, but the hyphens are consistent. I believe using regex to capture the text betwe ...

In order to ensure a valid JSON for parsing in JavaScript, one must reverse the usage of single quotes and double quotes. This adjustment

Received an API response structured like this: [{'name': 'men', 'slug': 'men'}, {'name': 'women', 'slug': 'women'}] After stringifying: const data = JSON.stringify(resp) " ...

Upcoming challenge regarding naming files

I came across a new problem: Error: ENOENT: file or directory not found, rename '/home/user/my-web/.next/export/en/cities/berlin.html' -> '/home/user/my-web/.next/server/pages/en/cities/berlin.html' What could be causing this issu ...

Upon shutting down the Alert Window, the submit button remains unresponsive

I am facing an issue where, upon detecting an identical value during submission and triggering an error, the button becomes unclickable and feels like it is stuck on saving. Can anyone provide assistance with this situation? Here is the jQuery code I am u ...

What is the process for updating npm on a Windows operating system?

Seeking advice on upgrading my npm version, I attempted Option 3 for Windows as outlined in the npm documentation. However, upon installation, I received a message stating that npm.exe already exists in the nodejs folder. Despite trying to overwrite it u ...