Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // these are your keyframes, in a format compatible with THREE.Vector3.
- // Please note that the time `t` is expected in milliseconds here.
- // (must have properties named x, y and z - otherwise the copy below doesn't work)
- const keyframes = [
- {t: 0, x: 318, y: 24, z: 3},
- {t: 25, x: 318, y: 24, z: 3},
- // ... and so on
- ];
- // find a pair of keyframes [a, b] such that `a.t < t` and `b.t > t`.
- // In other words, find the previous and next keyframe given the
- // specific time `t`. If no previous or next keyframes is found, null
- // is returned instead.
- function findNearestKeyframes(t) {
- let prevKeyframe = null;
- for (let i = 0; i < keyframes.length; i++) {
- if (keyframes[i].t > t) {
- return [prevKeyframe, keyframes[i]];
- }
- prevKeyframe = keyframes[i];
- }
- return [prevKeyframe, null];
- }
- const tmpV3 = new THREE.Vector3();
- function loop(t) {
- const [prevKeyframe, nextKeyframe] = findNearestKeyframes(t);
- // (...not handling cases where there is no prev or next here)
- // compute the progress of time between the two keyframes
- // (0 when t === prevKeyframe.t and 1 when t === nextKeyframe.t)
- let progress = (t - prevKeyframe.t) / (nextKeyframe.t - prevKeyframe.t);
- // copy position from previous keyframe, and interpolate towards the
- // next keyframe linearly
- tmpV3.copy(nextKeyframe);
- someObject.position
- .copy(prevKeyframe)
- .lerp(tmpV3, progress);
- // (...render scene)
- requestAnimationFrame(loop);
- }
- // start the animation-loop
- requestAnimationFrame(loop);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement