| | 1 | | using DCL.Models; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using DCL.Helpers; |
| | 5 | | using UnityEngine; |
| | 6 | | using DCL.Controllers; |
| | 7 | |
|
| | 8 | | namespace DCL.Components |
| | 9 | | { |
| | 10 | | public class DCLAnimator : BaseComponent |
| | 11 | | { |
| | 12 | | [System.Serializable] |
| | 13 | | public class Model : BaseModel |
| | 14 | | { |
| | 15 | | [System.Serializable] |
| | 16 | | public class DCLAnimationState |
| | 17 | | { |
| | 18 | | public string name; |
| | 19 | | public string clip; |
| | 20 | | public AnimationClip clipReference; |
| | 21 | | public bool playing; |
| | 22 | |
|
| | 23 | | [Range(0, 1)] |
| 20 | 24 | | public float weight = 1f; |
| | 25 | |
|
| 20 | 26 | | public float speed = 1f; |
| 20 | 27 | | public bool looping = true; |
| | 28 | | public bool shouldReset = false; |
| | 29 | |
|
| 0 | 30 | | public DCLAnimationState Clone() { return (DCLAnimationState) this.MemberwiseClone(); } |
| | 31 | | } |
| | 32 | |
|
| | 33 | | public DCLAnimationState[] states; |
| | 34 | |
|
| 11 | 35 | | public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); } |
| | 36 | | } |
| | 37 | |
|
| | 38 | | [System.NonSerialized] |
| | 39 | | public Animation animComponent = null; |
| | 40 | |
|
| 7 | 41 | | Dictionary<string, AnimationClip> clipNameToClip = new Dictionary<string, AnimationClip>(); |
| 7 | 42 | | Dictionary<AnimationClip, AnimationState> clipToState = new Dictionary<AnimationClip, AnimationState>(); |
| | 43 | |
|
| 12 | 44 | | private void Awake() { model = new Model(); } |
| | 45 | |
|
| 12 | 46 | | private void OnDestroy() { entity.OnShapeUpdated -= OnComponentUpdated; } |
| | 47 | |
|
| | 48 | | public override IEnumerator ApplyChanges(BaseModel model) |
| | 49 | | { |
| 11 | 50 | | entity.OnShapeUpdated -= OnComponentUpdated; |
| 11 | 51 | | entity.OnShapeUpdated += OnComponentUpdated; |
| | 52 | |
|
| 11 | 53 | | UpdateAnimationState(); |
| | 54 | |
|
| 11 | 55 | | return null; |
| | 56 | | } |
| | 57 | |
|
| 0 | 58 | | new public Model GetModel() { return (Model) model; } |
| | 59 | |
|
| 20 | 60 | | private void OnComponentUpdated(IDCLEntity e) { UpdateAnimationState(); } |
| | 61 | |
|
| | 62 | | private void Initialize() |
| | 63 | | { |
| 21 | 64 | | if (entity == null || animComponent != null) |
| 9 | 65 | | return; |
| | 66 | |
|
| | 67 | | //NOTE(Brian): fetch all the AnimationClips in Animation component. |
| 12 | 68 | | animComponent = transform.parent.GetComponentInChildren<Animation>(true); |
| | 69 | |
|
| 12 | 70 | | if (animComponent == null) |
| 7 | 71 | | return; |
| | 72 | |
|
| 5 | 73 | | clipNameToClip.Clear(); |
| 5 | 74 | | clipToState.Clear(); |
| 5 | 75 | | int layerIndex = 0; |
| | 76 | |
|
| 5 | 77 | | animComponent.playAutomatically = true; |
| 5 | 78 | | animComponent.enabled = true; |
| 5 | 79 | | animComponent.Stop(); //NOTE(Brian): When the GLTF is created by GLTFSceneImporter a frame may be elapsed, |
| | 80 | | //putting the component in play state if playAutomatically was true at that point. |
| 5 | 81 | | animComponent.clip?.SampleAnimation(animComponent.gameObject, 0); |
| | 82 | |
|
| 24 | 83 | | foreach (AnimationState unityState in animComponent) |
| | 84 | | { |
| 7 | 85 | | clipNameToClip[unityState.clip.name] = unityState.clip; |
| | 86 | |
|
| 7 | 87 | | unityState.clip.wrapMode = WrapMode.Loop; |
| 7 | 88 | | unityState.layer = layerIndex; |
| 7 | 89 | | unityState.blendMode = AnimationBlendMode.Blend; |
| 7 | 90 | | layerIndex++; |
| | 91 | | } |
| 5 | 92 | | } |
| | 93 | |
|
| | 94 | | void UpdateAnimationState() |
| | 95 | | { |
| 21 | 96 | | Initialize(); |
| | 97 | |
|
| 21 | 98 | | if (clipNameToClip.Count == 0 || animComponent == null) |
| 7 | 99 | | return; |
| | 100 | |
|
| 14 | 101 | | Model model = (Model) this.model; |
| | 102 | |
|
| 14 | 103 | | if (model.states == null || model.states.Length == 0) |
| 0 | 104 | | return; |
| | 105 | |
|
| 68 | 106 | | for (int i = 0; i < model.states.Length; i++) |
| | 107 | | { |
| 20 | 108 | | Model.DCLAnimationState state = model.states[i]; |
| | 109 | |
|
| 20 | 110 | | if (clipNameToClip.ContainsKey(state.clip)) |
| | 111 | | { |
| 20 | 112 | | AnimationState unityState = animComponent[state.clip]; |
| 20 | 113 | | unityState.weight = state.weight; |
| 20 | 114 | | unityState.wrapMode = state.looping ? WrapMode.Loop : WrapMode.Default; |
| 20 | 115 | | unityState.clip.wrapMode = unityState.wrapMode; |
| 20 | 116 | | unityState.speed = state.speed; |
| | 117 | |
|
| 20 | 118 | | state.clipReference = unityState.clip; |
| | 119 | |
|
| 20 | 120 | | if (state.shouldReset) |
| 6 | 121 | | ResetAnimation(state); |
| | 122 | |
|
| 20 | 123 | | if (state.playing) |
| | 124 | | { |
| 17 | 125 | | if (!animComponent.IsPlaying(state.clip)) |
| 16 | 126 | | animComponent.Play(state.clip); |
| 16 | 127 | | } |
| | 128 | | else |
| | 129 | | { |
| 3 | 130 | | if (animComponent.IsPlaying(state.clip)) |
| 1 | 131 | | animComponent.Stop(state.clip); |
| | 132 | | } |
| | 133 | | } |
| | 134 | | } |
| 14 | 135 | | } |
| | 136 | |
|
| | 137 | | public void ResetAnimation(Model.DCLAnimationState state) |
| | 138 | | { |
| 7 | 139 | | if (state == null || state.clipReference == null) |
| | 140 | | { |
| 0 | 141 | | Debug.LogError("Clip not found"); |
| 0 | 142 | | return; |
| | 143 | | } |
| | 144 | |
|
| 7 | 145 | | animComponent.Stop(state.clip); |
| | 146 | |
|
| | 147 | | //Manually sample the animation. If the reset is not played again the frame 0 wont be applied |
| 7 | 148 | | state.clipReference.SampleAnimation(animComponent.gameObject, 0); |
| 7 | 149 | | } |
| | 150 | |
|
| | 151 | | public Model.DCLAnimationState GetStateByString(string stateName) |
| | 152 | | { |
| 4 | 153 | | Model model = (Model) this.model; |
| | 154 | |
|
| 14 | 155 | | for (var i = 0; i < model.states.Length; i++) |
| | 156 | | { |
| 7 | 157 | | if (model.states[i].name == stateName) |
| | 158 | | { |
| 4 | 159 | | return model.states[i]; |
| | 160 | | } |
| | 161 | | } |
| | 162 | |
|
| 0 | 163 | | return null; |
| | 164 | | } |
| | 165 | |
|
| 0 | 166 | | public override int GetClassId() { return (int) CLASS_ID_COMPONENT.ANIMATOR; } |
| | 167 | | } |
| | 168 | | } |