Cube of RGB vertices created using Three.js

I am attempting to create an RGB cube using Three.js without textures, only vertices. I have followed various tutorials but my code is not working as expected. Can anyone provide some advice? Thank you.

https://jsfiddle.net/yjru14q3/

var geom = new THREE.Geometry();

        geom.vertices = vertices;
        geom.vertexColors = colors;
        var colors = [];
        colors[0] = new THREE.Color( 0, 0, 0 );
        ....
        var vertices = [];
        vertices[0] = new THREE.Vector3( 0, 0, 0 );
        ....
        var material = new THREE.MeshBasicMaterial({ 
            vertexColors: THREE.VertexColors,
            side: THREE.DoubleSide, // in case we go inside the cube
        });

        var cube = new THREE.Mesh(geom, material);
        scene.add(cube);

Answer №1

When utilizing geometry.colors[] in conjunction with vertexColors: THREE.VertexColors, it functions seamlessly when the geometry is used for THREE.Points().

https://i.sstatic.net/7qznb.png

If you wish to implement vertex colors for faces' vertices of THREE.Mesh(), it's recommended to refer to this specific example:

var geom = new THREE.BoxGeometry(1, 1, 1);
var faceIndices = ['a', 'b', 'c'];
var vertexIndex, point;
geom.faces.forEach(function(face) { // iterate through faces
  for (var i = 0; i < 3; i++) {
    vertexIndex = face[ faceIndices[ i ] ]; // obtain the index of the face's vertex
    point = geom.vertices[vertexIndex]; // locate the vertex within the vertices array
    color = new THREE.Color( // create a color
      point.x + 0.5, //apply xyz as rgb
      point.y + 0.5,
      point.z + 0.5
    );
    face.vertexColors[ i ] = color; // store the color in the face's vertexColors array
  }
});

var mat = new THREE.MeshBasicMaterial({
  vertexColors: THREE.VertexColors
});

var cube = new THREE.Mesh(geom, mat);
scene.add(cube);

View jsfiddle example

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

Is it possible to create a dynamic input form that appears within a textarea using jquery?

I've designed a unique form using a textarea to display messages from users, rather than the traditional ul list. Additionally, I have included an input form where users can type their messages. My goal is to make this input form visible on the textar ...

Using Node-twitter within the Express framework

I'm currently exploring the node-twitter module within the express framework. Although I have a basic understanding of node, including routing, rendering jade templates, and installing packages, I find myself overwhelmed by the sheer number of package ...

An array mapping itself leads to an unexpected and peculiar outcome

After trying out various techniques for creating JavaScript arrays, I came across an unexpected outcome. By utilizing map to push an array of self-references (DEMO): a=[1,1,1,1,1,1,1,1,1,1]; a=a.map(a.push,a); I ended up with the following result (when u ...

Creating a standalone executable for Node.js without including the entire Node.js framework

Trying to pack a simple hello world script into an executable has led to some challenges. Both pkg and nexe seem to include the entirety of Node.js in the output file, resulting in quite larger files than necessary (around 30 MB). Although EncloseJS was fo ...

How come the checkboxes for trees are not being checked in the child component while using Ant Tree Design?

I'm currently troubleshooting an issue with the Ant Tree component. I have a setup where the PageAdmin component fetches data for selected nodes in the tree, stores it in the checkedKeys array, and passes it as props to the CustomTree component. While ...

Maintain the link's functionality even after it's been clicked by utilizing the same

I have a fixed navbar with same-page anchors and I want to keep the link active after it has been clicked and taken to that section of the page. While this is simple for links to another page, I'm struggling to figure out how to do it here. Here&apos ...

Develop an asynchronous function that can fetch a result at a later time within an Express route

I need to retrieve an Excel file from Box.com using their API and then convert the Excel data into JSON format. Afterwards, I plan to display this JSON data using Express and Handlebars. Below is the function I've created for fetching the Excel file ...

Tips for saving a webcam captured image location into a MySQL database

I've been experiencing issues with the code I used to capture an image. While the image is successfully stored in the specified folder, I'm unable to store the image path in the database. The code stops executing after displaying the "Uploading.. ...

Summing an Array in Javascript

I am having trouble calculating the sum of an array in my code. Instead of adding up all the numbers, it is concatenating them together. For example, if I have grades 90, 95, and 100, the sum shows as 09590100. The first FOR loop successfully pushes the ...

Troubleshooting a cross-origin resource sharing problem in Express.js

I've set up Express as the backend for my client application, but I keep encountering an issue when trying to make a request to the GET /urls endpoint. The error message I receive is: Access to fetch at 'http://localhost:5000/urls' from orig ...

JavaScript HTTP request causes website navbar animation to malfunction

Currently, I am assisting a client with their static website. This task isn't my primary role, but due to some expertise in this area, I have volunteered to support them. To streamline the process and avoid duplicating HTML code across multiple files, ...

jQuery alert: A TypeError was caught stating that the object being referred to does not have the 'dialog' method available

For a few days now, I have been tirelessly searching for a solution to this issue and it has caused me quite a bit of stress. My problem lies in echoing a JQuery popup script within php: echo '<link rel="stylesheet" href="http://code.jquery.c ...

What is the best way to access a nested array within a React component?

Here is an example of the array data: { "firstname": "Jessica", "lastname": "Wood", "favorite": { "movie": Flint Town, "drink": Sprite } } When attempting to access the "drink" property, I encountered an error stating "Objects are ...

An issue with three.js involving sprites, the material of sprites, and detecting mouse clicks

Hello there, I appreciate you taking the time to read this. I've been diving into various tutorials on how to click objects using the mouse in three.js. However, none of them mentioned whether it's possible with sprites. To try and work around th ...

Should we pass <script> tags or unsanitized values to the Handlebars layout?

How can I pass values to handlebars that are not sanitized upon output? For instance, I want to ensure that when using the code res.render('index', {script: '<script src="bundle.js"></script>'}), it is not rendered as {{scri ...

HTML5 Boilerplate and optimizing the critical rendering path by deferring scripts and styles

Previously, I constructed my website layouts using the HTML5 Boilerplate method: incorporating styles and Modernizr in the head section, jQuery (either from Google CDN or as a hosted file) along with scripts just before the closing body tag. This is an exa ...

Tips for retrieving all JavaScript source links from a website URL during page download

Is there a way to determine if a website is using a specific online JavaScript source, such as fontawesome? Some sources may only become apparent once they are actually loaded, rather than being visible in the website's source HTML. I attempted to us ...

Dramatist: What are effective methods for distinguishing and monitoring frames within a webpage?

When it comes to storing my own copy of frames, I want to utilize the page.on("frameattached") and page.on("framedetached") events to properly manage the lifecycle of the frames. My main concern is how I can uniquely identify them across these events. I ...

Is it more beneficial to reference JavaScript libraries in individual user controls or in the master page when constructing a website?

Which of the following scenarios is most effective in terms of client-side (JavaScript) library referencing and loading? Assuming that the web solution is well-designed with components properly encapsulated Just to clarify, the master page mentioned coul ...

Unable to add an item from an array to the data property in Vue.js

When fetching data from Laravel, I use the following response: $unserialize = unserialize($import->field_names); return response()->json( $unserialize, 200 ) ; On Vue JS, I can view the response using: console.log(response); The data is displayed i ...