Unable to modify the textures of objects in three.js by pressing a key

I am attempting to switch materials for a .obj loaded model when a key is pressed, but I am running into an issue where only the first element in the material array is being used. I have double-checked that my loop is functioning correctly.

Check out the DEMO here. If you have any suggestions on how to fix this issue, please let me know!

Here is the CODE

Answer №1

When the user presses the "C" key during a keydown event, we search through the object to determine if the specific child is a mesh with the material name "Book_Cover".

However, because the materials in the colors array were not assigned names, the code inside the condition

if (child.material.name == "Book_Cover") { /*...*/ }

did not run as anticipated.

TL;DR

make sure to include the name property when defining your materials:

var MyColor = new THREE.MeshPhongMaterial({
        //...,
        name: "Book_Cover"
});

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

The function is not operational while executing addEventListener

I'm encountering some bugs in my Angular 1.5 project with TypeScript. I'm trying to retrieve the scrollTop value from the .uc-card element. public container = document.querySelector(".uc-card"); In my $onInit, I have: public $onInit() { this ...

Retrieve JSON array object values by iterating through each element using $.each

Here is how my JSON data is organized: [{ "industry": [{ "Supermart": [{ "merchant": [{ "name": "Lazada", "banner": "abc.com/img.jpg", "url": "http://lazada.com.my" }] ...

Strategies for re-rendering a React component when the useState value remains the same or retains its previous value

I am currently using a useState hook to store the value of selectFolderId: const [selectFolderId, useSelectFolderId] = React.useState(documentStore.id) Despite trying to update selectFolderId with the new value from DocumentStore by using a useEffect hook ...

Managing email delivery and responses within Nextjs server functions using Nodemailer and React Email package

Currently, I'm working on a Next.js project that involves sending emails. The functionality works as expected, but I've encountered an issue when trying to verify if the email was successfully sent or not. Here's my current setup: await tran ...

Having trouble accessing a section of my website that uses JQuery Mobile and tags

I'm encountering a small issue and I'm not sure how to resolve it. The problem I'm facing is with a HTML5 code containing different pages (tags with ids). I'm utilizing Jquery Mobile to switch between these pages using buttons. Strangel ...

"Enhance Your Browse experience: Chrome Extension that automatically appends content to pages

I'm currently developing a Chrome extension that detects when a URL on a specific domain changes, and if the URL matches a certain pattern, it will add new HTML content to the webpage. Here is an excerpt from my manifest.json file: { "name": "Ap ...

Identify instances of repeated values in input fields

I'm working on a complex html form that includes multiple email fields. To ensure data integrity, I want to prevent users from entering the same email in more than one field. After some trial and error, I successfully identified duplicate emails by s ...

The act of exporting components from the main index file allows for

I have assigned a components folder where I created various components that I want to export to the index.js file and then export all of them from there. This is an example from one of the components files: export default ToggleSwitch; Now, in the inde ...

verifying the presence of a specific key within a dictionary in Python

Check out the javascript code snippet: const isValid = function (area, row, col, num) { if (area[num] || row[num] || col[num]) { return false; } else { return true; } }; I attempted to convert this into python but encountered some difficulti ...

Divide using two separators

const inputString = 'db.employee.find({"city":"Paris"},{"emp_id":1}' const result = inputString.split(/\.|\[); // splitting based on . or [ Is there a way to split a string based on two delimiters, such as . and [? For example, if we ...

Sending a JavaScript array to an MVC controller in .NET 5.0 via a POST request

Trying to send data from a JavaScript array to an MVC controller. While debugging, I end up in the method public void GetJSArray(List<SelectorModel> selectorModels) However, the selectorModels is currently showing as null. Below is the model being ...

Retrieving data from external JSON files

I implemented this JavaScript code to retrieve JSON data from an external server. The JSON data gets loaded from the server, but I'm unsure how to extract markers from the Google Map using this external JSON data. var map, infowindow; //// // Fet ...

In what ways can two identical-looking arrays actually be distinct from each other?

Explore this JavaScript snippet: var st, ary, ary1, ary2; ary = [["a","b","c"], ["d","e","f"]]; // 2D array ary1 = ary[1]; st = "d,e,f"; ary2 = st.split(','); st = typeof(ary1) + " " + ary1.length + " " + ary1 + '\n'; // A ...

No content sent in the request body while implementing fetch

Attempting to send graphql calls from a React component to a PHP server using the fetch method for the first time. The setup involves React JS on the client-side and Symfony 4 on the server-side. Despite indications that data is being sent in the browser ...

What is the process of setting up a route for a logged-in user on the profile page in Next

In my current web application using Next.js and Redux, I have a profile page feature that allows users to view other users by accessing URLs like website.com/aadhit or website.com/robert. My issue arises when a logged-in user with the username "richard" a ...

Server.js node is unresponsive and throwing an error message

Currently, I have started working with expressJs nodeJs but I am facing an issue at the beginning when trying to run the node server.js command. It seems like there might be a missing module that needs to be installed, but I am unsure which one is missing. ...

Locating the right selector for adding a div to its parent element in jQuery

I have come across an interesting HTML structure: <div class="dropdownedit"> <div class="dropbtn">textxyz</div> <div class="dropdown-content" style="display: none;"> <div href="#" class="ocond" id="text1">text1</div> &l ...

The AJAX POST form consistently submits data from the initial entry in the list

Upon investigation, I have discovered that the issue is linked to my AJAX posting method. When I submit the form using the standard method (action=""), it successfully populates my database based on the selected record from the list. However, when I util ...

Invoke the controller by clicking inside an iframe using AngularJS

I am attempting to attach an event to an iframe in order to trigger a function in the angular controller whenever any part of the iframe is clicked. My goal is to achieve this using either the jQuery lite provided by angular, or with pure javascript, depen ...

What is the best way to send a POST request with an array containing multiple objects within it?

Unique Context Currently, I am expanding my knowledge of JavaScript by working on a REST API project using node.JS and express. I have encountered a challenge while attempting to parse an array of objects that contains nested arrays of objects. Below is a ...