How can I change a string into hexadecimal code (0x000000) for Three 3d Objects using JavaScript?

I'm attempting to create a multitude of objects simultaneously while aiming for the color to gradually fade. Nonetheless, despite using .toString(16) to form a string, there seems to be an issue with this line of code:

new THREE.MeshBasicMaterial({ color: '0x' + color, wireframe: false, opacity: 0.5 });

Presently, here's the code:

    for (var i = 0; i < 10; i++) {
        for (var j = 0; j < 10; j++) {
            for (var k = 0; k < 10; k++) {
                geometry = new THREE.CubeGeometry(50, 50, 50);
                colorr = 254 / 10 * k;
                colorr = Math.round(colorr);
                colorr = colorr.toString(16);
                colorg = 254 / 10 * k;
                colorg = Math.round(colorg);
                colorg = colorg.toString(16);
                colorb = 254 / 10 * k;
                colorb = Math.round(colorb);
                colorb = colorb.toString(16);
                color = '0x' + colorr + colorg + colorb;
                material[i] = new THREE.MeshBasicMaterial({ color: color, wireframe: false, opacity: 0.5 });


                mesh[i] = new THREE.Mesh(geometry, material[i]);
                mesh[i].position.x = -500 + (k * 100);
                mesh[i].position.y = -500 + (j * 100);
                mesh[i].position.z = -500 + (i * 100);
                scene.add(mesh[i]);
                objects.push(mesh[i]);

            }
        }
    }

Unfortunately, the end result is simply a dull greyish black color.

Answer №1

> 0xe5e5e5

15066597

> Converting '0xe5e5e5' from hex to decimal using parseInt()

15066597

Simply put, when you see "0x" in front of a number in JavaScript, it signifies that the number is written in hexadecimal format for visual clarity resembling CSS colors. However, both JavaScript and Three.js treat these numbers as regular decimal values. By utilizing the parseInt() function with the appropriate radix, you can accurately convert a hex string into its decimal equivalent, allowing you to work with color codes effortlessly.

Answer №2

Making the components from 0 to 1 and using... would simplify the process.

let color = new THREE.Color().setRGB(r, g, b);

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

Leveraging the power of Material-UI and React, modify the appearance of the "carrot" icon within the

Currently implementing MUI's textfield based on the information found at this link: https://mui.com/components/text-fields/. While there are numerous resources available for changing the font of the input text, I have not come across any documentation ...

JavaScript may not display height as anticipated

Currently, I am experimenting with JavaScript in order to achieve a website section that is 100% the height of the screen minus the "nav" bar. Imagine it as having two visible components - the first one being large and the second one small at the bottom. ...

What is the method for applying a distinct texture to each face of a DodecahedronGeometry?

I have just started exploring three js and successfully created a polyhedron with a single texture. However, incorporating multiple textures along with captions seems to be more advanced. ...

Exploring the world of JavaScript by dynamically retrieving all class functions

Is there a way to retrieve an array of all functions from a given class, including functions inherited from parent classes? For instance: class Foo extends Bar { funcA() {} } class Bar { funcB() {} } const instanceFoo = new Foo(); getClass ...

angular primeng table has a checkbox to select all checkboxes

Is there a way to check all checkboxes in a table when the checkbox in the table header is clicked? I am currently utilizing angular 12 and primeNG table for this functionality. <p-table styleClass="text-sm" [value]="value" [loading] ...

Using JQuery, it is possible to search for elements by one specific class while excluding others

Looking for a way to target elements with a specific class, but only if they don't have another class attached to them. Here is an example: <li class="target-class exclude-class"></li> <li class="target-class exclude-class"></li& ...

Having trouble displaying the image in my administrative tool

I am currently developing an admin application that will showcase uploaded product images stored in a database. The images are saved as object IDs in MongoDB. However, the image container in the admin app shows the count of images stored in the database bu ...

Exploring SQL Components with JavaScript

Here is the code I am currently using: //This function handles all games and their attributes function handleGames(){ sql.query('SELECT id FROM games', function (err, rows){ if(err){ console.log(String(err).error.bgWhite) ...

Incorporating a hyperlink within a confirmation return statement

Looking to include a link within a return confirmation <a onclick="return confirm('Make sure you've read our policies before downloading the App');">[...] The policies should be clickable at the end. ...

The concept of asynchronicity and callbacks in JavaScript

As a newcomer to the world of JavaScript and Stack Overflow, I have been learning about synchronous, asynchronous, and callbacks through various videos and blogs. However, I still have a lingering doubt. If synchronous code means following a sequential ord ...

Having difficulty resolving all parameters for the component: (?, [object Object]) in the Jasmine component Unit Test

While defining a UT for a component with an extended class using i8nService and ChangeDetectionRef, I encountered an error preventing me from instantiating it: Failed: Can't resolve all parameters for BrandingMultiselectComponent: (?, [object Object] ...

Having trouble navigating the Request and Response handling in Expressjs/Nodejs?

As I continue to delve deeper into this code, confusion seems to cloud my understanding. Here is the provided source: var express = require('express') , http = require('http') , server = express() ; var home = require('./ro ...

Ensuring the accuracy of a condition within an HTML form through the utilization of

I am interested in designing an HTML page that includes a small form. The form should include: Name Gender Date of Birth "I Agree" checkbox Submit button One important condition to note is that if the individual's age falls between 20 and 25 (calc ...

Creating a polygon path in Google Maps v3 using an array of coordinates

I'm currently working on creating a polygon around US counties. The coordinates for this polygon are coming from a database and then being json encoded for use in JavaScript. Below is the JSON encoded array generated from PHP code: {"Abbeville-sc":[[ ...

What are the fundamentals of type conversion in struts2?

In my struts.xml file, I have the following code: <s:textfield id="thresholdParameter_1" name="gmathreshold.distinctBnumberRatio"> </s:textfield></td> The gmathreshold is a bean with a member variable distinctBnumberRatio which is a ...

Ways to update an angular page using the router without resorting to window.location.reload

I have a specific method for resetting values in a component page. The process involves navigating from the "new-edition" page to the "my-editions" component and then returning to the original location at "new-edition". I am currently using this approach ...

How can I generate spheres in threejs with varying radii while all passing through the shared point located at coordinates (0,0,200)?

Is there a way to generate spheres in threejs where the radius increases while always passing through a single point at 0,0,200? This would allow the origin of each new sphere to move along the z-axis. Appreciate any help, AriemWebgl ...

Steps to showcase a HTML modal based on the outcome of a JavaScript/AJAX database query

Currently in the process of developing a Facebook game utilizing html and Javascript. With two modals on an html page (referred to as ModalA and ModalB), the goal is to link both modals to a single button, triggering the display of only one modal based on ...

The bidirectional data binding in isolated scope is malfunctioning in AngularJS

I've just started learning angularjs and I'm working with isolated scope. I seem to be having issues with two-way binding using isolated scope. Can someone please review my code and help me debug this issue? When I remove age : '=' from ...

Interactive sidebar scrolling feature linked with the main content area

I am working with a layout that uses flexboxes for structure. Both the fixed sidebar and main container have scroll functionality. However, I have encountered an issue where scrolling in the sidebar causes the scroll in the main container to activate whe ...