问题
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