| | 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 | |
|
| | 41 | | private string lastLoadedModelSrc; |
| | 42 | |
|
| 7 | 43 | | Dictionary<string, AnimationClip> clipNameToClip = new Dictionary<string, AnimationClip>(); |
| 7 | 44 | | Dictionary<AnimationClip, AnimationState> clipToState = new Dictionary<AnimationClip, AnimationState>(); |
| | 45 | |
|
| 12 | 46 | | private void Awake() { model = new Model(); } |
| | 47 | |
|
| | 48 | | private void OnDestroy() |
| | 49 | | { |
| 6 | 50 | | entity.OnShapeLoaded -= OnEntityShapeLoaded; |
| | 51 | |
|
| 6 | 52 | | var animationShape = entity.GetSharedComponent(typeof(BaseShape)) as LoadableShape; |
| | 53 | |
|
| 6 | 54 | | if (animationShape != null) |
| 5 | 55 | | animationShape.OnLoaded -= OnShapeLoaded; |
| 6 | 56 | | } |
| | 57 | |
|
| | 58 | | public override IEnumerator ApplyChanges(BaseModel model) |
| | 59 | | { |
| 11 | 60 | | entity.OnShapeLoaded -= OnEntityShapeLoaded; |
| 11 | 61 | | entity.OnShapeLoaded += OnEntityShapeLoaded; |
| | 62 | |
|
| | 63 | | //Note: If the entity is still loading the Shape, We wait until it is fully loaded to init it |
| | 64 | | // If we don't wait, this can cause an issue with the asset bundles not loadings animations |
| | 65 | |
|
| 11 | 66 | | if (IsEntityShapeLoaded()) |
| 4 | 67 | | UpdateAnimationState(); |
| | 68 | |
|
| 11 | 69 | | return null; |
| | 70 | | } |
| | 71 | |
|
| 0 | 72 | | new public Model GetModel() { return (Model) model; } |
| | 73 | |
|
| | 74 | | private bool IsEntityShapeLoaded() |
| | 75 | | { |
| 11 | 76 | | var animationShape = entity.GetSharedComponent(typeof(BaseShape)) as LoadableShape; |
| | 77 | |
|
| 11 | 78 | | if (animationShape == null) |
| 2 | 79 | | return false; |
| | 80 | |
|
| 9 | 81 | | return animationShape.isLoaded; |
| | 82 | | } |
| | 83 | |
|
| | 84 | | private void OnEntityShapeLoaded(IDCLEntity shapeEntity) |
| | 85 | | { |
| 5 | 86 | | var animationShape = shapeEntity.GetSharedComponent(typeof(BaseShape)) as LoadableShape; |
| | 87 | |
|
| 5 | 88 | | if (animationShape == null) |
| 0 | 89 | | return; |
| | 90 | |
|
| 5 | 91 | | var shapeModel = animationShape.GetModel() as LoadableShape.Model; |
| | 92 | |
|
| 5 | 93 | | if (shapeModel == null) |
| 0 | 94 | | return; |
| | 95 | |
|
| 5 | 96 | | if ( shapeModel.src == lastLoadedModelSrc ) |
| 0 | 97 | | return; |
| | 98 | |
|
| 5 | 99 | | lastLoadedModelSrc = shapeModel.src; |
| | 100 | |
|
| 5 | 101 | | if ( animationShape.isLoaded ) |
| | 102 | | { |
| 5 | 103 | | UpdateAnimationState(); |
| 5 | 104 | | } |
| | 105 | | else |
| | 106 | | { |
| 0 | 107 | | animationShape.OnLoaded -= OnShapeLoaded; |
| 0 | 108 | | animationShape.OnLoaded += OnShapeLoaded; |
| | 109 | | } |
| 0 | 110 | | } |
| | 111 | |
|
| | 112 | | private void OnShapeLoaded(LoadableShape shape) |
| | 113 | | { |
| 0 | 114 | | shape.OnLoaded -= OnShapeLoaded; |
| 0 | 115 | | UpdateAnimationState(); |
| 0 | 116 | | } |
| | 117 | |
|
| | 118 | | private void Initialize() |
| | 119 | | { |
| 9 | 120 | | if (entity == null || animComponent != null) |
| 4 | 121 | | return; |
| | 122 | |
|
| | 123 | | //NOTE(Brian): fetch all the AnimationClips in Animation component. |
| 5 | 124 | | animComponent = transform.parent.GetComponentInChildren<Animation>(true); |
| | 125 | |
|
| 5 | 126 | | if (animComponent == null) |
| 0 | 127 | | return; |
| | 128 | |
|
| 5 | 129 | | clipNameToClip.Clear(); |
| 5 | 130 | | clipToState.Clear(); |
| 5 | 131 | | int layerIndex = 0; |
| | 132 | |
|
| 5 | 133 | | animComponent.playAutomatically = true; |
| 5 | 134 | | animComponent.enabled = true; |
| 5 | 135 | | animComponent.Stop(); //NOTE(Brian): When the GLTF is created by GLTFSceneImporter a frame may be elapsed, |
| | 136 | | //putting the component in play state if playAutomatically was true at that point. |
| 5 | 137 | | animComponent.clip?.SampleAnimation(animComponent.gameObject, 0); |
| | 138 | |
|
| 24 | 139 | | foreach (AnimationState unityState in animComponent) |
| | 140 | | { |
| 7 | 141 | | clipNameToClip[unityState.clip.name] = unityState.clip; |
| | 142 | |
|
| 7 | 143 | | unityState.clip.wrapMode = WrapMode.Loop; |
| 7 | 144 | | unityState.layer = layerIndex; |
| 7 | 145 | | unityState.blendMode = AnimationBlendMode.Blend; |
| 7 | 146 | | layerIndex++; |
| | 147 | | } |
| 5 | 148 | | } |
| | 149 | |
|
| | 150 | | void UpdateAnimationState() |
| | 151 | | { |
| 9 | 152 | | Initialize(); |
| | 153 | |
|
| 9 | 154 | | if (clipNameToClip.Count == 0 || animComponent == null) |
| 0 | 155 | | return; |
| | 156 | |
|
| 9 | 157 | | Model model = (Model) this.model; |
| | 158 | |
|
| 9 | 159 | | if (model.states == null || model.states.Length == 0) |
| 0 | 160 | | return; |
| | 161 | |
|
| 44 | 162 | | for (int i = 0; i < model.states.Length; i++) |
| | 163 | | { |
| 13 | 164 | | Model.DCLAnimationState state = model.states[i]; |
| | 165 | |
|
| 13 | 166 | | if (clipNameToClip.ContainsKey(state.clip)) |
| | 167 | | { |
| 13 | 168 | | AnimationState unityState = animComponent[state.clip]; |
| 13 | 169 | | unityState.weight = state.weight; |
| 13 | 170 | | unityState.wrapMode = state.looping ? WrapMode.Loop : WrapMode.Default; |
| 13 | 171 | | unityState.clip.wrapMode = unityState.wrapMode; |
| 13 | 172 | | unityState.speed = state.speed; |
| | 173 | |
|
| 13 | 174 | | state.clipReference = unityState.clip; |
| | 175 | |
|
| 13 | 176 | | unityState.enabled = state.playing; |
| | 177 | |
|
| 13 | 178 | | if (state.shouldReset) |
| 3 | 179 | | ResetAnimation(state); |
| | 180 | |
|
| | 181 | |
|
| 13 | 182 | | if (state.playing && !animComponent.IsPlaying(state.clip)) |
| | 183 | | { |
| 3 | 184 | | animComponent.Play(state.clip); |
| | 185 | | } |
| | 186 | | } |
| | 187 | | } |
| 9 | 188 | | } |
| | 189 | |
|
| | 190 | | public void ResetAnimation(Model.DCLAnimationState state) |
| | 191 | | { |
| 4 | 192 | | if (state == null || state.clipReference == null) |
| | 193 | | { |
| 0 | 194 | | Debug.LogError("Clip not found"); |
| 0 | 195 | | return; |
| | 196 | | } |
| | 197 | |
|
| 4 | 198 | | animComponent.Stop(state.clip); |
| | 199 | |
|
| | 200 | | //Manually sample the animation. If the reset is not played again the frame 0 wont be applied |
| 4 | 201 | | state.clipReference.SampleAnimation(animComponent.gameObject, 0); |
| 4 | 202 | | } |
| | 203 | |
|
| | 204 | | public Model.DCLAnimationState GetStateByString(string stateName) |
| | 205 | | { |
| 4 | 206 | | Model model = (Model) this.model; |
| | 207 | |
|
| 14 | 208 | | for (var i = 0; i < model.states.Length; i++) |
| | 209 | | { |
| 7 | 210 | | if (model.states[i].name == stateName) |
| | 211 | | { |
| 4 | 212 | | return model.states[i]; |
| | 213 | | } |
| | 214 | | } |
| | 215 | |
|
| 0 | 216 | | return null; |
| | 217 | | } |
| | 218 | |
|
| 0 | 219 | | public override int GetClassId() { return (int) CLASS_ID_COMPONENT.ANIMATOR; } |
| | 220 | | } |
| | 221 | | } |