简体   繁体   中英

Applying texture to threejs object

I'm having trouble applying a texture to an object I exported. My code looks like this:

var loader = new THREE.ObjectLoader();
var texture = THREE.ImageUtils.loadTexture('models/mountain/mountain.png');

loader.load("models/mountain/mountain.json", function (obj) {

  var material = new THREE.MeshPhongMaterial({
    map: texture
  });
  mesh = new THREE.Mesh( obj, material );

  scene.add( mesh );
});

Just adding the obj to the scene works fine, but when I have to set a mesh and texture I get an error. What should the correct syntax be?

your problem may be that the "obj" returned by the ObjectLoader is actually just a Object3D. The objects containing the actual geometry and materials are children of this "obj".

So to change material you need to:

for(var i = 0; i < obj.children.length; i++)
{
    obj.children[i].material = new THREE.PhongMaterial...
}

Also, please look into the MTL loader. OBJ/MTL loader is the usual way to use textured OBJs, as seen in the example: http://threejs.org/examples/#webgl_loader_obj_mtl

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM