Unable to modify the color of a THREE.Mesh material that is present in the scene during runtime

I have already added some mesh to the scene. I am attempting to change the color of the material. The color change is done via a color palette in the menu. I am utilizing a jQuery plugin for this:

I am aware that the color palette can provide me with HSB values, but I am using the following function from the plugin:

$.colpick.hsbToHex(hsb)

It returns a string, not an integer value:

$.colpick.hsbToHex(hsb) receives an object like {h:0, s:100, b:100} and returns the corresponding HEX string (without the #).

Here is where I call a function to change the mesh color:

var buttonColorPicker = document.getElementById( 'color-palette-button' );
buttonColorPicker.onclick = function( sender ) {
    this.changeColorForActiveMesh( this.temporary.colorPicker )
}.bind( this );

This is the function that changes the material:

// Note: colorHex is a string, not an integer in this case
// I am converting the string to the RGB object
// by using bit shifting to get an actual integer representation
GC3D.Utils.prototype.changeColorForActiveMesh = function( colorHex ) {
    this.scene.children.forEach(function( item ) {
        if ( item instanceof THREE.Mesh ) {
            var rgbObject = this.convertHexToRgb( colorHex );
            var rgbInt = ( ( rgbObject.red & 0x0ff ) << 16 )
                         | ( ( rgbObject.green & 0x0ff ) << 8 )
                         | ( rgbObject.blue & 0x0ff );
            item.material.color.setHex( rgbInt );
        }
    }.bind( this ));
};

I have also prepared a function to convert from hex to RGB object:

GC3D.Utils.prototype.convertHexToRgb = function( hex ) {
    var rgbObject = /^0x?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
    return rgbObject ? {
        red: parseInt( rgbObject[1], 16 ),
        green: parseInt( rgbObject[2], 16 ),
        blue: parseInt( rgbObject[3], 16 )
    } : undefined;
};

There is a /^0x? part in Regex because I save the converted hex value from the jQuery plugin as temporary values.

Proof:

I also conducted a test here: To verify the accuracy of the final integer count in my function, and it seems to be working fine. So what could be wrong? Is there another way to change the color in THREE.JS?

UPDATE #1:

I have switched the material type from MeshLambert to MeshBasic, and now the palette is correctly changing the color! However, this is not a solution to the issue, as I am still curious why MeshLambert is not functioning well in my code... I have attempted to set different ambient/color values, but it behaves strangely...

For example:

new THREE.MeshLambertMaterial({
        color: 0xff00ff,
        ambient: 0x167aff,
        wireframe: false
});

It displays as red:

But 0xff00ff is: And 0x167aff is:

How can it be red?

Answer №1

It appears from the provided screenshot that there may be an issue with how the hex value is being received.

To resolve this, you can simply use the setHex function to set the color of the material directly.

Within the if condition block (specifically, line 741), the code would look something like this:

if ( item instanceof THREE.Mesh ) {
        item.material.ambient.setHex(colorHex);// sets the ambient color

        item.material.color.setHex(colorHex);// sets the diffused color
   }

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

What is the best way to include the ID in a Vue.js Ajax request for posts?

Is there a way to pass an "id" obtained as data in vue js? The id is coming as "agnt.basic.actor". Considering the presence of multiple ids, how can I achieve this? <tr v-for="agnt in agentlist"> <td v-if="agnt.basic">{{agnt.basic.actor}}< ...

creating a multi-page form using HTML and JavaScript

I need help creating a multi-page form with a unique tab display. The first page should not have a tab-pill, while the following pages should display tabs without including the first page tab. Users can navigate to the first page using only the previous b ...

PHP server-side detects empty AJAX post requests

I am encountering an issue while making an AJAX request to a PHP controller using jQuery ajax. The problem arises when attempting to access the posted data in PHP, as the $_POST variable is empty. Below is the function causing the trouble: function GetSer ...

toggle the outer div along with its corresponding inner div simultaneously

On one of my pages (let's call it "page1"), I have multiple divs, some nested within others. These divs are initially hidden and toggle on when a specific link is clicked (and off when clicked again). To view a nested div, the outer div must be opened ...

Able to retrieve individual elements from an array, but not able to loop through them

I am facing an issue with my code where I have two arrays - one containing URLs and the other external data. These two arrays are perfectly aligned, but when I try to access them in a loop using AJAX requests, only the first element of the URL array prints ...

looping through a linked data object with ng-repeat

I am working with a Node object in my Angular controller. Each Node contains a next property which points to the next item: $scope.Stack = function () { this.top = null; this.rear = null; this.size = 0; this.max_size = 15; ...

Retrieving cached data using $http in AngularJS

When making a request to an API using $http in AngularJS, I am receiving cached results. Below is the AngularJS code snippet: $scope.validate = function(){ var encodedUserNameAndPassword = Base64.encode($scope.username + ':' + $scope.passwo ...

Guide to swapping images based on conditions using Angular JS

I am trying to display an image based on data received from an API response instead of text. If the value is true, I want to show an access symbol. If the value is false, I want to show a deny symbol. However, when attempting this, I am only getting th ...

Retrieve information stored in a component's data variable

After creating a Vue repository using vue init webpack my-app My main.js file looks like this -- // The Vue build version to load with the import command // (runtime-only or standalone) has been set in webpack.base.conf with an alias. import Vue fro ...

What is the best way to determine if a value from my array is present within a different object?

I have an array with oid and name data that I need to compare against an object to see if the oid value exists within it. Here is the array: const values = [ { "oid": "nbfwm6zz3d3s00", "name": "" ...

"Functioning seamlessly in Chrome, yet encountering compatibility issues in Firefox - the

ERRORS ENCOUNTERED IN FIREFOX: ReferenceError: reference to undefined property G.search es6-shim.min.js:10:7752 ReferenceError: reference to undefined property G[e] es6-shim.min.js:10:1 mutating the [[Prototype]] of an object will cause your code to run v ...

Clicking on the Angular Material Table will reveal the items for display

Only when I click on the table do items display. Upon initially loading the page, the table is empty for reasons unknown to me. I retrieve data from Rest-API Cloud Blobstorage and populate the empty Array with it. Check out the code snippet below: impor ...

What is the process of permanently modifying an HTML document using JavaScript?

I'm interested in creating a basic comment section using JavaScript. Here is the structure of the form : <form> <textarea cols="50" rows="10" placeholder="Share your thoughts ..." id="theComment"></textarea><br/> < ...

"Hey, do you need a see-through background for your

Currently, I am implementing the JS library found at . Unfortunately, I am struggling to make the background transparent or any other color. It appears that the issue lies in the fact that the tab styles are being overridden by the JS library whenever the ...

Resetting an Angular Material select component

I've encountered an issue with Angular Material. Currently, I have a form with two select elements. The problem arises when I choose a value in one of the selects, it resets and loses its value. Could this be a bug? Or am I possibly making a mistake ...

Unable to scroll when utilizing ng-html-bind functionality

My device specifications: $ ionic info Your system details: Cordova CLI: 6.2.0 Gulp version: CLI version 3.9.1 Gulp local: Ionic Framework Version: 1.2.4 Ionic CLI Version: 2.0.0 Ionic App Lib Version: 2.0.0-beta.20 OS: Distributor ID: LinuxMint Desc ...

Difficulty incorporating openId selector into Asp.Net MVC 2

I have been attempting to implement OpenID login functionality on a website using the openid selector JavaScript library. I am following the guidelines provided on this particular site. However, as someone who is not typically a web programmer, I am facing ...

What is the best way to retrieve the current quality label from JWPlayer using JavaScript?

My goal is to retrieve the Current Quality Label from JWPlayer 7 using JS, but instead of getting the defined labels like 360p, 480p, 720p, I'm only receiving numbers such as 1, 2, 3... This is what I've tried: playerInstance.getCurrentQuality( ...

The Vuetify accordion template is not appearing due to a v-for loop issue in Nuxt.js

My goal is to set up an FAQ page using Nuxt.js. The template I obtained from Vuetify doesn't display correctly on my localhost. Instead, I'm encountering errors. If I replace 'v-for "(item,i) in 5" : key="i"' as per the template source ...

Tips for verifying the input field with specific requirements in Angular 6

There is an input field that needs to validate text based on certain logic conditions: No spaces should be allowed in the formula. The operators (and,or) must be lowercase and enclosed in curly brackets {}. The number of opening '(&apos ...