首页 > 解决方案 > 三JS | 加载 OBJ 数据合并网格

问题描述

在我的 ThreeJS 应用程序中,我从本地服务器加载了一个 .OBJ 文件。例如,我现在想为每个子网格(子组件)赋予不同的颜色。

执行以下操作,ThreeJS 总是为多个网格着色:

var counter = 0;
object.traverse(function (child) {

    if (child instanceof THREE.Mesh) {
        //Also tested with if(counter == 300) or if (child.name == '625')...
        if(child.id == 635){
            var randomColor = '#' + (Math.random().toString(16)+'00000').slice(2,8)
            child.material.color.set(randomColor);
        }
        counter++;
    }
});

结果是:

在此处输入图像描述

然而,在您选择的 3D 模型查看器(我测试过 FreeCAD、Blender、Autodesk 和 Creo)中,每个组件似乎都是单一的,因为它应该是......:

在此处输入图像描述

这是怎么发生的?或者是否有我没有看到的导出选项?完整代码:https ://www.toptal.com/developers/hastebin/fosugibuni.php (不是很壮观)...

标签: javascripttypescriptthree.js3dwavefront

解决方案


推荐阅读