static public void ConvertMotionEntities ( this GameObjectConversionSystem gcs, GameObject motionMain, Transform[] bones, ComponentType[] motionTypes, ComponentType[] streamTypes, MotionClip motionClip, AvatarMask streamMask ) { var em = gcs.DstEntityManager; var enabledBoneObjects = getEnabledBoneObjects(bones, motionMain, streamMask); var enabledBoneIds = getEnabledBoneIds(bones, motionMain, streamMask); var motionEntity = createMotionEntity(gcs, motionMain, motionTypes); var posStreamEntities = createStreamEntitiesOfSection(gcs, enabledBoneObjects, streamTypes); var rotStreamEntities = createStreamEntitiesOfSection(gcs, enabledBoneObjects, streamTypes); initMotionEntity(em, motionEntity, posStreamEntities, rotStreamEntities, motionClip); initStreamEntities(gcs, motionEntity, posStreamEntities, enabledBoneIds); initStreamEntities(gcs, motionEntity, rotStreamEntities, enabledBoneIds); setBoneStreamLinks(gcs, posStreamEntities, rotStreamEntities, enabledBoneObjects); setLinkForObjectEntity(gcs, motionMain, motionEntity); }
static void initMotionEntity (EntityManager em, Entity motionEntity, Entity[] poss, Entity[] rots, MotionClip motionClip) { var motionBlobData = motionClip.ConvertToBlobData(); em.SetComponentData(motionEntity, new Motion.ClipData { MotionClipData = motionBlobData }); em.SetComponentData(motionEntity, new Motion.InitializeData { MotionIndex = 5, DelayTime = 0.0f, IsContinuous = true, IsLooping = true, TimeScale = 1.0f, } ); em.SetComponentData(motionEntity, new Motion.StreamLinkData { PositionStreamTop = poss.First(), RotationStreamTop = rots.First(), } ); }
public virtual Vector3 CalculateAngularVelocity() { if (MotionClip != null && MotionClip.rotateType == EMotionType.MT_Override) { return(MotionClip.GetAngularVelocity()); } if (RootMotionClip != null && RootMotionClip.rotateType == EMotionType.MT_Override) { return(RootMotionClip.GetAngularVelocity()); } Vector3 res = Vector3.zero; if (MotionClip is object) { res += MotionClip.GetAngularVelocity(); } if (RootMotionClip is object) { res += RootMotionClip.GetAngularVelocity(); } if (Controller is object) { res += Controller.GetInputAngularVelocity(); } return(res); }
protected void FixedUpdate() { if (MotionClip is object) { MotionClip.TickMotion(Time.fixedDeltaTime); } Velocity = CalculateVeloctiy(); AngularVelocity = CalculateAngularVelocity(); CharacterController.Move((Velocity + Gravity) * Time.fixedDeltaTime); Debug.Log(CharacterController.isGrounded); transform.eulerAngles += AngularVelocity * Time.fixedDeltaTime; }
public void Init(MotionController controller, MotionClip source, Transform reference, IgnoreRootMotionOnBone ignore, int samples = 50) { Clip = source.Clip; Samples = samples; Name = Clip.name; Stationary = source.Stationary; FixFootSkating = source.FixFootSkating; m_controller = controller; m_gameObject = controller.gameObject; m_reference = reference; m_ignore = ignore; }
public virtual Vector3 CalculateVeloctiy() { if (MotionClip != null && MotionClip.motionType == EMotionType.MT_Override) { return(MotionClip.GetVelocity()); } if (RootMotionClip != null && RootMotionClip.motionType == EMotionType.MT_Override) { return(RootMotionClip.GetVelocity()); } // 计算重力 if (CharacterController.isGrounded) { Gravity = Physics.gravity; } else { Gravity += Physics.gravity * Time.fixedDeltaTime; } Vector3 res = Vector3.zero; if (MotionClip is object) { res += MotionClip.GetVelocity(); } if (RootMotionClip is object) { res += RootMotionClip.GetVelocity(); } if (Controller is object) { Vector3 inputVelocity = Controller.GetInputVelocity(); res += inputVelocity; if (IsDirectVelocity && inputVelocity.sqrMagnitude >= 0.1f) { transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(inputVelocity, Vector3.up), Time.fixedDeltaTime * 10); } } return(res); }
static public (NativeArray <Entity> bonePrefabs, Entity posturePrefab) CreatePrefabs (EntityManager em, NativeArray <Entity> streamPrefabs, Entity drawPrefab, MotionClip motionClip) { var postArchetype = postureArchetypeCache.GetOrCreateArchetype(em); var boneArchetype = boneArchetypeCache.GetOrCreateArchetype(em); var posturePrefab = em.CreateEntity(postArchetype); var bonePrefabs = createBonePrefabs(em, motionClip, boneArchetype); setBoneId(em, bonePrefabs, bonePrefabs.Length); setStreamLinks(em, bonePrefabs, streamPrefabs, motionClip); setDrawLinks(em, bonePrefabs, drawPrefab, motionClip); setBoneRelationLinks(em, bonePrefabs, posturePrefab, motionClip); em.SetComponentData(posturePrefab, new Rotation { Value = quaternion.identity }); return(bonePrefabs, posturePrefab); NativeArray <Entity> createBonePrefabs (EntityManager em_, MotionClip motionClip_, EntityArchetype archetype) { var boneLength = motionClip_.StreamPaths.Length; var bonePrefabs_ = new NativeArray <Entity>(boneLength, Allocator.Temp); em_.CreateEntity(archetype, bonePrefabs_); return(bonePrefabs_); } void setBoneId(EntityManager em_, NativeArray <Entity> bonePreafabs_, int boneLength) { em_.SetComponentData(bonePreafabs_, from x in Enumerable.Range(0, bonePreafabs_.Length) select new DrawTransform.IndexData { BoneLength = boneLength, BoneId = x } ); } void setStreamLinks( EntityManager em_, NativeArray <Entity> bonePrefabs_, NativeArray <Entity> streamPrefabs_, MotionClip motionClip_ ) { var boneLength = motionClip_.StreamPaths.Length; var qPosStreams = streamPrefabs_ .Take(boneLength); var qRotStreams = streamPrefabs_ .Skip(boneLength) .Take(boneLength); var qStreamlinkers = from ents in (qPosStreams, qRotStreams).Zip() select new Bone.Stream0LinkData { PositionStreamEntity = ents.x, RotationStreamEntity = ents.y, }; em_.SetComponentData(bonePrefabs_, qStreamlinkers); } void setDrawLinks( EntityManager em_, NativeArray <Entity> bonePrefabs_, Entity drawPrefab_, MotionClip motionClip_ ) { var boneLength = motionClip_.StreamPaths.Length; var qDrawLinker = Enumerable .Repeat(new DrawTransform.LinkData { DrawInstanceEntity = drawPrefab_ }, boneLength); em_.SetComponentData(bonePrefabs_, qDrawLinker); } unsafe void setBoneRelationLinks (EntityManager em_, NativeArray <Entity> bonePrefabs_, Entity posturePrefab_, MotionClip motionClip_) { var qParentIds = motionClip_.StreamPaths //.QueryParentIdList(); .Select(x => 0); // まにあわせ var qEnts = bonePrefabs_ .Prepend(posturePrefab_) .Append(Entity.Null); var qPathDepthCounts = from path in motionClip_.StreamPaths select path.Where(x => x == '/').Count() + 1 ; using (var parentIds = qParentIds.ToNativeArray(Allocator.Temp)) using (var boneEnts = qEnts.ToNativeArray(Allocator.Temp)) using (var boneDepthLevels = qPathDepthCounts.ToNativeArray(Allocator.Temp)) { foreach (var i in Enumerable.Range(0, motionClip.StreamPaths.Length)) { var ent = boneEnts[i + 1]; var parent = boneEnts[parentIds[i] + 1]; var lv = boneDepthLevels[i]; setLvLinker(ent, parent, lv); } } void setLvLinker(Entity ent, Entity parent, int lv) { switch (lv) { case 1: em_.AddComponentData(ent, new Bone.Lv01LinkData { ParentBoneEntity = parent }); break; case 2: em_.AddComponentData(ent, new Bone.Lv02LinkData { ParentBoneEntity = parent }); break; case 3: em_.AddComponentData(ent, new Bone.Lv03LinkData { ParentBoneEntity = parent }); break; case 4: em_.AddComponentData(ent, new Bone.Lv04LinkData { ParentBoneEntity = parent }); break; case 5: em_.AddComponentData(ent, new Bone.Lv05LinkData { ParentBoneEntity = parent }); break; case 6: em_.AddComponentData(ent, new Bone.Lv06LinkData { ParentBoneEntity = parent }); break; case 7: em_.AddComponentData(ent, new Bone.Lv07LinkData { ParentBoneEntity = parent }); break; case 8: em_.AddComponentData(ent, new Bone.Lv08LinkData { ParentBoneEntity = parent }); break; case 9: em_.AddComponentData(ent, new Bone.Lv09LinkData { ParentBoneEntity = parent }); break; } } } } }
void OnGUI() { if (m_style == null) { m_style = new EditorStyle(); m_style.SetupStyle(); } GUI.enabled = m_idle; EditorGUILayout.BeginVertical(m_style.GreyBox); m_style.DrawTitleBar("Required component (Motion Controller)"); if (m_controller == null) { m_controller = (MotionController)EditorGUILayout.ObjectField(m_controller, typeof(MotionController), true); } else { EditorGUILayout.BeginHorizontal(); GUI.enabled = false; EditorGUILayout.ObjectField(m_controller, typeof(MotionController), true); GUI.enabled = m_idle; if (GUILayout.Button("Unlock")) { RemoveController(); } EditorGUILayout.EndHorizontal(); } EditorGUILayout.EndVertical(); if (m_controller == null) { Clips = null; m_originalPos = null; m_originalRot = null; m_selectedPoseBase = 0; EditorGUILayout.HelpBox("Before setting up animations you must set all the required components, right now you are missing the Motion Controller component.", MessageType.Error); return; } if (!m_controller.Ready) { Clips = null; EditorGUILayout.HelpBox("Before setting up animations you must setup the Root bone and Legs of your Motion Controller.", MessageType.Error); return; } if (m_controller.Animator == null) { Clips = null; EditorGUILayout.HelpBox("The selected MotionController has no Animator component defined.", MessageType.Error); return; } if (m_controller.Animator.runtimeAnimatorController == null) { Clips = null; EditorGUILayout.HelpBox("The selected Animator GameObject has no AnimatorController.", MessageType.Error); return; } m_animator = m_controller.Animator; if (m_animator.avatar == null) { Clips = null; EditorGUILayout.HelpBox("The selected Animator GameObject has no Avatar.", MessageType.Error); return; } var rCtrl = m_animator.runtimeAnimatorController; if (rCtrl.animationClips == null || rCtrl.animationClips.Length == 0) { m_list = null; EditorGUILayout.HelpBox("AnimatorController has no animations, it should not be empty.", MessageType.Error); return; } m_scrollPos = EditorGUILayout.BeginScrollView(m_scrollPos, false, false); EditorGUILayout.BeginVertical(m_style.GreyBox); m_style.DrawTitleBar("Animations from Animator Controller"); if (Clips == null || Clips.Length == 0) { var clips = rCtrl.animationClips; Clips = new MotionClip[clips.Length]; m_animationPoses = new string[clips.Length]; for (int i = 0; i < clips.Length; i++) { var clip = clips[i]; m_animationPoses[i] = clip.name; var m = new MotionClip() { Clip = clip, Id = clip.GetInstanceID(), Name = clip.name, FixFootSkating = false, }; var nm = clip.name.ToLower(); if (nm.Contains("stand") || nm.Contains("idle")) { m.Stationary = true; m.FixFootSkating = true; } if (nm.Contains("walk") || nm.Contains("run") || nm.Contains("sprint") || nm.Contains("strafe")) { m.Stationary = false; } Clips[i] = m; } } m_so.Update(); m_list.DoLayoutList(); m_so.ApplyModifiedProperties(); m_style.DrawTitleBar("Animation analisis"); m_ignoreRootMotion = (IgnoreRootMotionOnBone)EditorGUILayout.EnumPopup("Ignore Root motion", m_ignoreRootMotion); m_selectedPoseBase = EditorGUILayout.Popup("Standing pose", m_selectedPoseBase, m_animationPoses); m_samples = EditorGUILayout.IntSlider("Samples", m_samples, Int.Ten, Int.OneHundred);//EditorGUI.IntSlider(EditorGUILayout.GetControlRect(false, m_progHeight), "Samples", m_samples, Int.Ten, Int.OneHundred); if (m_idle) { if (!Clips[m_selectedPoseBase].Stationary) { EditorGUILayout.HelpBox("Selected Standing pose is not Stationary.", MessageType.Error); } else { EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Start analisis")) { StartProcess(); } if (MotionAsset == null || MotionAsset.MotionCount == Int.Zero) { GUI.enabled = false; } if (GUILayout.Button("Save data")) { SaveAsset(); } GUI.enabled = m_idle; EditorGUILayout.EndHorizontal(); } } else { ProcessAnimation(); string clipName; if (m_currentAnimation > -Int.One) { clipName = Clips[m_currentAnimation].Name; } else { clipName = Clips[Int.Zero].Name; } GUI.enabled = true; var prog = m_progress * Float.OneHundred; var sts = prog.ToString("0") + "% " + clipName; if (m_progress >= Float.One) { EditorGUI.ProgressBar(EditorGUILayout.GetControlRect(false, m_progHeight), m_progress, "Done"); } else { EditorGUI.ProgressBar(EditorGUILayout.GetControlRect(false, m_progHeight), m_progress, sts); } } EditorGUILayout.EndVertical(); EditorGUILayout.EndScrollView(); if (!m_idle && (m_uiDelay != m_lastUIDelay)) { m_lastUIDelay = m_uiDelay; Repaint(); } if (!m_idle && m_currentAnimation == (Clips.Length - Int.One)) { m_uiDelay += Float.DotOne; if (m_uiDelay < Float.One) { return; } m_idle = true; FinishAnalisis(); SaveAsset(); return; } if (m_reset) { MotionAsset = null; m_reset = false; } }
public void addChild(MotionClip c) { childs.Add(c); }
void OnGUI() { GUILayout.Label("AnimationCreate", EditorStyles.boldLabel); GUILayout.Label("방향과 상태가 있는 오브젝트의 애니메이션을 만듭니다.\n" + "Generate AnimationClips and their Controller with States and Directions\n", EditorStyles.helpBox); /* * GUILayout.Label("스프라이트경로를 다음과 같이 배치합니다. : Assets/Resources/오브젝트경로/상태/방향/0~n\n" + * "Assets/Resources/오브젝트 경로/anims 폴더를 만들어 둡니다.\n" + * "Put sprites in path : Assets/Resources/(objectPath)/(State)/(Direction)/0~n\n" + * "Make the path Assets/Resources/objectPath/anims for save", EditorStyles.helpBox); * GUILayout.Label("오브젝트 경로(Object Path):", EditorStyles.miniLabel); * FolderName = GUILayout.TextField(FolderName, EditorStyles.textField); * * * GUILayout.Label("상태(States):", EditorStyles.miniLabel); * GUILayout.Label("상태들을 열거합니다. ';'로 구분합니다.\n" + * "Input the states. It will slice with ';'.", EditorStyles.helpBox); */ GUILayout.Label("방향스프라이트 갯수:", EditorStyles.miniLabel); if (!targetController) { directionCount = EditorGUILayout.IntSlider(directionCount, 1, 8); } else { EditorGUILayout.IntSlider(directionCount, 1, 8); } GUILayout.Label("컨트롤러(새로 생성 시 비워둠):", EditorStyles.miniLabel); var target = EditorGUILayout.ObjectField(targetController, typeof(AnimatorController), true) as AnimatorController; if (target != targetController) { targetController = target; getAnims = false; } if (!targetController) { newControllerName = EditorGUILayout.TextField("새 컨트롤러 이름:Resource/", newControllerName); } if (!targetController && GUILayout.Button("Create Animator")) { CreateAnimator(newControllerName); } scrollPoint = EditorGUILayout.BeginScrollView(scrollPoint); if (targetController && !getAnims) { GetAnimation(); getAnims = true; FolderName = AssetDatabase.GetAssetPath(targetController).Split('.')[0]; } else if (!targetController) { getAnims = false; } if (targetController) { foreach (var item in spriteLists) { item.show(); } } EditorGUILayout.EndScrollView(); GUILayout.Label("부모 상태이름:", EditorStyles.miniLabel); newStateMachine = GUILayout.TextField(newStateMachine, EditorStyles.textField); if (GUILayout.Button("새 부모상태 생성")) { targetController.layers[0].stateMachine.AddStateMachine(newStateMachine); GetAnimation(); } if (GUILayout.Button("새 상태 생성")) { AnimatorStateMachine machine; if (popups[parentNumber] == "Default") { machine = targetController.layers[0].stateMachine; } else { machine = spriteLists[parentNumber].myStateMachine; } MotionClip m = new MotionClip(null, machine, null, targetClipName); m.stateNumber = stateNumber; m.weaponNumber = weaponNumber; m.comboCount = comboCount; m.oneShot = oneShot; m.state = m.parent.AddState(targetClipName); m.setTransition(); m.state.speedParameterActive = true; m.state.speedParameter = "SpeedParam"; spriteLists.Add(m); GetAnimation(); } GUILayout.Label("새 상태 설정", EditorStyles.miniLabel); stateNumber = EditorGUILayout.IntField("상태번호", stateNumber); WeaponNumberUse = GUILayout.Toggle(WeaponNumberUse, "무기번호 사용"); if (WeaponNumberUse) { weaponNumber = EditorGUILayout.IntField("무기번호", weaponNumber); } comboCount = EditorGUILayout.IntField("구분번호", comboCount); oneShot = EditorGUILayout.Toggle("OneShot", oneShot); GUILayout.Label("상태이름:", EditorStyles.miniLabel); targetClipName = GUILayout.TextField(targetClipName, EditorStyles.textField); GUILayout.Label("부모 상태", EditorStyles.miniLabel); if (popups != null) { parentNumber = EditorGUILayout.Popup(parentNumber, popups); } if (notSaved) { GUI.color = notSavedColor; } if (GUILayout.Button("모든 변경사항 저장하기")) { foreach (var item in spriteLists) { item.save(); } AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); GetAnimation(); notSaved = false; } }
void GetAnimation() { spriteLists = new List <MotionClip>(); string result = ""; int viewCount = 1; if (targetController != null) { var StateList = targetController.layers[0].stateMachine.stateMachines; foreach (var state in StateList) { var motionList = state.stateMachine.states; result += state.stateMachine.name + " : "; MotionClip parent = new MotionClip(null, targetController.layers[0].stateMachine, null, state.stateMachine.name); parent.myStateMachine = state.stateMachine; foreach (var motion in motionList) { result += (motion.state.name) + ";"; var clip = motion.state.motion as AnimationClip; MotionClip m = new MotionClip(motion.state.motion, state.stateMachine, motion.state, motion.state.name); m.direction = 0; if (clip) { var aniSprites = AnimationUtility.GetObjectReferenceCurve(clip, spriteBinding); m.frameRate = (int)clip.frameRate; if (aniSprites != null) { List <Sprite> spList = new List <Sprite>(); foreach (var sprite in aniSprites) { Sprite s = sprite.value as Sprite; if (s != null) { spList.Add(s); } } if (spList.Count > 0) { m.sprites = spList; } } else { } } parent.addChild(m); } spriteLists.Add(parent); result += "\n"; } var StateList2 = targetController.layers[0].stateMachine.states; foreach (var state in StateList2) { result += state.state.name + ";"; var clip = state.state.motion as AnimationClip; MotionClip m = new MotionClip(state.state.motion, targetController.layers[0].stateMachine, state.state, state.state.name); m.direction = 0; if (clip) { var aniSprites = AnimationUtility.GetObjectReferenceCurve(clip, spriteBinding); if (aniSprites != null) { m.frameRate = (int)clip.frameRate; List <Sprite> spList = new List <Sprite>(); foreach (var sprite in aniSprites) { Sprite s = sprite.value as Sprite; if (s != null) { spList.Add(s); } } if (spList.Count > 0) { m.sprites = spList; } } else { } } spriteLists.Add(m); } foreach (var param in targetController.parameters) { if (param.name == "MaxView") { viewCount = param.defaultInt; } } popups = new string[spriteLists.Count + 1]; int i = 0; foreach (var item in spriteLists) { if (spriteLists[i].myStateMachine) { popups[i] = spriteLists[i].name; i++; } } popups[i] = "Default"; } else { result = "There is no Animaton"; } States = result; directionCount = viewCount; }
private void Awake() { CharacterController = GetComponent <CharacterController>(); MotionClip = new MotionClip(transform); RootMotionClip = new RootMotionClip(GetComponentInChildren <Animator>()); }
public void ApplyRotate(int priority, EMotionType rotateType, EDirectType asixType, float rotateAngle, float duration = 0.0f, AnimationCurve rotateCurve = null) { MotionClip.ApplyRotate(priority, rotateType, asixType, rotateAngle, duration, rotateCurve); }
public void ApplyMotion(int priority, EMotionType motionType, EDirectType directType, float distance, float duration = 0.0f, AnimationCurve moveCurve = null) { MotionClip.ApplyMotion(priority, motionType, directType, distance, duration, moveCurve); }