nanogl-gltf
Version:
46 lines (45 loc) • 1.53 kB
JavaScript
import GltfTypes from '../types/GltfTypes';
/**
* The Animation element contains the data to animate a node, linking with samplers and channels.
*/
export default class Animation {
constructor() {
this.gltftype = GltfTypes.ANIMATION;
/**
* Duration of the animation
*/
this.duration = 0;
}
/**
* Parse the Animation data, creates the samplers and channels.
*
* Is async as it needs to wait for all the samplers and channels to be created.
* @param gltfLoader GLTFLoader to use
* @param data Data to parse
*/
async parse(gltfLoader, data) {
const samplerPromises = data.samplers.map((data) => gltfLoader._loadElement(data));
this.samplers = await Promise.all(samplerPromises);
for (const sampler of this.samplers) {
this.duration = Math.max(sampler.maxTime);
}
const channelPromises = data.channels.map((data) => gltfLoader._loadElement(data));
this.channels = await Promise.all(channelPromises);
}
/**
* Evaluate the animation at a given time, updating the nodes properties.
* It evaluates each channel, which in turn evaluates the sampler and apply the value to the node.
* @param t Time to evaluate the animation at
*/
evaluate(t) {
for (const channel of this.channels) {
channel.evaluate(t);
}
}
getChannel(i) {
return this.channels[i];
}
getSampler(i) {
return this.samplers[i];
}
}