Tips for adjusting the color of a loaded object in three.js using JSONLoader

My experience with Three.js involves using the AssimpJSONLoader to load objects. One specific object I loaded was cube_cone, a simple 3D model featuring a cube with a cone on top.

var loader1 = new THREE.AssimpJSONLoader(); 
loader1.load( 'models/assimp/cube_cone.json', function ( object ) {
        object.scale.multiplyScalar( 1 );
        object.material.color.setHex(0x666666);
        scene.add( object );

}, onProgress, onError );

My first challenge was changing the color of the object. I attempted to do so by using: "object.material.color.setHex(...)".

Additionally, I wanted to manipulate the color of a specific child object within cube_cone, for example, just the cone. This child object, named test_cone.json, contains children with names like "3DSMesh_0" and "3DSMesh_1". How can I access these children to modify only part of the object?

As a beginner in Javascript, any assistance you can provide would be greatly appreciated.

Answer №1

It's always helpful to provide an example as a response to your previous comment.

let myObject = new THREE.Group(); //declaring a global variable
...
let loader = new THREE.AssimpJSONLoader();
loader.load('models/assimp/cube_cone.json', function (loadedObject) { //callback function for when object is loaded
    myObj = loadedObject; //setting the loaded object to the global variable inside the callback
}, onProgress, onError);
...
function animate() {
    ...
    myObject.rotation.y += 0.1; // accessing the global variable in the animation function
    ...
}

You can also assign a child of an object, like this:

myObject = loadedObject.getObjectByName("3DSMesh_1", 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

Upon returning to the previous page, the checkbox remains checked and cannot be unchecked

Here is the code I'm using to append checkbox values with a hash in the URL. However, when navigating back, the checkboxes remain checked. Take a look at the code snippet below: <html> <head> <script src="http://ajax.googleapis.com/aja ...

What could be causing the issue with local storage in React JS not functioning in real time?

I tried implementing this code from a blog post to use local storage with React, but I'm facing an issue where it doesn't update in real-time after refreshing. I believe using the effect hook can potentially solve this, but I'm struggling to ...

Display JSON information on an ejs page in Expressjs

I'm having difficulties retrieving 2 values from a JSON file and displaying them on my webpage: obj["fruit"] and obj["thyroid"]. My setup involves using ejs as the template view engine alongside express.js. Despite console.log working properly, the m ...

What is the process for obtaining the longitude and latitude coordinates from a map?

I've been doing my best, but I'm still unable to locate the longitude and latitude of all markers on the map... Link: www.eci-polldaymonitoring.nic.in/psl/default.aspx Rajasthan -> ajmer -> Ajmer north -> press clickhere .. If anyone ...

only the final object is visible in the Json array once it has been constructed

When calling a restful service to retrieve documents in the database as a JSON String, there are two loops foaList and otaList. Using just one loop results in the correct JSON response: The following code successfully retrieves the desired response: def ...

Extracting a precise value from an array nested within another array

Within my nested associative arrays, I am trying to figure out how to splice a specific value. In a regular array, this is easily done with: arr.splice(arr.indexOf('specific'), 1); But when it comes to an array structure like this: arr['h ...

When utilizing both jQuery and JSON, the date value is being shown in an incorrect format

When retrieving the created date value from SQL Server and displaying it in a div using jQuery, I am receiving the value as "/Date(1385716781000)/" instead of the desired dd/mm/yy format. The value I am getting in my model is "2013-11-29 14:03:33.160", bu ...

What could be the reason behind my Discord bot never being marked as ready?

I'm currently working on developing a Discord bot using discord.js, but I've encountered an issue. The bot is never getting ready because it's not logging anything in the console. Here's the script snippet I'm using: import Discord ...

Decode a JSON string that has already been encoded

Currently, I am dealing with JSON strings in which the quotes are not properly escaped. The strings are structured like this: { "foo" : "hello my name is "michael"" } Is there a practical way in JS/PHP to escape the quotes within the value without manual ...

Utilizing the power of Vue Router with DataTables

I am currently facing an issue where I want to include links or buttons in my DataTable rows that can navigate to a vue route when clicked. The straightforward approach would be to add a normal <a> element with the href attribute set to "/item/$ ...

Error: Property 'blogCategory' is unreadable because it is undefined

Having trouble rendering blog posts from a json file in React const BlogPost = (props) => { const [post, setPost] = useState({ id: "", blogCategory:"", blogTitle:"", postedOn:"", ...

Utilize JavaScript's $.Map() function in combination with Math.Max.Apply() to determine the tallest height possible

This code snippet demonstrates how to find the maximum height using jQuery. The $.fn. syntax is utilized to add this method as a jQuery Plugin. $.Map() creates a new array. Math.Max.Apply retrieves the max number from an array. $.fn.t ...

Navigating a JSON array with Python: A guide to parsing

Here is my JSON array and I need to create a new string based on it. I wrote a simple function to try returning a value from the JSON data, but I encountered an issue with Error was expected string or buffer def blah(myjson): data = json.loads(myjson ...

using next-i18next for internationalization in an array of objects

I am currently working on a project where I have an array of objects named "Options" that are being used as a prop for a dropdown/select Material-UI component. In order to localize the labels, I want to integrate the next-i18next library. I followed the ...

dismiss facial recognition event

Does anyone know how to implement a cancel button in Facebox when using it to delete data from MySQL? I want the window to unload when the user clicks on cancel. This is the code snippet I am currently using with Facebox: Are You Sure You Want To Delete ...

Upon clicking the link, the JavaScript functionality failed to activate

When I click on a link and try to add a class, it's not working. I want to create a "modal" div. Here is the PHP/HTML code: <?php if(isset($_GET['ido'])) { ?> <div id="modal" class="modal" style="background: blue; width: 1 ...

An unexpected error has occurred: Uncaught promise rejection with the following message: Assertion error detected - The type provided is not a ComponentType and does not contain the 'ɵcmp' property

I encountered an issue in my Angular app where a link was directing to an external URL. When clicking on that link, I received the following error message in the console: ERROR Error: Uncaught (in promise): Error: ASSERTION ERROR: Type passed in is not Co ...

Iterating over JSONObject to Form Array in Java

I am currently working with a JSONObject in the following format: [{"ascii_name":"Rio de Janeiro"},{"ascii_name":"Riyadh"},{"ascii_name":"Rome"},{"ascii_name":"Rawalpindi"},{"ascii_name":"Rabat"},{"ascii_name":"Recife"},{"ascii_name":"Ra's Bayrut"}, ...

What is the best method to handle errors when retrieving JSON data and updating it using fetch()?

I need to figure out a way for the setMessage not to appear when encountering a PUT ERROR 404 not found in the updateTemplate function. I attempted using catch(err) but was unsuccessful. Here is the complete code snippet: My unique version of the code... ...

What could be the reason for the malfunctioning of my JavaScript code?

I have obtained some code from a tutorial on coding website called JSFiddle $(function() { $('.toggler').click(function() { $(this).find('div').slideToggle(); }); }); <div class="toggler"> This is the title < ...