// Some heavy stuff private void Initialize1() { ResHelper.LoadHeadMesh(); _bodyMesh = ResHelper.LoadBodyMesh(); _bodyAvatar = ResHelper.LoadBodyAvatar(); _headMesh = ResHelper.LoadHeadMesh(); _headAvatar = ResHelper.LoadHeadAvatar(); Debug.Assert(_bodyAvatar != null, nameof(_bodyAvatar) + " != null"); Debug.Assert(_bodyMesh != null, nameof(_bodyMesh) + " != null"); Debug.Assert(_headAvatar != null, nameof(_headAvatar) + " != null"); Debug.Assert(_headMesh != null, nameof(_headMesh) + " != null"); _bodyBoneList = ResHelper.BuildBoneHierachy(_bodyAvatar, _bodyMesh); _headBoneList = ResHelper.BuildBoneHierachy(_headAvatar, _headMesh); do { // Fix "KUBI" (neck) bone's parent var kubiParent = _bodyBoneList.SingleOrDefault(bn => bn.Path == "MODEL_00/BASE/MUNE1/MUNE2/KUBI"); var kubiBone = _headBoneList.SingleOrDefault(bn => bn.Path == "KUBI"); Debug.Assert(kubiParent != null); Debug.Assert(kubiBone != null); kubiParent.AddChild(kubiBone); Debug.Assert(kubiBone.Parent != null); // Don't forget to remove it from its old parent (or it will be updated twice from two parents). // The original parents and grandparents of KUBI are not needed; they are just like model anchors and shouldn't be animated. // See decompiled model for more information. kubiBone.Parent.RemoveChild(kubiBone); kubiBone.Parent = kubiParent; // Set its new initial parameters. // Since the two bones (KUBI and MODEL_00/BASE/MUNE1/MUNE2/KUBI) actually share the exact same transforms, // set its local transform to identity (t=0, q=0). kubiBone.InitialPosition = Vector3.Zero; kubiBone.InitialRotation = Quaternion.Identity; kubiBone.LocalPosition = Vector3.Zero; kubiBone.LocalRotation = Quaternion.Identity; // Now inform the rest of the head bones the new bone hierarchy. Force them to do so. foreach (var bone in _headBoneList) { bone.Initialize(true); } } while (false); (_danceData, _, _) = ResHelper.LoadDance(); #if DEBUG do { var influencingBones = _bodyBoneList.Where((_, i) => _bodyMesh.Skin.Any(sk => sk.Any(a => a.BoneIndex == i))); Debug.Print("Bones that influences the mesh:"); foreach (var bone in influencingBones) { Debug.Print("#{0} \"{1}\"", bone.Index, bone.Path); } } while (false); #endif _animation = BodyAnimation.CreateFrom(_danceData); #if DEBUG do { var animatedBoneNames = _animation.KeyFrames.Select(f => f.Path).Distinct(); Debug.Print("Animated bone names:"); foreach (var boneName in animatedBoneNames) { Debug.Print(boneName); } } while (false); #endif _initialized1 = true; }
public BodyAnimation Convert() { return(BodyAnimation.CreateFrom(AnimationClip)); }
private static IReadOnlyList <VmdBoneFrame> CreateBoneFrames([NotNull] CharacterImasMotionAsset bodyMotion, [NotNull] Avatar avatar, [NotNull] PmxModel pmx) { var mltdHierarchy = BoneUtils.BuildBoneHierarchy(avatar); var pmxHierarchy = BoneUtils.BuildBoneHierarchy(pmx); if (ConversionConfig.Current.AppendIKBones || ConversionConfig.Current.AppendEyeBones) { throw new NotSupportedException("Character motion frames generation (from MLTD) is not supported when appending bones (eyes and/or IK) is enabled."); } else { Debug.Assert(mltdHierarchy.Count == pmxHierarchy.Count, "Hierarchy number should be equal between MLTD and MMD."); } foreach (var mltdBone in mltdHierarchy) { mltdBone.Initialize(); } foreach (var pmxBone in pmxHierarchy) { pmxBone.Initialize(); } var animation = BodyAnimation.CreateFrom(bodyMotion); var boneCount = mltdHierarchy.Count; var animatedBoneCount = animation.BoneCount; var keyFrameCount = animation.KeyFrames.Count; do { void MarkNamedBone(string name) { var bone = pmx.Bones.FirstOrDefault(b => b.Name == name); if (bone != null) { bone.IsMltdKeyBone = true; } else { Debug.Print("Warning: trying to mark bone {0} as MLTD key bone but the bone is missing from the model.", name); } } var names1 = animation.KeyFrames.Take(animatedBoneCount) .Select(kf => kf.Path).ToArray(); var names = names1.Select(BoneUtils.GetVmdBoneNameFromBonePath).ToArray(); // Mark MLTD key bones. foreach (var name in names) { MarkNamedBone(name); } // Special cases MarkNamedBone("KUBI"); MarkNamedBone("頭"); } while (false); Debug.Assert(keyFrameCount % animatedBoneCount == 0, "keyFrameCount % animatedBoneCount == 0"); var iterationTimes = keyFrameCount / animatedBoneCount; var boneFrameList = new List <VmdBoneFrame>(); for (var i = 0; i < iterationTimes; ++i) { if (ConversionConfig.Current.Transform60FpsTo30Fps) { if (i % 2 == 1) { continue; } } var keyFrameIndexStart = i * animatedBoneCount; for (var j = 0; j < animatedBoneCount; ++j) { var keyFrame = animation.KeyFrames[keyFrameIndexStart + j]; var mltdBoneName = keyFrame.Path.Replace("BODY_SCALE/", string.Empty); var targetBone = mltdHierarchy.SingleOrDefault(bone => bone.Name == mltdBoneName); if (targetBone == null) { //throw new ArgumentException("Bone not found."); continue; // Shika doesn't have the "POSITION" bone. } BoneNode transferredBone = null; foreach (var kv in BoneAttachmentMap) { if (kv.Key == mltdBoneName) { transferredBone = mltdHierarchy.SingleOrDefault(bone => bone.Name == kv.Value); if (transferredBone == null) { throw new ArgumentException(); } break; } } if (keyFrame.HasPositions) { var x = keyFrame.PositionX.Value; var y = keyFrame.PositionY.Value; var z = keyFrame.PositionZ.Value; var t = new Vector3(x, y, z); t = t.FixUnityToOpenTK(); if (ConversionConfig.Current.ScaleToVmdSize) { t = t * ScalingConfig.ScaleUnityToPmx; } targetBone.LocalPosition = t; //if (transferredBone != null) { // transferredBone.LocalPosition = t; //} } if (keyFrame.HasRotations) { var x = keyFrame.AngleX.Value; var y = keyFrame.AngleY.Value; var z = keyFrame.AngleZ.Value; var q = UnityRotation.EulerDeg(x, y, z); q = q.FixUnityToOpenTK(); targetBone.LocalRotation = q; if (transferredBone != null) { transferredBone.LocalRotation = q; } } } foreach (var mltdBone in mltdHierarchy) { mltdBone.UpdateTransform(); } for (var j = 0; j < boneCount; ++j) { var pmxBone = pmxHierarchy[j]; var mltdBone = mltdHierarchy[j]; { var pb = pmx.Bones.FirstOrDefault(b => b.Name == pmxBone.Name); Debug.Assert(pb != null, $"PMX bone with the name \"{pmxBone.Name}\" should exist."); if (!pb.IsMltdKeyBone) { continue; } } var skinMatrix = mltdBone.SkinMatrix; var mPmxBindingPose = pmxBone.BindingPose; var mWorld = pmxBone.Parent?.WorldMatrix ?? Matrix4.Identity; // skinMatrix == inv(mPmxBindingPose) x mLocal x mWorld var mLocal = mPmxBindingPose * skinMatrix * mWorld.Inverted(); // Here, translation is in... world coords? WTF? var t = mLocal.ExtractTranslation(); var q = mLocal.ExtractRotation(); if (pmxBone.Parent != null) { t = t - (pmxBone.InitialPosition - pmxBone.Parent.InitialPosition); } int frameIndex; if (ConversionConfig.Current.Transform60FpsTo30Fps) { frameIndex = i / 2; } else { frameIndex = i; } var vmdBoneName = BoneUtils.GetVmdBoneNameFromBoneName(mltdBone.Path); var boneFrame = new VmdBoneFrame(frameIndex, vmdBoneName); boneFrame.Position = t; boneFrame.Rotation = q; boneFrameList.Add(boneFrame); pmxBone.LocalPosition = t; pmxBone.LocalRotation = q; pmxBone.UpdateTransform(); } } return(boneFrameList); }