如何合并多个线与三个js中的间隙

时间:2013-05-27 13:09:32

标签: merge geometry three.js line

我正在成功加载一个数据集,其中包含代表城市中建筑物的多边形数据。所以每个多边形代表一个建筑物。在Threejs中,我可以成功地表示它们,但是当我尝试将单独的线条合并为一个几何体时,所有线条都是连接的(我理解为什么这是有道理的,因为所有顶点都被添加到同一个集合中)。

但是,如何将这些单独的建筑多边形合并为一个几何体,而不会使单独的建筑多边形的线相互连接?

我现在的代码:

var geometry = new THREE.Geometry();

            for (var i = 0; i < data.length; i++) {
                var temp = new THREE.Geometry();
                var polygon = data[i];
                for (var j = 0; j < polygon.length; j++) {
                    temp.vertices.push(new THREE.Vector3(polygon[j][0], polygon[j][1], 0));
                }
                temp.vertices.push(new THREE.Vector3(polygon[0][0], polygon[0][1], 0));

                THREE.GeometryUtils.merge(geometry, temp);
            }
            scene.add(new THREE.Line(geometry, buildingMaterial, THREE.LineStrip));

data是包含建筑物的所有多边形数据的变量。

1 个答案:

答案 0 :(得分:3)

解决:

            var cube = new THREE.Geometry();

            for (var i = 0; i < data.length; i++) {
                var shape = new THREE.Shape(_.uniq(_.map(data[i], function(d) { return new THREE.Vector3(d[0], d[1], 100); })));
                shape.extrude({amount: 100})
                var points = shape.createPointsGeometry();
                THREE.GeometryUtils.merge(cube, shape.extrude({amount: -1 - Math.random() * 1, bevelSegments: 0, steps: 1 , bevelSegments: 0, bevelSize: 0, bevelThickness: 0}));

            }

            var mesh = new THREE.Mesh(cube, new THREE.MeshBasicMaterial({color: 0x333333, wireframe: false}));
            scene.add(mesh);