简体   繁体   English

将纹理应用于threejs对象

[英]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. 只是将obj添加到场景中就可以了,但是当我必须设置网格和纹理时,会出现错误。 What should the correct syntax be? 正确的语法应该是什么?

your problem may be that the "obj" returned by the ObjectLoader is actually just a Object3D. 您的问题可能是ObjectLoader返回的“ obj”实际上只是一个Object3D。 The objects containing the actual geometry and materials are children of this "obj". 包含实际几何图形和材料的对象是此“ 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. 另外,请查看MTL加载程序。 OBJ/MTL loader is the usual way to use textured OBJs, as seen in the example: http://threejs.org/examples/#webgl_loader_obj_mtl 如示例所示,OBJ / MTL加载器是使用带纹理的OBJ的常用方法: http ://threejs.org/examples/#webgl_loader_obj_mtl

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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