如何在三个js中跟踪动画对象的运动?


for (let ai = 0, al = gltf.animations.length; ai < al; ai++ ) {

for (let ti = 0, tl = gltf.animations[ai].tracks.length; ti < tl; ti++ ) {
let parts = gltf.animations[ai].tracks[ti].name.split('.'); // name is 'uuid'.position
if ( parts && parts.length > 1 && parts[1] == 'position') {
var varObj = scene.getObjectByProperty('uuid', parts[0]);
// find the 1st downstream mesh to add callbacks to..
let c = varObj.children[0];
let d = 1;
while ( !c.isMesh ) {
c = c.children[0];
d = d + 1;
}
c.onBeforeRender = function() {
// go back upstream to find the parent whos position is changing..
let p = this.parent;
let x = this.d - 1;
while ( x ) {
p = p.parent;
x = x - 1;
}
this.b4pos.set(p.position.x, p.position.y, p.position.z);
};
c.onAfterRender = function() {
// go back upstream to find the parent whos position is changing..
let p = this.parent;
let x = this.d - 1;
while ( x ) {
p = p.parent;
x = x - 1;
}

let delta = (p.position.x - this.b4pos.x) +
(p.position.y - this.b4pos.y) +
(p.position.z - this.b4pos.z);
if ( delta != 0 ) {
console.log('Object3D associated with mesh is moved');
}
};
c.b4pos = new THREE.Vector3(0, 0, 0);
c.d = d;
}
}

}

加载glTF后,我运行所有动画,寻找移动对象的动画(改变'.position')。我尝试直接在那些Object3D上添加回调,但回调没有触发。所以,我找到了第一个下行线'mesh'并在那里添加了回调。触发回调。但是在动画中引用的原始Object3D永远不会改变其位置中的值。-我错过了什么?

明白了。位置在。onbeforerender/。onafterrender之外改变。b4pos只需要保存一次,即第一次调用. onbeforerender时。从那时起,比较新位置与。onafterrender中的b4pos。

最新更新