我正在尝试通过以下包装函数定义我从OBJLoader加载的网格物料:
function applyTexture(src){
var texture = new THREE.Texture();
var loader = new THREE.ImageLoader();
loader.addEventListener( 'load', function ( event ) {
texture.image = event.content;
texture.needsUpdate = true;
// find the meshes from the loaded OBJ and apply the texture to it.
object.traverse( function ( child ) {
if ( child instanceof THREE.Mesh ) {
if(child.name.indexOf("Lens") < 0){
child.dynamic = true;
child.material = new THREE.MeshLambertMaterial( { color: 0xdddddd, shading: THREE.FlatShading, map : texture } );
// also tried:
//child.material = new THREE.MeshPhongMaterial( { color: 0x000000, specular: 0x666666, emissive: 0x000000, ambient: 0x000000, shininess: 10, shading: THREE.SmoothShading, map : texture} );
// and:
//child.material = new THREE.MeshBasicMaterial({map : texture});
child.material.map = texture; // won't throw the WebGL Warning, but won't show the texture either;
} else {
// works just fine.
child.material = new THREE.MeshPhongMaterial( { color: 0x000000, specular: 0x666666, emissive: 0x000011, ambient: 0x000000, shininess: 10, shading: THREE.SmoothShading, opacity: 0.6, transparent: true } );
}
}
});
});
loader.load( src );
}
当纹理加载并且是时候将材质应用到网格物体时,我开始在控制台上收到以下警告:
.WebGLRenderingContext: GL ERROR :GL_INVALID_OPERATION : glDrawElements: attempt to access out of range vertices in attribute 0
并且网格本身消失。
我在这里做错了什么?
正如@WestLangley指出的那样:在渲染完东西之后,永远不要尝试应用纹理/材质。在将对象渲染到场景之前创建材质,然后使用以下命令更改它们:
obj.material.map = texture
答案 0 :(得分:12)
使用WebGLRenderer
,在网格渲染一次后,无法从没有纹理的材质切换到具有纹理的材质。这是因为,如果没有初始纹理,几何图形将没有必要的烘焙WebGL UV缓冲区。
解决方法是从具有简单白色纹理的材料开始。
更新:或者,你可以以无纹理材质开始,然后在添加纹理时设置以下标志:
material.needsUpdate = true;
geometry.buffersNeedUpdate = true;
geometry.uvsNeedUpdate = true;
three.js r.58
答案 1 :(得分:2)
从搅拌机加载场景时我也遇到了这个错误。对我来说,当我想要打开纹理的每个网格展开uv时,问题就解决了。