Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I hide faces of a mesh in three.js?

I want to make parts of a mesh invisible at runtime. Can I set these parts invisible/transparent, e.g. by changing attributes of single faces? The mesh itself uses only one material.


Exemplary illustration as the editor understands this question: Imagine a mesh (here with a geometry of 20 vertices) where each quad of four vertices builds up a Face4. Now, some parts of the mesh should be made invisible (here two faces are invisible).

example

like image 438
user1455053 Avatar asked Jun 14 '12 00:06

user1455053


People also ask

What is a mesh in three js?

A Three. js Mesh is a base class that inherits from Object3d and is used to instantiate polygonal objects by combining a Geometry with a Material. Mesh is also the base class for the more advanced MorphAnimMesh and SkinnedMesh classes.

How do you make a polygon in 3 js?

You can create a polygon from vertices with the following code: var geom = new THREE. Geometry(); var v1 = new THREE. Vector3(0,0,0); var v2 = new THREE.

What can you do with three js?

Three. js is a powerful library for creating three-dimensional models and games. With just a few lines of JavaScript, you can create anything from simple 3D patterns to photorealistic, real-time scenes. You can build simple and complex 3D geometrics, animate and move objects through a lifelike scene, and more.


1 Answers

You can assign a different material to each face. Here is an example where the faces share a material, but some faces are transparent:

// geometry
var geometry = new THREE.BoxGeometry( 100, 100, 100, 4, 4, 4 );

// materials
materials = [
    new THREE.MeshLambertMaterial( { color: 0xffff00, side: THREE.DoubleSide } ),
    new THREE.MeshBasicMaterial( { transparent: true, opacity: 0 } )
];

// assign material to each face
for( var i = 0; i < geometry.faces.length; i++ ) {
    geometry.faces[ i ].materialIndex = THREE.Math.randInt( 0, 1 );
}
geometry.sortFacesByMaterialIndex(); // optional, to reduce draw calls

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

Updated Fiddle showing one way to change a material at runtime: http://jsfiddle.net/e0x88z7w/

EDIT: MeshFaceMaterial has been deprecated. Post and fiddle updated.

three.js r.87

like image 53
WestLangley Avatar answered Sep 23 '22 06:09

WestLangley