From ef119af6ec775a9cbc6b7fb22385aa9fe4580ee2 Mon Sep 17 00:00:00 2001 From: ChiaNing Date: Sun, 25 Apr 2021 20:20:22 +0800 Subject: [PATCH] hot point (#8540) * hot point * Revert * Optimize non-skeletal curves Co-authored-by: shrinktofit --- cocos/2d/components/sprite.ts | 5 +- cocos/2d/renderer/batcher-2d.ts | 60 ++++++++----- cocos/core/animation/animation-clip.ts | 8 ++ cocos/core/animation/animation-manager.ts | 4 +- cocos/core/animation/animation-state.ts | 50 ++++++----- cocos/core/animation/cross-fade.ts | 101 +++++++++++++--------- 6 files changed, 140 insertions(+), 88 deletions(-) diff --git a/cocos/2d/components/sprite.ts b/cocos/2d/components/sprite.ts index 6794be12ec..f11c8a425f 100644 --- a/cocos/2d/components/sprite.ts +++ b/cocos/2d/components/sprite.ts @@ -546,6 +546,7 @@ export class Sprite extends Renderable2D { public changeMaterialForDefine () { let texture; + const lastInstanceMaterialType = this._instanceMaterialType; if (this._spriteFrame) { texture = this._spriteFrame.texture; } @@ -564,7 +565,9 @@ export class Sprite extends Renderable2D { } else { this._instanceMaterialType = InstanceMaterialType.ADD_COLOR_AND_TEXTURE; } - this.updateMaterial(); + if (lastInstanceMaterialType !== this._instanceMaterialType) { + this.updateMaterial(); + } } protected _updateBuiltinMaterial () { diff --git a/cocos/2d/renderer/batcher-2d.ts b/cocos/2d/renderer/batcher-2d.ts index 51bd09c07f..5ebf8ac150 100644 --- a/cocos/2d/renderer/batcher-2d.ts +++ b/cocos/2d/renderer/batcher-2d.ts @@ -49,6 +49,7 @@ import { SpriteFrame } from '../assets'; import { TextureBase } from '../../core/assets/texture-base'; import { sys } from '../../core/platform/sys'; import { Mat4 } from '../../core/math'; +import { value } from '../../core/utils/js-typed'; const _dsInfo = new DescriptorSetInfo(null!); const m4_1 = new Mat4(); @@ -274,32 +275,47 @@ export class Batcher2D { public uploadBuffers () { if (this._batches.length > 0) { const calls = this._doUploadBuffersCall; - for (const key of calls.keys()) { - const list = calls.get(key); - list!.call(key, this); - } + calls.forEach((value, key) => { + value.call(key, this); + }); + // for (const key of calls.keys()) { + // const list = calls.get(key); + // list!.call(key, this); + // } const buffers = this._meshBuffers; - for (const i of buffers.keys()) { - const list = buffers.get(i); - if (list) { - list.forEach((bb) => { - bb.uploadBuffers(); - bb.reset(); - }); - } - } + buffers.forEach((value, key) => { + value.forEach((bb) => { + bb.uploadBuffers(); + bb.reset(); + }); + }); + // for (const i of buffers.keys()) { + // const list = buffers.get(i); + // if (list) { + // list.forEach((bb) => { + // bb.uploadBuffers(); + // bb.reset(); + // }); + // } + // } const customs = this._customMeshBuffers; - for (const i of customs.keys()) { - const list = customs.get(i); - if (list) { - list.forEach((bb) => { - bb.uploadBuffers(); - bb.reset(); - }); - } - } + customs.forEach((value, key) => { + value.forEach((bb) => { + bb.uploadBuffers(); + bb.reset(); + }); + }); + // for (const i of customs.keys()) { + // const list = customs.get(i); + // if (list) { + // list.forEach((bb) => { + // bb.uploadBuffers(); + // bb.reset(); + // }); + // } + // } this._descriptorSetCache.update(); } diff --git a/cocos/core/animation/animation-clip.ts b/cocos/core/animation/animation-clip.ts index eafd78b064..9cefd039a0 100644 --- a/cocos/core/animation/animation-clip.ts +++ b/cocos/core/animation/animation-clip.ts @@ -190,6 +190,14 @@ export class AnimationClip extends Asset { @serializable public events: AnimationClip.IEvent[] = []; + /** + * Sets if node TRS curves in this animation can be blended. + * Normally this flag is enabled for model animation and disabled for other case. + * @internal This is an internal slot. Never use it in your code. + */ + @serializable + public enableTrsBlending = false; + @serializable private _duration = 0; diff --git a/cocos/core/animation/animation-manager.ts b/cocos/core/animation/animation-manager.ts index 6a1ccd40ff..c2d0ed7f70 100644 --- a/cocos/core/animation/animation-manager.ts +++ b/cocos/core/animation/animation-manager.ts @@ -80,8 +80,8 @@ export class AnimationManager extends System { } const iterator = this._anims; const array = iterator.array; - for (iterator.i = 0; iterator.i < array.length; ++iterator.i) { - const anim = array[iterator.i]; + for (let i = 0, length = array.length; i < length; ++i) { + const anim = array[i]; if (!anim.isMotionless) { anim.update(dt); } diff --git a/cocos/core/animation/animation-state.ts b/cocos/core/animation/animation-state.ts index 4e0e264b86..5074ba780b 100644 --- a/cocos/core/animation/animation-state.ts +++ b/cocos/core/animation/animation-state.ts @@ -287,8 +287,9 @@ export class AnimationState extends Playable { set playbackRange (value) { assertIsTrue(value.max > value.min); - this._playbackRange.min = value.min; - this._playbackRange.max = value.max; + this._playbackRange.min = Math.max(this._playbackRange.min, 0); + this._playbackRange.max = Math.min(this._playbackRange.max, this.duration); + this._playbackDuration = this._playbackRange.max - this._playbackRange.min; this.setTime(0.0); } @@ -373,6 +374,8 @@ export class AnimationState extends Playable { enabled: false, }; private _playbackRange: { min: number; max: number; }; + private _playbackDuration = 0; + private _invDuration = 1.0; constructor (clip: AnimationClip, name = '') { super(); @@ -382,6 +385,7 @@ export class AnimationState extends Playable { min: 0, max: this._clip.duration, }; + this._playbackDuration = clip.duration; } /** @@ -404,6 +408,7 @@ export class AnimationState extends Playable { const clip = this._clip; this.duration = clip.duration; + this._invDuration = 1.0 / this.duration; this.speed = clip.speed; this.wrapMode = clip.wrapMode; this.frameRate = clip.sample; @@ -411,6 +416,7 @@ export class AnimationState extends Playable { min: 0, max: clip.duration, }; + this._playbackDuration = clip.duration; if ((this.wrapMode & WrapModeMask.Loop) === WrapModeMask.Loop) { this.repeatCount = Infinity; @@ -428,7 +434,7 @@ export class AnimationState extends Playable { valueAdapter: IValueProxyFactory | undefined, isConstant: boolean, ): BoundTargetT | null => { - if (!isTargetingTRS(path) || !this._blendStateBuffer) { + if (!clip.enableTrsBlending || !isTargetingTRS(path) || !this._blendStateBuffer) { return createFn(rootTarget, path, valueAdapter); } else { const targetNode = evaluatePath(rootTarget, ...path.slice(0, path.length - 1)); @@ -670,9 +676,10 @@ export class AnimationState extends Playable { this._blendStateWriterHost.weight = this.weight; this._blendStateWriterHost.enabled = true; + const commonTargetStatuses = this._commonTargetStatuses; // Before we sample, we pull values of common targets. - for (let iCommonTarget = 0; iCommonTarget < this._commonTargetStatuses.length; ++iCommonTarget) { - const commonTargetStatus = this._commonTargetStatuses[iCommonTarget]; + for (let iCommonTarget = 0, length = commonTargetStatuses.length; iCommonTarget < length; ++iCommonTarget) { + const commonTargetStatus = commonTargetStatuses[iCommonTarget]; if (!commonTargetStatus) { continue; } @@ -680,13 +687,14 @@ export class AnimationState extends Playable { commonTargetStatus.changed = false; } - for (let iSamplerSharedGroup = 0, szSamplerSharedGroup = this._samplerSharedGroups.length; + let index = 0; + let lerpRequired = false; + const samplerSharedGroups = this._samplerSharedGroups; + for (let iSamplerSharedGroup = 0, szSamplerSharedGroup = samplerSharedGroups.length; iSamplerSharedGroup < szSamplerSharedGroup; ++iSamplerSharedGroup) { - const samplerSharedGroup = this._samplerSharedGroups[iSamplerSharedGroup]; - const sampler = samplerSharedGroup.sampler; - const { samplerResultCache } = samplerSharedGroup; - let index = 0; - let lerpRequired = false; + const samplerSharedGroup = samplerSharedGroups[iSamplerSharedGroup]; + const { sampler, samplerResultCache } = samplerSharedGroup; + if (!sampler) { index = 0; } else { @@ -708,9 +716,10 @@ export class AnimationState extends Playable { } } - for (let iCurveInstance = 0, szCurves = samplerSharedGroup.curves.length; + const curves = samplerSharedGroup.curves; + for (let iCurveInstance = 0, szCurves = curves.length; iCurveInstance < szCurves; ++iCurveInstance) { - const curveInstance = samplerSharedGroup.curves[iCurveInstance]; + const curveInstance = curves[iCurveInstance]; curveInstance.applySample(ratio, index, lerpRequired, samplerResultCache, this.weight); if (curveInstance.commonTargetIndex !== undefined) { const commonTargetStatus = this._commonTargetStatuses[curveInstance.commonTargetIndex]; @@ -722,8 +731,8 @@ export class AnimationState extends Playable { } // After sample, we push values of common targets. - for (let iCommonTarget = 0; iCommonTarget < this._commonTargetStatuses.length; ++iCommonTarget) { - const commonTargetStatus = this._commonTargetStatuses[iCommonTarget]; + for (let iCommonTarget = 0, length = commonTargetStatuses.length; iCommonTarget < length; ++iCommonTarget) { + const commonTargetStatus = commonTargetStatuses[iCommonTarget]; if (!commonTargetStatus) { continue; } @@ -759,13 +768,12 @@ export class AnimationState extends Playable { } private simpleProcess () { - const playbackStart = this._getPlaybackStart(); - const playbackEnd = this._getPlaybackEnd(); - const playbackDuration = playbackEnd - playbackStart; + const playbackStart = this._playbackRange.min; + const playbackDuration = this._playbackDuration; let time = this.time % playbackDuration; if (time < 0) { time += playbackDuration; } - const ratio = (playbackStart + time) / this.duration; + const ratio = (playbackStart + time) * this._invDuration; this._sampleCurves(ratio); if (!EDITOR || legacyCC.GAME_VIEW) { @@ -867,11 +875,11 @@ export class AnimationState extends Playable { } private _getPlaybackStart () { - return Math.max(this._playbackRange.min, 0); + return this._playbackRange.min; } private _getPlaybackEnd () { - return Math.min(this._playbackRange.max, this.duration); + return this._playbackRange.max; } private _sampleEvents (wrapInfo: WrappedInfo) { diff --git a/cocos/core/animation/cross-fade.ts b/cocos/core/animation/cross-fade.ts index c973f8f4ef..c990841d05 100644 --- a/cocos/core/animation/cross-fade.ts +++ b/cocos/core/animation/cross-fade.ts @@ -58,52 +58,20 @@ export class CrossFade extends Playable { return; } - // Set all state's weight to 0. - for (let iManagedState = 0; iManagedState < this._managedStates.length; ++iManagedState) { - const state = this._managedStates[iManagedState].state; + const managedStates = this._managedStates; + const fadings = this._fadings; + + if (managedStates.length === 1 && fadings.length === 1) { + const state = managedStates[0].state; if (state) { - state.weight = 0; + state.weight = 1.0; } + } else { + this._calculateWeights(deltaTime); } - // Allocate weights. - let absoluteWeight = 1.0; - let deadFadingBegin = this._fadings.length; - for (let iFading = 0; iFading < this._fadings.length; ++iFading) { - const fading = this._fadings[iFading]; - fading.easeTime += deltaTime; - // We should properly handle the case of - // `fading.easeTime === 0 && fading.easeDuration === 0`, which yields `NaN`. - const relativeWeight = fading.easeDuration === 0 ? 1 : clamp01(fading.easeTime / fading.easeDuration); - const weight = relativeWeight * absoluteWeight; - absoluteWeight *= (1.0 - relativeWeight); - if (fading.target.state) { - fading.target.state.weight += weight; - } - if (fading.easeTime >= fading.easeDuration) { - deadFadingBegin = iFading + 1; - fading.easeTime = fading.easeDuration; - break; - } - } - - // Kill fadings having no lifetime. - if (deadFadingBegin !== this._fadings.length) { - for (let iDeadFading = deadFadingBegin; iDeadFading < this._fadings.length; ++iDeadFading) { - const deadFading = this._fadings[iDeadFading]; - --deadFading.target.reference; - if (deadFading.target.reference <= 0) { - if (deadFading.target.state) { - deadFading.target.state.stop(); - } - remove(this._managedStates, deadFading.target); - } - } - this._fadings.splice(deadFadingBegin); - } - - for (let iManagedState = 0; iManagedState < this._managedStates.length; ++iManagedState) { - const state = this._managedStates[iManagedState].state; + for (let iManagedState = 0; iManagedState < managedStates.length; ++iManagedState) { + const state = managedStates[iManagedState].state; if (state && state.isMotionless) { state.sample(); } @@ -196,4 +164,53 @@ export class CrossFade extends Playable { legacyCC.director.getAnimationManager().removeCrossFade(this); this.clear(); } + + private _calculateWeights (deltaTime: number) { + const managedStates = this._managedStates; + const fadings = this._fadings; + + // Set all state's weight to 0. + for (let iManagedState = 0; iManagedState < managedStates.length; ++iManagedState) { + const state = managedStates[iManagedState].state; + if (state) { + state.weight = 0; + } + } + + // Allocate weights. + let absoluteWeight = 1.0; + let deadFadingBegin = this._fadings.length; + for (let iFading = 0; iFading < this._fadings.length; ++iFading) { + const fading = this._fadings[iFading]; + fading.easeTime += deltaTime; + // We should properly handle the case of + // `fading.easeTime === 0 && fading.easeDuration === 0`, which yields `NaN`. + const relativeWeight = fading.easeDuration === 0 ? 1 : clamp01(fading.easeTime / fading.easeDuration); + const weight = relativeWeight * absoluteWeight; + absoluteWeight *= (1.0 - relativeWeight); + if (fading.target.state) { + fading.target.state.weight += weight; + } + if (fading.easeTime >= fading.easeDuration) { + deadFadingBegin = iFading + 1; + fading.easeTime = fading.easeDuration; + break; + } + } + + // Kill fadings having no lifetime. + if (deadFadingBegin !== this._fadings.length) { + for (let iDeadFading = deadFadingBegin; iDeadFading < this._fadings.length; ++iDeadFading) { + const deadFading = this._fadings[iDeadFading]; + --deadFading.target.reference; + if (deadFading.target.reference <= 0) { + if (deadFading.target.state) { + deadFading.target.state.stop(); + } + remove(this._managedStates, deadFading.target); + } + } + this._fadings.splice(deadFadingBegin); + } + } }