在 Three.js 中为 OBJLoader 模型分配材料 [英] Assigning materials to an OBJLoader model in three.js

查看:37
本文介绍了在 Three.js 中为 OBJLoader 模型分配材料的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码导入了一个 OBJ 模型:

I've imported an OBJ model with the following code:

var loader = new THREE.OBJLoader();
loader.addEventListener('load', function (geometry) {
    object = geometry.content;
    scene.add(object);
});
loader.load('ship.obj');

它工作正常,但每当我尝试向其添加材质时,它要么没有效果,要么模型消失.我以为我可以这样做:

It works fine, but whenever I try to add a material to it, it either has no effect or the model disappears. I assumed I could do it like this:

var ship = new THREE.Mesh(object, material);

但这行不通.有没有人知道这样做的方法,或者是否有可能?我也尝试过使用 OBJMTLLoader,但它只会增加复杂性,同时仍然不允许我更改材料.

But that doesn't work. does anyone know of a way to do this, or if it's even possible? I've tried using OBJMTLLoader too, but it just adds complication while still not allowing me to change the material.

推荐答案

此答案现已过时.相反,请参阅@mightypile 的答案.

This answer is now outdated. Instead, see the answer by @mightypile.

假设你已经正确定义了material,试试这个:

Assuming you have properly defined material, try this:

loader.addEventListener( 'load', function ( event ) {

    var object = event.content;

    object.traverse( function ( child ) {

        if ( child instanceof THREE.Mesh ) {

            child.material = material;

        }

    } );

    scene.add( object );

});

这篇关于在 Three.js 中为 OBJLoader 模型分配材料的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