public void Parse(string path)
 {
     Path   = path;
     Source = File.ReadAllText(Path, Encoding.UTF8);
     Bvh    = Bvh.Parse(Source);
     bvhList.Add(Bvh);
 }
        public void GuessBoneMapping_daz_friendry()
        {
            var bvh      = Bvh.Parse(daz_friendry_herarchy);
            var detector = new BvhSkeletonEstimator();
            var skeleton = detector.Detect(bvh);

            Assert.AreEqual("hip", skeleton.GetBoneName(HumanBodyBones.Hips));
            Assert.AreEqual("abdomen", skeleton.GetBoneName(HumanBodyBones.Spine));
            Assert.AreEqual("chest", skeleton.GetBoneName(HumanBodyBones.Chest));

            Assert.AreEqual("neck", skeleton.GetBoneName(HumanBodyBones.Neck));
            Assert.AreEqual("head", skeleton.GetBoneName(HumanBodyBones.Head));

            Assert.AreEqual("lCollar", skeleton.GetBoneName(HumanBodyBones.LeftShoulder));
            Assert.AreEqual("lShldr", skeleton.GetBoneName(HumanBodyBones.LeftUpperArm));
            Assert.AreEqual("lForeArm", skeleton.GetBoneName(HumanBodyBones.LeftLowerArm));
            Assert.AreEqual("lHand", skeleton.GetBoneName(HumanBodyBones.LeftHand));

            Assert.AreEqual("rCollar", skeleton.GetBoneName(HumanBodyBones.RightShoulder));
            Assert.AreEqual("rShldr", skeleton.GetBoneName(HumanBodyBones.RightUpperArm));
            Assert.AreEqual("rForeArm", skeleton.GetBoneName(HumanBodyBones.RightLowerArm));
            Assert.AreEqual("rHand", skeleton.GetBoneName(HumanBodyBones.RightHand));

            Assert.AreEqual("lThigh", skeleton.GetBoneName(HumanBodyBones.LeftUpperLeg));
            Assert.AreEqual("lShin", skeleton.GetBoneName(HumanBodyBones.LeftLowerLeg));
            Assert.AreEqual("lFoot", skeleton.GetBoneName(HumanBodyBones.LeftFoot));

            Assert.AreEqual("rThigh", skeleton.GetBoneName(HumanBodyBones.RightUpperLeg));
            Assert.AreEqual("rShin", skeleton.GetBoneName(HumanBodyBones.RightLowerLeg));
            Assert.AreEqual("rFoot", skeleton.GetBoneName(HumanBodyBones.RightFoot));
        }
        public void GuessBoneMapping_mocapdatacom()
        {
            var bvh      = Bvh.Parse(mocapdata_com_hierarchy);
            var detector = new BvhSkeletonEstimator();
            var skeleton = detector.Detect(bvh);

            Assert.AreEqual(0, skeleton.GetBoneIndex(HumanBodyBones.Hips));

            Assert.AreEqual("Hips", skeleton.GetBoneName(HumanBodyBones.Hips));
            Assert.AreEqual("Chest", skeleton.GetBoneName(HumanBodyBones.Spine));
            Assert.AreEqual("Chest2", skeleton.GetBoneName(HumanBodyBones.Chest));

            Assert.AreEqual("Neck", skeleton.GetBoneName(HumanBodyBones.Neck));
            Assert.AreEqual("Head", skeleton.GetBoneName(HumanBodyBones.Head));

            Assert.AreEqual("LeftCollar", skeleton.GetBoneName(HumanBodyBones.LeftShoulder));
            Assert.AreEqual("LeftShoulder", skeleton.GetBoneName(HumanBodyBones.LeftUpperArm));
            Assert.AreEqual("LeftElbow", skeleton.GetBoneName(HumanBodyBones.LeftLowerArm));
            Assert.AreEqual("LeftWrist", skeleton.GetBoneName(HumanBodyBones.LeftHand));

            Assert.AreEqual("RightCollar", skeleton.GetBoneName(HumanBodyBones.RightShoulder));
            Assert.AreEqual("RightShoulder", skeleton.GetBoneName(HumanBodyBones.RightUpperArm));
            Assert.AreEqual("RightElbow", skeleton.GetBoneName(HumanBodyBones.RightLowerArm));
            Assert.AreEqual("RightWrist", skeleton.GetBoneName(HumanBodyBones.RightHand));

            Assert.AreEqual("LeftHip", skeleton.GetBoneName(HumanBodyBones.LeftUpperLeg));
            Assert.AreEqual("LeftKnee", skeleton.GetBoneName(HumanBodyBones.LeftLowerLeg));
            Assert.AreEqual("LeftAnkle", skeleton.GetBoneName(HumanBodyBones.LeftFoot));

            Assert.AreEqual("RightHip", skeleton.GetBoneName(HumanBodyBones.RightUpperLeg));
            Assert.AreEqual("RightKnee", skeleton.GetBoneName(HumanBodyBones.RightLowerLeg));
            Assert.AreEqual("RightAnkle", skeleton.GetBoneName(HumanBodyBones.RightFoot));
        }
            public void AddCurves(Bvh bvh, AnimationClip clip, float toMeter)
            {
                AddCurve(bvh, clip, PositionX, -toMeter);
                AddCurve(bvh, clip, PositionY, toMeter);
                AddCurve(bvh, clip, PositionZ, toMeter);

                var pathWithProp = default(Bvh.PathWithProperty);

                bvh.TryGetPathWithPropertyFromChannel(RotationX, out pathWithProp);

                // rotation
                var curveX = new AnimationCurve();
                var curveY = new AnimationCurve();
                var curveZ = new AnimationCurve();
                var curveW = new AnimationCurve();

                for (int i = 0; i < bvh.FrameCount; ++i)
                {
                    var time = (float)(i * bvh.FrameTime.TotalSeconds);
                    var q    = GetRotation(i).ReverseX();
                    curveX.AddKey(time, q.x);
                    curveY.AddKey(time, q.y);
                    curveZ.AddKey(time, q.z);
                    curveW.AddKey(time, q.w);
                }
                clip.SetCurve(pathWithProp.Path, typeof(Transform), "localRotation.x", curveX);
                clip.SetCurve(pathWithProp.Path, typeof(Transform), "localRotation.y", curveY);
                clip.SetCurve(pathWithProp.Path, typeof(Transform), "localRotation.z", curveZ);
                clip.SetCurve(pathWithProp.Path, typeof(Transform), "localRotation.w", curveW);
            }
