Setting the hex color of a material in Three.js using material.color.setHex with a Mesh

My code is configured to change the colors of certain objects based on different conditions. Currently, it utilizes loops like

for (j = 0; j < objects[i].children.length; j++) {
    objects[i].children[j].material.color.setHex(0x1A75FF);
}

to update the color when necessary from the render() function.

The issue arises when dealing with MeshLambertMaterial and MeshFaceMaterial objects. The provided code works flawlessly for MeshLambertMaterial, but throws an error

Cannot read property 'setHex' of undefined
when trying to execute it on a MeshFaceMaterial object.

I have been unable to find a solution to this problem so far. If there is a fix that involves converting all scene objects to MeshFaceMaterial allowing the color setting process to be unified in one function, I am willing to make that adjustment.

(What I absolutely do not want is to handle separate sets of code within the loop based on whether the object has LambertMaterial or FaceMaterial!)

Answer №1

let items = data[i].elements[j].item.items;
if ( items ) {
    for ( let x=0,y=items.length; x < y; x++ ) {
        items[x].color.setHex( 0xFF4500 );
    }
} else {
    data[i].elements[j].item.color.setHex( 0xFF4500 );
}

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 modify a large portion of JSON data in a nodejs environment?

I am currently working with two separate JSON files - data.json and dataForUpdate.json The contents of data.json are as follows: [{"name": "test", "isDefault": true, "settings": { "type": "Separation", "scanner": { "brightness": 0 ...

Implementing a JavaScript function that uses the AJAX method to confirm and update

I am currently attempting to update a database using the JavaScript confirm() function in combination with AJAX. My goal is to have "accepted_order.php" run if the user clicks "OK," and "declined_order.php" run if the user clicks "Cancel," all without caus ...

Unable to set two image layers on HTML canvas to display in different colors as anticipated

I am facing a challenge with stacking 3 images: an outline, a white base, and a pattern that is also white. My goal is to layer these images where the base is at the bottom with one color assigned to it, the pattern is stacked on top with a different color ...

Checking with `validate.js` IF statement

I created a form that includes a hidden field. When the user selects "telephone" using a radio button, an additional field is displayed. For validation purposes, I incorporated validate.js into the form. Everything was functioning correctly until I introd ...

Terminate the rethinkdb data stream

Currently delving into the world of RethinkDB and am eager to utilize the changes() method to fetch a changefeed. While I've figured out how to initiate them, the documentation doesn't quite explain how to halt a changefeed. Is it sufficient to ...

Effortless JavaScript function for retrieving the chosen value from a dropdown menu/select element

I've figured out how to retrieve the value or text of a selected item in a dropdown menu: document.getElementById('selNames').options[document.getElementById('selNames').selectedIndex].value In order to simplify this code, I&apos ...

Issue with Vue not displaying HTML content retrieved from an API

I am currently utilizing Vue in a Shopify environment and focusing on enhancing a collection page. The issue I encountered is that when clicking on a filter, it acts as an href which updates the URL and triggers a page reload. My objective is to display a ...

Is there a way to bring in an NPM module without utilizing the keywords 'require' or 'import'?

After spending hours searching for a solution, I am still struggling with importing third-party libraries into my project that does not support 'import' or 'require'. It's frustrating to hit a roadblock like this, especially when w ...

Capture a fragment of a scene and convert it into a unique texture using THREE.JS

I'm interested in creating a texture of a specific area in my scene, similar to the example shown in the official documentation for three.js framebuffer here. As I examine the code provided, there's one particular aspect that's unclear to me ...

Tips for restricting the width of Arabic paragraphs in text

I need assistance with aligning an Arabic paragraph to end in the same virtual place, similar to how it would be in English. For example: hi how are you good morning ev Something like this - any help with that? ...

URL not functioning properly on Django CMS menu

After setting up django-cms and creating a collapsible menu with categories and subcategories, I encountered an issue. When clicking on a main category, the URL appears correct but it does not navigate to the corresponding page. Main categories without chi ...

Remove the carriage return and newline characters from a Python variable

After successfully obtaining the page source DOM of an external website, I found that it contained \r\n and significant amounts of white space. import urllib.request request = urllib.request.Request('http://example.com') response = ur ...

Troubleshooting the issue of AngularJs location.path not successfully transferring parameters

My page has a Login section. Login.html <ion-view view-title="Login" name="login-view"> <ion-content class="padding"> <div class="list list-inset"> <label class="item item-input"> <input type="te ...

Discovering routes in Angular2

I'm attempting to replicate something similar to this example here: AngularJS show div based on url/condition <span id="page-locator" *ngIf="location.path() == '/overview'">test</span> Unfortunately, it's not working as ex ...

What is the best way to invoke a function and dynamically load a section of a view using AJAX?

I want to implement a feature where I can hide one div and load another div using an Ajax function. However, I am facing issues with redirecting to the function as intended. My goal is to load the div without having to refresh the page. <script type="t ...

Accessing a specific value based on a selected option in Angular 2+

Examining the Angular 2+ code snippet below, there is an Array defined in TypeScript: models = [{idModele: 1, model: "Bianca", price: 500}, {idModele: 2, model: "etc", price: 600} ] The query here is about setting up a mechanism where selecting a specifi ...

Leaving the pipeline of route-specific middleware in Express/Node.js

My implementation involves a sequence of "route specific middleware" for this particular route: var express = require('express'); var server = express(); var mw1 = function(req, resp, next) { //perform actions if (suc ...

An alert box displays N times when the submit button is clicked N times

Consider the following function: function validateForm() { var elements = ["firstname", "lastname", "password", "favfood", "favsport"]; document.getElementById('register').setAttribute('noValidate', true); document.getElement ...

When using Node.js, you may encounter the error message: "TypeError: brevo.ApiClient is not a constructor

My goal is to set up an automatic email sending system that, upon receiving details like name and email, will send a confirmation email to the provided email address with the message "subscribed." I've been working on this task for about 7 hours strai ...

Having trouble transferring data between Vue.JS components

Wondering how to pass data from the parent component (Home route) to the child component (playlist route) using props? Encountering a "TypeError: Cannot read property 'length' of undefined" error in the child component? These two components are c ...