Jay*_*tti 6 texture-mapping three.js
我正在尝试通过以下包装函数定义从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 );
}
Run Code Online (Sandbox Code Playgroud)
当纹理加载并且是时候将材质应用于网格物体时,我开始在控制台上收到以下警告:
.WebGLRenderingContext: GL ERROR :GL_INVALID_OPERATION : glDrawElements: attempt to access out of range vertices in attribute 0
Run Code Online (Sandbox Code Playgroud)
并且网格本身消失了.
我在这做错了什么?
正如@WestLangley指出的那样:在渲染完东西之后,永远不要尝试应用纹理/材质.在将对象渲染到场景之前创建材质,然后使用以下命令更改它们:
obj.material.map = texture
Run Code Online (Sandbox Code Playgroud)
Wes*_*ley 12
在WebGLRenderer网格渲染一次后,您无法从没有纹理的材质切换到具有纹理的材质.这是因为,如果没有初始纹理,几何体将不具有必要的烘焙WebGL UV缓冲区.
解决方法是从具有简单白色纹理的材料开始.
更新:或者,您可以从无纹理材质开始,然后在添加纹理时设置以下标志:
material.needsUpdate = true;
geometry.buffersNeedUpdate = true;
geometry.uvsNeedUpdate = true;
Run Code Online (Sandbox Code Playgroud)
three.js r.58
| 归档时间: |
|
| 查看次数: |
3926 次 |
| 最近记录: |