HoloLens 2 Gait Training
The purpose of this project is to create gait training application.
Loading...
Searching...
No Matches
AvatarAnimationState.cs
Go to the documentation of this file.
1using System;
2using Microsoft.MixedReality.Toolkit.UI;
3using System.Collections.Generic;
4using TMPro;
5using UnityEngine;
6
10[RequireComponent(typeof(Animator))]
11public class AvatarAnimationState : MonoBehaviour
12{
13
15 private Animator m_Animator;
16
19
22
25 private int m_NoiseIndex;
26
29 private TextMeshPro m_NoiseDataPanelTitle;
30
32 private string m_NoisePatternLbl = "Pink";
33
35 private bool m_IsAnimationLocked = false;
36
38 private const float m_DefaultAnimationSpeed = 1.0f;
39
41 [SerializeField] private GameObject m_DialogPrefabMedium = null;
42
47 private bool m_IdleElementFlag = false;
48
50 private int m_Counter = 1;
51
53 private List<float> m_AnimationLength = null;
54
56 private Dictionary<string, float> m_OriginalAnimationLength;
57
59 public List<float> AnimationLength { get => m_AnimationLength; }
60
65 void Awake()
66 {
67 m_Animator = GetComponent<Animator>();
68 m_SetNoise = GameObject.Find("NoiseController").GetComponent<SetNoise>();
69 m_NoiseDataPanelTitle = GameObject.FindGameObjectWithTag("NoiseDataPanelTitle").GetComponent<TextMeshPro>();
70 m_NoiseController = GameObject.Find("NoiseController").GetComponent<NoiseController>();
71 m_AnimationLength = new List<float>();
72 }
73
77 private void Start()
78 {
79 m_OriginalAnimationLength = new Dictionary<string, float>();
81 m_NoiseIndex = 0;
82 }
83
91 private void Update()
92 {
93
94 // Check the player is moving or not.
96 m_Animator.SetBool("IsWalking", playerMoving);
97 m_Animator.SetInteger("NoiseLvl", (int)m_SetNoise.GetNoisePattern());
98
100 {
102 }
103 else
104 {
105 m_IsAnimationLocked = false;
106 m_Animator.speed = m_DefaultAnimationSpeed;
107 }
108
109 // TODO Future work is needed to change the avatar speed.
110 // m_Animator.speed = CamMovementTracker.m_CamTrackerInstance.Speed;
111 }
112
118 {
119 if( m_Animator.GetCurrentAnimatorStateInfo(0).IsName("Joel_PGN") )
120 {
122 }
123 else if( m_Animator.GetCurrentAnimatorStateInfo(0).IsName("Joel_Iso") )
124 {
126 }
127 else if( m_Animator.GetCurrentAnimatorStateInfo(0).IsName("Joel_WGN") )
128 {
130 }
131 else if( m_Animator.GetCurrentAnimatorStateInfo(0).IsName("Idle") )
132 {
133 m_NoisePatternLbl = "Idle";
134 m_NoiseDataPanelTitle.text = m_NoisePatternLbl + " Noise = " + m_DefaultAnimationSpeed;
135 }
136 else
137 {
138 Debug.Log(" Animation is unknown ");
139 }
140 }
141
146 private void ResetNoiseAfterEnd( int noiseLength, string lbl )
147 {
148 m_NoisePatternLbl = lbl;
149
150 if( m_NoiseIndex >= noiseLength )
151 {
153 m_NoiseIndex = 0;
154 }
155 }
156
161 {
162 Dialog.Open( m_DialogPrefabMedium, DialogButtonType.OK, "Good Job!", "Times Up!" +
163 " You covered required distance.", true);
164 }
165
171 private void RunGaitCycle()
172 {
173 float noiseValue = m_DefaultAnimationSpeed;
174 float desiredSpeed = m_DefaultAnimationSpeed;
175
176 if( String.Equals( m_NoisePatternLbl, "Pink" ) || ( String.Equals( m_NoisePatternLbl, "Random" ) ) ||
177 String.Equals( m_NoisePatternLbl, "ISO" ) )
178 {
179 noiseValue = Mathf.Abs( m_NoiseController.BaseNoise.NoiseValueList[m_NoiseIndex] );
180 }
181 else if( String.Equals( m_NoisePatternLbl, "Idle" ) )
182 {
183 m_IdleElementFlag = true;
184 return;
185 }
186 else
187 {
188 m_NoisePatternLbl = "ERROR!!!";
189 noiseValue = m_DefaultAnimationSpeed;
190 }
191
192 float animationLength = 0.0f;
193 bool isValidAnimLength = m_OriginalAnimationLength.TryGetValue( m_NoisePatternLbl, out animationLength );
194
195 if( isValidAnimLength && noiseValue != 0 )
196 {
197 desiredSpeed = ( animationLength / noiseValue );
198 }
199
200 m_IsAnimationLocked = true;
201 m_Animator.speed = desiredSpeed;
202 m_NoiseDataPanelTitle.text = m_NoisePatternLbl + " Noise = " + noiseValue;
203
204 float len = m_Animator.GetCurrentAnimatorStateInfo(0).length;
205 m_AnimationLength.Add( len );
206
207 // Only for debug.
208 // Debug.Log( "Number: " + noiseValue + " Time: " + Time.realtimeSinceStartup + " Len: " + len );
209 }
210
215 {
216 AnimationClip[] clips = m_Animator.runtimeAnimatorController.animationClips;
217
218 foreach( AnimationClip clip in clips )
219 {
220 switch( clip.name )
221 {
222 case "Joel_PGN":
223 m_OriginalAnimationLength.Add( "Pink", clip.length );
224 break;
225
226 case "Joel_Iso":
227 m_OriginalAnimationLength.Add( "ISO", clip.length );
228 break;
229
230 case "Joel_WGN":
231 m_OriginalAnimationLength.Add( "Random", clip.length );
232 break;
233
234 default:
235 break;
236 }
237 }
238 }
239
245 private void LockdownAnimation()
246 {
247 if( m_IsAnimationLocked == false )
248 {
249 RunGaitCycle();
250 }
251 }
252
257 private void UnlockAnimation()
258 {
259 m_IsAnimationLocked = false;
260
261 if ( ( m_IsAnimationLocked == false ) && ( m_IdleElementFlag == false ) )
262 {
263 m_NoiseIndex++;
264 }
265 else
266 {
267 m_Counter++;
268
269 // Prevent first element from being skipped.
270 if( m_NoiseIndex == 0 && m_Counter == 2 )
271 {
272 m_Counter = 1;
273 m_IdleElementFlag = false;
274 }
275
276 // Prevent element from being skipped after transtion from idle to moving.
277 if( m_Counter == 3 )
278 {
279 m_Counter = 1;
280 m_IdleElementFlag = false;
281 }
282 }
283 }
284
289 {
290 m_AnimationLength.Clear();
291 m_NoiseIndex = 0;
292 m_Counter = 1;
293 }
294}
This class is used to control the avartar animation state.
void OpenConfirmationDialogMedium()
Opens confirmation dialog example
const float m_DefaultAnimationSpeed
Default value of animation speed.
TextMeshPro m_NoiseDataPanelTitle
This is also use as an error message display and current noise display.
string m_NoisePatternLbl
To identify the current noise pattern.
void RunGaitCycle()
Expand and Shrink gait cycle animation The completion time of animation is the value of pink or white...
void ApplyCorrectNoisePattern()
Get the calculated noise pattern and apply that to animation speed. This noise will shrink or expand ...
void Awake()
Gets called first when script is invoked Cached the references to Animator, SetNoise and NoiseDataPan...
Animator m_Animator
Reference to the animator
NoiseController m_NoiseController
Reference to NoiseController class.
int m_NoiseIndex
Noise index for traveling the noise list. At the end of the travel, it will be reset to 0.
void UnlockAnimation()
Unlock the animation after it played out. Mapped to animation event.
bool m_IdleElementFlag
When Avatar goes to Idle, m_NoiseIndex will increment by 1. This will omit the next noise value from ...
GameObject m_DialogPrefabMedium
UI confirmation pop-up panel.
void Update()
Gets called per each frame The CamMovementTracker singleton instance has speed and IsMoving Flags....
List< float > m_AnimationLength
This list stores timestamps of the animation length of the gait cycle.
void ResetNoiseAfterEnd(int noiseLength, string lbl)
When we reached to the end of the colred noise list, we looped back to the beginning.
List< float > AnimationLength
Property to get the Animation length (Read-Only)
bool m_IsAnimationLocked
This flag is used to lock down the animation till it played out.
int m_Counter
Use to apply previous noise after changing idle to moving.
void LockdownAnimation()
Lockdown the animation until it played out. Animation will unlock after it complete the current loop....
void StoreAnimationClipsLength()
Store the original animation clips values in a dictionary.
Dictionary< string, float > m_OriginalAnimationLength
This stores the length of the original gait cycle animations.
void ResetAnimationLengthList()
Called after user click cancel pattern button in Noise UI.
void Start()
Cached a reference to ScaledNoisePatterns class.
SetNoise m_SetNoise
Reference to the SetNoise Script.
bool NoiseAppliedFlag
Property to get noise applied flag (Read-Only)
List< float > NoiseValueList
Property to get colored noise distribution (Read-Only)
A singleton Instance to track Camera Movements This will also calculate distance, rotation and speed ...
static CamMovementTracker m_CamTrackerInstance
Singleton Instance We have 1 Camera instance and 1 User at all time. Therefore, I believe Singleton i...
bool IsMoving
Property to get and set IsMoving flag.
This class initiate appopriate noise objects.
BaseNoiseClass BaseNoise
This class is used to set noise pattern levels. When user press a button, it will change the pattern.
Definition: SetNoise.cs:12
ColoredNoise GetNoisePattern()
Gettr to get the user's selection.
Definition: SetNoise.cs:129