Generate mesh faces for vertices in THREE.js

后端 未结 2 1629
花落未央
花落未央 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.

    0 讨论(0)
  • 2020-12-31 23:48

    Assuming you are generating your vertices in a concave fashion and in a counterclockwise manner then if you have 3 sides (triangle) you connect vertex 0 with 1 with 2. If you have 4 sides (quad) you connect vertex 0 with 1 with 2 (first triangle) and then vertex 0 with 2 with 3. If you have 5 sides (pentagon) you connect vertex 0 with 1 with 2 (first triangle) then vertex 0 with 2 with 3 (second triangle and then vertex 0 with 3 with 4. I think you get the pattern.

    0 讨论(0)
提交回复
热议问题