首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从PLYLoaders将两个几何图形合并成一个几何图形

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

Stack Overflow用户
提问于 2018-08-11 17:11:48
回答 1查看 82关注 0票数 0

我有两个层文件,我想合并成一个文件,但是对于PLYLoader的PLYLoader方法,我有一个问题,我如何能够同时访问两个加载器的几何?

我加载了这两个层文件,并为新形状定义了新的几何学(BufferGeometry类),但是当I试图访问几何图形属性时,变量mesh1mesh2没有定义。

这是我的代码:

代码语言:javascript
复制
//  ...
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);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-11 17:23:19

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

我们可以重构它来使用应许,这使得处理异步加载变得更加容易。

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

代码语言:javascript
复制
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();
  });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51801982

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档