123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using kt.Utility;
- using UnityEngine;
- public class TongueCtrl
- {
- public TongueCtrl(TBodySkin tgt_face, GameObject face_obj)
- {
- TongueCtrl $this = this;
- this.face = tgt_face;
- this.body = this.face.body;
- Action<string> action = delegate(string bone_name)
- {
- Transform transform = face_obj.transform.SearchChildByName(bone_name);
- $this.BoneDataDic[transform] = new TongueCtrl.BoneData(transform);
- };
- action("Bero1");
- action("Bero2");
- action("Bero3");
- action("Bero4");
- action("Bero5");
- action("Bero_nub");
- foreach (TongueCtrl.FaceMorph faceMorph in (TongueCtrl.FaceMorph[])Enum.GetValues(typeof(TongueCtrl.FaceMorph)))
- {
- this.FaceMorphDataDic[faceMorph] = new TongueCtrl.MorphData(faceMorph, this.face.morph as TMorphSkin);
- }
- }
- public void TongueReset()
- {
- this.LastIsEnable = this.isEnable;
- this.isEnable = false;
- this.IsFirstFrame = true;
- this.BlendStatusReset();
- foreach (TongueCtrl.BoneData boneData in this.BoneDataDic.Values)
- {
- Transform bone = boneData.bone;
- boneData.chacheLocalPos = bone.localPosition;
- boneData.chacheLocalAngles = bone.localEulerAngles;
- bone.localPosition = boneData.initLocalPos;
- bone.localEulerAngles = boneData.initLocalAngles;
- }
- foreach (KeyValuePair<TongueCtrl.FaceMorph, TongueCtrl.MorphData> keyValuePair in this.FaceMorphDataDic)
- {
- TongueCtrl.MorphData value = keyValuePair.Value;
- value.SaveBlendBaseValue();
- if (TongueCtrl.TongueUseMorph.Contains(keyValuePair.Key))
- {
- value.targetValue = 0f;
- }
- }
- }
- public void BlendStatusReset()
- {
- this.BlendWeight = 0f;
- this.ElapsedTime = 0f;
- }
- public void SetTargetValue(TongueCtrl.FaceMorph morph, float value)
- {
- this.GetMorphData(morph).targetValue = value;
- }
- public TongueCtrl.MorphData GetMorphData(TongueCtrl.FaceMorph morph)
- {
- return this.FaceMorphDataDic[morph];
- }
- public void TongueUpdate()
- {
- bool flag = this.BlendWeight < 1f;
- if (flag)
- {
- this.ElapsedTime += Time.deltaTime;
- this.BlendWeight = ((this.blendTime <= 0f) ? 1f : Mathf.Clamp01(this.ElapsedTime / this.blendTime));
- }
- if (this.isEnable)
- {
- foreach (TongueCtrl.FaceMorph key in TongueCtrl.TongueNotUseMorph)
- {
- this.FaceMorphDataDic[key].targetValue = 0f;
- this.FaceMorphDataDic[key].Blend(this.BlendWeight);
- }
- foreach (TongueCtrl.FaceMorph key2 in TongueCtrl.TongueUseMorph)
- {
- this.FaceMorphDataDic[key2].Blend(this.BlendWeight);
- }
- bool boBallGAG = this.face.morph.boBallGAG;
- bool boLookTooth = this.face.morph.boLookTooth;
- bool boLipSync = this.face.morph.boLipSync;
- this.face.morph.boBallGAG = false;
- this.face.morph.boLookTooth = false;
- this.face.morph.boLipSync = false;
- this.face.morph.FixBlendValues_Face();
- this.face.morph.boBallGAG = boBallGAG;
- this.face.morph.boLookTooth = boLookTooth;
- this.face.morph.boLipSync = boLipSync;
- }
- else
- {
- if (!flag || !this.LastIsEnable)
- {
- this.IsFirstFrame = false;
- return;
- }
- if (this.IsFirstFrame)
- {
- foreach (TongueCtrl.FaceMorph key3 in TongueCtrl.TongueUseMorph)
- {
- this.FaceMorphDataDic[key3].targetValue = this.FaceMorphDataDic[key3].morphValue;
- }
- }
- foreach (TongueCtrl.FaceMorph key4 in TongueCtrl.TongueUseMorph)
- {
- this.FaceMorphDataDic[key4].Blend(this.BlendWeight);
- }
- this.face.morph.FixBlendValues_Face();
- foreach (TongueCtrl.BoneData boneData in this.BoneDataDic.Values)
- {
- Transform bone = boneData.bone;
- Vector3 localPosition = Vector3.Lerp(boneData.chacheLocalPos, boneData.initLocalPos, this.BlendWeight);
- Vector3 localEulerAngles = Vector3.Lerp(boneData.chacheLocalAngles, boneData.initLocalAngles, this.BlendWeight);
- bone.localPosition = localPosition;
- bone.localEulerAngles = localEulerAngles;
- }
- }
- this.IsFirstFrame = false;
- }
- public static readonly TongueCtrl.FaceMorph[] TongueNotUseMorph = new TongueCtrl.FaceMorph[]
- {
- TongueCtrl.FaceMorph.TangA,
- TongueCtrl.FaceMorph.TangOpen,
- TongueCtrl.FaceMorph.TangOut,
- TongueCtrl.FaceMorph.TangS,
- TongueCtrl.FaceMorph.TangUp
- };
- public static readonly TongueCtrl.FaceMorph[] TongueUseMorph = new TongueCtrl.FaceMorph[]
- {
- TongueCtrl.FaceMorph.TangRoll,
- TongueCtrl.FaceMorph.TangWidth,
- TongueCtrl.FaceMorph.TangI,
- TongueCtrl.FaceMorph.MouthFera,
- TongueCtrl.FaceMorph.MouthFeraR,
- TongueCtrl.FaceMorph.MouthA,
- TongueCtrl.FaceMorph.MouthS
- };
- public readonly TBody body;
- public readonly TBodySkin face;
- private readonly Dictionary<TongueCtrl.FaceMorph, TongueCtrl.MorphData> FaceMorphDataDic = new Dictionary<TongueCtrl.FaceMorph, TongueCtrl.MorphData>();
- public bool isEnable;
- private bool LastIsEnable;
- private float ElapsedTime;
- public float blendTime = 0.5f;
- private float BlendWeight;
- private Dictionary<Transform, TongueCtrl.BoneData> BoneDataDic = new Dictionary<Transform, TongueCtrl.BoneData>();
- private bool IsFirstFrame;
- public enum FaceMorph
- {
- TangRoll,
- TangWidth,
- TangA,
- TangI,
- TangOpen,
- TangOut,
- TangS,
- TangUp,
- MouthFera,
- MouthFeraR,
- MouthA,
- MouthS
- }
- public class MorphData
- {
- public MorphData(TongueCtrl.FaceMorph morph_type, TMorphSkin morph_skin)
- {
- this.morphName = morph_type.ToString().ToLower();
- this.morph = morph_skin;
- }
- public int morphIndex
- {
- get
- {
- return (int)this.morph.hash[this.morphName];
- }
- }
- public float morphValue
- {
- get
- {
- return this.morph.GetBlendValues(this.morphIndex);
- }
- private set
- {
- this.morph.SetBlendValues(this.morphIndex, value);
- }
- }
- public void SaveBlendBaseValue()
- {
- this.BlendBaseValue = this.morph.GetBlendValues(this.morphIndex);
- }
- public void Blend(float weight)
- {
- float morphValue = Mathf.Lerp(this.BlendBaseValue, this.targetValue, weight);
- this.morphValue = morphValue;
- }
- public void ApplyTargetValue()
- {
- this.morphValue = this.targetValue;
- }
- public readonly TMorphSkin morph;
- public readonly string morphName;
- private float BlendBaseValue;
- public float targetValue;
- }
- private class BoneData
- {
- public BoneData(Transform tongue_bone)
- {
- this.bone = tongue_bone;
- this.initLocalPos = this.bone.localPosition;
- this.initLocalAngles = this.bone.localEulerAngles;
- }
- public readonly Transform bone;
- public readonly Vector3 initLocalPos;
- public readonly Vector3 initLocalAngles;
- public Vector3 chacheLocalPos;
- public Vector3 chacheLocalAngles;
- }
- }
|