从 PLYLoaders 将两个几何图形合并为一个几何图形

Merge two geometries into one geometry from PLYLoaders

我有两个 PLY 文件,我想合并为一个,但我对 PLYLoader 的 de loaders 方法有问题,我如何才能同时访问两个加载器的几何图形?

我加载了两个层文件并为新形状定义了新几何体 (BufferGeometry class),但是变量 mesh1mesh2 未定义 当我尝试访问几何属性时。

这是我的代码:

//  ...
var mesh1, material1;
var mesh2, material2;
var singleGeometry, material, mesh;
// ...

init();
animate();

singleGeometry = BufferGeometry();

function init() {
  //   ...
  singleGeometry = new THREE.BufferGeometry();
  //  ...

  var loader = new THREE.PLYLoader();
  loader.load("path/ply1.ply", function(geometry) {
    geometry.computeVertexNormals();
    material = new THREE.MeshStandardMaterial({ color: 0x0055ff, flatShading: true });
    mesh1 = new THREE.Mesh(geometry, material);

    mesh1.position.y = -0.2;
    mesh1.position.z = 0.3;
    mesh1.rotation.x = -Math.PI / 2;
    mesh1.scale.multiplyScalar(0.01);
    mesh1.castShadow = true;
    mesh1.receiveShadow = true;
  });
  loader.load("path/ply2.ply", function(geometry) {
    geometry.computeVertexNormals();
    material = new THREE.MeshStandardMaterial({ color: 0x0055ff, flatShading: true });
    mesh2 = new THREE.Mesh(geometry, material);

    mesh2.position.x = -0.2;
    mesh2.position.y = -0.02;
    mesh2.position.z = -0.2;
    mesh2.scale.multiplyScalar(0.0006);
    mesh2.castShadow = true;
    mesh2.receiveShadow = true;
  });

  var loader = new THREE.PLYLoader();
  loader.load("ply/Paso_1_mandible.ply", function(geometry) {
    geometry.computeVertexNormals();
    material = new THREE.MeshStandardMaterial({ color: 0x0055ff, flatShading: true });
    mesh1 = new THREE.Mesh(geometry, material);

    mesh1.position.y = -0.2;
    mesh1.position.z = 0.3;
    mesh1.rotation.x = -Math.PI / 2;
    mesh1.scale.multiplyScalar(0.01);
    mesh1.castShadow = true;
    mesh1.receiveShadow = true;
  });
  loader.load("ply/Lucy100k.ply", function(geometry) {
    geometry.computeVertexNormals();
    material = new THREE.MeshStandardMaterial({ color: 0x0055ff, flatShading: true });
    mesh2 = new THREE.Mesh(geometry, material);

    mesh2.position.x = -0.2;
    mesh2.position.y = -0.02;
    mesh2.position.z = -0.2;
    mesh2.scale.multiplyScalar(0.0006);
    mesh2.castShadow = true;
    mesh2.receiveShadow = true;
  });
  singleGeometry.mergeBufferGeometries([mesh1.geometry, mesh2.geometry]);
  material = new THREE.MeshPhongMaterial({ color: 0xff0000 });
  mesh = new THREE.Mesh(singleGeometry, material);
  scene.add(mesh);
}

问题是您在尝试合并之前没有等待 loader 加载网格 mesh1mesh2

我们可以重构它以使用 Promises,这使得处理异步加载变得容易得多。

(我删除了从单个几何体生成网格的代码,因为它显然没有被使用。)

var plyLoader = new THREE.PLYLoader();
function loadAsset(path) {
  return new Promise(resolve => {
    plyLoader.load(path, resolve);
  });
}

var singleGeometry, material, mesh;

Promise.all([
  loadAsset("ply/Paso_1_mandible.ply"),
  loadAsset("ply/Lucy100k.ply"),
])
  .then(geometries => {
    geometries.forEach(geometry => geometry.computeVertexNormals());
    var singleGeometry = new THREE.BufferGeometry();
    singleGeometry.mergeBufferGeometries(geometries);
    material = new THREE.MeshPhongMaterial({ color: 0xff0000 });
    mesh = new THREE.Mesh(singleGeometry, material);
    scene.add(mesh);
  })
  .then(() => {
    animate();
  });