Are alterations in hues within three.js program an unusual quirk of three.js?

I'm trying to change the color of a cube in three.js to a random color. I've looked at various resources, like Changing color of cube in three.js, but for some reason, my code doesn't update the cube's color despite the console output showing the correct values. However, manually setting the color using

cube.material.color.setRGB(100, 0, 0);
works! What could be going wrong?

var r = 1;
var g = 1;
var b = 1;

var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
var renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

var geometry = new THREE.CubeGeometry(1, 1, 1);
var material = new THREE.MeshPhongMaterial();
var cube = new THREE.Mesh(geometry, material);

scene.add(cube);

var directionalLight = new THREE.DirectionalLight('white', 1);
directionalLight.position.set(1, 0, 1);
scene.add(directionalLight);

camera.position.z = 5;

r = Math.round(Math.random() * (255 - 0) + 0);
g = Math.round(Math.random() * (255 - 0) + 0);
b = Math.round(Math.random() * (255 - 0) + 0);

/* The console output below is correct, but there seems to be an issue with setting the color. Manually inputting the color values does work! */

console.log(r, g, b);
cube.material.color.setRGB(r, g, b);

function render() {

    requestAnimationFrame(render);
    renderer.render(scene, camera);

    cube.rotation.x += 0.01;
    cube.rotation.y += 0.01;
    //cube.position.z += 0.01;

}
render();

Answer №1

When you want to define a THREE.Color by using the setRGB( r, g, b ) function, note that the values of r, g, and b should be between zero and 1.

color.setRGB( Math.random(), Math.random(), Math.random() );

For more information, visit .

This code is for three.js version r.85

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

How can I adjust the column width in OfficeGen?

Currently, I am utilizing officeGen for the purpose of generating word documents. <sup> let table = [ [ { val: "TT", fontFamily: "Times New Roman", }, { val: "Ten hang", ...

Can you eliminate the commas and turn it into a string?

function EncryptMessage(message) { var encryptedArr = []; for(i=0;i<message.length+1;i++){ var unicode = message.charCodeAt(i); var encryptedUnicode; var newCharacter = String.fromCharCode(encryptedUnicode); if( ...

Sending data from a Node.js backend to a React.js frontend using res.send

How can I pass a string from my nodejs backend using res.send? app.post("/user", (req,res) => { console.log(req.body.email); res.send('haha'); }); I need to perform certain operations on the front end based on the value of the string retriev ...

Showing the information obtained from an API request once the user submits the form without the need to reload the page

I am currently working on a form that will take search query requests from users upon submission and then display the results by making an API call. My goal is to show these results without the page having to refresh, using AJAX. The backend connection to ...

Employing ng-click within a displayed column of Datatable

Currently, I am utilizing a plain Datatable in AngularJS without any Angular-Datatable libraries. Everything appears to be working well except for one issue. Here is the datatable code snippet: $scope.siInfoTable = $('#siInfoTable').DataTable({ ...

Bring Component into Vue if necessary

I have multiple menu types and would like to determine which type of menu to use by configuring it in .env.local. For example: VUE_APP_MENU_TYPE=2 Here is the code snippet from my javascript file: let menu = false; if (process.env.VUE_APP_MENU_TYPE === &q ...

Having trouble selecting checkboxes in React Native

When working on React Native tests, I utilize react-native-paper. Below is the code snippet: {(!props.question.isSingleAnswer && props.question.answers.length) && <View> {props.question.answers.map((item) => ( ...

Tips for generating a fresh array by conditionally including an extra property based on the comparison of two arrays

I am working with two different arrays: lockers: [ { locker_id: 1, label: { size: 1, label: "small" } }, { locker_id: 2, label: { size: 3, label: "medium" } }, { locker_id: 3 ...

Verify if the current day falls within the range of Monday to Sunday using Node.js

Currently developing a food delivery app similar to foodpanda. Encountering an issue where a restaurant's operating days are from Monday to Friday, and I need to prevent users from placing orders on Saturdays and Sundays (or any other specified servic ...

Troubleshooting axios GET request in JavaScript: despite successfully pushing data to an array, encountering errors when using promise

I'm attempting to send a GET request to a free API in order to retrieve an array of all French departments. Initially, I encountered an issue where I was getting an empty result, which I later figured out was due to not waiting for the GET request to ...

Navigating my smart contract through a web browser

After successfully deploying my contract on ropsten network, I attempted to interact with it through a web browser. However, I encountered an error message stating that it is not a function. Interestingly, when I tried interacting with the contract on Node ...

What are the best practices for implementing image-slice in node.js?

I attempted to utilize image-slice to divide an image into multiple parts using Node.js. I tried installing npm i image-to-slices, sudo port install cairo, npm i canvas, and brew install pkg-config cairo pango libpng jpeg giflib. However, I still encounte ...

Setting up grunt-contrib-nodeunit to generate JUnit XML output: a step-by-step guide

I have been searching for information on how to configure reporters in the grunt-contrib-nodeunit module, as I recently added this task to my Gruntfile.js. nodeunit: { all: ['nodeunit/**/*.test.js'], } Does anyone know how to instruct Grunt ...

Using Angular to retrieve JSON data from a static file

Currently, I am attempting to implement this AngularJS example from a tutorial on Login Tutorial Example. Instead of storing the username and password as simple strings, I am trying to retrieve them from a local file. I understand that this may not be the ...

Issue with Angular JS orderBy when sorting by numerical fields is not functioning as expected

Within my controller, I implemented code to convert the rank into a Float data type. details.rank = ''; $scope.order = function (predicate) { $scope.reverse = ($scope.predicate === predicate) ? !$scope.reverse : false; if (predicate == & ...

Employ ImageMagic in a synchronous manner

Consider utilizing imagemagick Required to use imagemagick in a synchronous manner. Meaning the following code should execute only after the image conversion is complete (regardless of any errors). The only solution I can see involves using deasync: co ...

Gradually make text disappear while scrolling

On my webpage, I have a background image that remains fixed and centered. When the content on the page is long enough to overflow, I want the text to appear inside the image and fade out as you scroll towards the top or bottom of the image. I do not want a ...

Opt for utilizing variables rather than string values containing relative paths in a TypeScript project

Is it possible to implement a namespace-like structure within a large project for importing modules? For instance, instead of import {formatNumber} from '../../../utils/formatters' Could I use import {formatNumber} from utils.formatters I b ...

Implementing Guid regex in Angular ui-router state: a step-by-step guide

I'm working on a route .state('mystate', { url: '/{id}', templateUrl: '/views/partial.html' }); The id parameter needs to be in the form of a GUID like "2a542f61-5fff-4607-845d-972e6c3ad1e4". How can I include ...

When working with a JavaScript array of class objects, it may appear as though the array is empty when it

Currently puzzled by the behavior of this array. Despite my efforts to find an answer, I haven't had any luck so far. I initialize var allMenuItems = []; at the top and then in getMenuItems(), I push multiple new class objects. However, when I call co ...