mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-02 08:42:43 +08:00
remove 3d animation clip (#4184)
This commit is contained in:
committed by
Weiliang Wang
parent
1fb5a90d82
commit
e7a7aacb4c
@@ -1,371 +0,0 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated engine source code (the "Software"), a limited,
|
||||
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
|
||||
to use Cocos Creator solely to develop games on your target platforms. You shall
|
||||
not use Cocos Creator software for developing other software or tools that's
|
||||
used for developing games. You are not granted to publish, distribute,
|
||||
sublicense, and/or sell copies of Cocos Creator.
|
||||
|
||||
The software or tools in this License Agreement are licensed, not sold.
|
||||
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
// @ts-check
|
||||
import { _decorator } from '../../core/data/index';
|
||||
const { ccclass, property } = _decorator;
|
||||
import { Asset } from '../../assets/asset';
|
||||
import { Quat, Vec3 } from '../../core/value-types';
|
||||
import quat from '../../core/vmath/quat';
|
||||
import { clamp } from '../../core/vmath/utils';
|
||||
import vec3 from '../../core/vmath/vec3';
|
||||
import { Node } from '../../scene-graph/node';
|
||||
import { ccenum } from '../../core/value-types/enum';
|
||||
|
||||
const tmpvec3 = new Vec3();
|
||||
const tmpquat = new Quat();
|
||||
|
||||
function _binaryIndexOf (array: number[], key: number) {
|
||||
let lo = 0;
|
||||
let hi = array.length - 1;
|
||||
while (lo <= hi) {
|
||||
const mid = ((lo + hi) >> 1);
|
||||
const val = array[mid];
|
||||
if (val < key) {
|
||||
lo = mid + 1;
|
||||
} else if (val > key) {
|
||||
hi = mid - 1;
|
||||
} else {
|
||||
return mid;
|
||||
}
|
||||
}
|
||||
return lo;
|
||||
}
|
||||
|
||||
export enum AnimationTargetProperty {
|
||||
position,
|
||||
rotation,
|
||||
scale,
|
||||
}
|
||||
|
||||
cc.AnimationTargetProperty = AnimationTargetProperty;
|
||||
|
||||
ccenum(AnimationTargetProperty);
|
||||
|
||||
export interface IPropertyAnimation {
|
||||
/**
|
||||
* Target Property to animate.
|
||||
*/
|
||||
property: AnimationTargetProperty;
|
||||
|
||||
/**
|
||||
* Index of keys into animation's keysList property;
|
||||
*/
|
||||
indexOfKeys: number;
|
||||
|
||||
/**
|
||||
* Property values.
|
||||
*/
|
||||
values: number[];
|
||||
}
|
||||
|
||||
export interface IAnimationChannel {
|
||||
/**
|
||||
* Target node's path in scene graph.
|
||||
*/
|
||||
target: string;
|
||||
|
||||
/**
|
||||
* Properties animation.
|
||||
*/
|
||||
propertyAnimations: IPropertyAnimation[];
|
||||
}
|
||||
|
||||
@ccclass('cc.AnimationClip')
|
||||
export default class AnimationClip extends Asset {
|
||||
@property
|
||||
private _channels: IAnimationChannel[] = [];
|
||||
|
||||
@property
|
||||
private _keysList: number[][] = [];
|
||||
|
||||
@property
|
||||
private _length: number = 0.0;
|
||||
|
||||
get keysList () {
|
||||
return this._keysList;
|
||||
}
|
||||
|
||||
get channels () {
|
||||
return this._channels;
|
||||
}
|
||||
|
||||
get length () {
|
||||
return this._length;
|
||||
}
|
||||
}
|
||||
|
||||
cc.AnimationClip = AnimationClip;
|
||||
|
||||
export class AnimationTarget {
|
||||
private _nodes: Node[] = [];
|
||||
private _pathMap: Map<string, number> = new Map();
|
||||
|
||||
public add (path: string, node: Node) {
|
||||
if (this._pathMap.has(path)) {
|
||||
return;
|
||||
}
|
||||
const index = this._nodes.length;
|
||||
this._nodes.push(node);
|
||||
this._pathMap.set(path, index);
|
||||
}
|
||||
|
||||
public get (path: string) {
|
||||
return this._pathMap.get(path);
|
||||
}
|
||||
|
||||
get nodes () {
|
||||
return this._nodes;
|
||||
}
|
||||
|
||||
public getDefaultPosition (nodeIndex: number) {
|
||||
return this._nodes[nodeIndex].getPosition();
|
||||
// return this._defaultPositions[nodeIndex];
|
||||
}
|
||||
|
||||
public getDefaultScale (nodeIndex: number) {
|
||||
return this._nodes[nodeIndex].getScale();
|
||||
// return this._defaultScales[nodeIndex];
|
||||
}
|
||||
|
||||
public getDefaultRotation (nodeIndex: number) {
|
||||
return this._nodes[nodeIndex].getRotation();
|
||||
// return this._defaultRotations[nodeIndex];
|
||||
}
|
||||
|
||||
// _saveDefaultTRS() {
|
||||
// this._defaultPositions = new Array(this._nodes.length);
|
||||
// this._defaultScales = new Array(this._nodes.length);
|
||||
// this._defaultRotations = new Array(this._nodes.length);
|
||||
// this._nodes.forEach((node, index) => {
|
||||
// this._defaultPositions[index] = node.getPosition();
|
||||
// this._defaultScales[index] = node.getScale();
|
||||
// this._defaultRotations[index] = node.getRotation();
|
||||
// });
|
||||
// }
|
||||
}
|
||||
|
||||
/**
|
||||
* The AnimationTarget class represents the progress of blended sampling.
|
||||
*/
|
||||
export class AnimationSampler {
|
||||
private _animationTarget: AnimationTarget;
|
||||
private _nodeSamplingStates: NodeSamplingState[];
|
||||
|
||||
constructor (animationTarget: AnimationTarget) {
|
||||
this._animationTarget = animationTarget;
|
||||
|
||||
const targetNodes = this._animationTarget.nodes;
|
||||
this._nodeSamplingStates = new Array(targetNodes.length);
|
||||
this._animationTarget.nodes.forEach((targetNode, index) => {
|
||||
this._nodeSamplingStates[index] = new NodeSamplingState(
|
||||
targetNode,
|
||||
this._animationTarget.getDefaultPosition(index),
|
||||
this._animationTarget.getDefaultScale(index),
|
||||
this._animationTarget.getDefaultRotation(index));
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets this state to get sampling start.
|
||||
*/
|
||||
public reset () {
|
||||
this._nodeSamplingStates.forEach(
|
||||
(nodeSamlingState) => nodeSamlingState.reset());
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the sampling result.
|
||||
*/
|
||||
public apply () {
|
||||
this._nodeSamplingStates.forEach(
|
||||
(nodeSamlingState) => nodeSamlingState.apply());
|
||||
}
|
||||
|
||||
/** Sample data of this animation clip in a specific time and blend that data
|
||||
* with a weight together with previous data(if exist, or blank if not exists) sampled before.
|
||||
*
|
||||
* @param clip
|
||||
* @param t The time.
|
||||
* @param weight The weight.
|
||||
* @param [mask] The skeleton mask.
|
||||
*/
|
||||
public sample (clip: AnimationClip, t: number, weight: number, mask?: any) {
|
||||
t = clamp(t, 0, clip.length);
|
||||
|
||||
const binaryIndicesOfKeysList: number[] = new Array(clip.keysList.length);
|
||||
binaryIndicesOfKeysList.fill(-1);
|
||||
const calcBinarayIndicesOfKeys = (iKeys: number) => {
|
||||
let v = binaryIndicesOfKeysList[iKeys];
|
||||
if (v < 0) {
|
||||
const keys = clip.keysList[iKeys];
|
||||
if (keys.length === 1) {
|
||||
v = 0;
|
||||
} else {
|
||||
v = _binaryIndexOf(keys, t);
|
||||
}
|
||||
binaryIndicesOfKeysList[iKeys] = v;
|
||||
}
|
||||
return v;
|
||||
};
|
||||
|
||||
const getState = (channel: IAnimationChannel) => {
|
||||
const iNode = this._animationTarget.get(channel.target);
|
||||
if (iNode === undefined) {
|
||||
return null;
|
||||
}
|
||||
return this._nodeSamplingStates[iNode];
|
||||
};
|
||||
|
||||
const tempVec3_1 = new Vec3();
|
||||
const tempVec3_2 = new Vec3();
|
||||
const tempQuat_1 = new Quat();
|
||||
const tempQuat_2 = new Quat();
|
||||
|
||||
clip.channels.forEach((channel) => {
|
||||
const nodeSamlingState = getState(channel);
|
||||
if (!nodeSamlingState) {
|
||||
return;
|
||||
}
|
||||
channel.propertyAnimations.forEach((propertyAnimation) => {
|
||||
const getPositionAt = (index: number, out: Vec3) => vec3.set(out,
|
||||
propertyAnimation.values[index * 3 + 0],
|
||||
propertyAnimation.values[index * 3 + 1],
|
||||
propertyAnimation.values[index * 3 + 2]);
|
||||
const getRotationAt = (index: number, out: Quat) => quat.set(out,
|
||||
propertyAnimation.values[index * 4 + 0],
|
||||
propertyAnimation.values[index * 4 + 1],
|
||||
propertyAnimation.values[index * 4 + 2],
|
||||
propertyAnimation.values[index * 4 + 3]);
|
||||
const getScaleAt = getPositionAt;
|
||||
const keys = clip.keysList[propertyAnimation.indexOfKeys];
|
||||
const idx = calcBinarayIndicesOfKeys(propertyAnimation.indexOfKeys);
|
||||
|
||||
if (idx === 0) {
|
||||
if (propertyAnimation.property === AnimationTargetProperty.position) {
|
||||
const p = getPositionAt(0, tempVec3_1);
|
||||
nodeSamlingState.blendPosition(p, weight);
|
||||
} else if (propertyAnimation.property === AnimationTargetProperty.rotation) {
|
||||
const r = getRotationAt(0, tempQuat_1);
|
||||
nodeSamlingState.blendRotation(r, weight);
|
||||
} else if (propertyAnimation.property === AnimationTargetProperty.scale) {
|
||||
const s = getScaleAt(0, tempVec3_1);
|
||||
nodeSamlingState.blendScale(s, weight);
|
||||
}
|
||||
} else {
|
||||
const loIdx = Math.max(idx - 1, 0);
|
||||
const hiIdx = Math.min(idx, keys.length);
|
||||
const ratio = (t - keys[loIdx]) / (keys[hiIdx] - keys[loIdx]);
|
||||
if (propertyAnimation.property === AnimationTargetProperty.position) {
|
||||
const a = getPositionAt(loIdx, tempVec3_1);
|
||||
const b = getPositionAt(hiIdx, tempVec3_2);
|
||||
vec3.lerp(tmpvec3, a, b, ratio);
|
||||
nodeSamlingState.blendPosition(tmpvec3, weight);
|
||||
} else if (propertyAnimation.property === AnimationTargetProperty.rotation) {
|
||||
const a = getRotationAt(loIdx, tempQuat_1);
|
||||
const b = getRotationAt(hiIdx, tempQuat_2);
|
||||
quat.slerp(tmpquat, a, b, ratio);
|
||||
nodeSamlingState.blendRotation(tmpquat, weight);
|
||||
} else if (propertyAnimation.property === AnimationTargetProperty.scale) {
|
||||
const a = getScaleAt(loIdx, tempVec3_1);
|
||||
const b = getScaleAt(hiIdx, tempVec3_2);
|
||||
vec3.lerp(tmpvec3, a, b, ratio);
|
||||
nodeSamlingState.blendScale(tmpvec3, weight);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
class NodeSamplingState {
|
||||
private _target: Node;
|
||||
private _defaultPosition: Vec3;
|
||||
private _defaultScale: Vec3;
|
||||
private _defaultRotation: Quat;
|
||||
private _sumPosWeight = 0;
|
||||
private _sumScaleWeight = 0;
|
||||
private _sumRotWeight = 0;
|
||||
|
||||
constructor (target: Node, defaultPosition: Vec3, defaultScale: Vec3, defaultRotation: Quat) {
|
||||
this._target = target;
|
||||
this._defaultPosition = defaultPosition;
|
||||
this._defaultScale = defaultScale;
|
||||
this._defaultRotation = defaultRotation;
|
||||
}
|
||||
|
||||
public reset () {
|
||||
this._sumPosWeight = 0.0;
|
||||
this._sumScaleWeight = 0.0;
|
||||
this._sumRotWeight = 0.0;
|
||||
this._target.setPosition(0, 0, 0);
|
||||
this._target.setScale(0, 0, 0);
|
||||
this._target.setRotation(0, 0, 0, 1);
|
||||
}
|
||||
|
||||
public blendPosition (position: Vec3, weight: number) {
|
||||
vec3.scaleAndAdd(tmpvec3, this._target.getPosition(), position, weight);
|
||||
this._target.setPosition(tmpvec3);
|
||||
this._sumPosWeight += weight;
|
||||
}
|
||||
|
||||
public blendScale (scale: Vec3, weight: number) {
|
||||
vec3.scaleAndAdd(tmpvec3, this._target.getScale(), scale, weight);
|
||||
this._target.setScale(tmpvec3);
|
||||
this._sumScaleWeight += weight;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inspired by:
|
||||
* https://gamedev.stackexchange.com/questions/62354/method-for-interpolation-between-3-quaternions
|
||||
*/
|
||||
public blendRotation (rotation: Quat, weight: number) {
|
||||
if (weight === 0) {
|
||||
return;
|
||||
}
|
||||
const t = weight / (this._sumRotWeight + weight);
|
||||
quat.slerp(tmpquat, this._target.getRotation(), rotation, t);
|
||||
this._target.setRotation(tmpquat);
|
||||
this._sumRotWeight += weight;
|
||||
}
|
||||
|
||||
public apply () {
|
||||
if (CC_DEV) {
|
||||
if (this._sumPosWeight > 1.0 || this._sumRotWeight > 1.0 || this._sumScaleWeight > 1.0) {
|
||||
throw new Error(`Unexpected.`);
|
||||
}
|
||||
}
|
||||
|
||||
if (this._sumPosWeight < 1.0) {
|
||||
this.blendPosition(this._defaultPosition, 1.0 - this._sumPosWeight);
|
||||
}
|
||||
if (this._sumScaleWeight < 1.0) {
|
||||
this.blendScale(this._defaultScale, 1.0 - this._sumScaleWeight);
|
||||
}
|
||||
if (this._sumRotWeight < 1.0) {
|
||||
this.blendRotation(this._defaultRotation, 1.0 - this._sumRotWeight);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -23,7 +23,6 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
export { default as AnimationClip } from './animation-clip';
|
||||
export { AudioClip } from './audio/clip';
|
||||
export { EffectAsset } from './effect-asset';
|
||||
export { Material } from './material';
|
||||
|
||||
@@ -1,421 +0,0 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
||||
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos2d-x.org
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
// @ts-check
|
||||
import { Component } from '../../components/component';
|
||||
import { ccclass, executeInEditMode, executionOrder, menu, property } from '../../core/data/class-decorator';
|
||||
import { ccenum } from '../../core/value-types/enum';
|
||||
import AnimationClip, { AnimationSampler, AnimationTarget } from '../assets/animation-clip';
|
||||
import { clamp01 } from '../../core/vmath';
|
||||
|
||||
enum BlendMode {
|
||||
/**
|
||||
* !#en Animations will be blended.
|
||||
*
|
||||
* !#ch 动画将被混合
|
||||
*/
|
||||
Blend = 'blend',
|
||||
/**
|
||||
* !#en Animations will be added.
|
||||
*
|
||||
* !#ch 动画将被附加
|
||||
*/
|
||||
Additive = 'additive',
|
||||
}
|
||||
|
||||
ccenum(BlendMode);
|
||||
|
||||
enum WrapMode {
|
||||
/**
|
||||
* !#en Only once
|
||||
*
|
||||
* !#ch 只播放一次
|
||||
*/
|
||||
Once = 'once',
|
||||
|
||||
/**
|
||||
* !#en Loop
|
||||
*
|
||||
* !#ch 循环播放
|
||||
*/
|
||||
Loop = 'loop',
|
||||
|
||||
/**
|
||||
* !#en Ping Pong
|
||||
*
|
||||
* !#ch 乒乓模式播放
|
||||
*/
|
||||
PinpPong = 'ping-pong',
|
||||
|
||||
/**
|
||||
* !#en Plays back the animation. The last frame is played until the end.
|
||||
*
|
||||
* !#ch 回放动画。当到结尾时,会持续播放最后一帧。
|
||||
*/
|
||||
Clamp = 'clamp',
|
||||
}
|
||||
|
||||
ccenum(WrapMode);
|
||||
|
||||
/**
|
||||
* !#en Animation State Class
|
||||
*
|
||||
* !#ch 动画状态类
|
||||
*/
|
||||
class AnimationState {
|
||||
/**
|
||||
* !#en The blend mode of animation state.
|
||||
*
|
||||
* !#ch 动画状态的混合模式
|
||||
*/
|
||||
public static BlendMode = BlendMode;
|
||||
|
||||
/**
|
||||
* !#en Wrapping mode of the animation.
|
||||
*
|
||||
* !#ch 动画状态的循环模式
|
||||
*/
|
||||
public static WrapMode = WrapMode;
|
||||
|
||||
/**
|
||||
* !#en An animation clip that is played through this state
|
||||
*
|
||||
* !#ch 通过此状态播放的动画剪辑
|
||||
*/
|
||||
public clip: AnimationClip;
|
||||
|
||||
/**
|
||||
* !#en Blend mode
|
||||
*
|
||||
* !#ch 混合模式
|
||||
*/
|
||||
public blendMode: BlendMode = BlendMode.Blend;
|
||||
|
||||
/**
|
||||
* !#en Wrapping mode
|
||||
*
|
||||
* !#ch 循环模式
|
||||
*/
|
||||
public wrapMode: WrapMode = WrapMode.Loop;
|
||||
|
||||
/**
|
||||
* !#en The playback speed
|
||||
*
|
||||
* !#ch 播放速度
|
||||
*/
|
||||
public speed = 1.0;
|
||||
|
||||
/**
|
||||
* !#en The current time of the animation.
|
||||
*
|
||||
* !#ch 当前动画播放时间
|
||||
*/
|
||||
public time: number = 0.0;
|
||||
|
||||
/**
|
||||
* !#en The weight of animation.
|
||||
*
|
||||
* !#ch 动画权重
|
||||
*/
|
||||
public weight: number = 1.0;
|
||||
|
||||
constructor (clip: AnimationClip) {
|
||||
this.clip = clip;
|
||||
}
|
||||
}
|
||||
|
||||
interface IBlendedAnimationState {
|
||||
state: AnimationState | null;
|
||||
blendTime: number;
|
||||
blendDuration: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Animation Controller
|
||||
*
|
||||
* !#ch 动画控制器
|
||||
*/
|
||||
class AnimationCtrl {
|
||||
private _animationTarget: AnimationTarget | null = null;
|
||||
private _animationSampler: AnimationSampler | null = null;
|
||||
private _animations: IBlendedAnimationState[] = [];
|
||||
|
||||
/**
|
||||
* !#en Set up the animation target
|
||||
*
|
||||
* !#ch 设置动画目标
|
||||
*/
|
||||
public setAnimationTarget (animationTarget: AnimationTarget | null) {
|
||||
this._animationTarget = animationTarget;
|
||||
if (this._animationTarget) {
|
||||
this._animationSampler = new AnimationSampler(this._animationTarget);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Animation fades in and out
|
||||
*
|
||||
* !#ch 动画的淡入淡出功能
|
||||
* @param to
|
||||
* @param duration
|
||||
*/
|
||||
public crossFade (to: AnimationState | null, duration: number) {
|
||||
this._animations.unshift({
|
||||
state: to,
|
||||
blendDuration: duration,
|
||||
blendTime: 0.0,
|
||||
});
|
||||
}
|
||||
|
||||
public tick (deltaTime: number) {
|
||||
if (!this._animationSampler) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this._animations.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._animationSampler.reset();
|
||||
|
||||
let absoluteWeight = 1.0;
|
||||
for (let i = 0; i < this._animations.length; ++i) {
|
||||
if (absoluteWeight === 0) {
|
||||
this._animations.splice(i);
|
||||
break;
|
||||
}
|
||||
const ba = this._animations[i];
|
||||
ba.blendTime += deltaTime;
|
||||
const relativeWeight = clamp01(ba.blendTime / ba.blendDuration);
|
||||
const weight = relativeWeight * absoluteWeight;
|
||||
absoluteWeight = absoluteWeight * (1.0 - relativeWeight);
|
||||
if (ba.state) {
|
||||
ba.state.time += deltaTime;
|
||||
const t = this._getTime(ba.state);
|
||||
this._animationSampler.sample(ba.state.clip, t, weight);
|
||||
}
|
||||
}
|
||||
|
||||
this._animationSampler.apply();
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Get the play time of an animation clip in current animation state
|
||||
*
|
||||
* !#ch 返回当前动画状态下的动画剪辑播放时间
|
||||
* @param state - Specify animation status
|
||||
*/
|
||||
private _getTime (state: AnimationState) {
|
||||
let t = state.time;
|
||||
const length = state.clip.length;
|
||||
t *= state.speed;
|
||||
|
||||
if (state.wrapMode === 'once') {
|
||||
if (t > length) {
|
||||
t = 0.0;
|
||||
}
|
||||
} else if (state.wrapMode === 'loop') {
|
||||
t %= length;
|
||||
} else if (state.wrapMode === 'ping-pong') {
|
||||
const order = Math.floor(t / length);
|
||||
if (order % 2 === 1) {
|
||||
t = length - t % length;
|
||||
}
|
||||
}
|
||||
|
||||
return t;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* Basic animation component for playing animation
|
||||
*
|
||||
* !#ch
|
||||
* 基础动画组件用于动画播放
|
||||
*/
|
||||
@ccclass('cc.AnimationComponent')
|
||||
@executionOrder(99)
|
||||
@executeInEditMode
|
||||
@menu('Components/AnimationComponent')
|
||||
export class AnimationComponent extends Component {
|
||||
@property({ type: [AnimationClip] })
|
||||
public _clips: AnimationClip[] = [];
|
||||
|
||||
/**
|
||||
* !#en Current clip
|
||||
*
|
||||
* !#ch 当前动画剪辑
|
||||
*/
|
||||
@property({ type: AnimationClip })
|
||||
public currentClip: AnimationClip | null = null;
|
||||
|
||||
/**
|
||||
* !#en Whether to start the animation automatically
|
||||
*
|
||||
* !#ch 是否启动时自动播放动画
|
||||
*/
|
||||
@property
|
||||
public playAutomatically: boolean = false;
|
||||
|
||||
private _preview = false;
|
||||
|
||||
private _animationController: AnimationCtrl = new AnimationCtrl();
|
||||
|
||||
private _name2states: { [x: string]: AnimationState | undefined; } = {};
|
||||
|
||||
private _animationTarget: AnimationTarget | null = null;
|
||||
|
||||
@property({ type: Boolean })
|
||||
get preview () {
|
||||
return this._preview;
|
||||
}
|
||||
|
||||
set preview (value) {
|
||||
this._preview = value;
|
||||
if (value) {
|
||||
if (this.currentClip) {
|
||||
cc.engine.animatingInEditMode = true;
|
||||
this.play(this.currentClip.name);
|
||||
}
|
||||
} else {
|
||||
cc.engine.animatingInEditMode = false;
|
||||
this._animationController.crossFade(null, 0);
|
||||
}
|
||||
}
|
||||
|
||||
constructor () {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Clip required for animation playback
|
||||
*
|
||||
* !#ch 动画剪辑列表
|
||||
*/
|
||||
@property({ type: [AnimationClip] })
|
||||
get clips () {
|
||||
return this._clips;
|
||||
}
|
||||
|
||||
set clips (val) {
|
||||
this._clips = val;
|
||||
}
|
||||
|
||||
public onLoad () {
|
||||
this._clips.forEach((clip) => {
|
||||
this._name2states[clip.name] = new AnimationState(clip);
|
||||
});
|
||||
|
||||
this._resetTarget();
|
||||
|
||||
if (!CC_EDITOR && this.playAutomatically && this.currentClip) {
|
||||
this.play(this.currentClip.name);
|
||||
}
|
||||
}
|
||||
|
||||
public update (deltaTime: number) {
|
||||
this._animationController.tick(deltaTime);
|
||||
}
|
||||
|
||||
public onDestroy () {
|
||||
}
|
||||
|
||||
public onEnable () {
|
||||
}
|
||||
|
||||
public onDisable () {
|
||||
this._animationTarget = null;
|
||||
this._animationController.setAnimationTarget(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Add an animation clip to the animation status list
|
||||
*
|
||||
* !#ch 添加动画剪辑到动画状态列表
|
||||
* @param name - The name of animation clip
|
||||
* @param animationClip - Animation clip
|
||||
*/
|
||||
public addClip (name: string, animationClip: AnimationClip) {
|
||||
if (this._name2states[name]) {
|
||||
console.warn(`Failed to add clip ${name}, the name already exsits.`);
|
||||
return;
|
||||
}
|
||||
|
||||
this._clips.push(animationClip);
|
||||
this._name2states[name] = new AnimationState(animationClip);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Get the animation state through the animation clip name
|
||||
*
|
||||
* !#ch 返回动画剪辑相对应的动画状态
|
||||
* @param name - The name of animation clip
|
||||
*/
|
||||
public getState (name: string) {
|
||||
return this._name2states[name];
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Play the name of the animation clip
|
||||
*
|
||||
* !#ch 播放相对应名字的动画剪辑
|
||||
* @param name - The name of animation clip
|
||||
* @param fadeDuration - Fade duration
|
||||
*/
|
||||
public play (name: string, fadeDuration = 0.3) {
|
||||
if (!this._animationTarget) {
|
||||
return;
|
||||
}
|
||||
|
||||
const state = this._name2states[name];
|
||||
if (!state) {
|
||||
console.warn(`Failed to play animation ${name}, not found.`);
|
||||
return;
|
||||
}
|
||||
|
||||
state.time = 0.0;
|
||||
this._animationController.crossFade(state, fadeDuration);
|
||||
}
|
||||
|
||||
public _resetTarget () {
|
||||
const animationTarget = new AnimationTarget();
|
||||
this._clips.forEach((clip) => {
|
||||
clip.channels.forEach((channel) => {
|
||||
const targetPath = channel.target;
|
||||
if (animationTarget.get(targetPath)) {
|
||||
return;
|
||||
}
|
||||
const targetNode = this.node.getChildByPath(targetPath);
|
||||
if (!targetNode) {
|
||||
console.warn(`Animation target ${targetPath} not found in scene graph.`);
|
||||
return;
|
||||
}
|
||||
animationTarget.add(targetPath, targetNode);
|
||||
});
|
||||
});
|
||||
this._animationTarget = animationTarget;
|
||||
this._animationController.setAnimationTarget(this._animationTarget);
|
||||
}
|
||||
}
|
||||
@@ -24,10 +24,10 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
import { AnimationComponent } from './animation-component';
|
||||
import { AudioSourceComponent } from './audio-source-component';
|
||||
import { CameraComponent } from './camera-component';
|
||||
import { DirectionalLightComponent } from './directional-light-component';
|
||||
import { EditorCameraComponent } from './editor-camera-component';
|
||||
import { LightComponent } from './light-component';
|
||||
import { ModelComponent } from './model-component';
|
||||
import { ParticleSystemComponent } from './particle/particle-system-component';
|
||||
@@ -37,11 +37,9 @@ import { RenderableComponent } from './renderable-component';
|
||||
import { SkinningModelComponent } from './skinning-model-component';
|
||||
import { SphereLightComponent } from './sphere-light-component';
|
||||
import { SpotLightComponent } from './spot-light-component';
|
||||
import { EditorCameraComponent } from './editor-camera-component';
|
||||
export * from './physics';
|
||||
|
||||
export {
|
||||
AnimationComponent,
|
||||
AudioSourceComponent,
|
||||
CameraComponent,
|
||||
LightComponent,
|
||||
@@ -54,7 +52,7 @@ export {
|
||||
SphereColliderComponent,
|
||||
};
|
||||
|
||||
cc.AnimationComponent = AnimationComponent;
|
||||
// cc.AnimationComponent = AnimationComponent;
|
||||
cc.AudioSourceComponent = AudioSourceComponent;
|
||||
cc.CameraComponent = CameraComponent;
|
||||
cc.EditorComponent = EditorCameraComponent;
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
import { LegacyAnimationComponent } from '../components/legacy-animation-component';
|
||||
import { AnimationComponent } from '../components/animation-component';
|
||||
import { binarySearchEpsilon as binarySearch } from '../core/data/utils/binary-search';
|
||||
import { MutableForwardIterator } from '../core/utils/array';
|
||||
import { Node } from '../scene-graph';
|
||||
import { LegacyAnimationClip } from './animation-clip';
|
||||
import { EventAnimCurve, EventInfo } from './animation-curve';
|
||||
import { LegacyAnimationState } from './animation-state';
|
||||
import { AnimationState } from './animation-state';
|
||||
import { Playable } from './playable';
|
||||
import { WrapModeMask } from './types';
|
||||
|
||||
@@ -12,13 +11,13 @@ import { WrapModeMask } from './types';
|
||||
* The actual animator for Animation Component.
|
||||
*/
|
||||
export class AnimationAnimator extends Playable {
|
||||
public _anims = new MutableForwardIterator<LegacyAnimationState>([]);
|
||||
public _anims = new MutableForwardIterator<AnimationState>([]);
|
||||
|
||||
constructor (public target: Node, public animation: LegacyAnimationComponent) {
|
||||
constructor (public target: Node, public animation: AnimationComponent) {
|
||||
super();
|
||||
}
|
||||
|
||||
public playState (state: LegacyAnimationState, startTime?: number) {
|
||||
public playState (state: AnimationState, startTime?: number) {
|
||||
if (!state.clip) {
|
||||
return;
|
||||
}
|
||||
@@ -37,7 +36,7 @@ export class AnimationAnimator extends Playable {
|
||||
this.play();
|
||||
}
|
||||
|
||||
public stopStatesExcept (state: LegacyAnimationState) {
|
||||
public stopStatesExcept (state: AnimationState) {
|
||||
const iterator = this._anims;
|
||||
const array = iterator.array;
|
||||
for (iterator.i = 0; iterator.i < array.length; ++iterator.i) {
|
||||
@@ -84,19 +83,19 @@ export class AnimationAnimator extends Playable {
|
||||
}
|
||||
}
|
||||
|
||||
public stopState (state: LegacyAnimationState) {
|
||||
public stopState (state: AnimationState) {
|
||||
if (state) {
|
||||
state.stop();
|
||||
}
|
||||
}
|
||||
|
||||
public pauseState (state: LegacyAnimationState) {
|
||||
public pauseState (state: AnimationState) {
|
||||
if (state) {
|
||||
state.pause();
|
||||
}
|
||||
}
|
||||
|
||||
public resumeState (state: LegacyAnimationState) {
|
||||
public resumeState (state: AnimationState) {
|
||||
if (state) {
|
||||
state.resume();
|
||||
}
|
||||
@@ -106,14 +105,14 @@ export class AnimationAnimator extends Playable {
|
||||
}
|
||||
}
|
||||
|
||||
public setStateTime (state: LegacyAnimationState, time: number): void;
|
||||
public setStateTime (state: AnimationState, time: number): void;
|
||||
|
||||
public setStateTime (time: number): void;
|
||||
|
||||
public setStateTime (state: LegacyAnimationState | number, time?: number) {
|
||||
public setStateTime (state: AnimationState | number, time?: number) {
|
||||
if (time !== undefined) {
|
||||
(state as LegacyAnimationState).setTime(time);
|
||||
(state as LegacyAnimationState).sample();
|
||||
(state as AnimationState).setTime(time);
|
||||
(state as AnimationState).sample();
|
||||
}
|
||||
else {
|
||||
for (const anim of this._anims.array) {
|
||||
@@ -149,7 +148,7 @@ export class AnimationAnimator extends Playable {
|
||||
}
|
||||
}
|
||||
|
||||
public _reloadClip (state: LegacyAnimationState) {
|
||||
public _reloadClip (state: AnimationState) {
|
||||
initClipData(this.target, state);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,8 +43,8 @@ function isLerpable (object: any): object is ILerpable {
|
||||
return object.lerp;
|
||||
}
|
||||
|
||||
@ccclass('cc.LegacyAnimationClip')
|
||||
export class LegacyAnimationClip extends Asset {
|
||||
@ccclass('cc.AnimationClip')
|
||||
export class AnimationClip extends Asset {
|
||||
|
||||
/**
|
||||
* !#en Duration of this animation.
|
||||
@@ -67,7 +67,7 @@ export class LegacyAnimationClip extends Asset {
|
||||
return null;
|
||||
}
|
||||
|
||||
const clip = new LegacyAnimationClip();
|
||||
const clip = new AnimationClip();
|
||||
clip.sample = sample || clip.sample;
|
||||
|
||||
clip._duration = spriteFrames.length / clip.sample;
|
||||
@@ -289,4 +289,4 @@ function lerpObject (from: ILerpable, to: ILerpable, t: number): ILerpable {
|
||||
return from.lerp(to, t);
|
||||
}
|
||||
|
||||
cc.LegacyAnimationClip = LegacyAnimationClip;
|
||||
cc.AnimationClip = AnimationClip;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { ccclass } from '../core/data/class-decorator';
|
||||
import { errorID } from '../core/platform/CCDebug';
|
||||
import { Node } from '../scene-graph';
|
||||
import { LegacyAnimationState } from './animation-state';
|
||||
import { AnimationState } from './animation-state';
|
||||
|
||||
@ccclass
|
||||
export class AnimationManager {
|
||||
@@ -36,14 +36,14 @@ export class AnimationManager {
|
||||
|
||||
}
|
||||
|
||||
public addAnimation (anim: LegacyAnimationState) {
|
||||
public addAnimation (anim: AnimationState) {
|
||||
const index = this._anims.array.indexOf(anim);
|
||||
if (index === -1) {
|
||||
this._anims.push(anim);
|
||||
}
|
||||
}
|
||||
|
||||
public removeAnimation (anim: LegacyAnimationState) {
|
||||
public removeAnimation (anim: AnimationState) {
|
||||
const index = this._anims.array.indexOf(anim);
|
||||
if (index >= 0) {
|
||||
this._anims.fastRemoveAt(index);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Node } from '../scene-graph';
|
||||
import { AnimationAnimator } from './animation-animator';
|
||||
import { LegacyAnimationClip } from './animation-clip';
|
||||
import { AnimationClip } from './animation-clip';
|
||||
import { AnimCurve } from './animation-curve';
|
||||
import { Playable } from './playable';
|
||||
import { WrapMode, WrapModeMask, WrappedInfo } from './types';
|
||||
@@ -14,7 +14,7 @@ import { WrapMode, WrapModeMask, WrappedInfo } from './types';
|
||||
* 大多数情况下 动画组件 是足够和易于使用的。如果您需要更多的动画控制接口,请使用 AnimationState。
|
||||
*
|
||||
*/
|
||||
export class LegacyAnimationState extends Playable {
|
||||
export class AnimationState extends Playable {
|
||||
|
||||
/**
|
||||
* !#en The clip that is being played by this animation state.
|
||||
@@ -176,11 +176,11 @@ export class LegacyAnimationState extends Playable {
|
||||
private _lastWrappedInfo: WrappedInfo | null = null;
|
||||
private _process = this.process;
|
||||
private _target: Node | null = null;
|
||||
private _clip: LegacyAnimationClip;
|
||||
private _clip: AnimationClip;
|
||||
private _name: string;
|
||||
private _lastIterations?: number;
|
||||
|
||||
constructor (clip: LegacyAnimationClip, name?: string) {
|
||||
constructor (clip: AnimationClip, name?: string) {
|
||||
super();
|
||||
this._clip = clip;
|
||||
this._name = name || (clip && clip.name);
|
||||
@@ -460,4 +460,4 @@ export class LegacyAnimationState extends Playable {
|
||||
}
|
||||
}
|
||||
|
||||
cc.LegacyAnimationState = LegacyAnimationState;
|
||||
cc.AnimationState = AnimationState;
|
||||
|
||||
+19
-19
@@ -1,5 +1,5 @@
|
||||
|
||||
import { AnimationAnimator, LegacyAnimationClip, LegacyAnimationState } from '../animation';
|
||||
import { AnimationAnimator, AnimationClip, AnimationState } from '../animation';
|
||||
import { ccclass, executeInEditMode, executionOrder, menu, property } from '../core/data/class-decorator';
|
||||
import { EventTargetFactory, IEventTargetCallback } from '../core/event/event-target-factory';
|
||||
import { warnID } from '../core/platform/CCDebug';
|
||||
@@ -67,11 +67,11 @@ ccenum(EventType);
|
||||
* - lastframe : 假如动画循环次数大于 1,当动画播放到最后一帧时
|
||||
* - finished : 动画播放完成时
|
||||
*/
|
||||
@ccclass('cc.LegacyAnimationComponent')
|
||||
@ccclass('cc.AnimationComponent')
|
||||
@executionOrder(99)
|
||||
@executeInEditMode
|
||||
@menu('Components/AnimationComponent')
|
||||
export class LegacyAnimationComponent extends EventTargetFactory(Component) {
|
||||
export class AnimationComponent extends EventTargetFactory(Component) {
|
||||
|
||||
@property({ type: Boolean })
|
||||
get preview () {
|
||||
@@ -93,7 +93,7 @@ export class LegacyAnimationComponent extends EventTargetFactory(Component) {
|
||||
* !#en Animation will play the default clip when start game.
|
||||
* !#zh 在勾选自动播放或调用 play() 时默认播放的动画剪辑。
|
||||
*/
|
||||
@property({ type: LegacyAnimationClip })
|
||||
@property({ type: AnimationClip })
|
||||
get defaultClip () {
|
||||
return this._defaultClip;
|
||||
}
|
||||
@@ -132,7 +132,7 @@ export class LegacyAnimationComponent extends EventTargetFactory(Component) {
|
||||
this._currentClip = value;
|
||||
}
|
||||
|
||||
@property({ type: [LegacyAnimationClip] })
|
||||
@property({ type: [AnimationClip] })
|
||||
get clips () {
|
||||
return this._clips;
|
||||
}
|
||||
@@ -149,17 +149,17 @@ export class LegacyAnimationComponent extends EventTargetFactory(Component) {
|
||||
private _animator: AnimationAnimator | null = null;
|
||||
private _nameToState = createMap(true);
|
||||
private _didInit = false;
|
||||
private _currentClip: LegacyAnimationClip | null = null;
|
||||
private _currentClip: AnimationClip | null = null;
|
||||
|
||||
/**
|
||||
* !#en All the clips used in this animation.
|
||||
* !#zh 通过脚本可以访问并播放的 AnimationClip 列表。
|
||||
*/
|
||||
@property({ type: [LegacyAnimationClip] })
|
||||
private _clips: LegacyAnimationClip[] = [];
|
||||
@property({ type: [AnimationClip] })
|
||||
private _clips: AnimationClip[] = [];
|
||||
|
||||
@property
|
||||
private _defaultClip: LegacyAnimationClip | null = null;
|
||||
private _defaultClip: AnimationClip | null = null;
|
||||
|
||||
constructor () {
|
||||
super();
|
||||
@@ -378,7 +378,7 @@ export class LegacyAnimationComponent extends EventTargetFactory(Component) {
|
||||
* @param clip the clip to add
|
||||
* @return The AnimationState which gives full control over the animation clip.
|
||||
*/
|
||||
public addClip (clip: LegacyAnimationClip, newName?: string): LegacyAnimationState | undefined {
|
||||
public addClip (clip: AnimationClip, newName?: string): AnimationState | undefined {
|
||||
if (!clip) {
|
||||
warnID(3900);
|
||||
return;
|
||||
@@ -406,7 +406,7 @@ export class LegacyAnimationComponent extends EventTargetFactory(Component) {
|
||||
}
|
||||
|
||||
// replace state
|
||||
const newState = new LegacyAnimationState(clip, newName);
|
||||
const newState = new AnimationState(clip, newName);
|
||||
this._nameToState[newName] = newState;
|
||||
return newState;
|
||||
}
|
||||
@@ -422,7 +422,7 @@ export class LegacyAnimationComponent extends EventTargetFactory(Component) {
|
||||
* 但是如果 force 参数为 true,则会强制停止该动画,然后移除该动画剪辑和相关的动画。这时候如果 clip 是 defaultClip,defaultClip 将会被重置为 null。
|
||||
* @param {Boolean} [force=false] - If force is true, then will always remove the clip and any animation states based on it.
|
||||
*/
|
||||
public removeClip (clip: LegacyAnimationClip, force?: boolean) {
|
||||
public removeClip (clip: AnimationClip, force?: boolean) {
|
||||
if (!clip) {
|
||||
warnID(3901);
|
||||
return;
|
||||
@@ -512,7 +512,7 @@ export class LegacyAnimationComponent extends EventTargetFactory(Component) {
|
||||
* // register event to all animation
|
||||
* animation.on('play', this.onPlay, this);
|
||||
*/
|
||||
public on (type: string, callback: (state: LegacyAnimationState) => void, target?: Object | null, useCapture?: boolean) {
|
||||
public on (type: string, callback: (state: AnimationState) => void, target?: Object | null, useCapture?: boolean) {
|
||||
this._init();
|
||||
|
||||
const ret = super.on(type, callback, target, useCapture);
|
||||
@@ -578,11 +578,11 @@ export class LegacyAnimationComponent extends EventTargetFactory(Component) {
|
||||
this._nameToState = createMap(true);
|
||||
|
||||
// create animation states
|
||||
let state: LegacyAnimationState | null = null;
|
||||
let defaultClipState: LegacyAnimationState | undefined;
|
||||
let state: AnimationState | null = null;
|
||||
let defaultClipState: AnimationState | undefined;
|
||||
for (const clip of this._clips) {
|
||||
if (clip) {
|
||||
state = new LegacyAnimationState(clip);
|
||||
state = new AnimationState(clip);
|
||||
|
||||
if (CC_EDITOR) {
|
||||
this._animator!._reloadClip(state);
|
||||
@@ -595,7 +595,7 @@ export class LegacyAnimationComponent extends EventTargetFactory(Component) {
|
||||
}
|
||||
}
|
||||
if (this._defaultClip && !defaultClipState) {
|
||||
state = new LegacyAnimationState(this._defaultClip);
|
||||
state = new AnimationState(this._defaultClip);
|
||||
|
||||
if (CC_EDITOR) {
|
||||
this._animator!._reloadClip(state);
|
||||
@@ -606,9 +606,9 @@ export class LegacyAnimationComponent extends EventTargetFactory(Component) {
|
||||
}
|
||||
}
|
||||
|
||||
cc.LegacyAnimationComponent = LegacyAnimationComponent;
|
||||
cc.AnimationComponent = AnimationComponent;
|
||||
|
||||
function equalClips (clip1: LegacyAnimationClip | null, clip2: LegacyAnimationClip | null) {
|
||||
function equalClips (clip1: AnimationClip | null, clip2: AnimationClip | null) {
|
||||
if (clip1 === clip2) {
|
||||
return true;
|
||||
}
|
||||
@@ -27,4 +27,4 @@
|
||||
export { Component } from './component';
|
||||
export { EventHandler } from './component-event-handler';
|
||||
export {default as MissingScript} from './missing-script';
|
||||
export { LegacyAnimationComponent } from './legacy-animation-component';
|
||||
export { AnimationComponent } from './animation-component';
|
||||
|
||||
Reference in New Issue
Block a user