Adjust the hue of a single side of the cube with THREE.js

I'm currently delving into the world of OOP with Three.js, which is proving to be quite challenging. To start, I decided to create a box within the scene. Now, my next goal is to change the color of one face of that cube.

var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera( 100, window.innerWidth/window.innerHeight, 0.1, 1000 );
camera.position.set(5, 5, 10);

var geometry = new THREE.BoxGeometry(5,2,5);
var material = new THREE.MeshBasicMaterial({color:0xff0ff0, side:THREE.DoubleSide});

var mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);

// Here is where I attempt to change the color of the face, but it's not working
mesh.geometry.faces[5].color.setHex(0xffffff); 

var wireframe = new THREE.WireframeHelper(mesh, 0x00ff00);
scene.add(wireframe);

var renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
renderer.render(scene, camera);

EDIT: The reason why my initial attempt failed was due to using the color 0xffffff. As long as the color isn't set to white (0xffffff), this program should work as intended.

Answer №1

If you're looking to modify the color of a specific side of the cube, you must define vertexColors within your material settings.

var geo = new THREE.BoxGeometry( 5, 2, 5 );

var mat = new THREE.MeshBasicMaterial( { color:0xff0ff0, vertexColors: THREE.FaceColors } );

var mesh = new THREE.Mesh( geo, mat );

mesh.geometry.faces[ 5 ].color.setHex( 0x00ffff ); 

The displayed face color will be the designated face color enhanced by the material color.

If you decide to alter a face color after the mesh has been rendered at least once, you'll need to specify:

mesh.geometry.colorsNeedUpdate = true;

Compatible with three.js version r.80

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

Utilizing a dictionary for comparing with an API response in order to generate an array of unique objects by eliminating duplicates

I currently have a React component that utilizes a dictionary to compare against an API response for address state. The goal is to map only the states that are returned back as options in a dropdown. Below is the mapping function used to create an array o ...

PointerLock, Collision Detection, and Three.js: A Dynamic Trio

I'm developing a 3D virtual home project and encountering an issue with collision detection. I am using PointerLockControls for camera movement, but I am unsure how to detect collisions in all directions effectively. To simplify the problem, I have st ...

Unexpected behavior observed when executing JavaScript Promise.all code

My current issue involves retrieving data from a mySQL DB by using the following code. Within the database, there are multiple tables including an Activity table with a channelId field, a Channel table with a userId field, and a User table with a userName ...

Exploring the world of Socket.Io Listeners, diving deep into the

I have successfully created a server using NodeJS, Socket.IO, and Express. This server works perfectly with the website I developed. However, I encountered some issues when attempting to create an Android application. There is a method on socket.io that r ...

Toggling Selected Items with Jquery Show/Hide Feature

Is there a way to toggle the visibility of selected items in the mobile menu? For instance, when I click on the menu toggle, I would like all the items in the Div class sidebar to be displayed but any content within its child div with class="sticky" shoul ...

Utilizing React to implement a search functionality with pagination and Material UI styling for

My current project involves retrieving a list of data and searching for a title name from a series of todos Here is the prototype I have developed: https://codesandbox.io/s/silly-firefly-7oe25 In the demo, you can observe two working cases in App.js & ...

Guide to creating a new window without a menu bar for an onclick function in electronJS

After trying to remove the menu bar from the main window using win.setMenu(null) in the main.js file, I encountered a new issue. When opening a new window (referred to as the "add items window"), I struggled to find a way to hide the menu bar in it as well ...

The Vue application successfully compiles but fails to load in the web browser

Upon running npm run serve on my Vue app, the console displays the following output: DONE Compiled successfully in 17450ms 2:15:55 PM App running at: - Local: ...

Troubleshooting error handling in Node.js using Express and MongoDB

hey there, I'm new to Node.js and I'm working on creating a basic notes app using express and MongoDB. However, I'm encountering an issue when trying to update a note which displays "Cannot PUT" followed by the note's ID. Below is the ...

Autocomplete feature tailored for specific country's geography

My website needs to have an input field with autocomplete for places within a specific country. I specifically want to receive suggestions only for places in 'The Netherlands' in the autocomplete feature. The code currently provides suggestions ...

Erroneous Marker Placement Detected

Here is the data from my DataTable: Country Types of sales Total sales($) Name State United states of America chemicals 12662871 Obama GA Unite ...

Enhancing the session helper in Silex with additional values

Hey there, I'm currently working on a basic shopping cart using an MVC framework called Silex. However, I've run into a JavaScript/AJAX issue that I could use some help with. My problem arises when trying to add a product to the basket. The issue ...

Error encountered while trying to authenticate user through post request

I have written a post route request function below to handle user login. However, I keep encountering 401 unauthorized errors when making the request. Can anyone suggest any modifications or refactorings that could potentially fix this issue? Thank you i ...

The scrolling behavior of Chrome tabs or bookmarks can impact the functionality of an Angular

I recently came across a strange problem with my angularjs page that includes a slick carrousel. In the controller, I have code that listens for the $destroy event to remove certain injected elements by slick. However, I noticed that whenever I scroll ov ...

Learn the secret to displaying and concealing images with jquery!

I'm working on enhancing a question I recently published. After scrolling down the page 200px, the image changes its size. I'm also looking to implement additional functions that I'm struggling to make work. Specifically, I want to achiev ...

Customizing the background color of an option using HTML and CSS

I am currently developing an option form where each selection will appear in a different color as the user scrolls down. Once a selection is made, it should be saved and displayed with the chosen color. https://i.sstatic.net/gvdpt.png My goal is to save ...

Can you explain the process of a put request in Angular, Express, and Mongoose?

My colleague and I are currently deciphering the inner workings of a code snippet generated by a tutorial. Our main focus is on how the client/server communication flows once line 8 of factory.js is triggered: factory.js app.factory('postFactory&apo ...

Guide on retrieving information from Spark and showcasing it through Angular

Trying to navigate the world of Spark framework and AngularJS as a beginner, I set out to create a basic REST application. However, I hit a roadblock when it came to retrieving data from the server and displaying it using Angular. My initial task was simp ...

Trying to connect to a socket.io server from a remotely hosted page returns an error stating "require undefined."

Trying to connect to a socket.io server hosted on Heroku from a remote client. The client site is hosted on XAMPP running on my PC. Encountering an issue with the client-side JavaScript const io = require("socket.io-client"); An error is thrown in the co ...

Calling a function within another function is not allowed in Typescript

Essentially, I have an Angular Web Page that uploads a file to the server via a POST request, which is then received by my NodeJS app. The issue arises when attempting to retrieve the file path in subirArchivo() and pass it to a function called InsertaPer ...