The color of each vertex reflects its elevation

I am currently developing an innovative audio analyzer, which can be found at this link:

The analyzer is built using the technology of Three.js.

Here is a snippet of the code:

// Here goes your JavaScript code
// It's quite lengthy, but it involves setting up the scene qualities, handling audio data, creating 3D objects, and much more!

However, I would like to enhance the visual appeal by changing the color of vertices based on their Z-axis height.

I envision assigning colors from an array to vertices based on their positions. Something like this:

//Pseudo-code :
var colors = [0x000000, 0x888888, 0xFFFFFF]
for i = 0 to vertices.length :
  if (vertices[i].position.z <= 66) then vertices[i].color = colors[0]
  if (vertices[i].position.z > 66 && vertices[i].position.z < 133) then vertices[i].color = colors[1]
  if (vertices[i].position.z >= 133) then vertices[i].color = colors[2]

Could you assist me in implementing this feature?

PS: The HTML code provided includes placeholders for shaders that could potentially enhance the material appearance.

Answer №1

I successfully managed to get those shaders up and running:

// Vertex shader
varying vec3 col;

void main() {
    col = position;
    vec4 offset = vec4(col.x, col.y, col.z, 1.0);
    gl_Position = projectionMatrix * modelViewMatrix * offset;
}
// Fragment shader
varying vec3 col;
lowp float g;

void main(void) {
    g = float(col.z > 5.0);
    gl_FragColor = vec4(0.6*g, 0.5*(1.0-g) + 0.6*g, 0.6*g, 1.0);
}

The color output is a mix of green and gray, although the transition isn't very smooth:

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

Utilizing Bootstrap to create a row of four columns

I am utilizing Bootstrap 4.5.1 to showcase 4 columns, arranged in a row. Here is how my 4 columns are currently displayed: View I would like these 4 columns to be aligned in a single row without wrapping. Below is the code snippet for achieving this: ...

The function elem.colResizable does not exist

I am attempting to create resizable table columns in AngularJS using the following directive: myApp.directive('colResizeable', function() { return { restrict: 'A', link: function(scope, elem) { setTimeout(function() { ...

Display or conceal a field depending on the user's input

I need to display a checkbox only when the firstname matches abc or if the email is [email protected]. var x = abc; //will be dynamic var y = abc @gmail.com jQuery("#firstname").on('change', (function(avalue) { return function(e) { ...

Dividing JSON File Entries Among Several Files

I am facing a challenge with a file that contains an overwhelming amount of data objects in JSON format. The structure is as follows: { "type": "FeatureCollection", "features": [ { "type": "Feature", "properties": {}, "geometry": ...

Tips for avoiding template errors when retrieving data using Nuxt's fetch() method

After transitioning my vue app to a nuxt app, I encountered an issue with vue-slick-corousel. In the original vue app, everything worked smoothly with vue-slick-carousel, but after making the necessary modifications for nuxt, the carousel wouldn't dis ...

Retrieving the latest file uploaded in a data table

Below is the HTML code snippet provided: <table> <tbody><tr> <td> <label for="DocumentsName">Title</label> </td> <td> <input na ...

Utilize Bootstrap v4.6 to control the visibility of navigation pills based on specific times of the day using JavaScript

My webpage contains two nav-pills that I want to hide during certain hours of the day and then make visible again later on. However, there is a third nav-pill that should remain unaffected and be visible at all times. function showNavPill() { let now = ne ...

Issue with the styling of the fourth level menu option persists

I'm stuck trying to style the fourth level and can't figure out why. Would really appreciate some help untangling this CSS mess! Check out the Fiddler: Menu Demo ...

There is an irritating 5px gap between the divs in the Avada WordPress theme. While using margin-top: -5px helps to reduce this spacing, padding-top: -5

Struggling to remove a persistent 5px whitespace on my website eternalminerals.com See the issue highlighted in this screenshot: Trying to eliminate the whitespace by adjusting margins in Google Chrome, but unable to apply styles to Avada shortcodes: &l ...

Challenges experienced during the process of uploading a website to the server

I seem to have encountered an issue where the Navigation background image is missing after uploading my website onto the server. Surprisingly, everything else seems to be working perfectly. What could possibly be the cause of this discrepancy? navbar-de ...

What purpose does this JavaScript function serve by executing a for loop with no clear purpose?

Trying to solve this problem has been quite frustrating for me. I have a simple function nested within another one, and everything seems fine when I use console.log. However, as soon as I introduce the Firebase functionality, the code starts repeating itse ...

Learning how to implement positional audio in Three.js using Tone.js

While I have had success using Tone.js to create tones within a Three.js environment by using oscillator = new Tone.Oscillator(440, "sine").toMaster();, I am struggling to figure out how to link that tone to an AudioListener in Three.js to achieve position ...

Utilizing the $divide feature in MongoDB aggregation

As a newcomer to javascript, I am seeking assistance with calculating the average number of sales (averageSales = totalSales/ numOrders). I am encountering some challenges in this process and would highly appreciate any help or guidance provided. ...

JavaScript: Efficient ways to extract necessary elements from an array

Currently, I am in the process of creating a tokenizer that constructs an abstract tree. My specific goal is to gather all the "text" elements from an array produced by this tokenizer. The output looks like this: { "error": false, "tokens": [ { ...

Regular expression used to split a unary operator

Although there are many regex questions related to my issue, none of them address my specific problem as I do not have parentheses in my expressions. Let's take a look at an example: 1*2+3-99/50 I am trying to tokenize this expression in order to co ...

The dynamic duo: Selenium and HTML!

For the past few days, I've been using selenium to extract information from a private database of company data. Unfortunately, due to the password protection of the database, I can't share the entire python code or HTML code. Here is the specifi ...

The default setting for the checkbox is that it is pre-selected

I am diving into the world of Vue.js Currently, I have a set of checkboxes: <input type="checkbox" value="users" class="pull-right" v-model="user.permissions"> <input type="checkbox" value="edit user" v-model="user.permissions"> <input typ ...

Where do JQuery and framesets vanish to?

Whenever I attempt to use the console to create an element with the tag frameset, it returns no result: $('<div id="content" data-something="hello" />') => [<div id=​"content" data-something=​"hello">​</div>​] $(&apo ...

How can recursion be implemented when the items are interdependent?

I am looking to create a function that can generate a list of individuals upon whom a specific person relies. The complexity here lies in the fact that these individuals may, in turn, rely on the original person. To illustrate: const people = { ' ...

Leveraging PrismJS within an Angular application

I am currently implementing prismjs in my Angular app Here's what I have so far app.directive('nagPrism', [function() { return { restrict: 'A', transclude: true, scope: { source: '=&a ...