Spaces:
Running
Running
File size: 9,103 Bytes
2b7aae2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 | import { AnimationUtils } from './AnimationUtils.js';
import { KeyframeTrack } from './KeyframeTrack.js';
import { BooleanKeyframeTrack } from './tracks/BooleanKeyframeTrack.js';
import { ColorKeyframeTrack } from './tracks/ColorKeyframeTrack.js';
import { NumberKeyframeTrack } from './tracks/NumberKeyframeTrack.js';
import { QuaternionKeyframeTrack } from './tracks/QuaternionKeyframeTrack.js';
import { StringKeyframeTrack } from './tracks/StringKeyframeTrack.js';
import { VectorKeyframeTrack } from './tracks/VectorKeyframeTrack.js';
import * as MathUtils from '../math/MathUtils.js';
import { NormalAnimationBlendMode } from '../constants.js';
class AnimationClip {
constructor(name, duration = -1, tracks, blendMode = NormalAnimationBlendMode) {
this.name = name;
this.tracks = tracks;
this.duration = duration;
this.blendMode = blendMode;
this.uuid = MathUtils.generateUUID();
// this means it should figure out its duration by scanning the tracks
if (this.duration < 0) {
this.resetDuration();
}
}
static parse(json) {
const tracks = [],
jsonTracks = json.tracks,
frameTime = 1.0 / (json.fps || 1.0);
for (let i = 0, n = jsonTracks.length; i !== n; ++i) {
tracks.push(parseKeyframeTrack(jsonTracks[i]).scale(frameTime));
}
const clip = new this(json.name, json.duration, tracks, json.blendMode);
clip.uuid = json.uuid;
return clip;
}
static toJSON(clip) {
const tracks = [],
clipTracks = clip.tracks;
const json = {
name: clip.name,
duration: clip.duration,
tracks: tracks,
uuid: clip.uuid,
blendMode: clip.blendMode,
};
for (let i = 0, n = clipTracks.length; i !== n; ++i) {
tracks.push(KeyframeTrack.toJSON(clipTracks[i]));
}
return json;
}
static CreateFromMorphTargetSequence(name, morphTargetSequence, fps, noLoop) {
const numMorphTargets = morphTargetSequence.length;
const tracks = [];
for (let i = 0; i < numMorphTargets; i++) {
let times = [];
let values = [];
times.push((i + numMorphTargets - 1) % numMorphTargets, i, (i + 1) % numMorphTargets);
values.push(0, 1, 0);
const order = AnimationUtils.getKeyframeOrder(times);
times = AnimationUtils.sortedArray(times, 1, order);
values = AnimationUtils.sortedArray(values, 1, order);
// if there is a key at the first frame, duplicate it as the
// last frame as well for perfect loop.
if (!noLoop && times[0] === 0) {
times.push(numMorphTargets);
values.push(values[0]);
}
tracks.push(new NumberKeyframeTrack('.morphTargetInfluences[' + morphTargetSequence[i].name + ']', times, values).scale(1.0 / fps));
}
return new this(name, -1, tracks);
}
static findByName(objectOrClipArray, name) {
let clipArray = objectOrClipArray;
if (!Array.isArray(objectOrClipArray)) {
const o = objectOrClipArray;
clipArray = (o.geometry && o.geometry.animations) || o.animations;
}
for (let i = 0; i < clipArray.length; i++) {
if (clipArray[i].name === name) {
return clipArray[i];
}
}
return null;
}
static CreateClipsFromMorphTargetSequences(morphTargets, fps, noLoop) {
const animationToMorphTargets = {};
// tested with https://regex101.com/ on trick sequences
// such flamingo_flyA_003, flamingo_run1_003, crdeath0059
const pattern = /^([\w-]*?)([\d]+)$/;
// sort morph target names into animation groups based
// patterns like Walk_001, Walk_002, Run_001, Run_002
for (let i = 0, il = morphTargets.length; i < il; i++) {
const morphTarget = morphTargets[i];
const parts = morphTarget.name.match(pattern);
if (parts && parts.length > 1) {
const name = parts[1];
let animationMorphTargets = animationToMorphTargets[name];
if (!animationMorphTargets) {
animationToMorphTargets[name] = animationMorphTargets = [];
}
animationMorphTargets.push(morphTarget);
}
}
const clips = [];
for (const name in animationToMorphTargets) {
clips.push(this.CreateFromMorphTargetSequence(name, animationToMorphTargets[name], fps, noLoop));
}
return clips;
}
// parse the animation.hierarchy format
static parseAnimation(animation, bones) {
if (!animation) {
console.error('THREE.AnimationClip: No animation in JSONLoader data.');
return null;
}
const addNonemptyTrack = function (trackType, trackName, animationKeys, propertyName, destTracks) {
// only return track if there are actually keys.
if (animationKeys.length !== 0) {
const times = [];
const values = [];
AnimationUtils.flattenJSON(animationKeys, times, values, propertyName);
// empty keys are filtered out, so check again
if (times.length !== 0) {
destTracks.push(new trackType(trackName, times, values));
}
}
};
const tracks = [];
const clipName = animation.name || 'default';
const fps = animation.fps || 30;
const blendMode = animation.blendMode;
// automatic length determination in AnimationClip.
let duration = animation.length || -1;
const hierarchyTracks = animation.hierarchy || [];
for (let h = 0; h < hierarchyTracks.length; h++) {
const animationKeys = hierarchyTracks[h].keys;
// skip empty tracks
if (!animationKeys || animationKeys.length === 0) continue;
// process morph targets
if (animationKeys[0].morphTargets) {
// figure out all morph targets used in this track
const morphTargetNames = {};
let k;
for (k = 0; k < animationKeys.length; k++) {
if (animationKeys[k].morphTargets) {
for (let m = 0; m < animationKeys[k].morphTargets.length; m++) {
morphTargetNames[animationKeys[k].morphTargets[m]] = -1;
}
}
}
// create a track for each morph target with all zero
// morphTargetInfluences except for the keys in which
// the morphTarget is named.
for (const morphTargetName in morphTargetNames) {
const times = [];
const values = [];
for (let m = 0; m !== animationKeys[k].morphTargets.length; ++m) {
const animationKey = animationKeys[k];
times.push(animationKey.time);
values.push(animationKey.morphTarget === morphTargetName ? 1 : 0);
}
tracks.push(new NumberKeyframeTrack('.morphTargetInfluence[' + morphTargetName + ']', times, values));
}
duration = morphTargetNames.length * (fps || 1.0);
} else {
// ...assume skeletal animation
const boneName = '.bones[' + bones[h].name + ']';
addNonemptyTrack(VectorKeyframeTrack, boneName + '.position', animationKeys, 'pos', tracks);
addNonemptyTrack(QuaternionKeyframeTrack, boneName + '.quaternion', animationKeys, 'rot', tracks);
addNonemptyTrack(VectorKeyframeTrack, boneName + '.scale', animationKeys, 'scl', tracks);
}
}
if (tracks.length === 0) {
return null;
}
const clip = new this(clipName, duration, tracks, blendMode);
return clip;
}
resetDuration() {
const tracks = this.tracks;
let duration = 0;
for (let i = 0, n = tracks.length; i !== n; ++i) {
const track = this.tracks[i];
duration = Math.max(duration, track.times[track.times.length - 1]);
}
this.duration = duration;
return this;
}
trim() {
for (let i = 0; i < this.tracks.length; i++) {
this.tracks[i].trim(0, this.duration);
}
return this;
}
validate() {
let valid = true;
for (let i = 0; i < this.tracks.length; i++) {
valid = valid && this.tracks[i].validate();
}
return valid;
}
optimize() {
for (let i = 0; i < this.tracks.length; i++) {
this.tracks[i].optimize();
}
return this;
}
clone() {
const tracks = [];
for (let i = 0; i < this.tracks.length; i++) {
tracks.push(this.tracks[i].clone());
}
return new this.constructor(this.name, this.duration, tracks, this.blendMode);
}
toJSON() {
return this.constructor.toJSON(this);
}
}
function getTrackTypeForValueTypeName(typeName) {
switch (typeName.toLowerCase()) {
case 'scalar':
case 'double':
case 'float':
case 'number':
case 'integer':
return NumberKeyframeTrack;
case 'vector':
case 'vector2':
case 'vector3':
case 'vector4':
return VectorKeyframeTrack;
case 'color':
return ColorKeyframeTrack;
case 'quaternion':
return QuaternionKeyframeTrack;
case 'bool':
case 'boolean':
return BooleanKeyframeTrack;
case 'string':
return StringKeyframeTrack;
}
throw new Error('THREE.KeyframeTrack: Unsupported typeName: ' + typeName);
}
function parseKeyframeTrack(json) {
if (json.type === undefined) {
throw new Error('THREE.KeyframeTrack: track type undefined, can not parse');
}
const trackType = getTrackTypeForValueTypeName(json.type);
if (json.times === undefined) {
const times = [],
values = [];
AnimationUtils.flattenJSON(json.keys, times, values, 'value');
json.times = times;
json.values = values;
}
// derived classes can define a static parse method
if (trackType.parse !== undefined) {
return trackType.parse(json);
} else {
// by default, we assume a constructor compatible with the base
return new trackType(json.name, json.times, json.values, json.interpolation);
}
}
export { AnimationClip };
|