123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384 |
- import defaultValue from "../../Core/defaultValue.js";
- import defined from "../../Core/defined.js";
- import Event from "../../Core/Event.js";
- import JulianDate from "../../Core/JulianDate.js";
- import ModelAnimationLoop from ".././ModelAnimationLoop.js";
- import ModelAnimationState from ".././ModelAnimationState.js";
- import ModelExperimentalAnimationChannel from "../ModelExperimental/ModelExperimentalAnimationChannel.js";
- /**
- * An active animation derived from a glTF asset. An active animation is an
- * animation that is either currently playing or scheduled to be played due to
- * being added to a model's {@link ModelExperimentalAnimationCollection}. An active animation
- * is an instance of an animation; for example, there can be multiple active
- * animations for the same glTF animation, each with a different start time.
- * <p>
- * Create this by calling {@link ModelExperimentalAnimationCollection#add}.
- * </p>
- *
- * @alias ModelExperimentalAnimation
- * @internalConstructor
- * @class
- *
- * @see ModelExperimentalAnimationCollection#add
- */
- function ModelExperimentalAnimation(model, animation, options) {
- this._animation = animation;
- this._name = animation.name;
- this._runtimeChannels = undefined;
- this._startTime = JulianDate.clone(options.startTime);
- this._delay = defaultValue(options.delay, 0.0); // in seconds
- this._stopTime = JulianDate.clone(options.stopTime);
- /**
- * When <code>true</code>, the animation is removed after it stops playing.
- * This is slightly more efficient that not removing it, but if, for example,
- * time is reversed, the animation is not played again.
- *
- * @type {Boolean}
- * @default false
- */
- this.removeOnStop = defaultValue(options.removeOnStop, false);
- this._multiplier = defaultValue(options.multiplier, 1.0);
- this._reverse = defaultValue(options.reverse, false);
- this._loop = defaultValue(options.loop, ModelAnimationLoop.NONE);
- /**
- * The event fired when this animation is started. This can be used, for
- * example, to play a sound or start a particle system, when the animation starts.
- * <p>
- * This event is fired at the end of the frame after the scene is rendered.
- * </p>
- *
- * @type {Event}
- * @default new Event()
- *
- * @example
- * animation.start.addEventListener(function(model, animation) {
- * console.log('Animation started: ' + animation.name);
- * });
- */
- this.start = new Event();
- /**
- * The event fired when on each frame when this animation is updated. The
- * current time of the animation, relative to the glTF animation time span, is
- * passed to the event, which allows, for example, starting new animations at a
- * specific time relative to a playing animation.
- * <p>
- * This event is fired at the end of the frame after the scene is rendered.
- * </p>
- *
- * @type {Event}
- * @default new Event()
- *
- * @example
- * animation.update.addEventListener(function(model, animation, time) {
- * console.log('Animation updated: ' + animation.name + '. glTF animation time: ' + time);
- * });
- */
- this.update = new Event();
- /**
- * The event fired when this animation is stopped. This can be used, for
- * example, to play a sound or start a particle system, when the animation stops.
- * <p>
- * This event is fired at the end of the frame after the scene is rendered.
- * </p>
- *
- * @type {Event}
- * @default new Event()
- *
- * @example
- * animation.stop.addEventListener(function(model, animation) {
- * console.log('Animation stopped: ' + animation.name);
- * });
- */
- this.stop = new Event();
- this._state = ModelAnimationState.STOPPED;
- // Set during animation update
- this._computedStartTime = undefined;
- this._duration = undefined;
- // To avoid allocations in ModelExperimentalAnimationCollection.update
- const that = this;
- this._raiseStartEvent = function () {
- that.start.raiseEvent(model, that);
- };
- this._updateEventTime = 0.0;
- this._raiseUpdateEvent = function () {
- that.update.raiseEvent(model, that, that._updateEventTime);
- };
- this._raiseStopEvent = function () {
- that.stop.raiseEvent(model, that);
- };
- this._model = model;
- this._localStartTime = undefined;
- this._localStopTime = undefined;
- initialize(this);
- }
- Object.defineProperties(ModelExperimentalAnimation.prototype, {
- /**
- * The glTF animation.
- *
- * @memberof ModelExperimentalAnimation.prototype
- *
- * @type {ModelComponents.Animation}
- * @readonly
- *
- * @private
- */
- animation: {
- get: function () {
- return this._animation;
- },
- },
- /**
- * The name that identifies this animation in the model, if it exists.
- *
- * @memberof ModelExperimentalAnimation.prototype
- *
- * @type {String}
- * @readonly
- */
- name: {
- get: function () {
- return this._name;
- },
- },
- /**
- * The runtime animation channels for this animation.
- *
- * @memberof ModelExperimentalAnimation.prototype
- *
- * @type {ModelExperimentalAnimationChannel[]}
- * @readonly
- *
- * @private
- */
- runtimeChannels: {
- get: function () {
- return this._runtimeChannels;
- },
- },
- /**
- * The {@link ModelExperimental} that owns this animation.
- *
- * @memberof ModelExperimentalAnimation.prototype
- *
- * @type {ModelExperimental}
- * @readonly
- *
- * @private
- */
- model: {
- get: function () {
- return this._model;
- },
- },
- /**
- * The starting point of the animation in local animation time. This is the minimum
- * time value across all of the keyframes belonging to this animation.
- *
- * @memberof ModelExperimentalAnimation.prototype
- *
- * @type {Number}
- * @readonly
- *
- * @private
- */
- localStartTime: {
- get: function () {
- return this._localStartTime;
- },
- },
- /**
- * The stopping point of the animation in local animation time. This is the maximum
- * time value across all of the keyframes belonging to this animation.
- *
- * @memberof ModelExperimentalAnimation.prototype
- *
- * @type {Number}
- * @readonly
- *
- * @private
- */
- localStopTime: {
- get: function () {
- return this._localStopTime;
- },
- },
- /**
- * The scene time to start playing this animation. When this is <code>undefined</code>,
- * the animation starts at the next frame.
- *
- * @memberof ModelExperimentalAnimation.prototype
- *
- * @type {JulianDate}
- * @readonly
- *
- * @default undefined
- */
- startTime: {
- get: function () {
- return this._startTime;
- },
- },
- /**
- * The delay, in seconds, from {@link ModelExperimentalAnimation#startTime} to start playing.
- *
- * @memberof ModelExperimentalAnimation.prototype
- *
- * @type {Number}
- * @readonly
- *
- * @default undefined
- */
- delay: {
- get: function () {
- return this._delay;
- },
- },
- /**
- * The scene time to stop playing this animation. When this is <code>undefined</code>,
- * the animation is played for its full duration and perhaps repeated depending on
- * {@link ModelExperimentalAnimation#loop}.
- *
- * @memberof ModelExperimentalAnimation.prototype
- *
- * @type {JulianDate}
- * @readonly
- *
- * @default undefined
- */
- stopTime: {
- get: function () {
- return this._stopTime;
- },
- },
- /**
- * Values greater than <code>1.0</code> increase the speed that the animation is played relative
- * to the scene clock speed; values less than <code>1.0</code> decrease the speed. A value of
- * <code>1.0</code> plays the animation at the speed in the glTF animation mapped to the scene
- * clock speed. For example, if the scene is played at 2x real-time, a two-second glTF animation
- * will play in one second even if <code>multiplier</code> is <code>1.0</code>.
- *
- * @memberof ModelExperimentalAnimation.prototype
- *
- * @type {Number}
- * @readonly
- *
- * @default 1.0
- */
- multiplier: {
- get: function () {
- return this._multiplier;
- },
- },
- /**
- * When <code>true</code>, the animation is played in reverse.
- *
- * @memberof ModelExperimentalAnimation.prototype
- *
- * @type {Boolean}
- * @readonly
- *
- * @default false
- */
- reverse: {
- get: function () {
- return this._reverse;
- },
- },
- /**
- * Determines if and how the animation is looped.
- *
- * @memberof ModelExperimentalAnimation.prototype
- *
- * @type {ModelAnimationLoop}
- * @readonly
- *
- * @default {@link ModelAnimationLoop.NONE}
- */
- loop: {
- get: function () {
- return this._loop;
- },
- },
- });
- function initialize(runtimeAnimation) {
- let localStartTime = Number.MAX_VALUE;
- let localStopTime = -Number.MAX_VALUE;
- const sceneGraph = runtimeAnimation._model.sceneGraph;
- const animation = runtimeAnimation._animation;
- const channels = animation.channels;
- const length = channels.length;
- const runtimeChannels = [];
- for (let i = 0; i < length; i++) {
- const channel = channels[i];
- const target = channel.target;
- // Ignore this channel if the target is invalid, i.e. if the node
- // it references doesn't exist.
- if (!defined(target)) {
- continue;
- }
- const nodeIndex = target.node.index;
- const runtimeNode = sceneGraph._runtimeNodes[nodeIndex];
- const runtimeChannel = new ModelExperimentalAnimationChannel({
- channel: channel,
- runtimeAnimation: runtimeAnimation,
- runtimeNode: runtimeNode,
- });
- const times = channel.sampler.input;
- localStartTime = Math.min(localStartTime, times[0]);
- localStopTime = Math.max(localStopTime, times[times.length - 1]);
- runtimeChannels.push(runtimeChannel);
- }
- runtimeAnimation._runtimeChannels = runtimeChannels;
- runtimeAnimation._localStartTime = localStartTime;
- runtimeAnimation._localStopTime = localStopTime;
- }
- /**
- * Evaluate all animation channels to advance this animation.
- *
- * @param {Number} time The local animation time.
- *
- * @private
- */
- ModelExperimentalAnimation.prototype.animate = function (time) {
- const runtimeChannels = this._runtimeChannels;
- const length = runtimeChannels.length;
- for (let i = 0; i < length; i++) {
- runtimeChannels[i].animate(time);
- }
- };
- export default ModelExperimentalAnimation;
|