Skip to content

Commit

Permalink
アニメーションコントローラー追加 / まばたきの動作をAnimationControllerに変更
Browse files Browse the repository at this point in the history
  • Loading branch information
sh-akira committed Jul 16, 2018
1 parent 85f97d5 commit 4372dc2
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 55 deletions.
128 changes: 128 additions & 0 deletions Assets/Scripts/AnimationController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

public class AnimationController
{
public class AnimationItem
{
public float Time { get; set; } //アニメーションにかける時間
public float StartValue { get; set; }
public float EndValue { get; set; }
public System.Action<float> SetAction { get; set; }
public System.Func<float> TimeInitializer { get; set; }

public void RunAction(float value)
{
SetAction?.Invoke(value);
}

public void Initialize()
{
if (TimeInitializer != null)
{
Time = TimeInitializer();
}
}
}

private bool isStart = false;
private float startTime = 0.0f;
public System.Action ResetAction { get; set; }

public List<AnimationItem> AnimationItems = new List<AnimationItem>();

public Dictionary<float, AnimationItem> CurrentAnimationItems = new Dictionary<float, AnimationItem>(); //Key:開始時間


private AnimationItem EndLastItem = null;
private AnimationItem CurrentItem = null;

private void InitializeAnimation()
{
CurrentAnimationItems.Clear();
EndLastItem = null;
CurrentItem = null;
var starttime = 0.0f;
foreach (var item in AnimationItems)
{
item.Initialize();
CurrentAnimationItems.Add(starttime, item);
starttime += item.Time == 0.0f ? 0.0001f : item.Time;
}
}

public void AddResetAction(System.Action resetAction)
{
ResetAction = resetAction;
}

public void AddWait(float? time, System.Func<float> timeInitializer = null)
{
AddAnimation(time, 0.0f, 0.0f, null, timeInitializer);
}

public void AddAnimation(float? time, float startValue, float endValue, System.Action<float> setAction, System.Func<float> timeInitializer = null)
{
AnimationItems.Add(new AnimationItem { Time = time ?? 0.0f, StartValue = startValue, EndValue = endValue, SetAction = setAction, TimeInitializer = timeInitializer });
}

public void Reset()
{
ResetAction?.Invoke();
}

public void ClearAnimations()
{
AnimationItems.Clear();
}

public bool Next()
{
if (isStart == false)
{
isStart = true;
startTime = Time.time;
InitializeAnimation();
}

var elapsedTime = Time.time - startTime;
var addTime = 0.0f;
AnimationItem lastitem = null;
foreach (var item in CurrentAnimationItems)
{
addTime = item.Key + item.Value.Time; //すべてのアニメーションの時間+今のアニメーション時間
if (addTime >= elapsedTime)
{//経過時間がまだアニメーションの終了時間に届いていない間(アニメーション中)
if (lastitem != null && EndLastItem != lastitem)
{//前回のアニメーションが終わりまで行ってない場合があるので100%で実行
lastitem.RunAction(lastitem.EndValue);
EndLastItem = lastitem;
}
if (CurrentItem != item.Value)
{//新しいアニメーションになったときには時間にかかわらずきちんと最初の値を使う
item.Value.RunAction(item.Value.StartValue);
CurrentItem = item.Value;
}
else
{
var currentTime = item.Value.Time + (elapsedTime - addTime);
var setvalue = item.Value.StartValue + (item.Value.EndValue - item.Value.StartValue) * (currentTime / item.Value.Time);
item.Value.RunAction(setvalue);
}
lastitem = item.Value;
return true;
}
}

//最後までアニメーションしたとき
if (lastitem != null)
{//最後のアニメーションが終わりまで行ってない場合があるので100%で実行
lastitem.RunAction(lastitem.EndValue);
}
isStart = false;
return false;
}
}
11 changes: 11 additions & 0 deletions Assets/Scripts/AnimationController.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 20 additions & 55 deletions Assets/Scripts/BlinkController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using VRM;

Expand Down Expand Up @@ -43,72 +44,28 @@ public BlendShapePreset DefaultFace
}
public string FacePresetName = null;

Coroutine m_coroutine;
private AnimationController animationController;

public void ImportVRMmodel(GameObject vrmmodel)
{
VRMmodel = vrmmodel;
proxy = null;
}

IEnumerator BlinkRoutine()
private void Start()
{
while (true)
{
while (EnableBlink)
{
while (proxy == null)
{
yield return null;
}
var nextBlinkTime = Time.time + BlinkTimeMin + Random.value * (BlinkTimeMax - BlinkTimeMin);
while (nextBlinkTime > Time.time)
{
yield return null;
}

// 閉じる
var value = 0.0f;
var closeSpeed = 1.0f / CloseAnimationTime;
while (true)
{
value += Time.deltaTime * closeSpeed;
if (value >= 1.0f)
{
break;
}

proxy.SetValue(BlendShapePreset.Blink, value);
yield return null;
}
proxy.SetValue(BlendShapePreset.Blink, 1.0f);

// wait...
yield return new WaitForSeconds(ClosingTime);

// open
value = 1.0f;
var openSpeed = 1.0f / OpenAnimationTime;
while (true)
{
value -= Time.deltaTime * openSpeed;
if (value < 0)
{
break;
}

proxy.SetValue(BlendShapePreset.Blink, value);
yield return null;
}
proxy.SetValue(BlendShapePreset.Blink, 0);
}
yield return null; //EnableBlink == false
}
CreateAnimation();
}

private void Start()
private void CreateAnimation()
{
m_coroutine = StartCoroutine(BlinkRoutine());
if (animationController == null) animationController = new AnimationController();
animationController.ClearAnimations();
animationController.AddResetAction(() => proxy.SetValue(BlendShapePreset.Blink, 0.0f));
animationController.AddWait(null, () => BlinkTimeMin + Random.value * (BlinkTimeMax - BlinkTimeMin));
animationController.AddAnimation(CloseAnimationTime, 0.0f, 1.0f, v => proxy.SetValue(BlendShapePreset.Blink, v));
animationController.AddWait(ClosingTime);
animationController.AddAnimation(OpenAnimationTime, 1.0f, 0.0f, v => proxy.SetValue(BlendShapePreset.Blink, v));
}

// Update is called once per frame
Expand All @@ -123,6 +80,14 @@ void Update()
proxy = VRMmodel.GetComponent<VRMBlendShapeProxy>();
}
}
if (animationController?.Next() == false)
{//最後まで行ったら値更新のためにアニメーション作り直す
CreateAnimation();
}
}
else
{
animationController?.Reset();
}

if (DefaultFace != BlendShapePreset.Neutral && proxy != null)
Expand Down

0 comments on commit 4372dc2

Please sign in to comment.