public static void AddBezierKeyframes(byte[] interpolation, int type, VMDHSConverter.FloatKeyframe prev_keyframe, VMDHSConverter.FloatKeyframe cur_keyframe, int interpolationQuality, ref List <VMDHSConverter.FloatKeyframe> keyframes, ref int index) { if (prev_keyframe == null || VMDHSConverter.IsLinear(interpolation, type)) { keyframes.Add(cur_keyframe); index = keyframes.Count <VMDHSConverter.FloatKeyframe>(); return; } Vector2 bezierHandle = VMDHSConverter.GetBezierHandle(interpolation, type, 0); Vector2 bezierHandle2 = VMDHSConverter.GetBezierHandle(interpolation, type, 1); for (int i = 0; i < interpolationQuality; i++) { float t = (float)(i + 1) / (float)interpolationQuality; Vector2 t2 = VMDHSConverter.SampleBezier(bezierHandle, bezierHandle2, t); keyframes.Add(VMDHSConverter.FloatKeyframe.Lerp(prev_keyframe, cur_keyframe, t2)); index = keyframes.Count <VMDHSConverter.FloatKeyframe>(); } }
public static VMDHSConverter.FloatKeyframe Lerp(VMDHSConverter.FloatKeyframe from, VMDHSConverter.FloatKeyframe to, Vector2 t) { return(new VMDHSConverter.FloatKeyframe(Mathf.Lerp(from.time, to.time, t.x), Mathf.Lerp(from.value, to.value, t.y))); }
private Dictionary <string, AnimationCurve> CollectCurve(VMDFormat format, string boneName, int interpolationQuality) { if (!format.motion_list.motion.ContainsKey(boneName)) { return(null); } Dictionary <string, AnimationCurve> dictionary = new Dictionary <string, AnimationCurve>(); List <VMDFormat.Motion> list = format.motion_list.motion[boneName]; Vector3 zero = Vector3.zero; this.GetKeyframeCount(list, 0, interpolationQuality); this.GetKeyframeCount(list, 1, interpolationQuality); this.GetKeyframeCount(list, 2, interpolationQuality); List <VMDHSConverter.FloatKeyframe> list2 = new List <VMDHSConverter.FloatKeyframe>(); List <VMDHSConverter.FloatKeyframe> list3 = new List <VMDHSConverter.FloatKeyframe>(); List <VMDHSConverter.FloatKeyframe> list4 = new List <VMDHSConverter.FloatKeyframe>(); VMDHSConverter.FloatKeyframe prev_keyframe = null; VMDHSConverter.FloatKeyframe prev_keyframe2 = null; VMDHSConverter.FloatKeyframe prev_keyframe3 = null; int num = 0; int num2 = 0; int num3 = 0; for (int i = 0; i < list.Count; i++) { float time = list[i].flame_no * 0.0333333351f; VMDHSConverter.FloatKeyframe floatKeyframe = new VMDHSConverter.FloatKeyframe(time, list[i].location.x * this.scale.x + zero.x); VMDHSConverter.FloatKeyframe floatKeyframe2 = new VMDHSConverter.FloatKeyframe(time, list[i].location.y * this.scale.y + zero.y); VMDHSConverter.FloatKeyframe floatKeyframe3 = new VMDHSConverter.FloatKeyframe(time, list[i].location.z * this.scale.z + zero.z); VMDHSConverter.FloatKeyframe.AddBezierKeyframes(list[i].interpolation, 0, prev_keyframe, floatKeyframe, interpolationQuality, ref list2, ref num); VMDHSConverter.FloatKeyframe.AddBezierKeyframes(list[i].interpolation, 1, prev_keyframe2, floatKeyframe2, interpolationQuality, ref list3, ref num2); VMDHSConverter.FloatKeyframe.AddBezierKeyframes(list[i].interpolation, 2, prev_keyframe3, floatKeyframe3, interpolationQuality, ref list4, ref num3); prev_keyframe = floatKeyframe; prev_keyframe2 = floatKeyframe2; prev_keyframe3 = floatKeyframe3; } if (list.Count != 0) { VMDHSConverter.FloatKeyframe[] custom_keys = list2.ToArray(); VMDHSConverter.FloatKeyframe[] custom_keys2 = list3.ToArray(); VMDHSConverter.FloatKeyframe[] custom_keys3 = list4.ToArray(); AnimationCurve value = new AnimationCurve(this.ToKeyframesForLocation(custom_keys)); AnimationCurve value2 = new AnimationCurve(this.ToKeyframesForLocation(custom_keys2)); AnimationCurve value3 = new AnimationCurve(this.ToKeyframesForLocation(custom_keys3)); dictionary.Add("localPosition.x", value); dictionary.Add("localPosition.y", value2); dictionary.Add("localPosition.z", value3); } VMDHSConverter.QuaternionKeyframe[] custom_keys4 = new VMDHSConverter.QuaternionKeyframe[this.GetKeyframeCount(list, 3, interpolationQuality)]; VMDHSConverter.QuaternionKeyframe prev_keyframe4 = null; int num4 = 0; for (int j = 0; j < list.Count; j++) { float time2 = list[j].flame_no * 0.0333333351f; Quaternion rotation = list[j].rotation; VMDHSConverter.QuaternionKeyframe quaternionKeyframe = new VMDHSConverter.QuaternionKeyframe(time2, rotation); VMDHSConverter.QuaternionKeyframe.AddBezierKeyframes(list[j].interpolation, 3, prev_keyframe4, quaternionKeyframe, interpolationQuality, ref custom_keys4, ref num4); prev_keyframe4 = quaternionKeyframe; } Keyframe[] array = null; Keyframe[] array2 = null; Keyframe[] array3 = null; this.ToKeyframesForRotation(custom_keys4, ref array, ref array2, ref array3, false); AnimationCurve value4 = new AnimationCurve(array); AnimationCurve value5 = new AnimationCurve(array2); AnimationCurve value6 = new AnimationCurve(array3); dictionary.Add("localEulerAngles.x", value4); dictionary.Add("localEulerAngles.y", value5); dictionary.Add("localEulerAngles.z", value6); return(dictionary); }
public AnimationClip CreateCameraAnimationClip(VMDFormat format, GameObject camera_root, int interpolationQuality) { AnimationClip animationClip = new AnimationClip(); if (VMDHSConverter.p_legacy != null) { VMDHSConverter.p_legacy.SetValue(animationClip, true, null); } BoneAdjustment boneAdjustment = this.boneAdjustment["camera"]; if (camera_root.transform.Find("camera") == null) { new GameObject("camera").transform.parent = camera_root.transform; } if (camera_root.transform.Find("length") == null) { new GameObject("length").transform.parent = camera_root.transform; } if (camera_root.transform.Find("view_angle") == null) { new GameObject("view_angle").transform.parent = camera_root.transform; } if (camera_root.transform.Find("perspective") == null) { new GameObject("perspective").transform.parent = camera_root.transform; } List <VMDHSConverter.FloatKeyframe> list = new List <VMDHSConverter.FloatKeyframe>(); List <VMDHSConverter.FloatKeyframe> list2 = new List <VMDHSConverter.FloatKeyframe>(); List <VMDHSConverter.FloatKeyframe> list3 = new List <VMDHSConverter.FloatKeyframe>(); List <VMDHSConverter.FloatLinearKeyframe> list4 = new List <VMDHSConverter.FloatLinearKeyframe>(); List <VMDHSConverter.FloatLinearKeyframe> list5 = new List <VMDHSConverter.FloatLinearKeyframe>(); List <Keyframe> list6 = new List <Keyframe>(); VMDHSConverter.FloatKeyframe prev_keyframe = null; VMDHSConverter.FloatKeyframe prev_keyframe2 = null; VMDHSConverter.FloatKeyframe prev_keyframe3 = null; int num = 0; int num2 = 0; int num3 = 0; int num4 = 0; while ((long)num4 < (long)((ulong)format.camera_list.camera_count)) { VMDFormat.CameraData cameraData = format.camera_list.camera[num4]; float num5 = 0.0333333351f * cameraData.flame_no; Vector3 location = cameraData.location; location.z = -location.z; location.x = -location.x; VMDHSConverter.FloatKeyframe floatKeyframe = new VMDHSConverter.FloatKeyframe(num5, location.x * this.scale.x); VMDHSConverter.FloatKeyframe floatKeyframe2 = new VMDHSConverter.FloatKeyframe(num5, location.y * this.scale.y); VMDHSConverter.FloatKeyframe floatKeyframe3 = new VMDHSConverter.FloatKeyframe(num5, location.z * this.scale.z); VMDHSConverter.FloatLinearKeyframe item = new VMDHSConverter.FloatLinearKeyframe(num5, cameraData.length * this.scale.z); VMDHSConverter.FloatLinearKeyframe item2 = new VMDHSConverter.FloatLinearKeyframe(num5, cameraData.viewing_angle); VMDHSConverter.FloatKeyframe.AddBezierKeyframes(cameraData.interpolation, 0, prev_keyframe, floatKeyframe, interpolationQuality, ref list, ref num); VMDHSConverter.FloatKeyframe.AddBezierKeyframes(cameraData.interpolation, 1, prev_keyframe2, floatKeyframe2, interpolationQuality, ref list2, ref num2); VMDHSConverter.FloatKeyframe.AddBezierKeyframes(cameraData.interpolation, 2, prev_keyframe3, floatKeyframe3, interpolationQuality, ref list3, ref num3); list4.Add(item); list5.Add(item2); prev_keyframe = floatKeyframe; prev_keyframe2 = floatKeyframe2; prev_keyframe3 = floatKeyframe3; Keyframe item3; item3 = new Keyframe(num5, (float)cameraData.perspective); list6.Add(item3); num4++; } if (format.camera_list.camera_count != 0u) { VMDHSConverter.FloatKeyframe[] custom_keys = list.ToArray(); VMDHSConverter.FloatKeyframe[] custom_keys2 = list2.ToArray(); VMDHSConverter.FloatKeyframe[] custom_keys3 = list3.ToArray(); VMDHSConverter.FloatLinearKeyframe[] custom_keys4 = list4.ToArray(); VMDHSConverter.FloatLinearKeyframe[] custom_keys5 = list5.ToArray(); AnimationCurve animationCurve = new AnimationCurve(this.ToKeyframesForLocation(custom_keys)); AnimationCurve animationCurve2 = new AnimationCurve(this.ToKeyframesForLocation(custom_keys2)); AnimationCurve animationCurve3 = new AnimationCurve(this.ToKeyframesForLocation(custom_keys3)); AnimationCurve animationCurve4 = new AnimationCurve(this.ToKeyframesForLocation(custom_keys4)); AnimationCurve animationCurve5 = new AnimationCurve(this.ToKeyframesForLocation(custom_keys5)); Keyframe[] array = list6.ToArray(); this.AddDummyKeyframe(ref array); AnimationCurve animationCurve6 = new AnimationCurve(array); animationClip.SetCurve("camera", typeof(Transform), "localPosition.x", animationCurve); animationClip.SetCurve("camera", typeof(Transform), "localPosition.y", animationCurve2); animationClip.SetCurve("camera", typeof(Transform), "localPosition.z", animationCurve3); animationClip.SetCurve("length", typeof(Transform), "localPosition.z", animationCurve4); animationClip.SetCurve("view_angle", typeof(Transform), "localPosition.z", animationCurve5); animationClip.SetCurve("perspective", typeof(Transform), "localPosition.z", animationCurve6); } List <VMDHSConverter.QuaternionKeyframe> list7 = new List <VMDHSConverter.QuaternionKeyframe>(); Quaternion identity = Quaternion.identity; int num6 = 0; while ((long)num6 < (long)((ulong)format.camera_list.camera_count)) { VMDFormat.CameraData cameraData2 = format.camera_list.camera[num6]; float time = 0.0333333351f * cameraData2.flame_no; Vector3 vector = cameraData2.rotation * 57.29578f; vector.y *= -1f; Quaternion quaternion = Quaternion.Euler(vector); quaternion = boneAdjustment.GetAdjustedRotation(quaternion); VMDHSConverter.QuaternionKeyframe item4 = new VMDHSConverter.QuaternionKeyframe(time, quaternion); list7.Add(item4); num6++; } Keyframe[] array2 = null; Keyframe[] array3 = null; Keyframe[] array4 = null; Dictionary <string, Keyframe[]> dictionary = this.ToKeyframesForRotation(list7.ToArray(), ref array2, ref array3, ref array4, false); AnimationCurve animationCurve7 = new AnimationCurve(dictionary["localRotation.x"]); AnimationCurve animationCurve8 = new AnimationCurve(dictionary["localRotation.y"]); AnimationCurve animationCurve9 = new AnimationCurve(dictionary["localRotation.z"]); AnimationCurve animationCurve10 = new AnimationCurve(dictionary["localRotation.w"]); animationClip.SetCurve("camera", typeof(Transform), "localRotation.x", animationCurve7); animationClip.SetCurve("camera", typeof(Transform), "localRotation.y", animationCurve8); animationClip.SetCurve("camera", typeof(Transform), "localRotation.z", animationCurve9); animationClip.SetCurve("camera", typeof(Transform), "localRotation.w", animationCurve10); return(animationClip); }
private void CreateKeysForLocation(VMDFormat format, AnimationClip clip, string current_bone, string bone_path, int interpolationQuality, GameObject current_obj = null) { try { if (this.boneNameMap.ContainsKey(current_bone)) { string text = this.boneNameMap[current_bone]; if (!format.motion_list.motion.ContainsKey(text)) { Console.WriteLine("bone {0} not found in motionlist", text); } else { HashSet <string> hashSet = new HashSet <string> { "全ての親", "センター", "グルーブ", "右足IK親", "右足IK", "右つま先IK", "左足IK親", "左足IK", "左つま先IK" }; Vector3 vector = Vector3.zero; List <VMDFormat.Motion> list = format.motion_list.motion[text]; this.GetKeyframeCount(list, 0, interpolationQuality); this.GetKeyframeCount(list, 1, interpolationQuality); this.GetKeyframeCount(list, 2, interpolationQuality); List <VMDHSConverter.FloatKeyframe> list2 = new List <VMDHSConverter.FloatKeyframe>(); List <VMDHSConverter.FloatKeyframe> list3 = new List <VMDHSConverter.FloatKeyframe>(); List <VMDHSConverter.FloatKeyframe> list4 = new List <VMDHSConverter.FloatKeyframe>(); VMDHSConverter.FloatKeyframe prev_keyframe = null; VMDHSConverter.FloatKeyframe prev_keyframe2 = null; VMDHSConverter.FloatKeyframe prev_keyframe3 = null; int num = 0; int num2 = 0; int num3 = 0; uint num4 = uint.MaxValue; for (int i = 0; i < list.Count; i++) { float num5 = list[i].flame_no * 0.0333333351f; if (num4 > list[i].flame_no) { num4 = list[i].flame_no; } Vector3 vector2 = list[i].location; if (!(vector2 == Vector3.zero) || hashSet.Contains(text)) { vector2.z = -vector2.z; vector2.x = -vector2.x; if (float.IsNaN(vector2.x) || float.IsNaN(vector2.y) || float.IsNaN(vector2.z)) { KKVMDPlugin.Logger.Log(BepInEx.Logging.LogLevel.Debug, string.Format("position value is NaN. Treat as / 100 of max. {0}", num5)); vector2 = new Vector3(3.40282359E+36f, 3.40282359E+36f, 3.40282359E+36f); } else if (vector2.magnitude > 1E+07f) { KKVMDPlugin.Logger.Log(BepInEx.Logging.LogLevel.Debug, string.Format("position value is too large. Treat as / 100 to avoid clipping. {0}, ({1} {2} {3})", new object[] { num5, vector2.x, vector2.y, vector2.z })); vector2 /= 100f; } if (text == "センター") { vector = this.HSModelBaseline.centerBasePos; } if (text == "グルーブ") { vector = this.HSModelBaseline.grooveBasePos; } if (text == "左足IK親") { vector = this.HSModelBaseline.leftIKCenterPos; } else if (text == "右足IK親") { vector = this.HSModelBaseline.rightIKCenterPos; } if (text == "右足IK") { vector = this.HSModelBaseline.rightFootPos; } else if (text == "左足IK") { vector = this.HSModelBaseline.leftFootPos; } if (text == "右つま先IK") { vector = this.HSModelBaseline.rightToePosRel; } else if (text == "左つま先IK") { vector = this.HSModelBaseline.leftToePosRel; } VMDHSConverter.FloatKeyframe floatKeyframe = new VMDHSConverter.FloatKeyframe(num5, vector2.x * this.scale.x + vector.x); VMDHSConverter.FloatKeyframe floatKeyframe2 = new VMDHSConverter.FloatKeyframe(num5, vector2.y * this.scale.y + vector.y); VMDHSConverter.FloatKeyframe floatKeyframe3 = new VMDHSConverter.FloatKeyframe(num5, vector2.z * this.scale.z + vector.z); VMDHSConverter.FloatKeyframe.AddBezierKeyframes(list[i].interpolation, 0, prev_keyframe, floatKeyframe, interpolationQuality, ref list2, ref num); VMDHSConverter.FloatKeyframe.AddBezierKeyframes(list[i].interpolation, 1, prev_keyframe2, floatKeyframe2, interpolationQuality, ref list3, ref num2); VMDHSConverter.FloatKeyframe.AddBezierKeyframes(list[i].interpolation, 2, prev_keyframe3, floatKeyframe3, interpolationQuality, ref list4, ref num3); prev_keyframe = floatKeyframe; prev_keyframe2 = floatKeyframe2; prev_keyframe3 = floatKeyframe3; } } VMDHSConverter.FloatKeyframe[] custom_keys = list2.ToArray(); VMDHSConverter.FloatKeyframe[] custom_keys2 = list3.ToArray(); VMDHSConverter.FloatKeyframe[] custom_keys3 = list4.ToArray(); if (list.Count != 0) { if (hashSet.Contains(text)) { AnimationCurve animationCurve = new AnimationCurve(this.ToKeyframesForLocation(custom_keys)); AnimationCurve animationCurve2 = new AnimationCurve(this.ToKeyframesForLocation(custom_keys2)); AnimationCurve animationCurve3 = new AnimationCurve(this.ToKeyframesForLocation(custom_keys3)); clip.SetCurve(bone_path, typeof(Transform), "localPosition.x", animationCurve); clip.SetCurve(bone_path, typeof(Transform), "localPosition.y", animationCurve2); clip.SetCurve(bone_path, typeof(Transform), "localPosition.z", animationCurve3); } else { KKVMDPlugin.Logger.Log(BepInEx.Logging.LogLevel.Error, string.Format("Location data for non movable bone {0} found. Ignore.", text)); } } } } } catch (KeyNotFoundException) { } }