| | 1 | | using DCL.Models; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using DCL.Helpers; |
| | 5 | | using UnityEngine; |
| | 6 | | using Decentraland.Sdk.Ecs6; |
| | 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)] |
| 22 | 24 | | public float weight = 1f; |
| | 25 | |
|
| 22 | 26 | | public float speed = 1f; |
| 22 | 27 | | public bool looping = true; |
| | 28 | | public bool shouldReset = false; |
| | 29 | |
|
| | 30 | | public DCLAnimationState Clone() => |
| 0 | 31 | | (DCLAnimationState) this.MemberwiseClone(); |
| | 32 | | } |
| | 33 | |
|
| | 34 | | public DCLAnimationState[] states; |
| | 35 | |
|
| | 36 | | public override BaseModel GetDataFromJSON(string json) => |
| 13 | 37 | | Utils.SafeFromJson<Model>(json); |
| | 38 | |
|
| | 39 | | public override BaseModel GetDataFromPb(ComponentBodyPayload pbModel) |
| | 40 | | { |
| 0 | 41 | | if (pbModel.PayloadCase != ComponentBodyPayload.PayloadOneofCase.Animator) |
| 0 | 42 | | return Utils.SafeUnimplemented<DCLAnimator, Model>(expected: ComponentBodyPayload.PayloadOneofCase.A |
| | 43 | |
|
| 0 | 44 | | if (pbModel.Animator.States.Count == 0) |
| 0 | 45 | | return new Model(); |
| | 46 | |
|
| 0 | 47 | | var model = new Model { states = new DCLAnimationState[pbModel.Animator.States.Count] }; |
| | 48 | |
|
| 0 | 49 | | for (var i = 0; i < pbModel.Animator.States.Count; i++) |
| | 50 | | { |
| 0 | 51 | | model.states[i] = new DCLAnimationState(); |
| | 52 | |
|
| 0 | 53 | | if (pbModel.Animator.States[i].HasName) model.states[i].name = pbModel.Animator.States[i].Name; |
| 0 | 54 | | if (pbModel.Animator.States[i].HasClip) model.states[i].clip = pbModel.Animator.States[i].Clip; |
| 0 | 55 | | if (pbModel.Animator.States[i].HasPlaying) model.states[i].playing = pbModel.Animator.States[i].Play |
| 0 | 56 | | if (pbModel.Animator.States[i].HasWeight) model.states[i].weight = pbModel.Animator.States[i].Weight |
| 0 | 57 | | if (pbModel.Animator.States[i].HasSpeed) model.states[i].speed = pbModel.Animator.States[i].Speed; |
| 0 | 58 | | if (pbModel.Animator.States[i].HasLooping) model.states[i].looping = pbModel.Animator.States[i].Loop |
| 0 | 59 | | if (pbModel.Animator.States[i].HasShouldReset) model.states[i].shouldReset = pbModel.Animator.States |
| | 60 | | } |
| | 61 | |
|
| 0 | 62 | | return model; |
| | 63 | | } |
| | 64 | | } |
| | 65 | |
|
| | 66 | | [System.NonSerialized] |
| | 67 | | public Animation animComponent = null; |
| | 68 | |
|
| | 69 | | private string lastLoadedModelSrc; |
| | 70 | |
|
| 11 | 71 | | Dictionary<string, AnimationClip> clipNameToClip = new Dictionary<string, AnimationClip>(); |
| 11 | 72 | | Dictionary<AnimationClip, AnimationState> clipToState = new Dictionary<AnimationClip, AnimationState>(); |
| | 73 | |
|
| 5 | 74 | | public override string componentName => "animator"; |
| | 75 | |
|
| | 76 | | private void Awake() |
| | 77 | | { |
| 8 | 78 | | model = new Model(); |
| 8 | 79 | | } |
| | 80 | |
|
| | 81 | | private void OnDestroy() |
| | 82 | | { |
| 8 | 83 | | if (entity == null) return; |
| | 84 | |
|
| 8 | 85 | | entity.OnShapeLoaded -= OnEntityShapeLoaded; |
| | 86 | |
|
| 8 | 87 | | if (scene.componentsManagerLegacy.GetSharedComponent(entity, typeof(BaseShape)) is LoadableShape animationSh |
| 0 | 88 | | animationShape.OnLoaded -= OnShapeLoaded; |
| 8 | 89 | | } |
| | 90 | |
|
| | 91 | | public override void Cleanup() |
| | 92 | | { |
| 8 | 93 | | base.Cleanup(); |
| 8 | 94 | | animComponent = null; |
| 8 | 95 | | } |
| | 96 | |
|
| | 97 | | public override IEnumerator ApplyChanges(BaseModel model) |
| | 98 | | { |
| 13 | 99 | | entity.OnShapeLoaded -= OnEntityShapeLoaded; |
| 13 | 100 | | entity.OnShapeLoaded += OnEntityShapeLoaded; |
| | 101 | |
|
| | 102 | | //Note: If the entity is still loading the Shape, We wait until it is fully loaded to init it |
| | 103 | | // If we don't wait, this can cause an issue with the asset bundles not loadings animations |
| | 104 | |
|
| 13 | 105 | | if (IsEntityShapeLoaded()) |
| 5 | 106 | | UpdateAnimationState(); |
| | 107 | |
|
| 13 | 108 | | return null; |
| | 109 | | } |
| | 110 | |
|
| | 111 | | public new Model GetModel() => |
| 1 | 112 | | (Model) model; |
| | 113 | |
|
| | 114 | | private bool IsEntityShapeLoaded() => |
| 13 | 115 | | (scene.componentsManagerLegacy.GetSharedComponent(entity, typeof(BaseShape)) as LoadableShape) |
| | 116 | | is { isLoaded: true }; |
| | 117 | |
|
| | 118 | | private void OnEntityShapeLoaded(IDCLEntity shapeEntity) |
| | 119 | | { |
| 5 | 120 | | var animationShape = scene.componentsManagerLegacy.GetSharedComponent(entity, (typeof(BaseShape))) as Loadab |
| | 121 | |
|
| 5 | 122 | | if (animationShape?.GetModel() is not LoadableShape.Model shapeModel) |
| 0 | 123 | | return; |
| | 124 | |
|
| 5 | 125 | | if ( shapeModel.src == lastLoadedModelSrc ) |
| 0 | 126 | | return; |
| | 127 | |
|
| 5 | 128 | | lastLoadedModelSrc = shapeModel.src; |
| | 129 | |
|
| 5 | 130 | | if (animationShape.isLoaded) |
| | 131 | | { |
| 5 | 132 | | UpdateAnimationState(); |
| | 133 | | } |
| | 134 | | else |
| | 135 | | { |
| 0 | 136 | | animationShape.OnLoaded -= OnShapeLoaded; |
| 0 | 137 | | animationShape.OnLoaded += OnShapeLoaded; |
| | 138 | | } |
| 0 | 139 | | } |
| | 140 | |
|
| | 141 | | private void OnShapeLoaded(LoadableShape shape) |
| | 142 | | { |
| 0 | 143 | | shape.OnLoaded -= OnShapeLoaded; |
| 0 | 144 | | UpdateAnimationState(); |
| 0 | 145 | | } |
| | 146 | |
|
| | 147 | | private void Initialize() |
| | 148 | | { |
| 10 | 149 | | if (entity == null || animComponent != null) |
| 4 | 150 | | return; |
| | 151 | |
|
| | 152 | | //NOTE(Brian): fetch all the AnimationClips in Animation component. |
| 6 | 153 | | animComponent = transform.parent.GetComponentInChildren<Animation>(true); |
| | 154 | |
|
| 6 | 155 | | if (animComponent == null) |
| 0 | 156 | | return; |
| | 157 | |
|
| 6 | 158 | | clipNameToClip.Clear(); |
| 6 | 159 | | clipToState.Clear(); |
| 6 | 160 | | int layerIndex = 0; |
| | 161 | |
|
| 6 | 162 | | animComponent.playAutomatically = true; |
| 6 | 163 | | animComponent.enabled = true; |
| 6 | 164 | | animComponent.Stop(); //NOTE(Brian): When the GLTF is created by GLTFSceneImporter a frame may be elapsed, |
| | 165 | | //putting the component in play state if playAutomatically was true at that point. |
| 6 | 166 | | animComponent.clip?.SampleAnimation(animComponent.gameObject, 0); |
| | 167 | |
|
| 28 | 168 | | foreach (AnimationState unityState in animComponent) |
| | 169 | | { |
| 8 | 170 | | clipNameToClip[unityState.clip.name] = unityState.clip; |
| | 171 | |
|
| 8 | 172 | | unityState.clip.wrapMode = WrapMode.Loop; |
| 8 | 173 | | unityState.layer = layerIndex; |
| 8 | 174 | | unityState.blendMode = AnimationBlendMode.Blend; |
| 8 | 175 | | layerIndex++; |
| | 176 | | } |
| 6 | 177 | | } |
| | 178 | |
|
| | 179 | | void UpdateAnimationState() |
| | 180 | | { |
| 10 | 181 | | Initialize(); |
| | 182 | |
|
| 10 | 183 | | if (clipNameToClip.Count == 0 || animComponent == null) |
| 0 | 184 | | return; |
| | 185 | |
|
| 10 | 186 | | Model model = (Model) this.model; |
| | 187 | |
|
| 10 | 188 | | if (model.states == null || model.states.Length == 0) |
| 0 | 189 | | return; |
| | 190 | |
|
| 48 | 191 | | for (int i = 0; i < model.states.Length; i++) |
| | 192 | | { |
| 14 | 193 | | Model.DCLAnimationState state = model.states[i]; |
| | 194 | |
|
| 14 | 195 | | if (clipNameToClip.ContainsKey(state.clip)) |
| | 196 | | { |
| 14 | 197 | | AnimationState unityState = animComponent[state.clip]; |
| 14 | 198 | | unityState.weight = state.weight; |
| 14 | 199 | | unityState.wrapMode = state.looping ? WrapMode.Loop : WrapMode.Default; |
| 14 | 200 | | unityState.clip.wrapMode = unityState.wrapMode; |
| 14 | 201 | | unityState.speed = state.speed; |
| | 202 | |
|
| 14 | 203 | | state.clipReference = unityState.clip; |
| | 204 | |
|
| 14 | 205 | | unityState.enabled = state.playing; |
| | 206 | |
|
| 14 | 207 | | if (state.shouldReset) |
| 3 | 208 | | ResetAnimation(state); |
| | 209 | |
|
| | 210 | |
|
| 14 | 211 | | if (state.playing && !animComponent.IsPlaying(state.clip)) |
| | 212 | | { |
| 3 | 213 | | animComponent.Play(state.clip); |
| | 214 | | } |
| | 215 | | } |
| | 216 | | } |
| 10 | 217 | | } |
| | 218 | |
|
| | 219 | | public void ResetAnimation(Model.DCLAnimationState state) |
| | 220 | | { |
| 4 | 221 | | if (state == null || state.clipReference == null) |
| | 222 | | { |
| 0 | 223 | | Debug.LogError("Clip not found"); |
| 0 | 224 | | return; |
| | 225 | | } |
| | 226 | |
|
| 4 | 227 | | animComponent.Stop(state.clip); |
| | 228 | |
|
| | 229 | | //Manually sample the animation. If the reset is not played again the frame 0 wont be applied |
| 4 | 230 | | state.clipReference.SampleAnimation(animComponent.gameObject, 0); |
| 4 | 231 | | } |
| | 232 | |
|
| | 233 | | public Model.DCLAnimationState GetStateByString(string stateName) |
| | 234 | | { |
| 4 | 235 | | Model model = (Model) this.model; |
| | 236 | |
|
| 14 | 237 | | for (var i = 0; i < model.states.Length; i++) |
| | 238 | | { |
| 7 | 239 | | if (model.states[i].name == stateName) |
| | 240 | | { |
| 4 | 241 | | return model.states[i]; |
| | 242 | | } |
| | 243 | | } |
| | 244 | |
|
| 0 | 245 | | return null; |
| | 246 | | } |
| | 247 | |
|
| 1 | 248 | | public override int GetClassId() { return (int) CLASS_ID_COMPONENT.ANIMATOR; } |
| | 249 | | } |
| | 250 | | } |