Beispiel #1
0
        public static Model Load(string name, Bvh.Bvh bvh)
        {
            var model = CreateFromBvh(bvh.Root);

            // estimate skeleton
            var skeleton = SkeletonEstimator.Detect(model.Root);

            if (skeleton == null)
            {
                throw new Exception("fail to estimate skeleton");
            }

            // foot to zero
            var minY = model.Nodes.Min(x => x.Translation.Y);
            var hips = model.Nodes.First(x => x.HumanoidBone == HumanoidBones.hips);

            if (model.Root.Children.Count != 1)
            {
                throw new Exception();
            }
            if (model.Root.Children[0] != hips)
            {
                throw new Exception();
            }
            hips.Translation -= new Vector3(0, minY, 0);

            // normalize scale
            var pos    = hips.Translation;
            var factor = 1.0f;

            if (pos.Y != 0)
            {
                factor = 1.0f / pos.Y;
                foreach (var x in hips.Traverse())
                {
                    x.LocalTranslation *= factor;
                }
                hips.Translation = new Vector3(pos.X, 1.0f, pos.Z);
            }

            // animation
            model.Animations.Add(LoadAnimation(name, bvh, model, factor));

            // add origin
            var origin = new Node("origin");

            origin.Add(model.Root.Children[0]);
            model.Nodes.Add(origin);
            model.Root.Add(origin);

            return(model);
        }
Beispiel #2
0
        public static string HumanoidBoneEstimate(this Model model)
        {
            var sb = new System.Text.StringBuilder();

            sb.Append("HumanoidBoneEstimate: ");

            // estimate skeleton
            var skeleton = SkeletonEstimator.Detect(model.Root);

            if (skeleton == null)
            {
                return("fail to estimate skeleton");
            }

            // rename bone
            foreach (var kv in skeleton)
            {
                kv.Value.Name = kv.Key.ToString();
            }

            if (model.Vrm == null)
            {
                sb.Append("add vrm humanoid");
                model.Vrm = new Vrm(new Meta
                {
                }, "UniVRM-0.51.0", "0.0");
            }
            else
            {
            }

            StringBuilder(sb, skeleton[HumanoidBones.hips]);

            foreach (var skin in model.Skins)
            {
                if (skin.Root == null)
                {
                    skin.Root = (Node)skeleton[HumanoidBones.hips].Parent;
                    sb.Append($"{skin}: set {skin.Root}\n");
                }
                else
                {
                    sb.Append($"{skin}: {skin.Root}\n");
                }
            }

            return(sb.ToString());;
        }