GPUAnimtion.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using GPUECSAnimationBaker.Engine.AnimatorSystem;
  4. using GpuEcsAnimationBaker.Engine.Data;
  5. using Unity.Collections;
  6. using Unity.Mathematics;
  7. using Unity.Transforms;
  8. using UnityEngine;
  9. public class GPUAnimtion
  10. {
  11. // public GpuEcsAnimatorBehaviour GameObject;
  12. //
  13. // public Renderer Renderer;
  14. public GpuEcsAnimationDataComponent gpuEcsAnimationData;
  15. public List<GpuEcsAnimationDataBufferElement> gpuEcsAnimationDataBuffer;
  16. public List<GpuEcsAnimationEventOccurenceBufferElement> gpuEcsAnimationEventOccurenceBuffer;
  17. public GpuEcsAnimatorShaderDataComponent gpuEcsAnimatorShaderData;
  18. public GpuEcsAnimatorInitializedComponent gpuEcsAnimatorInitialized;
  19. private GpuEcsAnimatorControlComponent gpuEcsAnimatorControl;
  20. private GpuEcsAnimatorControlStateComponent gpuEcsAnimatorControlState;
  21. private GpuEcsAnimatorTransitionInfoComponent gpuEcsAnimatorTransitionInfo;
  22. private GpuEcsAnimatorStateComponent gpuEcsAnimatorState;
  23. public List<GpuEcsAttachmentAnchorDataBufferElement> gpuEcsAttachmentAnchorData;
  24. public List<GpuEcsCurrentAttachmentAnchorBufferElement> gpuEcsCurrentAttachmentAnchors;
  25. public GpuEcsAnimatorEventBufferElement gpuEcsAnimatorEventBuffer;
  26. public int animtionIndex;
  27. // private MaterialPropertyBlock _materialPropertyBlock;
  28. // private Renderer renderer;
  29. public void SetSpeed(float speed)
  30. {
  31. gpuEcsAnimatorControl.animatorInfo.speedFactor = speed;
  32. }
  33. [ContextMenu("adadadsa")]
  34. public void SetAnimtionIndex(AnimatorInfo animatorInfo)
  35. {
  36. gpuEcsAnimatorControl.animatorInfo = animatorInfo;
  37. }
  38. public void SetAnimtionIndex(int animatorInfo)
  39. {
  40. gpuEcsAnimatorControl.animatorInfo.animationID = animatorInfo;
  41. }
  42. public void GetGPUAnchors(int index, out Vector3 pos, out Quaternion unityQuaternion)
  43. {
  44. if (gpuEcsCurrentAttachmentAnchors.Count <= index)
  45. {
  46. pos = Vector3.zero;
  47. unityQuaternion = Quaternion.identity;
  48. return;
  49. }
  50. GpuEcsCurrentAttachmentAnchorBufferElement gpuEcsCurrentAttachmentAnchorBufferElement =
  51. gpuEcsCurrentAttachmentAnchors[0];
  52. float3 pos2 = gpuEcsCurrentAttachmentAnchorBufferElement.currentTransform.TransformPoint(float3.zero);
  53. pos = new Vector3(pos2.x, pos2.y, pos2.z);
  54. float4 xuanzhaung = gpuEcsCurrentAttachmentAnchorBufferElement.currentTransform
  55. .TransformRotation(quaternion.identity).value;
  56. unityQuaternion = new Quaternion(xuanzhaung.x, xuanzhaung.y, xuanzhaung.z, xuanzhaung.w);
  57. }
  58. // Update is called once per frame
  59. public void Update()
  60. {
  61. Execute();
  62. // if (gpuEcsCurrentAttachmentAnchors.Count > 0)
  63. // {
  64. // float3 pos2 = gpuEcsCurrentAttachmentAnchors[0].currentTransform.TransformPoint(float3.zero);
  65. // float3 pos = LocalTransform.FromMatrix(gpuEcsCurrentAttachmentAnchors[0].currentTransform).Position;
  66. // Debug.Log(pos);
  67. // // Matrix4x4 matrix4X4 = new Matrix4x4();
  68. // // float3 pos= float3(0,0,0);
  69. // }
  70. // _materialPropertyBlock.SetMatrix("_AnimationState", gpuEcsAnimatorShaderData.shaderData);
  71. // _materialPropertyBlock.SetFloat("_EnableAnimation", 1);
  72. // renderer.SetPropertyBlock(_materialPropertyBlock);
  73. }
  74. public void Init(GpuEcsAnimatorBehaviour gpuEcsAnimatorBehaviour)
  75. {
  76. // _materialPropertyBlock = new MaterialPropertyBlock();
  77. // GpuEcsAnimatedMeshBehaviour ecsAnimatedMeshBehaviour = gpuEcsAnimatorBehaviour.gameObject.transform
  78. // .GetComponentInChildren<GpuEcsAnimatedMeshBehaviour>();
  79. // renderer = ecsAnimatedMeshBehaviour.GetComponent<Renderer>();
  80. // renderer.GetPropertyBlock(_materialPropertyBlock);
  81. gpuEcsAnimationData = new GpuEcsAnimationDataComponent()
  82. {
  83. nbrOfAttachmentAnchors = gpuEcsAnimatorBehaviour.nbrOfAttachmentAnchors,
  84. totalNbrOfFrames = gpuEcsAnimatorBehaviour.totalNbrOfFrames
  85. };
  86. gpuEcsAnimationDataBuffer =
  87. new List<GpuEcsAnimationDataBufferElement>();
  88. for (int animationIndex = 0; animationIndex < gpuEcsAnimatorBehaviour.animations.Length; animationIndex++)
  89. {
  90. GpuEcsAnimationData gpuEcsAnimationData = gpuEcsAnimatorBehaviour.animations[animationIndex];
  91. gpuEcsAnimationDataBuffer.Add(new GpuEcsAnimationDataBufferElement()
  92. {
  93. startFrameIndex = gpuEcsAnimationData.startFrameIndex,
  94. nbrOfFramesPerSample = gpuEcsAnimationData.nbrOfFramesPerSample,
  95. nbrOfInBetweenSamples = gpuEcsAnimationData.nbrOfInBetweenSamples,
  96. blendTimeCorrection = gpuEcsAnimationData.blendTimeCorrection,
  97. startEventOccurenceId = gpuEcsAnimationData.startEventOccurenceId,
  98. nbrOfEventOccurenceIds = gpuEcsAnimationData.nbrOfEventOccurenceIds,
  99. loop = gpuEcsAnimationData.loop
  100. });
  101. }
  102. gpuEcsAnimationEventOccurenceBuffer = new List<GpuEcsAnimationEventOccurenceBufferElement>();
  103. for (int animationEventOccurenceId = 0;
  104. animationEventOccurenceId < gpuEcsAnimatorBehaviour.animationEventOccurences.Length;
  105. animationEventOccurenceId++)
  106. {
  107. GpuEcsAnimationEventOccurence occurence =
  108. gpuEcsAnimatorBehaviour.animationEventOccurences[animationEventOccurenceId];
  109. gpuEcsAnimationEventOccurenceBuffer.Add(new GpuEcsAnimationEventOccurenceBufferElement()
  110. {
  111. eventNormalizedTime = occurence.eventNormalizedTime,
  112. eventId = occurence.eventId
  113. });
  114. }
  115. gpuEcsAnimatorShaderData = new GpuEcsAnimatorShaderDataComponent()
  116. {
  117. shaderData = new float4x4(
  118. 1f, 0, 0, 0,
  119. 0, 0, 0, 0,
  120. 0, 0, 0, 0,
  121. 0, 0, 0, 0)
  122. };
  123. int initialAnimationID = 0;
  124. GpuEcsAnimatorInitializerBehaviour initializer =
  125. gpuEcsAnimatorBehaviour.GetComponent<GpuEcsAnimatorInitializerBehaviour>();
  126. if (initializer != null) initialAnimationID = initializer.GetInitialAnimationID();
  127. gpuEcsAnimatorInitialized = new GpuEcsAnimatorInitializedComponent()
  128. {
  129. initialized = false
  130. };
  131. gpuEcsAnimatorControl = new GpuEcsAnimatorControlComponent()
  132. {
  133. animatorInfo = new AnimatorInfo()
  134. {
  135. animationID = initialAnimationID,
  136. blendFactor = 0,
  137. speedFactor = 1
  138. },
  139. transitionSpeed = 0,
  140. startNormalizedTime = 0
  141. };
  142. gpuEcsAnimatorControlState =
  143. new GpuEcsAnimatorControlStateComponent()
  144. {
  145. state = GpuEcsAnimatorControlStates.Start
  146. };
  147. gpuEcsAnimatorTransitionInfo =
  148. new GpuEcsAnimatorTransitionInfoComponent();
  149. gpuEcsAnimatorState = new GpuEcsAnimatorStateComponent();
  150. gpuEcsAttachmentAnchorData =
  151. new List<GpuEcsAttachmentAnchorDataBufferElement>();
  152. gpuEcsCurrentAttachmentAnchors =
  153. new List<GpuEcsCurrentAttachmentAnchorBufferElement>();
  154. if (gpuEcsAnimatorBehaviour.attachmentAnchorData != null && gpuEcsAnimatorBehaviour.nbrOfAttachmentAnchors > 0)
  155. {
  156. int anchorDataLength = gpuEcsAnimatorBehaviour.attachmentAnchorData.anchorTransforms.Length;
  157. NativeArray<GpuEcsAttachmentAnchorDataBufferElement> anchors =
  158. new NativeArray<GpuEcsAttachmentAnchorDataBufferElement>(anchorDataLength, Allocator.Temp);
  159. for (int i = 0; i < anchorDataLength; i++)
  160. anchors[i] = new GpuEcsAttachmentAnchorDataBufferElement()
  161. { anchorTransform = gpuEcsAnimatorBehaviour.attachmentAnchorData.anchorTransforms[i] };
  162. gpuEcsAttachmentAnchorData.AddRange(anchors);
  163. anchors.Dispose();
  164. NativeArray<GpuEcsCurrentAttachmentAnchorBufferElement> currentAnchorTransforms =
  165. new NativeArray<GpuEcsCurrentAttachmentAnchorBufferElement>(
  166. gpuEcsAnimatorBehaviour.nbrOfAttachmentAnchors,
  167. Allocator.Temp);
  168. gpuEcsCurrentAttachmentAnchors.AddRange(currentAnchorTransforms);
  169. currentAnchorTransforms.Dispose();
  170. }
  171. gpuEcsAnimatorEventBuffer = new GpuEcsAnimatorEventBufferElement();
  172. }
  173. public void Execute()
  174. {
  175. // gpuEcsAnimatorEventBuffer.Clear();
  176. if (!gpuEcsAnimatorInitialized.initialized)
  177. {
  178. // We switch immediately to the first animation, no transition
  179. gpuEcsAnimatorTransitionInfo = new GpuEcsAnimatorTransitionInfoComponent()
  180. {
  181. current = gpuEcsAnimatorControl.animatorInfo,
  182. blendPreviousToCurrent = 1f
  183. };
  184. gpuEcsAnimatorState = new GpuEcsAnimatorStateComponent()
  185. {
  186. currentNormalizedTime = gpuEcsAnimatorControl.startNormalizedTime,
  187. stoppedPrevious = false,
  188. stoppedCurrent = false
  189. };
  190. gpuEcsAnimatorInitialized.initialized = true;
  191. }
  192. else if (gpuEcsAnimatorControl.animatorInfo.animationID != gpuEcsAnimatorTransitionInfo.current.animationID)
  193. {
  194. // A new animation (or animation combination) has been started, so we need to do a transition
  195. // from the old one to the new
  196. gpuEcsAnimatorTransitionInfo = new GpuEcsAnimatorTransitionInfoComponent()
  197. {
  198. current = gpuEcsAnimatorControl.animatorInfo,
  199. previous = gpuEcsAnimatorTransitionInfo.current,
  200. blendPreviousToCurrent = 0f
  201. };
  202. gpuEcsAnimatorState = new GpuEcsAnimatorStateComponent()
  203. {
  204. previousNormalizedTime = gpuEcsAnimatorState.currentNormalizedTime,
  205. stoppedPrevious = gpuEcsAnimatorState.stoppedCurrent,
  206. currentNormalizedTime = gpuEcsAnimatorControl.startNormalizedTime,
  207. stoppedCurrent = false,
  208. };
  209. }
  210. else
  211. {
  212. // The same animation (or animation combination) is still running, but the parameters might have changed
  213. // (blendPrimaryToSecondary or speedFactor)
  214. gpuEcsAnimatorTransitionInfo.current = gpuEcsAnimatorControl.animatorInfo;
  215. }
  216. GpuEcsAnimatorControlStates controlState = gpuEcsAnimatorControlState.state;
  217. if (gpuEcsAnimatorControlState.state == GpuEcsAnimatorControlStates.Start)
  218. gpuEcsAnimatorState.stoppedCurrent = false;
  219. else if (gpuEcsAnimatorControlState.state == GpuEcsAnimatorControlStates.Stop)
  220. gpuEcsAnimatorState.stoppedCurrent = true;
  221. gpuEcsAnimatorControlState.state = GpuEcsAnimatorControlStates.KeepCurrentState;
  222. if (!gpuEcsAnimatorState.stoppedCurrent)
  223. {
  224. UpdateAnimatorState(ref gpuEcsAnimatorState.currentNormalizedTime, ref gpuEcsAnimatorState.stoppedCurrent,
  225. ref gpuEcsAnimatorEventBuffer,
  226. gpuEcsAnimatorTransitionInfo.current, controlState, gpuEcsAnimationDataBuffer,
  227. gpuEcsAnimationEventOccurenceBuffer,
  228. out float primaryBlendFactor, out float primaryTransitionToNextFrame, out int primaryFrameIndex,
  229. out float secondaryBlendFactor, out float secondaryTransitionToNextFrame, out int secondaryFrameIndex,
  230. forPrevious: false);
  231. if (gpuEcsAnimatorTransitionInfo.blendPreviousToCurrent >= 1f)
  232. {
  233. gpuEcsAnimatorShaderData.shaderData = new float4x4(
  234. primaryBlendFactor, primaryTransitionToNextFrame, primaryFrameIndex, 0,
  235. secondaryBlendFactor, secondaryTransitionToNextFrame, secondaryFrameIndex, 0,
  236. 0, 0, 0, 0,
  237. 0, 0, 0, 0);
  238. //Apply attachment anchor transforms
  239. for (int attachmentAnchorIndex = 0;
  240. attachmentAnchorIndex < gpuEcsAnimationData.nbrOfAttachmentAnchors;
  241. attachmentAnchorIndex++)
  242. {
  243. int baseIndex = gpuEcsAnimationData.totalNbrOfFrames * attachmentAnchorIndex;
  244. gpuEcsCurrentAttachmentAnchors[attachmentAnchorIndex] =
  245. new GpuEcsCurrentAttachmentAnchorBufferElement()
  246. {
  247. currentTransform = LerpBlend(gpuEcsAttachmentAnchorData, baseIndex,
  248. primaryFrameIndex, secondaryFrameIndex,
  249. primaryTransitionToNextFrame, secondaryTransitionToNextFrame,
  250. secondaryBlendFactor)
  251. };
  252. }
  253. }
  254. else
  255. {
  256. if (gpuEcsAnimatorControl.transitionSpeed == 0)
  257. gpuEcsAnimatorTransitionInfo.blendPreviousToCurrent = 1f;
  258. else
  259. {
  260. gpuEcsAnimatorTransitionInfo.blendPreviousToCurrent +=
  261. Time.deltaTime / gpuEcsAnimatorControl.transitionSpeed;
  262. if (gpuEcsAnimatorTransitionInfo.blendPreviousToCurrent > 1f)
  263. gpuEcsAnimatorTransitionInfo.blendPreviousToCurrent = 1f;
  264. }
  265. float previousToCurrent = gpuEcsAnimatorTransitionInfo.blendPreviousToCurrent;
  266. float currentToPrevious = 1f - previousToCurrent;
  267. UpdateAnimatorState(ref gpuEcsAnimatorState.previousNormalizedTime,
  268. ref gpuEcsAnimatorState.stoppedPrevious,
  269. ref gpuEcsAnimatorEventBuffer,
  270. gpuEcsAnimatorTransitionInfo.previous, controlState, gpuEcsAnimationDataBuffer,
  271. gpuEcsAnimationEventOccurenceBuffer,
  272. out float previousPrimaryBlendFactor, out float previousPrimaryTransitionToNextFrame,
  273. out int previousPrimaryFrameIndex,
  274. out float previousSecondaryBlendFactor, out float previousSecondaryTransitionToNextFrame,
  275. out int previousSecondaryFrameIndex,
  276. forPrevious: true);
  277. gpuEcsAnimatorShaderData.shaderData = new float4x4(
  278. previousToCurrent * primaryBlendFactor, primaryTransitionToNextFrame, primaryFrameIndex, 0,
  279. previousToCurrent * secondaryBlendFactor, secondaryTransitionToNextFrame, secondaryFrameIndex, 0,
  280. currentToPrevious * previousPrimaryBlendFactor, previousPrimaryTransitionToNextFrame,
  281. previousPrimaryFrameIndex, 0,
  282. currentToPrevious * previousSecondaryBlendFactor, previousSecondaryTransitionToNextFrame,
  283. previousSecondaryFrameIndex, 0);
  284. for (int attachmentAnchorIndex = 0;
  285. attachmentAnchorIndex < gpuEcsAnimationData.nbrOfAttachmentAnchors;
  286. attachmentAnchorIndex++)
  287. {
  288. int baseIndex = gpuEcsAnimationData.totalNbrOfFrames * attachmentAnchorIndex;
  289. float4x4 current = LerpBlend(gpuEcsAttachmentAnchorData, baseIndex,
  290. primaryFrameIndex, secondaryFrameIndex,
  291. primaryTransitionToNextFrame, secondaryTransitionToNextFrame,
  292. secondaryBlendFactor);
  293. float4x4 previous = LerpBlend(gpuEcsAttachmentAnchorData, baseIndex,
  294. previousPrimaryFrameIndex, previousSecondaryFrameIndex,
  295. previousPrimaryTransitionToNextFrame, previousSecondaryTransitionToNextFrame,
  296. previousSecondaryBlendFactor);
  297. gpuEcsCurrentAttachmentAnchors[attachmentAnchorIndex] =
  298. new GpuEcsCurrentAttachmentAnchorBufferElement()
  299. {
  300. currentTransform = LerpTransform(previous, current, previousToCurrent)
  301. };
  302. }
  303. }
  304. }
  305. }
  306. private float4x4 LerpBlend(in List<GpuEcsAttachmentAnchorDataBufferElement> gpuEcsAttachmentAnchorData,
  307. int baseIndex, int frameIndexA, int frameIndexB,
  308. float frameIndexATransitionToNextFrame, float frameIndexBTransitionToNextFrame,
  309. float t)
  310. {
  311. float4x4 result;
  312. if (t == 0)
  313. result = LerpNextFrame(gpuEcsAttachmentAnchorData, baseIndex, frameIndexA,
  314. frameIndexATransitionToNextFrame);
  315. else if (t == 1f)
  316. result = LerpNextFrame(gpuEcsAttachmentAnchorData, baseIndex, frameIndexB,
  317. frameIndexBTransitionToNextFrame);
  318. else
  319. {
  320. float4x4 primary = LerpNextFrame(gpuEcsAttachmentAnchorData, baseIndex, frameIndexA,
  321. frameIndexATransitionToNextFrame);
  322. float4x4 secondary = LerpNextFrame(gpuEcsAttachmentAnchorData, baseIndex, frameIndexB,
  323. frameIndexBTransitionToNextFrame);
  324. result = LerpTransform(primary, secondary, t);
  325. }
  326. return result;
  327. }
  328. private float4x4 LerpNextFrame(in List<GpuEcsAttachmentAnchorDataBufferElement> gpuEcsAttachmentAnchorData,
  329. int baseIndex, int frameIndex, float transitionToNextFrame)
  330. {
  331. return LerpTransform(
  332. gpuEcsAttachmentAnchorData[baseIndex + frameIndex].anchorTransform,
  333. gpuEcsAttachmentAnchorData[baseIndex + frameIndex + 1].anchorTransform,
  334. transitionToNextFrame
  335. );
  336. }
  337. private float4x4 LerpTransform(float4x4 valueA, float4x4 valueB, float t)
  338. {
  339. float3 posA = new float3(valueA.c3.x, valueA.c3.y, valueA.c3.z);
  340. quaternion rotA = new quaternion(valueA);
  341. float3 posB = new float3(valueB.c3.x, valueB.c3.y, valueB.c3.z);
  342. quaternion rotB = new quaternion(valueB);
  343. float3 pos = math.lerp(posA, posB, t);
  344. Quaternion rot = math.slerp(rotA, rotB, t);
  345. return float4x4.TRS(pos, rot, new float3(1f, 1f, 1f));
  346. }
  347. private void UpdateAnimatorState(ref float normalizedTime, ref bool stopped,
  348. ref GpuEcsAnimatorEventBufferElement gpuEcsAnimatorEventBuffer,
  349. AnimatorInfo animatorInfo,
  350. in GpuEcsAnimatorControlStates controlState,
  351. in List<GpuEcsAnimationDataBufferElement> gpuEcsAnimationDataBuffer,
  352. in List<GpuEcsAnimationEventOccurenceBufferElement> gpuEcsAnimationEventOccurenceBuffer,
  353. out float primaryBlendFactor, out float primaryTransitionToNextFrame, out int primaryFrameIndex,
  354. out float secondaryBlendFactor, out float secondaryTransitionToNextFrame, out int secondaryFrameIndex,
  355. bool forPrevious)
  356. {
  357. GpuEcsAnimationDataBufferElement animationData = gpuEcsAnimationDataBuffer[animatorInfo.animationID];
  358. if (animationData.nbrOfInBetweenSamples == 1)
  359. {
  360. float blendSpeedAdjustment = 1f;
  361. UpdateAnimationNormalizedTime(ref normalizedTime, ref stopped, ref gpuEcsAnimatorEventBuffer, animatorInfo,
  362. controlState,
  363. gpuEcsAnimationEventOccurenceBuffer, animationData, blendSpeedAdjustment,
  364. out float transitionToNextFrame, out int relativeFrameIndex, forPrevious);
  365. primaryBlendFactor = 1;
  366. primaryTransitionToNextFrame = transitionToNextFrame;
  367. primaryFrameIndex = animationData.startFrameIndex + relativeFrameIndex;
  368. secondaryBlendFactor = 0;
  369. secondaryTransitionToNextFrame = 0;
  370. secondaryFrameIndex = 0;
  371. }
  372. else
  373. {
  374. float endBlend = (float)(animationData.nbrOfInBetweenSamples - 1);
  375. float currentBlendSetFloat = animatorInfo.blendFactor * endBlend;
  376. int currentBlendSet = (int)math.floor(currentBlendSetFloat);
  377. float transitionToNextSet = currentBlendSetFloat - (float)currentBlendSet;
  378. float blendSpeedAdjustment = animatorInfo.blendFactor * animationData.blendTimeCorrection +
  379. (1f - animatorInfo.blendFactor);
  380. UpdateAnimationNormalizedTime(ref normalizedTime, ref stopped, ref gpuEcsAnimatorEventBuffer, animatorInfo,
  381. controlState,
  382. gpuEcsAnimationEventOccurenceBuffer, animationData, blendSpeedAdjustment,
  383. out float transitionToNextFrame, out int relativeFrameIndex, forPrevious);
  384. primaryBlendFactor = 1f - transitionToNextSet;
  385. primaryTransitionToNextFrame = transitionToNextFrame;
  386. primaryFrameIndex = animationData.startFrameIndex + currentBlendSet * animationData.nbrOfFramesPerSample +
  387. relativeFrameIndex;
  388. secondaryBlendFactor = transitionToNextSet;
  389. secondaryTransitionToNextFrame = transitionToNextFrame;
  390. secondaryFrameIndex = animationData.startFrameIndex +
  391. (currentBlendSet + 1) * animationData.nbrOfFramesPerSample + relativeFrameIndex;
  392. }
  393. }
  394. private void UpdateAnimationNormalizedTime(ref float normalizedTime, ref bool stopped,
  395. ref GpuEcsAnimatorEventBufferElement gpuEcsAnimatorEventBuffer,
  396. AnimatorInfo animatorInfo, in GpuEcsAnimatorControlStates controlState,
  397. in List<GpuEcsAnimationEventOccurenceBufferElement> gpuEcsAnimationEventOccurenceBuffer,
  398. GpuEcsAnimationDataBufferElement animationData, float blendSpeedAdjustment,
  399. out float transitionToNextFrame, out int relativeFrameIndex, bool forPrevious)
  400. {
  401. int endFrame = animationData.nbrOfFramesPerSample - 1;
  402. float animationLength = (float)endFrame / GlobalConstants.SampleFrameRate;
  403. float currentTime = normalizedTime * animationLength;
  404. if (!stopped) currentTime += Time.deltaTime * animatorInfo.speedFactor * blendSpeedAdjustment;
  405. float normalizedTimeLastUpdate = normalizedTime;
  406. normalizedTime = currentTime / animationLength;
  407. for (int eventOccurencId = animationData.startEventOccurenceId;
  408. eventOccurencId < animationData.startEventOccurenceId + animationData.nbrOfEventOccurenceIds;
  409. eventOccurencId++)
  410. {
  411. GpuEcsAnimationEventOccurenceBufferElement occurence = gpuEcsAnimationEventOccurenceBuffer[eventOccurencId];
  412. if (normalizedTimeLastUpdate < occurence.eventNormalizedTime &&
  413. normalizedTime > occurence.eventNormalizedTime)
  414. {
  415. //Trigger event
  416. gpuEcsAnimatorEventBuffer = (new GpuEcsAnimatorEventBufferElement()
  417. {
  418. animationId = animatorInfo.animationID,
  419. eventId = occurence.eventId
  420. });
  421. }
  422. }
  423. if (!forPrevious && (animationData.loop || controlState == GpuEcsAnimatorControlStates.Start))
  424. {
  425. while (normalizedTime >= 1f) normalizedTime -= 1f;
  426. }
  427. else
  428. {
  429. if (normalizedTime >= 1f)
  430. {
  431. normalizedTime = 1f;
  432. stopped = true;
  433. }
  434. }
  435. if (normalizedTime == 1f)
  436. {
  437. relativeFrameIndex = endFrame - 1;
  438. transitionToNextFrame = 1f;
  439. }
  440. else
  441. {
  442. float relativeFrameIndexFloat = normalizedTime * (float)endFrame;
  443. relativeFrameIndex = (int)math.floor(relativeFrameIndexFloat);
  444. transitionToNextFrame = relativeFrameIndexFloat - (float)relativeFrameIndex;
  445. }
  446. }
  447. }