Пример #5
0
        public Skeleton Detect(Bvh bvh)
        {
            var root = new BvhBone(bvh.Root.Name, Vector3.zero);

            root.Build(bvh.Root);
            return(Detect(root.Traverse().Select(x => (IBone)x).ToList()));
        }
Пример #6
0
        public static Bvh Parse(string src)
        {
            using (var r = new StringReader(src))
            {
                if (r.ReadLine() != "HIERARCHY")
                {
                    throw new BvhException("not start with HIERARCHY");
                }

                var root = ParseNode(r);

                if (root == null)
                {
                    return(null);
                }

                var frames    = 0;
                var frameTime = 0.0f;
                if (r.ReadLine() == "MOTION")
                {
                    var      tmp = r.ReadLine();
                    string[] frameSplited;
                    if (tmp.Split(':')[0] != "Frames")
                    {
                        frameSplited = r.ReadLine().Split(':');
                    }
                    else
                    {
                        frameSplited = tmp.Split(':');
                    }

                    if (frameSplited[0] != "Frames")
                    {
                        throw new BvhException("Frames is not found");
                    }

                    frames = int.Parse(frameSplited[1]);

                    var frameTimeSplited = r.ReadLine().Split(':');

                    if (frameTimeSplited[0] != "Frame Time")
                    {
                        throw new BvhException("Frame Time is not found");
                    }

                    frameTime = float.Parse(frameTimeSplited[1], CultureInfo.InvariantCulture.NumberFormat);
                }

                var bvh = new Bvh(root, frames, frameTime);

                for (int i = 0; i < frames; ++i)
                {
                    var line = r.ReadLine();
                    bvh.ParseFrame(i, line);
                }

                return(bvh);
            }
        }
        public static AnimationClip CreateAnimationClip(Bvh bvh, float scaling, bool f = false)
        {
            var clip = new AnimationClip();

            clip.legacy = true;

            var curveMap = new Dictionary <BvhNode, CurveSet>();
            var frameSet = new Dictionary <string, CurveSet>();
            int j        = 0;

            foreach (var node in bvh.Root.Traverse())
            {
                var set = new CurveSet(node);
                curveMap[node] = set;

                for (int i = 0; i < node.Channels.Length; ++i, ++j)
                {
                    var curve = bvh.Channels[j];
                    switch (node.Channels[i])
                    {
                    case Channel.Xposition: set.PositionX = curve; break;

                    case Channel.Yposition: set.PositionY = curve; break;

                    case Channel.Zposition: set.PositionZ = curve; break;

                    case Channel.Xrotation: set.RotationX = curve; break;

                    case Channel.Yrotation: set.RotationY = curve; break;

                    case Channel.Zrotation: set.RotationZ = curve; break;

                    default: throw new Exception();
                    }
                }

                frameSet[node.Name] = set;

                /* if (node.Name == "hip")
                 * {
                 *   int step = bvh.FrameCount / (GlobalData.cpsNum-1);
                 *
                 *   for (int k = 0, m = 0; k < bvh.FrameCount; k+= step, m++)
                 *   {
                 *       GlobalData.SetCps(set.GetPosition(k)* scaling, m);
                 *   }
                 * }*/
            }
            GlobalData.frameset.Add(frameSet);
            foreach (var set in curveMap)
            {
                set.Value.AddCurves(bvh, clip, scaling);
            }

            clip.EnsureQuaternionContinuity();

            return(clip);
        }
