I add a sprite to my scene after the fact. I want the sprite to track with a specific Object3D. I specified onBeforeRender and onAfterRender functions to the mesh associated with my Object3D. Then I play an animation and my Object3D moves, what changes should I be looking for in the mesh and/or Object3D to tell me how to update the sprite’s location.
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;
}
}
</code>
After loading my glTF, I run through all the animations looking for animations that move objects (change '.position'). I tried added callbacks directly on those Object3D but the callback is not triggered. So, I found the 1st down line 'mesh' and added the callback there. The call backs are triggered. But the original Object3D that is referenced in the animation never changes the values in it's position. - What am I missing?