public AvatarWithDescription CreateAvatar() { var map = Bones .Select((x, i) => new { i, x }) .Where(x => x.x != null) .ToDictionary(x => (HumanBodyBones)x.i, x => x.x.transform) ; var copy = Instantiate(gameObject); try { var avatarDescription = AvatarDescription.Create(map); avatarDescription.name = name + ".description"; var avatar = avatarDescription.CreateAvatar(copy.transform); avatar.name = name; var animator = GetComponent <Animator>(); if (animator != null) { animator.avatar = avatar; } var transfer = GetComponent <HumanPoseTransfer>(); if (transfer != null) { transfer.Avatar = avatar; } return(new AvatarWithDescription { Avatar = avatar, Description = avatarDescription, }); } finally { if (Application.isEditor) { DestroyImmediate(copy); } else { Destroy(copy); } } }
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; } }
/// <summary> /// /// </summary> /// <returns></returns> public Avatar CreateAvatar() { var map = Bones .Select((x, i) => new { i, x }) .Where(x => x.x != null) .ToDictionary(x => (HumanBodyBones)x.i, x => x.x.transform) ; if (Description == null) { Description = AvatarDescription.Create(); } Description.SetHumanBones(map); Description.name = name + ".description"; var avatar = Description.CreateAvatar(transform); avatar.name = name; var animator = GetComponent <Animator>(); if (animator != null) { var positionMap = transform.Traverse().ToDictionary(x => x, x => x.position); animator.avatar = avatar; foreach (var x in transform.Traverse()) { x.position = positionMap[x]; } } var transfer = GetComponent <HumanPoseTransfer>(); if (transfer != null) { transfer.Avatar = avatar; } return(avatar); }
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"; }
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); }
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 // }
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"; }