简体   繁体   English

如何从three.js中的一系列3D点创建3D多边形?

[英]How do I create a 3D polygon from a series of 3D points in three.js?

I wonder what would be the best way to generate a custom 3D polygon given an array of 3D points in three.js.我想知道在 Three.js 中给定一组 3D 点的情况下,生成自定义 3D 多边形的最佳方法是什么。 It is a simple polygon without holes.它是一个没有孔的简单多边形。 The points are ordered so that they represent neighboring vertices.这些点是有序的,以便它们代表相邻的顶点。 I can do it in 2D, but I don't want the vertices to be coplanar.我可以在 2D 中做到这一点,但我不希望顶点共面。 Thank you for your help!感谢您的帮助!

The main problem here is how to create a 3D mesh from a bunch of points.这里的主要问题是如何从一堆点创建一个 3D 网格。 In other words: you need to figure out which of the vertices should be connect to form a triangle.换句话说:您需要弄清楚哪些顶点应该连接以形成三角形。

This is an surprisingly complicated thing to do (well, at least I was surprised) and there are tons of scientific papers, libraries and so on about that.这是一件非常复杂的事情(好吧,至少我很惊讶)并且有大量的科学论文、图书馆等等。

However, in your case it is a bit simpler as you already know roughly how the vertices should get connected.但是,在您的情况下,它更简单一些,因为您已经大致知道顶点应该如何连接。 I would recommend you have a look at the earcut-library or libtess.js , both of which should be able to create the triangulations you need.我建议您查看earcut-librarylibtess.js ,它们都应该能够创建您需要的三角剖分。

Once you have that, you can roughly go with @lior-trau's recommendation for how to create the geometry from the result.一旦有了它,您就可以大致遵循@lior-trau 的建议,了解如何根据结果创建几何体。

     var geo = new THREE.Geometry();
     var mat = new THREE.MeshBasicMaterial();
     var middlePoint = new THREE.Vector3();
     for(var i=0;i<dots.length;i++){
        middlePoint.add(dots[i].position) 
        geo.vertices.push(new THREE.Vector3(dots[i].x,dots[i].y,dots[i].z));
     }
     middlePoint.divideScalar(dots.length);
     geo.vertices.push(middlePoint);

     for(var i=0;i<dots.length;i++){
        middlePoint.add(dots[i].position) 
        if(i >0){
          geo.faces.push(new THREE.Face3( geo.vertices.length-1,dots[i],dots[i-1]));
        }
     }

    var mesh = new THREE.Mesh(geo,mat);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM