示例#1
0
        private BoneNode[] BuildBoneHierarchy([NotNull] PrettyAvatar avatar, bool fixKubi)
        {
            var boneList        = new List <BoneNode>();
            var skeletonNodes   = avatar.AvatarSkeleton.Nodes;
            var scaleUnityToPmx = _scalingConfig.ScaleUnityToPmx;

            for (var i = 0; i < skeletonNodes.Length; i++)
            {
                var n = skeletonNodes[i];

                var parent   = n.ParentIndex >= 0 ? boneList[n.ParentIndex] : null;
                var boneId   = avatar.AvatarSkeleton.NodeIDs[i];
                var bonePath = avatar.BoneNamesMap[boneId];

                var boneIndex = avatar.AvatarSkeleton.NodeIDs.FindIndex(boneId);

                if (boneIndex < 0)
                {
                    throw new IndexOutOfRangeException();
                }

                var initialPose = avatar.AvatarSkeletonPose.Transforms[boneIndex];

                var t = initialPose.LocalPosition.ToOpenTK() * scaleUnityToPmx;
                var q = initialPose.LocalRotation.ToOpenTK();

                var bone = new BoneNode(parent, i, bonePath, t, q);

                boneList.Add(bone);
            }

            PostprocessBoneList(boneList, fixKubi, false);

            return(boneList.ToArray());
        }
示例#2
0
        public static int FindBoneIndexByNameHash([NotNull] this PrettyAvatar avatar, uint hash)
        {
            var list = avatar.AvatarSkeleton.NodeIDs;

            for (var i = 0; i < list.Length; ++i)
            {
                if (list[i] == hash)
                {
                    return(i);
                }
            }

            return(-1);
        }
示例#3
0
        public static IReadOnlyList <BoneNode> BuildBoneHierachy([NotNull] PrettyAvatar avatar, [NotNull] PrettyMesh mesh)
        {
            var boneList = new List <BoneNode>();

            for (var i = 0; i < avatar.AvatarSkeleton.Nodes.Length; i++)
            {
                var n = avatar.AvatarSkeleton.Nodes[i];

                var parent   = n.ParentIndex >= 0 ? boneList[n.ParentIndex] : null;
                var boneId   = avatar.AvatarSkeleton.NodeIDs[i];
                var bonePath = avatar.BoneNamesMap[boneId];

                var boneIndex = avatar.AvatarSkeleton.NodeIDs.FindIndex(boneId);

                if (boneIndex < 0)
                {
                    throw new IndexOutOfRangeException();
                }

                var initialPose = avatar.AvatarSkeletonPose.Transforms[boneIndex];

                var bone = new BoneNode(parent, i, bonePath, initialPose.LocalPosition.ToOpenTK(), initialPose.LocalRotation.ToOpenTK());

                boneList.Add(bone);
            }

            foreach (var bone in boneList)
            {
                bone.Initialize();
            }

#if DEBUG
            Debug.Print("Model bones:");

            for (var i = 0; i < boneList.Count; i++)
            {
                var bone = boneList[i];
                Debug.Print("[{0}]: {1}", i, bone.ToString());
            }
#endif

            return(boneList);
        }
示例#4
0
        public AnimationRenderer([NotNull] Game game,
                                 [NotNull] PrettyMesh bodyMesh, [NotNull] PrettyAvatar bodyAvatar, [NotNull, ItemNotNull] IReadOnlyList <BoneNode> bodyBoneList,
                                 [NotNull] PrettyMesh headMesh, [NotNull] PrettyAvatar headAvatar, [NotNull, ItemNotNull] IReadOnlyList <BoneNode> headBoneList,
                                 [NotNull] BodyAnimation animation)
        {
            _game         = game;
            _bodyMesh     = bodyMesh;
            _bodyAvatar   = bodyAvatar;
            _bodyBoneList = bodyBoneList;
            _headMesh     = headMesh;
            _headAvatar   = headAvatar;
            _headBoneList = headBoneList;
            _animation    = animation;

            #region Body

            var bodyVertexBuffer = new VertexBuffer();
            var bodyIndexBuffer  = new IndexBuffer();

            var bodyVertices = new PosNorm[bodyMesh.Vertices.Length];
            var bodyIndices  = new uint[bodyMesh.Indices.Length];

            for (var k = 0; k < bodyVertices.Length; ++k)
            {
                bodyVertices[k] = new PosNorm {
                    Position = bodyMesh.Vertices[k].ToOpenTK().FixCoordSystem(),
                    Normal   = bodyMesh.Normals[k].ToOpenTK().FixCoordSystem()
                };
            }

            _originalBodyVertices = bodyVertices;
            _bodyVertices         = (PosNorm[])bodyVertices.Clone();

            for (var k = 0; k < bodyIndices.Length; ++k)
            {
                bodyIndices[k] = bodyMesh.Indices[k];
            }

            bodyVertexBuffer.BufferData(bodyVertices, BufferUsageHint.StreamDraw);
            bodyIndexBuffer.BufferData(bodyIndices, BufferUsageHint.StaticDraw);

            _bodyVertexBuffer = bodyVertexBuffer;
            _bodyIndexBuffer  = bodyIndexBuffer;

            #endregion

            #region Head

            var headVertexBuffer = new VertexBuffer();
            var headIndexBuffer  = new IndexBuffer();

            var headVertices = new PosNorm[headMesh.Vertices.Length];
            var headIndices  = new uint[headMesh.Indices.Length];

            for (var k = 0; k < headVertices.Length; ++k)
            {
                headVertices[k] = new PosNorm {
                    Position = headMesh.Vertices[k].ToOpenTK().FixCoordSystem(),
                    Normal   = headMesh.Normals[k].ToOpenTK().FixCoordSystem()
                };
            }

            _originalHeadVertices = headVertices;
            _headVertices         = (PosNorm[])headVertices.Clone();

            for (var k = 0; k < headIndices.Length; ++k)
            {
                headIndices[k] = headMesh.Indices[k];
            }

            headVertexBuffer.BufferData(headVertices, BufferUsageHint.StreamDraw);
            headIndexBuffer.BufferData(headIndices, BufferUsageHint.StaticDraw);

            _headVertexBuffer = headVertexBuffer;
            _headIndexBuffer  = headIndexBuffer;

            #endregion
        }