Пример #8
0
        public static Bvh Parse(string src)
        {
            using (var r = new StringReader(src))
            {
                var first = r.ReadLine();
                if (first != "HIERARCHY")
                {
                    throw new BvhException("not start with HIERARCHY");
                }

                var root = ParseNode(r);
                if (root == null)
                {
                    return(null);
                }

                var frames    = 0;
                var frameTime = 0.0f;
                if (r.ReadLine() == "MOTION")
                {
                    var frameSplit = r.ReadLine().Split(':');
                    if (frameSplit[0] != "Frames")
                    {
                        throw new BvhException("Frames is not found");
                    }
                    frames = int.Parse(frameSplit[1]);

                    var frameTimeSplit = r.ReadLine().Split(':');
                    if (frameTimeSplit[0] != "Frame Time")
                    {
                        throw new BvhException("Frame Time is not found");
                    }
                    frameTime = float.Parse(frameTimeSplit[1], System.Globalization.CultureInfo.InvariantCulture);
                }

                var bvh = new Bvh(root, frames, frameTime);

                for (int i = 0; i < frames; ++i)
                {
                    var line = r.ReadLine();
                    bvh.ParseFrame(i, line);
                }

                return(bvh);
            }
        }
        public static AnimationClip CreateAnimationClip(Bvh bvh, float toMeter)
        {
            var clip = new AnimationClip();

            clip.legacy = true;

            var curveMap = new Dictionary <BvhNode, CurveSet>();

            int j = 0;

            foreach (var node in bvh.Root.Traverse())
            {
                var set = new CurveSet(node);
                curveMap[node] = set;

                for (int i = 0; i < node.Channels.Length; ++i, ++j)
                {
                    var curve = bvh.Channels[j];
                    switch (node.Channels[i])
                    {
                    case Channel.Xposition: set.PositionX = curve; break;

                    case Channel.Yposition: set.PositionY = curve; break;

                    case Channel.Zposition: set.PositionZ = curve; break;

                    case Channel.Xrotation: set.RotationX = curve; break;

                    case Channel.Yrotation: set.RotationY = curve; break;

                    case Channel.Zrotation: set.RotationZ = curve; break;

                    default: throw new Exception();
                    }
                }
            }

            foreach (var set in curveMap)
            {
                set.Value.AddCurves(bvh, clip, toMeter);
            }

            clip.EnsureQuaternionContinuity();

            return(clip);
        }
