Utilizing the child's property of an object in Three.js beyond the loader.load function

I am looking for a way to dynamically update the color property of an object using dat.GUI(). It is straightforward when working with objects created using three.js geometry. However, I am facing some challenges as I am working with imported objects (.obj & .mtl). Specifically, I need to update the property outside the loader.load function. Can anyone provide guidance on how to achieve this?

edit:

var mtlLoader = new THREE.MTLLoader();
mtlLoader.setPath( 'obj/Cube/' ); 
mtlLoader.load( 'cube.mtl', function( materials ) {
materials.preload();

    var objLoader = new THREE.OBJLoader();
    objLoader.setMaterials( materials );
    objLoader.setPath( 'obj/Cube/' );
    objLoader.load( 'cube.obj', function( object ) {
        object.traverse( function( child ) { 
            if ( child instanceof THREE.Mesh ) { 
                child.castShadow = true;
                child.material.color.set( 0x00ff00 );  // change color without dat.GUI
            } 
        } );
    companion = object;
    scene.add( object );
    });
});

gui = new dat.GUI();

parameters = 
{   
    color: "#ff0000",
    visible: true,
};

var objColor = gui.addColor( parameters, 'color' ).name('Color (Diffuse)').listen();
objColor.onChange(function(value) // onFinishChange
{   companion.material.color.setHex( value.replace("#", "0x") );   });

gui.open();
updateColor(companion);

function updateColor(obj)
{
    obj.material.color.setHex( parameters.color.replace("#", "0x") ); //I need to use the property here.
}

Answer №1

What do you think of this code sample? -

objColor.onUpdate(function(updatedValue) // upon FinishUpdating
{
    partner.traversal( function( offspring ) { 
        if ( offspring instanceof ThreeDObject.Mesh ) { 
            offspring.substance.tone.color.setMark(updatedValue.replace("#", "0x"));
            offspring.substance.requiresRefresh = true;
        } 
    } );   
});

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

Having difficulty refreshing metadata on the client side within Next.js through client-side data retrieval

Having some trouble with updating metadata using NextSeo when fetching data on the client side. useEffect(()=>{ fetch(assetURL) .then((res) => res.json()) .then((data) => { console.log(data); }) .catch((error) => { console.err ...

Does Python have a rest operator?

I have a good understanding of how to utilize the spread/rest syntax in JavaScript: // example of spread syntax const arr1 = [1, 2]; const arr2 = [3, 4]; console.log([...arr1, ...arr2]); // outputs [1, 2, 3, 4] ...

What is the best way to determine the total number of elements within this JSON data structure?

Is there a way to determine the number of elements in a JSON object using JavaScript? data = { name_data: { 35: { name: "AA", }, 47: { name: "BB", }, 48: { name: "CC", ...

Attempting to modify the key values within an object, however, it is mistakenly doubling the values instead

I'm encountering an issue when trying to update key values within an object. It seems to be adding duplicate values or combining all values together instead of assigning each name a specific language slug. I can't figure out where I'm going ...

Managing single sign-out with SSO using the Msal v2 library in a Node.js and Vue.js environment

The challenge: When using msal v2, logging in to the app via a Microsoft account saves parameters to the sessionStorage. Everything works perfectly until the user logs out on Office.com or any other site using Microsoft SSO. The problem arises because the ...

Is there a more effective method for emphasizing chosen options?

There are approximately 30 options available for users to select. Upon clicking an option, a content/html box specific to that selection is displayed while the others are hidden (similar to a picture lightbox but with html instead of thumbnails and main im ...

How can I sort child divs based on a data attribute containing recent date time using jQuery?

Organizing sections by their respective category names and reordering child div elements based on the data attribute, sorted in descending order from most recent to oldest published date. However, encountering issues with the function reArrangeVideosByRece ...

Encountering a problem in a NextJS application where an error occurs stating "cannot read property of undefined (reading setState)" within

In an attempt to resolve the issue, I have linked the method to this inside the constructor. Additionally, I have utilized the arrow function technique to address the problem at hand. Despite these efforts, I continue to encounter the following error: Unha ...

Transfer the HTML5 FullScreen (MAP) feature onto a separate display

I'm trying to integrate a leaflet map into my AngularJS Application, but I've hit a roadblock. My goal is to move the Fullscreen feature of the Map to a second screen (if one is available), allowing users to interact with the application on the ...

Is it possible to refresh the Dictionary using data from localStorage?

I attempted to retrieve all key/value pairs from the localStorage and use them to update my dictionary named keywordDict in the following manner: $(document).ready(function() { var store = allStorage(); console.log(store); $.ajax({ url: '/m ...

The tension settings in Chart.JS appear unusual

I recently updated to ChartJS v4.0.1 and noticed a new option called tension for curving the line chart. However, I'm not satisfied with how it looks. The tension option ranges from 0 to 1, and I've experimented with different values to enhance ...

localStorage is functional on desktop devices; however, it does not work on mobile devices running iOS version 12

After developing a basic Todos application using React, I decided to introduce the use of localStorage to maintain data persistence between page reloads. Below is an overview of how I implemented it: loadStateFromLocalStorage() { for (let key in this.st ...

Translating and resetting objects in ThreeJS

As a newcomer to Three.js, I recently loaded a collada (.dae) file containing multiple objects. My goal now is to move each object along its z-axis when the mouse hovers over it and then return it to its original position once the mouse leaves. To achiev ...

Guide on how to compare two arrays in JavaScript and identify mismatches by their respective indices

let x=["e","f","g","h"]; let y=["f","e","g","h"]; I want the following result: Inconsistent array from x Inconsistency array=["e", "f"]; ...

Is it possible to delete an element from both local storage and HTML by referencing its ID?

Experience a simple flashcard game where you enter a question and answer to create a new flash card stored as an object within the cards array. The newly created flash card is also displayed by appending a new element to the flash cards section on the webp ...

What is the best way to verify if a subscription is ready in a React component?

In order to effectively retrieve data from mongoDB in my meteor/react application, I have put together a simple example. One thing I would like to implement is a loading icon that will be displayed while the data is being fetched. To achieve this, I will ...

When using jQuery to add an item to a List, it disappears immediately after

I am facing an issue with the code I have. Whenever I click on the "Add" button to add an item, it briefly appears in the list but then disappears. I'm having trouble figuring out why this is happening. $(document).ready(function() { $("#submitB ...

Enhancing efficiency with Three.js for my time management script

After testing out the threejs library with a project (link: ), I noticed that the performance was quite poor. Any suggestions on how to improve it? The main components of my experiment include a timeFont class for generating instances, camera initializati ...

I am attempting to display only the description of the button that I click on

How can I ensure that only the container pressed will display its description when the description button is clicked, instead of all mapped containers showing their descriptions? `import { useState } from "react"; export default function Se ...

Issue with Material UI components: The Select component is collapsed and the autoWidth functionality is not

The Material UI (React) Select component is not expanding in width as expected, even with the autoWidth property. https://i.sstatic.net/h3H0V.png <FormControl margin="dense"> <InputLabel id="prefix-label">Prefi ...