Generate mesh faces for vertices in THREE.js

后端 未结 2 1691
花落未央
花落未央 2020-12-31 23:36

I\'m not sure if the answer is supposed to be blindingly obvious but it eludes me. I\'m doing the 3D Graphics class on Udacity that uses three.js. I\'m at a point where I\'m

2条回答
  •  情话喂你
    2020-12-31 23:47

    You can automate your triangulation

    For big polygons it can be quite a job to manually add the faces. You can automate the process of adding faces to the Mesh using the triangulateShape method in THREE.Shape.Utils like this:

    var vertices = [your vertices array]
    var holes = [];
    var triangles, mesh;
    var geometry = new THREE.Geometry();
    var material = new THREE.MeshBasicMaterial();
    
    geometry.vertices = vertices;
    
    triangles = THREE.Shape.Utils.triangulateShape ( vertices, holes );
    
    for( var i = 0; i < triangles.length; i++ ){
    
        geometry.faces.push( new THREE.Face3( triangles[i][0], triangles[i][1], triangles[i][2] ));
    
    }
    
    mesh = new THREE.Mesh( geometry, material );
    

    Where vertices is your array of vertices and with holes you can define holes in your polygon.

    Note: Be careful, if your polygon is self intersecting it will throw an error. Make sure your vertices array is representing a valid (non intersecting) polygon.

提交回复
热议问题