Пример #10
0
        public static Bvh Parse(string src)
        {
            using (var r = new StringReader(src))
            {
                if (r.ReadLine() != "HIERARCHY")
                {
                    throw new BvhException("not start with HIERARCHY");
                }

                var root = ParseNode(r);
                if (root == null)
                {
                    return(null);
                }

                if (r.ReadLine() != "MOTION")
                {
                    throw new BvhException("MOTION is not found");
                }

                var frameSplited = r.ReadLine().Split(':');
                if (frameSplited[0] != "Frames")
                {
                    throw new BvhException("Frames is not found");
                }
                var frames = int.Parse(frameSplited[1]);

                var frameTimeSplited = r.ReadLine().Split(':');
                if (frameTimeSplited[0] != "Frame Time")
                {
                    throw new BvhException("Frame Time is not found");
                }
                var frameTime = float.Parse(frameTimeSplited[1]);

                var bvh = new Bvh(root, frames, frameTime);

                for (int i = 0; i < frames; ++i)
                {
                    var line = r.ReadLine();
                    bvh.ParseFrame(i, line);
                }

                return(bvh);
            }
        }
Пример #11
0
            static void AddCurve(Bvh bvh, AnimationClip clip, ChannelCurve ch, float toMeter)
            {
                if (ch == null)
                {
                    return;
                }
                var pathWithProp = default(Bvh.PathWithProperty);

                bvh.TryGetPathWithPropertyFromChannel(ch, out pathWithProp);
                var curve = new AnimationCurve();

                for (int i = 0; i < bvh.FrameCount; ++i)
                {
                    var time  = (float)(i * bvh.FrameTime.TotalSeconds);
                    var value = ch.Keys[i] * toMeter;
                    curve.AddKey(time, value);
                }
                clip.SetCurve(pathWithProp.Path, typeof(Transform), pathWithProp.Property, curve);
            }
        public void GuessBoneMapping_mocap()
        {
            var bvh      = Bvh.Parse(bvh_mocap);
            var detector = new BvhSkeletonEstimator();
            var skeleton = detector.Detect(bvh);

            Assert.AreEqual(0, skeleton.GetBoneIndex(HumanBodyBones.Hips));

            Assert.AreEqual("Hips", skeleton.GetBoneName(HumanBodyBones.Hips));
            Assert.AreEqual("Spine", skeleton.GetBoneName(HumanBodyBones.Spine));
            Assert.AreEqual("Spine1", skeleton.GetBoneName(HumanBodyBones.Chest));

            Assert.AreEqual(null, skeleton.GetBoneName(HumanBodyBones.UpperChest));

            Assert.AreEqual("Neck", skeleton.GetBoneName(HumanBodyBones.Neck));
            Assert.AreEqual("Head", skeleton.GetBoneName(HumanBodyBones.Head));

            Assert.AreEqual("LeftShoulder", skeleton.GetBoneName(HumanBodyBones.LeftShoulder));
            Assert.AreEqual("LeftArm", skeleton.GetBoneName(HumanBodyBones.LeftUpperArm));
            Assert.AreEqual("LeftForeArm", skeleton.GetBoneName(HumanBodyBones.LeftLowerArm));
            Assert.AreEqual("LeftHand", skeleton.GetBoneName(HumanBodyBones.LeftHand));

            Assert.AreEqual("RightShoulder", skeleton.GetBoneName(HumanBodyBones.RightShoulder));
            Assert.AreEqual("RightArm", skeleton.GetBoneName(HumanBodyBones.RightUpperArm));
            Assert.AreEqual("RightForeArm", skeleton.GetBoneName(HumanBodyBones.RightLowerArm));
            Assert.AreEqual("RightHand", skeleton.GetBoneName(HumanBodyBones.RightHand));

            Assert.AreEqual("LeftUpLeg", skeleton.GetBoneName(HumanBodyBones.LeftUpperLeg));
            Assert.AreEqual("LeftLeg", skeleton.GetBoneName(HumanBodyBones.LeftLowerLeg));
            Assert.AreEqual("LeftFoot", skeleton.GetBoneName(HumanBodyBones.LeftFoot));
            Assert.AreEqual("LeftToeBase", skeleton.GetBoneName(HumanBodyBones.LeftToes));

            Assert.AreEqual("RightUpLeg", skeleton.GetBoneName(HumanBodyBones.RightUpperLeg));
            Assert.AreEqual("RightLeg", skeleton.GetBoneName(HumanBodyBones.RightLowerLeg));
            Assert.AreEqual("RightFoot", skeleton.GetBoneName(HumanBodyBones.RightFoot));
            Assert.AreEqual("RightToeBase", skeleton.GetBoneName(HumanBodyBones.RightToes));
        }
