Autodesk Forge如何对Buffer Geometry对象分组?SceneBuilder添加Group无显示
问题原因
Autodesk.Viewing.SceneBuilder扩展提供的modelBuilder.addMesh()方法仅支持接收单个THREE.Mesh实例作为参数,不支持直接传入THREE.Group对象,这是代码运行后Viewer无内容显示的核心原因。
解决方案
如果要实现对象分组管理、支持后续整组交互操作,可以按以下方式调整:
- 仍可创建
THREE.Group实例做本地对象的统一维护,仅在调用addMesh时遍历传入组内的单个Mesh对象 - 给同组的所有Mesh添加统一的自定义标识属性(比如
groupId),后续交互时可以通过该属性识别是否属于同一分组,执行整组移动、显隐、选中响应等操作 - 手动给设置了自定义matrix的Mesh关闭自动矩阵更新,避免自定义矩阵被Three.js默认逻辑覆盖
修正后的代码示例
this.sceneBuilder = await this.viewer.loadExtension( "Autodesk.Viewing.SceneBuilder" ); this.modelBuilder = await this.sceneBuilder.addNewModel({ modelNameOverride: "Beacon", conserveMemory: false, }); const sphereGeometry = new THREE.BufferGeometry().fromGeometry( new THREE.SphereGeometry(5, 8, 8) ); const sphereMaterial = new THREE.MeshPhongMaterial({ color: new THREE.Color(0.647, 0.165, 0.165), }); this.meshBeacon = new THREE.Mesh(sphereGeometry, sphereMaterial); this.meshBeacon2 = new THREE.Mesh(sphereGeometry, sphereMaterial); this.meshBeacon.matrix = new THREE.Matrix4().compose( position1, quaternion, scale ); this.meshBeacon.matrixAutoUpdate = false; // 关闭自动矩阵更新,避免自定义矩阵被覆盖 this.meshBeacon2.matrix = new THREE.Matrix4().compose( position2, quaternion, scale ); this.meshBeacon2.matrixAutoUpdate = false; // 关闭自动矩阵更新,避免自定义矩阵被覆盖 this.group = new THREE.Group(); this.group.add(this.meshBeacon); this.group.add(this.meshBeacon2); // 给组内所有Mesh添加统一分组标识,用于后续交互判断 this.group.traverse(child => { if(child.isMesh) { child.groupId = 666666; child.dbId = 666666; } }); console.log(this.group); // 逐个添加组内的Mesh对象到modelBuilder this.group.children.forEach(mesh => { if(mesh.isMesh) { this.modelBuilder.addMesh(mesh); } });
交互实现说明
后续做交互时,比如监听Viewer的点击事件,拿到选中Mesh的groupId,就可以匹配到对应的Group实例,执行整组的变换、样式修改等操作,和直接操作Group的效果完全一致。
内容的提问来源于stack exchange,提问作者surya kumara
相关产品推荐
相关产品推荐

