Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coloring faces of a Three.js BoxGeometry

I saw some solutions that accessed the THREE.BoxGeometry faces like that:

var geometry = new THREE.BoxGeometry(n,n,n)
let faces = geometry.faces;
for (let face of faces) {
  face.color.set(Math.random() * 0xffffff);
}

But it seems that the faces-array doesn't exist in the current Three.js version r129 (Uncaught TypeError: faces is not iterable).

How can I achieve an easy coloring of the six cube faces?

like image 571
ironmouse Avatar asked Jun 15 '21 16:06

ironmouse


People also ask

How to create geometries with three-Js?

We can create geometries with these classes. Three.js has some built-in primitive shapes, including a cube, sphere, polyhedra, torus, and torus knot. To create a Cube in Three.js, use Three.CubeGeometry.

What is the difference between mygeometry and faceindex?

Assuming that "myGeometry" is the geometry containing the face that you would like to change the color of, and "faceIndex" is the index of the particular face that you want to change the color of.

How do I create a box in threejs?

Always take note of what version of three.js you are using. To create a basic Box three.js example using the Box Geometry Constructor the First thing I am going to want to do is create a Mesh. This Mesh will accept a geometry as the first argument such as one that is created using the Box Geometry Constructor.

What can you learn from box geometry in JavaScript?

Much of what applies for a Box geometry will also apply for other built in geometries, custom geometries, and the buffer geometry class in general so to some extent by learning a thing or two about box geometry one learns a thing or two about geometry in general with threejs.


Video Answer


1 Answers

Try it like so:

let camera, scene, renderer, mesh;

init();
animate();

function init() {

    camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 10 );
    camera.position.z = 1;

    scene = new THREE.Scene();

    const geometry = new THREE.BoxGeometry( 0.2, 0.2, 0.2 ).toNonIndexed();
        
    // vertexColors must be true so vertex colors can be used in the shader
        
    const material = new THREE.MeshBasicMaterial( { vertexColors: true } ); 
        
    // generate color data for each vertex
        
    const positionAttribute = geometry.getAttribute( 'position' );
        
    const colors = [];
    const color = new THREE.Color();
        
    for ( let i = 0; i < positionAttribute.count; i += 3 ) {
        
            color.set( Math.random() * 0xffffff );
            
            // define the same color for each vertex of a triangle
            
            colors.push( color.r, color.g, color.b );
            colors.push( color.r, color.g, color.b );
            colors.push( color.r, color.g, color.b );
        
    }
        
    // define the new attribute
        
    geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );

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

    renderer = new THREE.WebGLRenderer( { antialias: true } );
    renderer.setSize( window.innerWidth, window.innerHeight );
    document.body.appendChild( renderer.domElement );

}

function animate() {

    requestAnimationFrame( animate );

    mesh.rotation.x += 0.01;
    mesh.rotation.y += 0.02;

    renderer.render( scene, camera );

}
body {
      margin: 0;
}
<script src="https://cdn.jsdelivr.net/npm/[email protected]/build/three.js"></script>

The previous geometry format was removed from the core with r125. More information in the following post at the three.js forum.

https://discourse.threejs.org/t/three-geometry-will-be-removed-from-core-with-r125/22401

like image 71
Mugen87 Avatar answered Oct 17 '22 18:10

Mugen87