【发布时间】:2015-06-01 19:21:39
【问题描述】:
我正在使用 Three.js,版本 71。我正在使用 Blender,2.73 版。
我使用 Blender 创建了一个带纹理的 collada 对象(.dae 文件),现在我想将它加载到我的 three.js 场景中。到目前为止,我只能加载从搅拌机导出的没有纹理的模型。
以下是我创建带纹理的 collada 对象的方法:
在搅拌机中,我只是使用默认立方体。使用右侧的设置,我为立方体添加了纹理。这是我放在立方体上的纹理(注意:它是 2048 X 2048,所以它是 2 的幂):
这是渲染模式下立方体的图像,以证明纹理在其上:
以下是我从 Blender 将立方体导出为 collada 时使用的导出设置:
这是我用来尝试加载带纹理的 collada 的一些代码:
var loader = new THREE.ColladaLoader();
var localObject;
loader.options.convertUpAxis = true;
loader.load( './models/test_texture.dae', function ( collada ) {
localObject = collada.scene;
localObject.scale.x = localObject.scale.y = localObject.scale.z = 32;
localObject.updateMatrix();
game.scene.add(localObject);
} );
这是我得到的错误:
[.WebGLRenderingContext]GL ERROR :GL_INVALID_OPERATION : glDrawElements: attempt to access out of range vertices in attribute 2
然后我用谷歌搜索了那个错误信息,有人说我需要计算切线。这是我的尝试以及我得到的错误:
var loader = new THREE.ColladaLoader();
var localObject;
loader.options.convertUpAxis = true;
loader.load( './models/test_texture.dae', function ( collada ) {
localObject = collada.scene;
localObject.scale.x = localObject.scale.y = localObject.scale.z = 32;
localObject.updateMatrix();
for (var i = collada.scene.children.length - 1; i >= 0; i--) {
var child = collada.scene.children[i];
// child.children[0] will give us the THREE.Mesh of the collada
if ( child.colladaId == "Cube" ) {
// ATTEMPT 1: Just tried computing tangets based on answer from neoRiley here: http://stackoverflow.com/questions/21200386/webgl-gl-error-gl-invalid-operation-gldrawelements-attempt-to-access-out-of
// child.children[0].geometry.computeTangents();
// ATTEMPT 2: Got this suggestion from Popov here: http://stackoverflow.com/questions/15717468/three-lod-and-normalmap-shader-fail
// child.children[0].geometry[ 0 ][ 0 ].computeTangents();
// child.children[0].geometry[ 1 ][ 0 ].computeTangents();
// ATTEMPT 3: Tried setting some update flags based on answer from Sayris here: http://stackoverflow.com/questions/13988615/webglrenderingcontext-error-loading-texture-maps
// child.children[0].geometry.buffersNeedUpdate = true;
// child.children[0].geometry.uvsNeedUpdate = true;
// child.children[0].material.needsUpdate = true;
// child.children[0].geometry.computeTangents();
}
};
game.scene.add(localObject);
} );
尝试 1 错误:
Uncaught TypeError: Cannot read property '0' of undefined
// Stack trace
three.js:9935 handleTriangle
three.js:9974 THREE.Geometry.computeTangents
myCode.js:116 (anonymous function)
ColladaLoader.js:204 parse
ColladaLoader.js:84 request.onreadystatechange
尝试 2 错误:
Uncaught TypeError: Cannot read property '0' of undefined
这来自自己的代码。我不认为THREE.Mesh的几何是二维的,但我还是试过了。
尝试 3 错误:(与尝试 1 错误相同)
Uncaught TypeError: Cannot read property '0' of undefined
// Stack trace
three.js:9935 handleTriangle
three.js:9974 THREE.Geometry.computeTangents
myCode.js:116 (anonymous function)
ColladaLoader.js:204 parse
ColladaLoader.js:84 request.onreadystatechange
【问题讨论】:
标签: javascript 3d three.js blender collada