Пример #13
0
        public static void Import(ImporterContext context)
        {
            //
            // parse
            //
            context.Source = File.ReadAllText(context.Path, Encoding.UTF8);
            context.Bvh    = Bvh.Parse(context.Source);
            Debug.LogFormat("parsed {0}", context.Bvh);

            //
            // build hierarchy
            //
            context.Root = new GameObject(Path.GetFileNameWithoutExtension(context.Path));
            var animator = context.Root.AddComponent <Animator>();

            BuildHierarchy(context.Root.transform, context.Bvh.Root, 1.0f);

            var minY = 0.0f;

            foreach (var x in context.Root.transform.Traverse())
            {
                if (x.position.y < minY)
                {
                    minY = x.position.y;
                }
            }

            var toMeter = 1.0f / (-minY);

            Debug.LogFormat("minY: {0} {1}", minY, toMeter);
            foreach (var x in context.Root.transform.Traverse())
            {
                x.localPosition *= toMeter;
            }

            // foot height to 0
            var hips = context.Root.transform.GetChild(0);

            hips.position = new Vector3(0, -minY * toMeter, 0);

            //
            // create AnimationClip
            //
            context.Animation          = BvhAnimation.CreateAnimationClip(context.Bvh, toMeter);
            context.Animation.name     = context.Root.name;
            context.Animation.legacy   = true;
            context.Animation.wrapMode = WrapMode.Loop;

            var animation = context.Root.AddComponent <Animation>();

            animation.AddClip(context.Animation, context.Animation.name);
            animation.clip = context.Animation;
            animation.Play();

            var boneMapping = context.Root.AddComponent <BoneMapping>();

            boneMapping.Bones[(int)HumanBodyBones.Hips] = hips.gameObject;
            boneMapping.GuessBoneMapping();
            var description = AvatarDescription.Create();

            BoneMapping.SetBonesToDescription(boneMapping, description);
            context.Avatar            = description.CreateAvatar(context.Root.transform);
            context.Avatar.name       = "Avatar";
            context.AvatarDescription = description;
            animator.avatar           = context.Avatar;

            var humanPoseTransfer = context.Root.AddComponent <HumanPoseTransfer>();

            humanPoseTransfer.Avatar = context.Avatar;

            // create SkinnedMesh for bone visualize
            var renderer = SkeletonMeshUtility.CreateRenderer(animator);

            context.Material        = new Material(Shader.Find("Standard"));
            renderer.sharedMaterial = context.Material;
            context.Mesh            = renderer.sharedMesh;
            context.Mesh.name       = "box-man";
        }
Пример #14
0
 public void Parse(string path, string source)
 {
     Path   = path;
     Source = source;
     Bvh    = Bvh.Parse(Source);
 }
Пример #15
0
 public void rawParse(string Source)
 {
     Bvh = Bvh.Parse(Source);
 }
