123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503 |
- import defaultValue from "../Core/defaultValue.js";
- import defined from "../Core/defined.js";
- import DeveloperError from "../Core/DeveloperError.js";
- import Event from "../Core/Event.js";
- import JulianDate from "../Core/JulianDate.js";
- import CesiumMath from "../Core/Math.js";
- import ModelAnimation from "./ModelAnimation.js";
- import ModelAnimationLoop from "./ModelAnimationLoop.js";
- import ModelAnimationState from "./ModelAnimationState.js";
- function ModelAnimationCollection(model) {
-
- this.animationAdded = new Event();
-
- this.animationRemoved = new Event();
- this._model = model;
- this._scheduledAnimations = [];
- this._previousTime = undefined;
- }
- Object.defineProperties(ModelAnimationCollection.prototype, {
-
- length: {
- get: function () {
- return this._scheduledAnimations.length;
- },
- },
- });
- function add(collection, index, options) {
- var model = collection._model;
- var animations = model._runtime.animations;
- var animation = animations[index];
- var scheduledAnimation = new ModelAnimation(options, model, animation);
- collection._scheduledAnimations.push(scheduledAnimation);
- collection.animationAdded.raiseEvent(model, scheduledAnimation);
- return scheduledAnimation;
- }
- ModelAnimationCollection.prototype.add = function (options) {
- options = defaultValue(options, defaultValue.EMPTY_OBJECT);
- var model = this._model;
- var animations = model._runtime.animations;
-
- if (!defined(animations)) {
- throw new DeveloperError(
- "Animations are not loaded. Wait for Model.readyPromise to resolve."
- );
- }
- if (!defined(options.name) && !defined(options.index)) {
- throw new DeveloperError(
- "Either options.name or options.index must be defined."
- );
- }
- if (defined(options.multiplier) && options.multiplier <= 0.0) {
- throw new DeveloperError("options.multiplier must be greater than zero.");
- }
- if (
- defined(options.index) &&
- (options.index >= animations.length || options.index < 0)
- ) {
- throw new DeveloperError("options.index must be a valid animation index.");
- }
-
- if (defined(options.index)) {
- return add(this, options.index, options);
- }
-
- var index;
- var length = animations.length;
- for (var i = 0; i < length; ++i) {
- if (animations[i].name === options.name) {
- index = i;
- break;
- }
- }
-
- if (!defined(index)) {
- throw new DeveloperError("options.name must be a valid animation name.");
- }
-
- return add(this, index, options);
- };
- ModelAnimationCollection.prototype.addAll = function (options) {
- options = defaultValue(options, defaultValue.EMPTY_OBJECT);
-
- if (!defined(this._model._runtime.animations)) {
- throw new DeveloperError(
- "Animations are not loaded. Wait for Model.readyPromise to resolve."
- );
- }
- if (defined(options.multiplier) && options.multiplier <= 0.0) {
- throw new DeveloperError("options.multiplier must be greater than zero.");
- }
-
- var scheduledAnimations = [];
- var model = this._model;
- var animations = model._runtime.animations;
- var length = animations.length;
- for (var i = 0; i < length; ++i) {
- scheduledAnimations.push(add(this, i, options));
- }
- return scheduledAnimations;
- };
- ModelAnimationCollection.prototype.remove = function (animation) {
- if (defined(animation)) {
- var animations = this._scheduledAnimations;
- var i = animations.indexOf(animation);
- if (i !== -1) {
- animations.splice(i, 1);
- this.animationRemoved.raiseEvent(this._model, animation);
- return true;
- }
- }
- return false;
- };
- ModelAnimationCollection.prototype.removeAll = function () {
- var model = this._model;
- var animations = this._scheduledAnimations;
- var length = animations.length;
- this._scheduledAnimations = [];
- for (var i = 0; i < length; ++i) {
- this.animationRemoved.raiseEvent(model, animations[i]);
- }
- };
- ModelAnimationCollection.prototype.contains = function (animation) {
- if (defined(animation)) {
- return this._scheduledAnimations.indexOf(animation) !== -1;
- }
- return false;
- };
- ModelAnimationCollection.prototype.get = function (index) {
-
- if (!defined(index)) {
- throw new DeveloperError("index is required.");
- }
-
- return this._scheduledAnimations[index];
- };
- function animateChannels(runtimeAnimation, localAnimationTime) {
- var channelEvaluators = runtimeAnimation.channelEvaluators;
- var length = channelEvaluators.length;
- for (var i = 0; i < length; ++i) {
- channelEvaluators[i](localAnimationTime);
- }
- }
- var animationsToRemove = [];
- function createAnimationRemovedFunction(
- modelAnimationCollection,
- model,
- animation
- ) {
- return function () {
- modelAnimationCollection.animationRemoved.raiseEvent(model, animation);
- };
- }
- ModelAnimationCollection.prototype.update = function (frameState) {
- var scheduledAnimations = this._scheduledAnimations;
- var length = scheduledAnimations.length;
- if (length === 0) {
-
- this._previousTime = undefined;
- return false;
- }
- if (JulianDate.equals(frameState.time, this._previousTime)) {
-
- return false;
- }
- this._previousTime = JulianDate.clone(frameState.time, this._previousTime);
- var animationOccured = false;
- var sceneTime = frameState.time;
- var model = this._model;
- for (var i = 0; i < length; ++i) {
- var scheduledAnimation = scheduledAnimations[i];
- var runtimeAnimation = scheduledAnimation._runtimeAnimation;
- if (!defined(scheduledAnimation._computedStartTime)) {
- scheduledAnimation._computedStartTime = JulianDate.addSeconds(
- defaultValue(scheduledAnimation.startTime, sceneTime),
- scheduledAnimation.delay,
- new JulianDate()
- );
- }
- if (!defined(scheduledAnimation._duration)) {
- scheduledAnimation._duration =
- runtimeAnimation.stopTime * (1.0 / scheduledAnimation.multiplier);
- }
- var startTime = scheduledAnimation._computedStartTime;
- var duration = scheduledAnimation._duration;
- var stopTime = scheduledAnimation.stopTime;
-
- var delta =
- duration !== 0.0
- ? JulianDate.secondsDifference(sceneTime, startTime) / duration
- : 0.0;
-
- if (
- duration !== 0.0 &&
- defined(stopTime) &&
- JulianDate.greaterThan(sceneTime, stopTime)
- ) {
- delta = JulianDate.secondsDifference(stopTime, startTime) / duration;
- }
- var pastStartTime = delta >= 0.0;
-
-
-
-
- var repeat =
- scheduledAnimation.loop === ModelAnimationLoop.REPEAT ||
- scheduledAnimation.loop === ModelAnimationLoop.MIRRORED_REPEAT;
- var play =
- (pastStartTime || (repeat && !defined(scheduledAnimation.startTime))) &&
- (delta <= 1.0 || repeat) &&
- (!defined(stopTime) || JulianDate.lessThanOrEquals(sceneTime, stopTime));
-
- if (play || scheduledAnimation._state === ModelAnimationState.ANIMATING) {
-
- if (play && scheduledAnimation._state === ModelAnimationState.STOPPED) {
- scheduledAnimation._state = ModelAnimationState.ANIMATING;
- if (scheduledAnimation.start.numberOfListeners > 0) {
- frameState.afterRender.push(scheduledAnimation._raiseStartEvent);
- }
- }
-
- if (scheduledAnimation.loop === ModelAnimationLoop.REPEAT) {
- delta = delta - Math.floor(delta);
- } else if (
- scheduledAnimation.loop === ModelAnimationLoop.MIRRORED_REPEAT
- ) {
- var floor = Math.floor(delta);
- var fract = delta - floor;
-
- delta = floor % 2 === 1.0 ? 1.0 - fract : fract;
- }
- if (scheduledAnimation.reverse) {
- delta = 1.0 - delta;
- }
- var localAnimationTime = delta * duration * scheduledAnimation.multiplier;
-
- localAnimationTime = CesiumMath.clamp(
- localAnimationTime,
- runtimeAnimation.startTime,
- runtimeAnimation.stopTime
- );
- animateChannels(runtimeAnimation, localAnimationTime);
- if (scheduledAnimation.update.numberOfListeners > 0) {
- scheduledAnimation._updateEventTime = localAnimationTime;
- frameState.afterRender.push(scheduledAnimation._raiseUpdateEvent);
- }
- animationOccured = true;
- if (!play) {
-
- scheduledAnimation._state = ModelAnimationState.STOPPED;
- if (scheduledAnimation.stop.numberOfListeners > 0) {
- frameState.afterRender.push(scheduledAnimation._raiseStopEvent);
- }
- if (scheduledAnimation.removeOnStop) {
- animationsToRemove.push(scheduledAnimation);
- }
- }
- }
- }
-
- length = animationsToRemove.length;
- for (var j = 0; j < length; ++j) {
- var animationToRemove = animationsToRemove[j];
- scheduledAnimations.splice(
- scheduledAnimations.indexOf(animationToRemove),
- 1
- );
- frameState.afterRender.push(
- createAnimationRemovedFunction(this, model, animationToRemove)
- );
- }
- animationsToRemove.length = 0;
- return animationOccured;
- };
- export default ModelAnimationCollection;
|