我正在尝试在 OBJ 对象上放置一个带有动画位移的基本着色器。着色器在 CubeBufferGeometry 上工作正常,但我无法让它与 OBJLoader 一起工作。
// OBJ Loader
var loader = new THREE.OBJLoader();
loader.load( "obj/statue.obj", function ( group ) {
geometry = group.children[ 0 ].geometry;
geometry.attributes.uv2 = geometry.attributes.uv;
geometry.center();
mesh = new THREE.Mesh( geometry, material );
mesh.scale.multiplyScalar( 15 );
mesh.position.y = -700;
mesh.traverse( function( node ) {
if( node.material ) {
node.material.side = THREE.DoubleSide;
}
});
mesh.traverse(function (child) {
if (child instanceof THREE.Mesh) {
child.geometry.computeFaceNormals();
}
});
scene.add( mesh );
// Vertex Displacement
vertexDisplacement = new Float32Array(geometry.attributes.position.count);
for (var i = 0; i < vertexDisplacement.length; i += 1){
vertexDisplacement[i] = Math.sin(i);
}
geometry.addAttribute('vertexDisplacement', new THREE.BufferAttribute(vertexDisplacement, 1));
} );
但是,在我的渲染/动画函数中,我的网格变量是未定义的(我已经将 var mesh 作为全局变量)。我认为这是因为我的网格也在 OBJLoader 函数中定义?
我得到:'类型错误:网格未定义'
这是我的渲染/动画功能:
var delta = 0;
function animate() {
requestAnimationFrame( animate );
mesh.rotation.x += 0.005;
mesh.rotation.y += 0.01;
delta +=0.1;
mesh.material.uniforms.delta.value = 0.5 + Math.sin(delta) * 0.5;
for (var i = 0; i < vertexDisplacement.length; i += 1) {
vertexDisplacement[i] = 0.5 + Math.sin(i * delta) * 0.25;
}
mesh.geometry.attributes.vertexDisplacement.needsUpdate = true;
renderer.render( scene, camera );
}