Having Trouble Assigning Three JS Material to Mesh Object

This is the code snippet for creating a glass material:

const glassMaterial = new THREE.MeshPhysicalMaterial( {
    // color: 0xffffff, metalness: 0.25, roughness: 0, transmission: 1.0
    color: 0xffffff,
    metalness: 0.25,
    roughness: 0,
    transmission: 1.0,
    transparent: true, // Enable transparency
    opacity: 0.8,
});

Here is the code to assign the glass material to an object in a 3D model:

loader.load('model/bentley.glb', (gltf) => {
    const carModel = gltf.scene.children[ 0 ]; 
    carModel.getObjectByName( 'glassclear_headlights' ).material = glassMaterial; 
    scene.add(carModel);
})

However, the issue faced is that the object which has been assigned the glass material appears unchanged and flat. The logged object data confirms the update but it doesn't reflect in the canvas.

Below is the detailed object data:

Object { isObject3D: true, uuid: "eb2f4654-2800-408f-b829-7e49494fd212", name: "glassclear_headlights", type: "Object3D", ... }

For the complete code which includes the 3D scene setup, loading models, and controlling the camera, refer to the code below.

import * as THREE from 'three';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { RGBELoader } from 'three/addons/loaders/RGBELoader.js';
import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';

// Code for setting up the 3D scene, loading models, and camera controls
// ...

If anyone can provide assistance or guidance regarding this issue, it would be greatly appreciated.

Answer №1

The object in three.js identified as "glassclear_headlights" is not a Mesh. It is more likely a parent element that contains one or more meshes. You can access and modify all the elements under this node by traversing through them, as demonstrated below:

loader.load('model/bentley.glb', (gltf) => {
    const carModel = gltf.scene;
    const headlights = carModel.getObjectByName( 'glassclear_headlights' );

    headlights.traverse((object) => {
      if (object.isMesh) {
        object.material = ...;
      }
    });

    scene.add(carModel);
})

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

Explore the possibilities of using a unique custom theme with next.js, less, and ant design

Trying to customize the default theme in antdesign has been a challenge for me. I've switched from sass to less, but there seems to be something that just won't work. I've exhaustively searched online for solutions - from official nextjs ex ...

Customize the popover in React Material-UI to your liking

I am currently utilizing a Select component within my application. https://i.stack.imgur.com/hjXlY.png I have created a custom modal component that I wish to display instead of the default list items when the select is clicked. Is there a method to overr ...

Is there a workaround for the issue of the NodeJS Web Cryptography API require() being undefined in an unsecure origin (Heroku App)?

My goal is to implement the experimental Web cryptography API (SubtleCrypto) on my Node.js server hosted on Herokuapp. The aim is to encrypt data from a fetch request sent from gitpages to herokuapp, concealing sensitive information from the browser consol ...

Correcting the invalid syntax due to EOF issue

How can we resolve the end of file error? The brackets appear to be valid based on ecma standards, but it's not clear what is missing. After using jsonlint, this error was found: *Error: Parse error on line 16: ...States" }] }]}{ "i ...

Developing a calendar with limited availability for selecting future dates and restricting the ability to choose past dates

I am currently working on creating a calendar in Vue.js that allows users to select dates only from the current day up to the next 30 days. Dates prior to the current date are disabled, as well as dates beyond the initial 30-day period. While I have exper ...

The reason behind the successful execution of the final .then in promise chaining

I am new to JavaScript and have encountered an interesting problem with the following code. Even though I haven't returned anything from the .then method, the last .then (blue color) works instead of the first one (red color). Can someone explain why ...

Disabling GPS with HTML5/phonegap: A step-by-step guide

Looking to create a function that can toggle GPS on and off for both iPhone and Android devices ...

Allow the json array to be transformed into a format suitable for input

Hey there, I've received a JSON array as the response of my ajax request: {"chart":{"2016-03-08":"1","2016-03-07":"4","2016-03-06":0,"2016-03-05" :0,"2016-03-04":0,"2016-03-03":"145","2016-03-02":0}} Now, I'm looking to create a chart using the ...

Utilizing the .map() function to retrieve an object from an array without a key

Exploring Dialogflow, I aim to retrieve the speech value for the object in the messages array that lacks a platform key: "messages": [ { "type": 0, "platform": "skype", "speech": "FOO" }, { "type": 0, "platform": ...

What is the best way to enable editing of a form when the edit button is clicked?

I have created a simple profile page where users can edit their profile information. I have placed a button at the end of the page. Initially, the form should be uneditable, but when the button is clicked, the form becomes editable. I attempted to use `dis ...

What is the most effective method for transferring data to a concealed input field?

I currently have a webpage with dual forms - one on the main page and another within a Bootstrap modal. The primary form includes fields like "Neck, Chest, Waist," while the modal's form only has an email field. To streamline the submission process, ...

Changes in tabs are discarded when switching between them within Material UI Tabs

I have been experiencing an issue with the Material UI tab component where changes made in tabs are discarded when switching between them. It seems that after switching, the tabs are rendered again from scratch. For example, let's say I have a textFie ...

The maximum number of $digest() iterations (10) has been exceeded in $rootScope. Exiting the process

Using UI-Router, I need to check the existing token every time a state change is initiated. The issue only occurs on the initial page load, and disappears upon refreshing the page. Below is the code snippet that I suspect might be causing the error: .run ...

Is it possible to eliminate the arrows from an input type while restricting the change to a specific component?

Is there a way to remove the arrows from my input field while still applying it only to the text fields within this component? <v-text-field class="inputPrice" type="number" v-model="$data._value" @change="send ...

How does Python interpret arrays from JavaScript?

As part of my API setup, I am sending parameters to a python script. One of these parameters happens to be a JavaScript array. Interestingly, when I check the array in Python, it only shows the first index. Here is the snippet of my Angular JS get request ...

Utilize jQuery to transform array values into date objects

I am receiving an array from a .net controller. The values I am getting for dates are: /Date(1445256000000)/ and /Date(1445256900000)/ Instead of this, I need to convert these into proper date values. Now that I have an array of objects, I want to upda ...

Adding an active class to a selected list item can easily be accomplished by targeting the

Hey there, I'm attempting to apply a class to the selected list item and also add a class when scrolling to a specific div. For instance, if I scroll to div#six, the number six (6) in the menu should also have the 'active' class. [Check out ...

Is there a way to eliminate the border of an image attribute pulled from an input field?

Seeking assistance with a persistent issue I'm facing. I have an input for an image and a script to display the selected image. However, when the input is empty, a frustrating black border appears around the image attribute. How can I remove this bord ...

Switch the dropdown selection depending on the checkbox status

I'm currently facing a bit of confusion with my project. I am constrained by an existing framework and need to come up with a workaround. To simplify, I am tasked with populating a dropdown list based on the selected checkboxes. I have managed to get ...

What is the best way to incorporate progress updates into a rendered jade page?

I am currently working on a small express.js application with a form. When the form is submitted, it triggers a python script that takes approximately 5 minutes to run and outputs messages to the console while it is running. At the moment, I am able to cal ...