示例#5
0
        public IReadOnlyList <BoneNode> BuildBoneHierarchy([NotNull] PrettyAvatar avatar, bool fixKubi)
        {
            var boneList      = new List <BoneNode>();
            var skeletonNodes = avatar.AvatarSkeleton.Nodes;

            for (var i = 0; i < skeletonNodes.Length; i++)
            {
                var n = skeletonNodes[i];

                var parent   = n.ParentIndex >= 0 ? boneList[n.ParentIndex] : null;
                var boneId   = avatar.AvatarSkeleton.NodeIDs[i];
                var bonePath = avatar.BoneNamesMap[boneId];

                var boneIndex = avatar.AvatarSkeleton.NodeIDs.FindIndex(boneId);

                if (boneIndex < 0)
                {
                    throw new IndexOutOfRangeException();
                }

                var initialPose = avatar.AvatarSkeletonPose.Transforms[boneIndex];

                var t = initialPose.Translation.ToOpenTK() * _scalingConfig.ScaleUnityToPmx;
                var q = initialPose.Rotation.ToOpenTK();

                var bone = new BoneNode(parent, i, bonePath, t, q);

                boneList.Add(bone);
            }

#if DEBUG
            Debug.Print("Model bones:");

            for (var i = 0; i < boneList.Count; i++)
            {
                var bone = boneList[i];
                Debug.Print("[{0}]: {1}", i, bone.ToString());
            }
#endif

            foreach (var bone in boneList)
            {
                bone.Initialize();
            }

            if (fixKubi)
            {
                // Fix "KUBI" (neck) bone's parent
                var kubiParent = boneList.SingleOrDefault(bn => bn.Path == "MODEL_00/BASE/MUNE1/MUNE2/KUBI");
                var kubiBone   = boneList.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;

                foreach (var bone in boneList)
                {
                    bone.Initialize(true);
                }
            }

            foreach (var bone in boneList)
            {
                var level  = 0;
                var parent = bone.Parent;

                while (parent != null)
                {
                    ++level;
                    parent = parent.Parent;
                }

                bone.Level = level;
            }

            return(boneList);
        }
示例#6
0
 public IReadOnlyList <BoneNode> BuildBoneHierarchy([NotNull] PrettyAvatar avatar)
 {
     return(BuildBoneHierarchy(avatar, true));
 }
        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);
        }
示例#8
0
 public static string FindBonePathByNameHash([NotNull] this PrettyAvatar avatar, uint hash)
 {
     avatar.BoneNamesMap.TryGetValue(hash, out var result);
     return(result);
 }
        public VmdMotion CreateCharacterAnimation([CanBeNull] IBodyAnimationSource bodyAnimationSource, [CanBeNull] PrettyAvatar avatar, [CanBeNull] PmxModel mltdPmxModel)
        {
            IReadOnlyList <VmdBoneFrame> frames;

            if (ProcessBoneFrames && (bodyAnimationSource != null && avatar != null && mltdPmxModel != null))
            {
                frames = CreateBoneFrames(bodyAnimationSource, avatar, mltdPmxModel);
            }
            else
            {
                frames = Array.Empty <VmdBoneFrame>();
            }

            return(new VmdMotion(ModelName, frames, null, null, null, null));
        }
示例#10
0
        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());
        }
示例#11
0
        public VmdMotion CreateDanceMotion([CanBeNull] IBodyAnimationSource mainDance, [NotNull] ScenarioObject baseScenario, [CanBeNull] ScenarioObject formationInfo, [CanBeNull] PrettyAvatar avatar, [CanBeNull] PmxModel mltdPmxModel, [CanBeNull] IBodyAnimationSource danceAppeal, int formationNumber, AppealType appealType)
        {
            VmdBoneFrame[] frames;

            if (ProcessBoneFrames && (mainDance != null && avatar != null && mltdPmxModel != null))
            {
                frames = CreateBoneFrames(mainDance, avatar, mltdPmxModel, baseScenario, formationInfo, danceAppeal, formationNumber, appealType);
            }
            else
            {
                frames = Array.Empty <VmdBoneFrame>();
            }

            return(new VmdMotion(ModelName, frames, null, null, null, null));
        }
示例#12
0
 public BoneNode[] BuildBoneHierarchy([NotNull] PrettyAvatar avatar)
 {
     return(BuildBoneHierarchy(avatar, true));
 }