private void UpdateMaterial(float deltaTime, GPUSkinningMaterial currMtrl) { int frameIndex = GetFrameIndex(); if (lastPlayingClip == playingClip && lastPlayingFrameIndex == frameIndex) { res.Update(deltaTime, currMtrl); return; } lastPlayingClip = playingClip; lastPlayingFrameIndex = frameIndex; float blend_crossFade = 1; int frameIndex_crossFade = -1; GPUSkinningFrame frame_crossFade = null; if (res.IsCrossFadeBlending(lastPlayedClip, crossFadeTime, crossFadeProgress)) { frameIndex_crossFade = GetCrossFadeFrameIndex(); frame_crossFade = lastPlayedClip.frames[frameIndex_crossFade]; blend_crossFade = res.CrossFadeBlendFactor(crossFadeProgress, crossFadeTime); } GPUSkinningFrame frame = playingClip.frames[frameIndex]; if (Visible || CullingMode == GPUSKinningCullingMode.AlwaysAnimate) { res.Update(deltaTime, currMtrl); res.UpdatePlayingData( mpb, playingClip, frameIndex, frame, playingClip.rootMotionEnabled && rootMotionEnabled, lastPlayedClip, GetCrossFadeFrameIndex(), crossFadeTime, crossFadeProgress ); mr.SetPropertyBlock(mpb); UpdateJoints(frame); } if (playingClip.rootMotionEnabled && rootMotionEnabled && frameIndex != rootMotionFrameIndex) { if (CullingMode != GPUSKinningCullingMode.CullCompletely) { rootMotionFrameIndex = frameIndex; DoRootMotion(frame_crossFade, 1 - blend_crossFade, false); DoRootMotion(frame, blend_crossFade, true); } } UpdateEvents(playingClip, frameIndex, frame_crossFade == null ? null : lastPlayedClip, frameIndex_crossFade); }
private void UpdateMaterial(float deltaTime) { int frameIndex = GetFrameIndex(); GPUSkinningFrame frame = playingClip.frames[frameIndex]; res.Update(deltaTime); res.UpdatePlayingData(mpb, playingClip, frameIndex, frame, playingClip.rootMotionEnabled && rootMotionEnabled); mr.SetPropertyBlock(mpb); UpdateJoints(frame); if (playingClip.rootMotionEnabled && rootMotionEnabled && frameIndex != rootMotionFrameIndex) { rootMotionFrameIndex = frameIndex; Quaternion rotation = transform.rotation; Quaternion deltaRotation = frame.rootMotionDeltaPositionQ; transform.rotation *= deltaRotation; Vector3 deltaPosition = transform.forward * frame.rootMotionDeltaPositionL; transform.Translate(deltaPosition, Space.World); transform.rotation = rotation; transform.rotation *= frame.rootMotionDeltaRotation; } }
/// <summary> /// 根据当前动画片段播放状态设置Material /// fzy remak:CPU资源消耗过多 /// </summary> /// <param name="deltaTime">本帧Update消耗的时间</param> /// <param name="currMtrl">当前使用的Material</param> private void UpdateMaterial(float deltaTime, GPUSkinningMaterial currMtrl) { int frameIndex = GetFrameIndex(); //Profiler.BeginSample("PlayerResources.Update"); //动画片段(WrapMode.Once)播放完毕 if (lastPlayingClip == playingClip && lastPlayingFrameIndex == frameIndex) { res.Update(deltaTime, currMtrl); return; } //Profiler.EndSample(); //记录上一帧播放的动画片段 lastPlayingClip = playingClip; //记录上一次播放的动画片段帧数(有可能跳帧) lastPlayingFrameIndex = frameIndex; float blend_crossFade = 1; int frameIndex_crossFade = -1; // 新建动画帧,用于crossFade GPUSkinningFrame frame_crossFade = null; //Profiler.BeginSample("PlayerResources.CrossFadeBlending"); if (res.IsCrossFadeBlending(lastPlayedClip, crossFadeTime, crossFadeProgress)) { frameIndex_crossFade = GetCrossFadeFrameIndex(); frame_crossFade = lastPlayedClip.frames[frameIndex_crossFade]; blend_crossFade = res.CrossFadeBlendFactor(crossFadeProgress, crossFadeTime); } //Profiler.EndSample(); //Profiler.BeginSample("PlayerResources.Update"); GPUSkinningFrame frame = playingClip.frames[frameIndex]; //模型可以被看见(Culling)或者CullingMode为AlwaysAnimate if (Visible || CullingMode == GPUSKinningCullingMode.AlwaysAnimate) { res.Update(deltaTime, currMtrl); res.UpdatePlayingData( mpb, playingClip, frameIndex, frame, playingClip.rootMotionEnabled && rootMotionEnabled, lastPlayedClip, GetCrossFadeFrameIndex(), crossFadeTime, crossFadeProgress ); mr.SetPropertyBlock(mpb); //bone.isExposed UpdateJoints(frame); } //Profiler.EndSample(); //Profiler.BeginSample("RootMotion"); if (playingClip.rootMotionEnabled && rootMotionEnabled && frameIndex != rootMotionFrameIndex) { if (CullingMode != GPUSKinningCullingMode.CullCompletely) { rootMotionFrameIndex = frameIndex; DoRootMotion(frame_crossFade, 1 - blend_crossFade, false); DoRootMotion(frame, blend_crossFade, true); } } //Profiler.EndSample(); UpdateEvents(playingClip, frameIndex, frame_crossFade == null ? null : lastPlayedClip, frameIndex_crossFade); }