private IReadOnlyList <VmdBoneFrame> CreateBoneFrames([NotNull] IBodyAnimationSource bodyMotionSource, [NotNull] PrettyAvatar avatar, [NotNull] PmxModel pmx) { var boneLookup = new BoneLookup(_conversionConfig); var mltdHierarchy = boneLookup.BuildBoneHierarchy(avatar); var pmxHierarchy = boneLookup.BuildBoneHierarchy(pmx); if (_conversionConfig.AppendIKBones || _conversionConfig.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 = bodyMotionSource.Convert(); var boneCount = mltdHierarchy.Count; var animatedBoneCount = animation.BoneCount; var keyFrameCount = animation.KeyFrames.Count; { 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(boneLookup.GetVmdBoneNameFromBonePath).ToArray(); // Mark MLTD key bones. foreach (var name in names) { MarkNamedBone(name); } // Special cases MarkNamedBone("KUBI"); MarkNamedBone("頭"); } Debug.Assert(keyFrameCount % animatedBoneCount == 0, "keyFrameCount % animatedBoneCount == 0"); var iterationTimes = keyFrameCount / animatedBoneCount; var boneFrameList = new List <VmdBoneFrame>(); // Reduce memory pressure of allocating new delegates (see mltdHierarchy.FirstOrDefault(...)) var boneMatchPredicateCache = new Func <PmxBone, bool> [boneCount]; for (var j = 0; j < boneCount; j += 1) { var refBone = pmx.Bones[j]; boneMatchPredicateCache[j] = bone => bone.Name == refBone.Name; } // Cache `mltdBoneName`s so we don't have to compute them all in every iteration var boneNameCache = new Dictionary <string, string>(); // OK, now perform iterations for (var i = 0; i < iterationTimes; ++i) { if (_conversionConfig.Transform60FpsTo30Fps) { if (i % 2 == 1) { continue; } } var keyFrameIndexStart = i * animatedBoneCount; for (var j = 0; j < animatedBoneCount; ++j) { var keyFrame = animation.KeyFrames[keyFrameIndexStart + j]; string mltdBoneName; if (boneNameCache.ContainsKey(keyFrame.Path)) { mltdBoneName = boneNameCache[keyFrame.Path]; } else { if (keyFrame.Path.Contains("BODY_SCALE/")) { mltdBoneName = keyFrame.Path.Replace("BODY_SCALE/", string.Empty); } else { mltdBoneName = keyFrame.Path; } boneNameCache.Add(keyFrame.Path, mltdBoneName); } 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) { // ReSharper disable once PossibleInvalidOperationException var x = keyFrame.PositionX.Value; // ReSharper disable once PossibleInvalidOperationException var y = keyFrame.PositionY.Value; // ReSharper disable once PossibleInvalidOperationException var z = keyFrame.PositionZ.Value; var t = new Vector3(x, y, z); t = t.FixUnityToOpenTK(); if (_conversionConfig.ScaleToVmdSize) { t = t * _scalingConfig.ScaleUnityToPmx; } targetBone.LocalPosition = t; //if (transferredBone != null) { // transferredBone.LocalPosition = t; //} } if (keyFrame.HasRotations) { // ReSharper disable once PossibleInvalidOperationException var x = keyFrame.AngleX.Value; // ReSharper disable once PossibleInvalidOperationException var y = keyFrame.AngleY.Value; // ReSharper disable once PossibleInvalidOperationException 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 predicate = boneMatchPredicateCache[j]; var pb = pmx.Bones.FirstOrDefault(predicate); #if DEBUG if (pb == null) { // Lazy evaluation of the assertion message Debug.Assert(pb != null, $"PMX bone with the name \"{pmxBone.Name}\" should exist."); } #endif 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.Transform60FpsTo30Fps) { frameIndex = i / 2; } else { frameIndex = i; } var vmdBoneName = boneLookup.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); }
private VmdBoneFrame[] CreateBoneFrames([NotNull] IBodyAnimationSource mainDance, [NotNull] PrettyAvatar avatar, [NotNull] PmxModel pmx, [NotNull] ScenarioObject baseScenario, [CanBeNull] ScenarioObject formationInfo, [CanBeNull] IBodyAnimationSource danceAppeal, int formationNumber, AppealType appealType) { var boneLookup = new BoneLookup(_conversionConfig); var mltdHierarchy = boneLookup.BuildBoneHierarchy(avatar); mltdHierarchy.AssertAllUnique(); var pmxHierarchy = boneLookup.BuildBoneHierarchy(pmx); pmxHierarchy.AssertAllUnique(); if (_conversionConfig.AppendIKBones || _conversionConfig.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.Length == pmxHierarchy.Length, "Hierarchy number should be equal between MLTD and MMD."); } foreach (var mltdBone in mltdHierarchy) { mltdBone.Initialize(); } foreach (var pmxBone in pmxHierarchy) { pmxBone.Initialize(); } var mainAnimation = mainDance.Convert(); var appealAnimation = danceAppeal?.Convert(); var mltdBoneCount = mltdHierarchy.Length; var animatedBoneCount = mainAnimation.BoneCount; var keyFrameCount = mainAnimation.KeyFrames.Length; { var names1 = mainAnimation.KeyFrames.Take(animatedBoneCount) .Select(kf => kf.Path).ToArray(); var names = names1.Select(boneLookup.GetVmdBoneNameFromBonePath).ToArray(); // Mark MLTD key bones. foreach (var name in names) { MarkNamedBoneAsKeyBone(pmx, name); } // Special cases MarkNamedBoneAsKeyBone(pmx, "KUBI"); MarkNamedBoneAsKeyBone(pmx, "頭"); } Debug.Assert(keyFrameCount % animatedBoneCount == 0, "keyFrameCount % animatedBoneCount == 0"); // Use this value to export visible frames only var resultFrameCount = (int)(mainAnimation.Duration * FrameRate.Mltd); // Use this value to export all frames, including invisible frames in normal MVs (e.g. seek targets) // var resultFrameCount = keyFrameCount / animatedBoneCount; var boneFrameList = new List <VmdBoneFrame>(); // Reduce memory pressure of allocating new delegates (see mltdHierarchy.FirstOrDefault(...)) var boneMatchPredicateCache = new Predicate <PmxBone> [mltdBoneCount]; for (var j = 0; j < mltdBoneCount; j += 1) { var refBone = pmx.Bones[j]; boneMatchPredicateCache[j] = bone => bone.Name == refBone.Name; } // Cache `mltdBoneName`s so we don't have to compute them all in every iteration var boneNameCache = new Dictionary <string, string>(); var transform60FpsTo30Fps = _conversionConfig.Transform60FpsTo30Fps; var scaleToVmdSize = _conversionConfig.ScaleToVmdSize; var unityToVmdScale = _scalingConfig.ScaleUnityToVmd; var baseFormationList = CollectFormationChanges(formationInfo, AppealType.None); var appealFormationList = CollectFormationChanges(formationInfo, appealType); var appealTimes = AppealHelper.CollectAppealTimeInfo(baseScenario); var seekFrameControls = CollectSeekFrames(baseScenario, formationNumber); var seekFrameCounter = 0; var lastSoughtFrame = -1; // OK, now perform iterations for (var mltdFrameIndex = 0; mltdFrameIndex < resultFrameCount; ++mltdFrameIndex) { if (transform60FpsTo30Fps) { if (mltdFrameIndex % 2 == 1) { continue; } } var shouldUseAppeal = appealType != AppealType.None && (appealTimes.StartFrame <= mltdFrameIndex && mltdFrameIndex < appealTimes.EndFrame) && appealAnimation != null; var animation = shouldUseAppeal ? appealAnimation : mainAnimation; int projectedFrameIndex; if (shouldUseAppeal) { var indexInAppeal = mltdFrameIndex - appealTimes.StartFrame; if (indexInAppeal >= appealAnimation.FrameCount) { indexInAppeal = appealAnimation.FrameCount - 1; } // `indexInAppeal`, unlike `mltdFrameIndex`, has not been scaled yet if (transform60FpsTo30Fps) { projectedFrameIndex = indexInAppeal / 2; } else { projectedFrameIndex = indexInAppeal; } } else { projectedFrameIndex = CalculateSeekFrameTarget(mltdFrameIndex, seekFrameControls, ref lastSoughtFrame, ref seekFrameCounter); } var formationList = shouldUseAppeal ? appealFormationList : baseFormationList; formationList.TryGetCurrentValue(mltdFrameIndex, out var formations); Vector4 idolOffset; if (formations == null || formations.Length < formationNumber) { idolOffset = Vector4.Zero; } else { idolOffset = formations[formationNumber - 1]; } var keyFrameIndexStart = projectedFrameIndex * animatedBoneCount; for (var j = 0; j < animatedBoneCount; ++j) { var keyFrame = animation.KeyFrames[keyFrameIndexStart + j]; var mltdBoneName = GetMltdBoneNameWithoutBodyScale(boneNameCache, keyFrame); // Uniqueness is asserted above var targetBone = mltdHierarchy.Find(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) { continue; } var attachmentTarget = kv.Value; // Uniqueness is asserted above transferredBone = mltdHierarchy.Find(bone => bone.Name == attachmentTarget); if (transferredBone == null) { throw new ArgumentException("Cannot find transferred bone."); } break; } if (keyFrame.HasPositions) { // ReSharper disable once PossibleInvalidOperationException var x = keyFrame.PositionX.Value; // ReSharper disable once PossibleInvalidOperationException var y = keyFrame.PositionY.Value; // ReSharper disable once PossibleInvalidOperationException var z = keyFrame.PositionZ.Value; if (string.Equals(keyFrame.Path, "MODEL_00", StringComparison.Ordinal)) { var worldRotation = Quaternion.FromAxisAngle(Vector3.UnitY, MathHelper.DegreesToRadians(idolOffset.W)); var newOrigin = worldRotation * new Vector3(x, y, z); var newPosition = newOrigin + idolOffset.Xyz; (x, y, z) = (newPosition.X, newPosition.Y, newPosition.Z); } var t = new Vector3(x, y, z); t = t.FixUnityToMmd(); if (scaleToVmdSize) { t = t * unityToVmdScale; } targetBone.LocalPosition = t; //if (transferredBone != null) { // transferredBone.LocalPosition = t; //} } if (keyFrame.HasRotations) { // ReSharper disable once PossibleInvalidOperationException var x = keyFrame.AngleX.Value; // ReSharper disable once PossibleInvalidOperationException var y = keyFrame.AngleY.Value; // ReSharper disable once PossibleInvalidOperationException var z = keyFrame.AngleZ.Value; if (string.Equals(keyFrame.Path, "MODEL_00", StringComparison.Ordinal)) { // The W component stores rotation y += idolOffset.W; } 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 < mltdBoneCount; ++j) { var pmxBone = pmxHierarchy[j]; var mltdBone = mltdHierarchy[j]; { var predicate = boneMatchPredicateCache[j]; var pb = pmx.Bones.Find(predicate); #if DEBUG if (pb == null) { // Lazy evaluation of the assertion message Debug.Assert(pb != null, $"PMX bone with the name \"{pmxBone.Name}\" should exist."); } #endif 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 vmdFrameIndex; if (_conversionConfig.Transform60FpsTo30Fps) { vmdFrameIndex = mltdFrameIndex / 2; } else { vmdFrameIndex = mltdFrameIndex; } var mltdBoneName = GetMltdBoneNameWithoutBodyScale(boneNameCache, mltdBone.Path); var vmdBoneName = boneLookup.GetVmdBoneNameFromBoneName(mltdBone.Path); var boneFrame = new VmdBoneFrame(vmdFrameIndex, vmdBoneName); var isMovable = BoneLookup.IsBoneMovable(mltdBoneName); boneFrame.Position = isMovable ? t : Vector3.Zero; boneFrame.Rotation = q; boneFrameList.Add(boneFrame); pmxBone.LocalPosition = t; pmxBone.LocalRotation = q; pmxBone.UpdateTransform(); } } return(boneFrameList.ToArray()); }