private void BuildSkeleton(Transform root)
        {
            var position = root.position;

            root.position = Vector3.zero;

            try
            {
                // hips -> spine -> chest
                var builder = new SkeletonBuilder(root);
                builder.AddHips(0.8f, 0.2f);
                builder.AddSpine(0.1f);
                builder.AddChest(0.2f);
                builder.AddNeck(0.1f);
                builder.AddHead(0.2f);
                builder.AddArm(0.1f, 0.3f, 0.3f, 0.1f);
                builder.AddLeg(0.1f, 0.3f, 0.4f, 0.1f, 0.1f);

                var description = AvatarDescription.Create(builder.Skeleton);
                var animator    = GetComponent <Animator>();
                animator.avatar = description.CreateAvatar(root);

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

                if (m_material == null)
                {
                    m_material = new Material(Shader.Find("Standard"));
                }
                renderer.sharedMaterial = m_material;
                //root.gameObject.AddComponent<BoneMapping>();

                var transfer = GetComponent <HumanPoseTransfer>();
                if (transfer != null)
                {
                    transfer.Avatar = animator.avatar;
                    transfer.Setup();
                }
            }
            finally
            {
                // restore position
                root.position = position;
            }
        }
Exemplo n.º 2
0
        public void Load()
        {
            //
            // build hierarchy
            //
            Root = new GameObject(System.IO.Path.GetFileNameWithoutExtension(Path));
            var hips        = BuildHierarchy(Root.transform, Bvh.Root, 1.0f);
            var skeleton    = Skeleton.Estimate(hips);
            var description = AvatarDescription.Create(hips.Traverse().ToArray(), skeleton);

            //
            // 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 Root.transform.Traverse())
                {
                    x.localPosition *= scaling;
                }

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

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

            animator.avatar = Avatar;

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

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

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

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

            humanPoseTransfer.Avatar = Avatar;

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

            Material = new Material(Shader.Find("Standard"));
            renderer.sharedMaterial = Material;
            Mesh      = renderer.sharedMesh;
            Mesh.name = "box-man";
        }
Exemplo n.º 3
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>();
        }
        public void Load()
        {
            //
            // build hierarchy
            //
            GlobalData.clear();

            if (Root == null)
            {
                Root     = new GameObject(System.IO.Path.GetFileNameWithoutExtension(Path));
                Root.tag = "cubeman";
                GlobalData.SetFocusObj(Root);
                GlobalData.animeobjname = Root.name;

                var hips        = BuildHierarchy(Root.transform, Bvh.Root, 1.0f, null);
                var skeleton    = Skeleton.Estimate(hips);
                var description = AvatarDescription.Create(hips.Traverse().ToArray(), skeleton);
                //
                // scaling. reposition
                //
                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;
                    GlobalData.m_scale = scaling;
                    foreach (var x in Root.transform.Traverse())
                    {
                        x.localPosition *= scaling;
                    }

                    var scaledHeight = hipHeight * scaling;
                    hips.position = new Vector3(0, scaledHeight, 0); // foot to ground
                }
                GlobalData.FrameCountList.Add(Bvh.FrameCount);
                Debug.Log("Bvh.FrameCount : " + Bvh.FrameCount);

                Avatar            = description.CreateAvatar(Root.transform);
                Avatar.name       = "Avatar";
                AvatarDescription = description;

                var animator = Root.AddComponent <Animator>();
                animator.avatar = Avatar;
                GlobalData.SetAvatar(Avatar);

                AnimationClip Animation = BvhAnimation.CreateAnimationClip(Bvh, scaling, true);
                Animation.name     = Root.name;
                Animation.legacy   = true;
                Animation.wrapMode = WrapMode.Loop;
                alginSec           = Animation.length;
                var animation = Root.AddComponent <Animation>();
                animation.AddClip(Animation, Animation.name);
                animation.clip = Animation;
                animation.Play();
                GlobalData.SetAnimationClip(Animation);

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

                // create SkinnedMesh for bone visualize
                var renderer = SkeletonMeshUtility.CreateRenderer(animator);
                Material = new Material(Shader.Find("Standard"));
                renderer.sharedMaterial = Material;
                Mesh      = renderer.sharedMesh;
                Mesh.name = "box-man";

                Root.AddComponent <BoneMapping>();
            }
            else
            {
                GlobalData.FrameCountList.Add(Bvh.FrameCount);
                Debug.Log("Bvh.FrameCount : " + Bvh.FrameCount);

                AnimationClip Animation = BvhAnimation.CreateAnimationClip(Bvh, scaling);
                Animation.name     = Root.name + bvhList.Count().ToString();
                Animation.legacy   = true;
                Animation.wrapMode = WrapMode.Loop;
                var animation = Root.GetComponent <Animation>();
                GlobalData.timeAlign = Animation.length / alginSec;
                animation.AddClip(Animation, Animation.name);
                animation.clip = Animation;

                animation.Play();
                //  GlobalData.SetAnimationClip(Animation);
            }

            // create AnimationClip
            //
        }
Exemplo n.º 5
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";
        }