动画Three.js顶点

时间:2018-07-08 04:15:17

标签: animation three.js position vertices

我正在尝试对collada模型中的各个顶点进行动画处理。 顶点可以动画一次,但之后不再动画。

1:加载我的collada

// name obj
var orgVerts = false;
var desVerts = false;
var material = new THREE.MeshBasicMaterial({ color:0x1e5679, wireframe:true });
var loader = new THREE.ColladaLoader();
var nameModel = false;

loader.options.convertUpAxis = true;
loader.load( '3d/name.dae', function ( collada ) {
    nameModel = collada.scene.children[0].children[0];
    nameModel.material = material;
    nameModel.geometry.dynamic = true;
    nameModel.position.set(0,0,0);//x,z,y- if you think in blender dimensions ;)
    nameModel.scale.x = .0035;
    nameModel.scale.y = .0035;
    nameModel.scale.z = .0035;
    scene.add(nameModel);
    orgVerts = nameModel.geometry.vertices; // make a backup of all verts
    genVerts(); // create a new array of random verts
});

2:在我的渲染功能中

var render = function () {
    requestAnimationFrame(render);
    // do stuff
    if(nameModel){
        if(window.globalCurrentSlide != 0){
            for(var r=0; r < nameModel.geometry.vertices.length; r++){
                var vert = desVerts[r]; // loop through all the destination verts
                nameModel.geometry.vertices[r].x = nameModel.geometry.vertices[r].x - (nameModel.geometry.vertices[r].x - vert.x)/20;
                nameModel.geometry.vertices[r].y = nameModel.geometry.vertices[r].y - (nameModel.geometry.vertices[r].y - vert.y)/20;
                nameModel.geometry.vertices[r].z = nameModel.geometry.vertices[r].z - (nameModel.geometry.vertices[r].z - vert.z)/20;
            }
        }else{
            for(var t=0; t < nameModel.geometry.vertices.length; t++){
                var vert2 = orgVerts[t]; // loop through all the original verts
                nameModel.geometry.vertices[t].x = nameModel.geometry.vertices[t].x - (nameModel.geometry.vertices[t].x - vert2.x)/20;
                nameModel.geometry.vertices[t].y = nameModel.geometry.vertices[t].y - (nameModel.geometry.vertices[t].y - vert2.y)/20;
                nameModel.geometry.vertices[t].z = nameModel.geometry.vertices[t].z - (nameModel.geometry.vertices[t].z - vert2.z)/20;
            }
        }
        nameModel.geometry.verticesNeedUpdate = true;
        nameModel.rotation.y += .005;
    }
    renderer.render( scene, camera );
}

window.globalCurrentSlide设置为0开始,一切都很好。如果我将window.globalCurrentSlide更改为1,则所有顶点正确设置动画... 但是当我将window.globalCurrentSlide更改为0时,这些顶点不会设置动画为原始位置。我已经调试了堆,可以100%说两者 desVertsorgVerts都没有变化,而且它们是正确的。有任何想法吗?真让我发疯。

PS:我知道代码可能会被压缩,我只是在玩ATM

1 个答案:

答案 0 :(得分:0)

答案是我的orgVerts只是参考,不是克隆。使用:

var geometry = nameModel.geometry.clone();
orgVerts = geometry.vertices;