ridthyself
ridthyself

Reputation: 839

Simple tetrahedron using THREE.geometry

From the example provided at THREEjs.org/docs:

var geometry = new THREE.Geometry();

geometry.vertices.push(
    new THREE.Vector3( -10,  10, 0 ),
    new THREE.Vector3( -10, -10, 0 ),
    new THREE.Vector3(  10, -10, 0 )
);

geometry.faces.push( new THREE.Face3( 0, 1, 2 ) );

geometry.computeBoundingSphere();

it's very straight forward to create a triangle by specifying it's vertices. If I want to go on to make a tetrahedron using this method, I find myself repeating this code three more times, once for each face of the tetrahedron, changing one of the vectors each time to define one of the triangles which makes up the tetrahedron...

Is there a simple way, if I define the fourth vector of a tetrahedron, to create a tetrahedron without having to define each face individually?

Something like:

var geometry = new THREE.Geometry();

geometry.vertices.push(
    new THREE.Vector3( -10,  10, 0 ),
    new THREE.Vector3( -10, -10, 0 ),
    new THREE.Vector3(  10, -10, 0 ),
    new THREE.Vector3(  10, 10, 10 ),
);

geometry.faces.push( new THREE.Face3( 0, 1, 2 ) );

geometry.computeBoundingSphere();

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

Of course this code doesn't work, the last vertex is simply ignored.

Upvotes: 0

Views: 2512

Answers (1)

gaitat
gaitat

Reputation: 12632

Take a look at src/extras/geometries/TetrahedronGeometry.js in the code of three.js

Copying the code here for reference:

THREE.TetrahedronGeometry = function ( radius, detail ) {

    var vertices = [ 1,  1,  1,   - 1, - 1,  1,   - 1,  1, - 1,    1, - 1, - 1];
    var indices = [ 2,  1,  0,    0,  3,  2,    1,  3,  0,    2,  3,  1];

    THREE.PolyhedronGeometry.call( this, vertices, indices, radius, detail );
};

THREE.TetrahedronGeometry.prototype = Object.create( THREE.Geometry.prototype );

Upvotes: 3

Related Questions