When using THREE.ObjectLoader, if the model has more than one texture, how do I get them to load properly?

笑着哭i 提交于 2019-12-23 01:28:20

问题


I have exported a model as a Three.js scene. I load it using ObjectLoader. It seems to load fine, but the textures are not applied. When I look at the json file, there are materials listed. Here is the code I have tried.

    loader.load( "MyModel.json", function(obj){

        var materialsFromFile = new THREE.MeshFaceMaterial(obj.materials);

        // create our mesh with the loaded geometry and materials
        mesh = new THREE.Mesh(obj.geometry, materialsFromFile);

        scene.add(mesh);

    });

UPDATE: I have a fix for this, perhaps someone knows a better way? What I do is: first I traverse the model after loading it

    obj.traverse(function(child){ initChild(child); });

In initChild(child) I do this:

    if(child.material != null)
    {
       var childName = child.material.name;
       child.material = new THREE.MeshPhongMaterial();
       child.material.name = childName;
       AssignMap(child.material);
    }

In AssignMap(material) I first load the textures, then assign them based on the material name:

    var texture_metal = new THREE.ImageUtils.loadTexture("media/texture_metal.jpg");
    var texture_glass = new THREE.ImageUtils.loadTexture("media/texture_glass.jpg");

    if(material.name == "metal texture")
        material.map = texture_metal;
    if(material.name == "glass texture")
        material.map = texture_glass; 

Have I gone mad? Is this is reasonable solution? I had looked all over and couldn't find much regarding using THREE.ObjectLoader() and getting mutiple textures to load correctly.

来源:https://stackoverflow.com/questions/25060317/when-using-three-objectloader-if-the-model-has-more-than-one-texture-how-do-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!