首页 > 解决方案 > 尝试将自定义网格添加到离线查看器

问题描述

我尝试将自定义 sphereMesh 添加到离线查看器。我看到这可以通过查看器的在线版本来完成。但是我得到一个未捕获的 TypeError: Cannot read property 'Add' of undefined 如果我在我的查看器中尝试这个。

<div id="MyViewerDiv"></div>

    <script>
           var myViewerDiv = document.getElementById('MyViewerDiv');
            var viewer = new Autodesk.Viewing.Private.GuiViewer3D(myViewerDiv);
      var options = {
          'env' : 'Local',
         'document' : './_3D_ 197440/_3D_.svf'
      };
    
      Autodesk.Viewing.Initializer(options, function() {
        viewer.start(options.document, options);
      });

     var geom = new THREE.SphereGeometry(1000, 8, 8);
     var material = new THREE.MeshBasicMaterial({ color: 0xff0000 });
     var sphereMesh = new THREE.Mesh(geom, material);
    
     sphereMesh.position.set(0, 0, 0);
     viewer.impl.scene.add(sphereMesh);
 
    </script>

标签: javascriptautodesk-forgeautodesk-viewer

解决方案


在添加自定义几何体的方式方面,在线查看器和离线查看器没有区别。在您的情况下,您遇到类似于“竞争条件”的情况,您尝试在初始化时使用资源:有时 init 进程首先完成 => 成功,有时它稍后完成 => 出错。

为了解决这个问题,有两种方法:

  1. 如文档(推荐)中所述,将您的代码添加到扩展中并加载扩展。示例:https ://apprentice3d.github.io/SD226781-Samples/02.html
  2. 将您的自定义网格代码移动到文档加载回调函数的主体中。示例:检查https://s3.amazonaws.com/sample-collection/GiroWatch_Latest.html的来源

推荐阅读