You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Forge Viewer导出场景独立元素为GLTF时输出为空的问题咨询

解决方案

你遇到的空导出问题是Forge Viewer的场景结构、自定义对象与原生Three.js GLTFExporter不兼容导致的,按以下步骤修复:

  • 匹配Three.js版本
    Forge Viewer v7版本内置的Three.js内核为0.123.x,你当前引入的r126版本GLTFExporter存在API兼容问题,替换为对应版本的导出器脚本即可。
  • 不要直接导出Viewer原生场景
    viewer.impl.scene存储的是Viewer内置的UI、辅助线对象,模型数据全部存储在viewer.impl.sceneAfter中,且该场景下包含大量Viewer内部的自定义渲染对象,原生GLTFExporter无法识别,需要自行构建临时导出场景。
  • 转换Forge自定义对象为标准Three.js对象
    Forge的几何体、材质、实例化片段都是自定义封装的,需要手动提取成标准Three.js Mesh再导出,参考代码如下:
const exporter = new THREE.GLTFExporter();
// 导出指定dbid的模型元素
const exportGLTFByDbId = (targetDbIds) => {
  // 新建临时导出场景
  const exportScene = new THREE.Scene();
  const instanceTree = viewer.model.getData().instanceTree;
  
  targetDbIds.forEach(dbid => {
    // 获取dbid对应的所有fragment id
    const fragIds = [];
    instanceTree.enumNodeFragments(dbid, (fragId) => {
      fragIds.push(fragId);
    });
    
    fragIds.forEach(fragId => {
      // 获取fragment对应的原生mesh
      const fragProxy = viewer.impl.getFragmentProxy(viewer.model, fragId);
      const mesh = viewer.impl.getRenderProxy(viewer.model, fragId);
      
      // 复制几何体、材质、世界矩阵,生成标准Three.js Mesh
      const exportGeometry = mesh.geometry.clone();
      const exportMaterial = mesh.material.clone();
      const exportMesh = new THREE.Mesh(exportGeometry, exportMaterial);
      
      // 应用fragment的世界矩阵
      fragProxy.getWorldMatrix(exportMesh.matrix);
      exportMesh.matrix.decompose(exportMesh.position, exportMesh.quaternion, exportMesh.scale);
      
      exportScene.add(exportMesh);
    });
  });
  
  // 导出临时场景
  exporter.parse(exportScene, (gltf) => {
    const output = JSON.stringify(gltf, null, 2);
    console.log(output);
    // saveString(output, 'model.gltf');
  }, { trs: true, binary: false });
}

// 调用示例:导出当前选中的元素
const selectedDbIds = viewer.getSelection();
if(selectedDbIds.length) {
  exportGLTFByDbId(selectedDbIds);
}
  • 额外注意点
    若导出的模型材质丢失,需要手动将Forge的自定义材质属性映射到Three.js标准MeshStandardMaterial的对应属性上,比如金属度、粗糙度、贴图等参数。

内容的提问来源于stack exchange,提问作者Abd-ElrAhman A Abd-ElAleem

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 00:36:04