Vadim S
Vadim S

Reputation: 358

Custom 3D object by coordinates using THREE.js

I have an array of (x,y,z) of vertices coordinates. And array of facets which contains indexes of vertices (from first array) for each facet (whereby each facet can have a different number of vertices).

How can I make custom 3D object using these data?

Upvotes: 1

Views: 977

Answers (1)

kovacsv
kovacsv

Reputation: 687

If you have only convex polygons, you can do something like this. The second loop creates triangle strip from your convex polygons.

var vertices = [
    0, 0, 0,
    1, 0, 0,
    1, 1, 0,
    0, 1, 0,
    0, 0, 1
];

var faces = [
    [0, 1, 2, 3],
    [0, 3, 4]
];

var geometry = new THREE.Geometry ();
var i, j, face;
for (i = 0; i < vertices.length; i += 3) {
    geometry.vertices.push (new THREE.Vector3 (
        vertices[i],
        vertices[i + 1],
        vertices[i + 2]
    ));
}

var i, j, face;
for (i = 0; i < faces.length; i++) {
    face = faces[i];
    for (j = 1; j < face.length - 1; j++) {
        geometry.faces.push (new THREE.Face3 (face[0], face[j], face[j + 1]));
    }
}

Here is a fiddle with the whole code: http://jsfiddle.net/g25v5t0k/

Upvotes: 2

Related Questions