Пример #16
0
        public static void Import(ImporterContext context)
        {
            //
            // parse
            //
            context.Source = File.ReadAllText(context.Path, Encoding.UTF8);
            context.Bvh    = Bvh.Parse(context.Source);
            Debug.LogFormat("parsed {0}", context.Bvh);

            //
            // build hierarchy
            //
            context.Root = new GameObject(Path.GetFileNameWithoutExtension(context.Path));

            BuildHierarchy(context.Root.transform, context.Bvh.Root, 1.0f);

            var hips        = context.Root.transform.GetChild(0);
            var estimater   = new BvhSkeletonEstimator();
            var skeleton    = estimater.Detect(hips.transform);
            var description = AvatarDescription.Create();

            //var values= ((HumanBodyBones[])Enum.GetValues(typeof(HumanBodyBones)));
            description.SetHumanBones(skeleton.ToDictionary(hips.Traverse().ToArray()));

            //
            // scaling. reposition
            //
            float scaling = 1.0f;

            {
                //var foot = animator.GetBoneTransform(HumanBodyBones.LeftFoot);
                var foot      = hips.Traverse().Skip(skeleton.GetBoneIndex(HumanBodyBones.LeftFoot)).First();
                var hipHeight = hips.position.y - foot.position.y;
                // hips height to a meter
                scaling = 1.0f / hipHeight;
                foreach (var x in context.Root.transform.Traverse())
                {
                    x.localPosition *= scaling;
                }

                var scaledHeight = hipHeight * scaling;
                hips.position = new Vector3(0, scaledHeight, 0); // foot to ground
            }

            //
            // avatar
            //
            context.Avatar            = description.CreateAvatar(context.Root.transform);
            context.Avatar.name       = "Avatar";
            context.AvatarDescription = description;
            var animator = context.Root.AddComponent <Animator>();

            animator.avatar = context.Avatar;

            //
            // create AnimationClip
            //
            context.Animation          = BvhAnimation.CreateAnimationClip(context.Bvh, scaling);
            context.Animation.name     = context.Root.name;
            context.Animation.legacy   = true;
            context.Animation.wrapMode = WrapMode.Loop;

            var animation = context.Root.AddComponent <Animation>();

            animation.AddClip(context.Animation, context.Animation.name);
            animation.clip = context.Animation;
            animation.Play();

            var humanPoseTransfer = context.Root.AddComponent <HumanPoseTransfer>();

            humanPoseTransfer.Avatar = context.Avatar;

            // create SkinnedMesh for bone visualize
            var renderer = SkeletonMeshUtility.CreateRenderer(animator);

            context.Material        = new Material(Shader.Find("Standard"));
            renderer.sharedMaterial = context.Material;
            context.Mesh            = renderer.sharedMesh;
            context.Mesh.name       = "box-man";

            context.Root.AddComponent <BoneMapping>();
        }
Пример #17
0
        public static void Import(ImporterContext context)
        {
            //
            // parse
            //
            context.Source = File.ReadAllText(context.Path, Encoding.UTF8);
            context.Bvh    = Bvh.Parse(context.Source);
            Debug.LogFormat("parsed {0}", context.Bvh);

            //
            // build hierarchy
            //
            context.Root = new GameObject(Path.GetFileNameWithoutExtension(context.Path));
            context.Root.AddComponent <Animator>();

            BuildHierarchy(context.Root.transform, context.Bvh.Root, 1.0f);

            var minY = 0.0f;

            foreach (var x in context.Root.transform.Traverse())
            {
                if (x.position.y < minY)
                {
                    minY = x.position.y;
                }
            }

            var toMeter = 1.0f / (-minY);

            Debug.LogFormat("minY: {0} {1}", minY, toMeter);
            foreach (var x in context.Root.transform.Traverse())
            {
                x.localPosition *= toMeter;
            }

            // foot height to 0
            var hips = context.Root.transform.GetChild(0);

            hips.position = new Vector3(0, -minY * toMeter, 0);

            //
            // create AnimationClip
            //
            context.Animation          = BvhAnimation.CreateAnimationClip(context.Bvh, toMeter);
            context.Animation.name     = context.Root.name;
            context.Animation.legacy   = true;
            context.Animation.wrapMode = WrapMode.Loop;

            var animation = context.Root.AddComponent <Animation>();

            animation.AddClip(context.Animation, context.Animation.name);
            animation.clip = context.Animation;
            animation.Play();

            var boneMapping = context.Root.AddComponent <BoneMapping>();

            boneMapping.Bones[(int)HumanBodyBones.Hips] = hips.gameObject;
            boneMapping.GuessBoneMapping();
            context.Avatar            = boneMapping.CreateAvatar();
            context.AvatarDescription = boneMapping.Description;

            var humanPoseTransfer = context.Root.AddComponent <HumanPoseTransfer>();

            humanPoseTransfer.Avatar = context.Avatar;
        }