Esempio n. 1
0
        public override void OnInspectorGUI()
        {
            var bones = m_target.Bones;

            if (bones == null)
            {
                return;
            }

            BoneField(HumanBodyBones.Hips, bones);

            if (bones[(int)HumanBodyBones.Hips] == null)
            {
                EditorGUILayout.HelpBox(@"First, you set hips", MessageType.Warning);
            }
            else
            {
                if (GUILayout.Button("Guess bone mapping"))
                {
                    m_target.GuessBoneMapping();
                }
                EditorGUILayout.HelpBox(@"Guess bones from hips", MessageType.Info);

                if (GUILayout.Button("Ensure T-Pose"))
                {
                    m_target.EnsureTPose();
                }
                EditorGUILayout.HelpBox(@"Arms to Horizontal", MessageType.Info);

                if (GUILayout.Button("Create avatar"))
                {
                    var description = AvatarDescription.Create(m_target.Description);
                    BoneMapping.SetBonesToDescription(m_target, description);
                    var avatar = description.CreateAvatarAndSetup(m_target.transform);
                    if (avatar != null)
                    {
                        avatar.name = "avatar";
#if UNITY_2018_2_OR_NEWER
                        var prefabRoot = PrefabUtility.GetCorrespondingObjectFromSource(m_target.gameObject);
#else
                        var prefabRoot = PrefabUtility.GetPrefabParent(m_target.gameObject);
#endif
                        var prefabPath = AssetDatabase.GetAssetPath(prefabRoot);

                        var path = (string.IsNullOrEmpty(prefabPath))
                            ? string.Format("Assets/{0}.asset", avatar.name)
                            : string.Format("{0}/{1}.asset", Path.GetDirectoryName(prefabPath), Path.GetFileNameWithoutExtension(prefabPath))
                        ;
                        path = EditorUtility.SaveFilePanel(
                            "Save avatar",
                            Path.GetDirectoryName(path),
                            string.Format("{0}.avatar.asset", serializedObject.targetObject.name),
                            "asset");
                        var assetPath = HumanPoseTransferEditor.ToAssetPath(path);
                        if (!string.IsNullOrEmpty(assetPath))
                        {
                            AssetDatabase.CreateAsset(description, assetPath); // overwrite
                            AssetDatabase.AddObjectToAsset(avatar, assetPath);

                            Debug.LogFormat("Create avatar {0}", path);
                            AssetDatabase.ImportAsset(assetPath);
                            Selection.activeObject = avatar;
                        }
                        else
                        {
                            Debug.LogWarning("fail to CreateAvatar");
                        }
                    }
                }
                EditorGUILayout.HelpBox(@"before create,

1. Model root transform should reset(origin without rotation)
2. Model forward to Z+(rotate child of model root)
3. Required bones filled
", MessageType.Info);
            }

            /*
             * m_settingsFoldout = EditorGUILayout.Foldout(m_settingsFoldout, "AvatarSettings");
             * if (m_settingsFoldout)
             * {
             *  EditorGUILayout.FloatField("armStretch", m_target.armStretch);
             *  EditorGUILayout.FloatField("legStretch", m_target.legStretch);
             *  EditorGUILayout.FloatField("upperArmTwist", m_target.upperArmTwist);
             *  EditorGUILayout.FloatField("lowerArmTwist", m_target.lowerArmTwist);
             *  EditorGUILayout.FloatField("upperLegTwist", m_target.upperLegTwist);
             *  EditorGUILayout.FloatField("lowerLegTwist", m_target.lowerLegTwist);
             *  EditorGUILayout.FloatField("feetSpacing", m_target.feetSpacing);
             *  EditorGUILayout.Toggle("hasTranslationDoF", m_target.hasTranslationDoF);
             *  //public BoneLimit[] human;
             * }
             */

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Arm", EditorStyles.boldLabel, GUILayout.Width(LABEL_WIDTH));
            EditorGUILayout.LabelField("Left", EditorStyles.boldLabel, GUILayout.Width(40));
            EditorGUILayout.Space();
            EditorGUILayout.LabelField("Right", EditorStyles.boldLabel, GUILayout.Width(40));
            EditorGUILayout.EndHorizontal();
            BoneField(HumanBodyBones.LeftShoulder, HumanBodyBones.RightShoulder, bones);
            BoneField(HumanBodyBones.LeftUpperArm, HumanBodyBones.RightUpperArm, bones);
            BoneField(HumanBodyBones.LeftLowerArm, HumanBodyBones.RightLowerArm, bones);
            BoneField(HumanBodyBones.LeftHand, HumanBodyBones.RightHand, bones);

            EditorGUILayout.LabelField("Body and Head", EditorStyles.boldLabel);
            BoneField(HumanBodyBones.Spine, bones);
            BoneField(HumanBodyBones.Chest, bones);
#if UNITY_5_6_OR_NEWER
            BoneField(HumanBodyBones.UpperChest, bones);
#endif
            BoneField(HumanBodyBones.Neck, bones);
            BoneField(HumanBodyBones.Head, bones);
            BoneField(HumanBodyBones.Jaw, bones);
            BoneField(HumanBodyBones.LeftEye, HumanBodyBones.RightEye, bones);

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Leg", EditorStyles.boldLabel, GUILayout.Width(LABEL_WIDTH));
            EditorGUILayout.LabelField("Left", EditorStyles.boldLabel, GUILayout.Width(40));
            EditorGUILayout.Space();
            EditorGUILayout.LabelField("Right", EditorStyles.boldLabel, GUILayout.Width(40));
            EditorGUILayout.EndHorizontal();
            BoneField(HumanBodyBones.LeftUpperLeg, HumanBodyBones.RightUpperLeg, bones);
            BoneField(HumanBodyBones.LeftLowerLeg, HumanBodyBones.RightLowerLeg, bones);
            BoneField(HumanBodyBones.LeftFoot, HumanBodyBones.RightFoot, bones);
            BoneField(HumanBodyBones.LeftToes, HumanBodyBones.RightToes, bones);

            m_handFoldout = EditorGUILayout.Foldout(m_handFoldout, "Hand");
            if (m_handFoldout)
            {
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("Thumb", EditorStyles.boldLabel, GUILayout.Width(LABEL_WIDTH));
                EditorGUILayout.LabelField("Left", EditorStyles.boldLabel, GUILayout.Width(40));
                EditorGUILayout.Space();
                EditorGUILayout.LabelField("Right", EditorStyles.boldLabel, GUILayout.Width(40));
                EditorGUILayout.EndHorizontal();
                BoneField(HumanBodyBones.LeftThumbProximal, HumanBodyBones.RightThumbProximal, bones);
                BoneField(HumanBodyBones.LeftThumbIntermediate, HumanBodyBones.RightThumbIntermediate, bones);
                BoneField(HumanBodyBones.LeftThumbDistal, HumanBodyBones.RightThumbDistal, bones);

                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("Index", EditorStyles.boldLabel, GUILayout.Width(LABEL_WIDTH));
                EditorGUILayout.LabelField("Left", EditorStyles.boldLabel, GUILayout.Width(40));
                EditorGUILayout.Space();
                EditorGUILayout.LabelField("Right", EditorStyles.boldLabel, GUILayout.Width(40));
                EditorGUILayout.EndHorizontal();
                BoneField(HumanBodyBones.LeftIndexProximal, HumanBodyBones.RightIndexProximal, bones);
                BoneField(HumanBodyBones.LeftIndexIntermediate, HumanBodyBones.RightIndexIntermediate, bones);
                BoneField(HumanBodyBones.LeftIndexDistal, HumanBodyBones.RightIndexDistal, bones);

                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("Middle", EditorStyles.boldLabel, GUILayout.Width(LABEL_WIDTH));
                EditorGUILayout.LabelField("Left", EditorStyles.boldLabel, GUILayout.Width(40));
                EditorGUILayout.Space();
                EditorGUILayout.LabelField("Right", EditorStyles.boldLabel, GUILayout.Width(40));
                EditorGUILayout.EndHorizontal();
                BoneField(HumanBodyBones.LeftMiddleProximal, HumanBodyBones.RightMiddleProximal, bones);
                BoneField(HumanBodyBones.LeftMiddleIntermediate, HumanBodyBones.RightMiddleIntermediate, bones);
                BoneField(HumanBodyBones.LeftMiddleDistal, HumanBodyBones.RightMiddleDistal, bones);

                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("Ring", EditorStyles.boldLabel, GUILayout.Width(LABEL_WIDTH));
                EditorGUILayout.LabelField("Left", EditorStyles.boldLabel, GUILayout.Width(40));
                EditorGUILayout.Space();
                EditorGUILayout.LabelField("Right", EditorStyles.boldLabel, GUILayout.Width(40));
                EditorGUILayout.EndHorizontal();
                BoneField(HumanBodyBones.LeftRingProximal, HumanBodyBones.RightRingProximal, bones);
                BoneField(HumanBodyBones.LeftRingIntermediate, HumanBodyBones.RightRingIntermediate, bones);
                BoneField(HumanBodyBones.LeftRingDistal, HumanBodyBones.RightRingDistal, bones);

                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("Little", EditorStyles.boldLabel, GUILayout.Width(LABEL_WIDTH));
                EditorGUILayout.LabelField("Left", EditorStyles.boldLabel, GUILayout.Width(40));
                EditorGUILayout.Space();
                EditorGUILayout.LabelField("Right", EditorStyles.boldLabel, GUILayout.Width(40));
                EditorGUILayout.EndHorizontal();
                BoneField(HumanBodyBones.LeftLittleProximal, HumanBodyBones.RightLittleProximal, bones);
                BoneField(HumanBodyBones.LeftLittleIntermediate, HumanBodyBones.RightLittleIntermediate, bones);
                BoneField(HumanBodyBones.LeftLittleDistal, HumanBodyBones.RightLittleDistal, bones);
            }

            EditorUtility.SetDirty(m_target);
        }
Esempio n. 2
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";
        }