繁体   English   中英

web-ifc-three中如何访问IFC项目的BufferGeometry

[英]How to access the BufferGeometry of IFC items in web-ifc-three

我正在尝试获取与我拥有的 expressId 相对应的元素的几何形状,即 BufferGeometry object(不是通过挑选)。

基本上我在问如何遍历 IFC model 并将每个 object 作为单独的 OBJ 导出。

我会注意到我有逆向工程代码来实现 package 的某些版本,但它使用未记录的功能,所以它自然会在以后的版本中中断(代码也是 colors 根据材料的颜色几何所以我不需要一个mtl):

不要复制此代码,它将不起作用

Object.values(bimModel.ifcManager.state.models[bimModel.modelID].items).forEach(type => {
  Object.entries(type.geometries).forEach(([id, geometry]) => {
    const properties = bimModel.getItemProperties(Number(id))
    const numVertices = geometry.getAttribute('position').count
    const color = type.material.color.toArray().map(x => x * 255)
    const vertexColors = new Uint8Array(Array.from({ length: numVertices }, () => color).flat())
    geometry.setAttribute('color', new BufferAttribute(vertexColors, 3, true))
  })
})

使它成为 OBJ 而不是 GLTF 的任何具体原因? 我知道您可以将 ifc model 制作成几个 GLTF 文件。

如果您不介意这是 GLTF 结果,我会跟进答案。 谢谢。

这正是我们将模型导出到 glTF 所做的。 基本工作流程是:

  • 确定您要导出的 IFC 类别。
  • 获取每个类别的所有项目。
  • 为每个项目重建网格。
  • 使用您选择的 Three.js 导出器导出网格。

让我们看一个从墙上获取所有网格的基本示例。 该过程并不像将每个 IFC 项目作为单独的网格那样简单,但这是使绘制调用最少的代价(否则,浏览器甚至无法支持中等大小的 IFC 文件):

import { IFCWALLSTANDARDCASE } from 'web-ifc';

async function getAllWallMeshes() {

  // Get all the IDs of the walls

  const wallsIDs = manager.getAllItemsOfType(0, IFCWALL, false);
  const meshes = [];
  const customID = 'temp-gltf-subset';

  for(const wallID of wallsIDs) {
    const coordinates = [];
    const expressIDs = [];
    const newIndices = [];

    const alreadySaved = new Map();

    // Get the subset for the wall

    const subset = viewer.IFC.loader.ifcManager.createSubset({
      ids: [wallID],
        modelID,
        removePrevious: true,
        customID
     });

    // Subsets have their own index, but share the BufferAttributes 
    // with the original geometry, so we need to rebuild a new 
    // geometry with this index

    const positionAttr = subset.geometry.attributes.position;
    const expressIDAttr = subset.geometry.attributes.expressID;

    const newGroups = subset.geometry.groups
         .filter((group) => group.count !== 0);

    const newMaterials = [];
    const prevMaterials = subset.material;
    let newMaterialIndex = 0;

    newGroups.forEach((group) => {
      newMaterials.push(prevMaterials[group.materialIndex]);
      group.materialIndex = newMaterialIndex++;
    });

    let newIndex = 0;
    for (let i = 0; i < subset.geometry.index.count; i++) {
      const index = subset.geometry.index.array[i];

      if (!alreadySaved.has(index)) {
        coordinates.push(positionAttr.array[3 * index]);
        coordinates.push(positionAttr.array[3 * index + 1]);
        coordinates.push(positionAttr.array[3 * index + 2]);

        expressIDs.push(expressIDAttr.getX(index));
        alreadySaved.set(index, newIndex++);
      }

      const saved = alreadySaved.get(index);
      newIndices.push(saved);
    }

    const geometryToExport = new BufferGeometry();
    const newVerticesAttr = new BufferAttribute(Float32Array.from(coordinates), 3);
    const newExpressIDAttr = new BufferAttribute(Uint32Array.from(expressIDs), 1);

    geometryToExport.setAttribute('position', newVerticesAttr);
    geometryToExport.setAttribute('expressID', newExpressIDAttr);
    geometryToExport.setIndex(newIndices);
    geometryToExport.groups = newGroups;
    geometryToExport.computeVertexNormals();

    const mesh = new Mesh(geometryToExport, newMaterials);
    meshes.push(mesh);
  }
 
  viewer.IFC.loader.ifcManager.removeSubset(modelID, undefined, customID);
  return meshes;
}

暂无
暂无

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

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