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 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 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 FaceMorphDataDic = new Dictionary(); public bool isEnable; private bool LastIsEnable; private float ElapsedTime; public float blendTime = 0.5f; private float BlendWeight; private Dictionary BoneDataDic = new Dictionary(); 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; } }