GpuEcsAnimatorBehaviour.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. using GpuEcsAnimationBaker.Engine.Data;
  3. using Unity.Collections;
  4. using Unity.Entities;
  5. using Unity.Mathematics;
  6. using UnityEngine;
  7. namespace GPUECSAnimationBaker.Engine.AnimatorSystem
  8. {
  9. public class GpuEcsAnimatorBehaviour : MonoBehaviour
  10. {
  11. public int totalNbrOfFrames;
  12. public int nbrOfAttachmentAnchors;
  13. public GpuEcsAnimationData[] animations;
  14. public GpuEcsAnimationEventOccurence[] animationEventOccurences;
  15. public TransformUsageFlags transformUsageFlags = TransformUsageFlags.Dynamic;
  16. public GpuEcsAttachmentAnchorData attachmentAnchorData;
  17. // public AnimationData[] BackAnimationDatas;
  18. }
  19. [Serializable]
  20. public struct GpuEcsAnimationEventOccurence
  21. {
  22. public float eventNormalizedTime;
  23. public int eventId;
  24. }
  25. public class GpuEcsAnimatorBaker : Baker<GpuEcsAnimatorBehaviour>
  26. {
  27. public override void Bake(GpuEcsAnimatorBehaviour authoring)
  28. {
  29. Entity entity = GetEntity(authoring.transformUsageFlags);
  30. AddComponent(entity, new GpuEcsAnimationDataComponent()
  31. {
  32. nbrOfAttachmentAnchors = authoring.nbrOfAttachmentAnchors,
  33. totalNbrOfFrames = authoring.totalNbrOfFrames
  34. });
  35. DynamicBuffer<GpuEcsAnimationDataBufferElement> gpuEcsAnimationDataBuffer = AddBuffer<GpuEcsAnimationDataBufferElement>(entity);
  36. for (int animationIndex = 0; animationIndex < authoring.animations.Length; animationIndex++)
  37. {
  38. GpuEcsAnimationData gpuEcsAnimationData = authoring.animations[animationIndex];
  39. gpuEcsAnimationDataBuffer.Add(new GpuEcsAnimationDataBufferElement()
  40. {
  41. startFrameIndex = gpuEcsAnimationData.startFrameIndex,
  42. nbrOfFramesPerSample = gpuEcsAnimationData.nbrOfFramesPerSample,
  43. nbrOfInBetweenSamples = gpuEcsAnimationData.nbrOfInBetweenSamples,
  44. blendTimeCorrection = gpuEcsAnimationData.blendTimeCorrection,
  45. startEventOccurenceId = gpuEcsAnimationData.startEventOccurenceId,
  46. nbrOfEventOccurenceIds = gpuEcsAnimationData.nbrOfEventOccurenceIds,
  47. loop = gpuEcsAnimationData.loop
  48. });
  49. }
  50. DynamicBuffer<GpuEcsAnimationEventOccurenceBufferElement> gpuEcsAnimationEventOccurenceBuffer = AddBuffer<GpuEcsAnimationEventOccurenceBufferElement>(entity);
  51. for (int animationEventOccurenceId = 0; animationEventOccurenceId < authoring.animationEventOccurences.Length; animationEventOccurenceId++)
  52. {
  53. GpuEcsAnimationEventOccurence occurence = authoring.animationEventOccurences[animationEventOccurenceId];
  54. gpuEcsAnimationEventOccurenceBuffer.Add(new GpuEcsAnimationEventOccurenceBufferElement()
  55. {
  56. eventNormalizedTime = occurence.eventNormalizedTime,
  57. eventId = occurence.eventId
  58. });
  59. }
  60. AddComponent(entity, new GpuEcsAnimatorShaderDataComponent()
  61. {
  62. shaderData = new float4x4(
  63. 1f, 0, 0, 0,
  64. 0, 0, 0, 0,
  65. 0, 0, 0, 0,
  66. 0, 0, 0, 0)
  67. });
  68. int initialAnimationID = 0;
  69. GpuEcsAnimatorInitializerBehaviour initializer = authoring.GetComponent<GpuEcsAnimatorInitializerBehaviour>();
  70. if (initializer != null) initialAnimationID = initializer.GetInitialAnimationID();
  71. AddComponent(entity, new GpuEcsAnimatorInitializedComponent()
  72. {
  73. initialized = false
  74. });
  75. AddComponent(entity, new GpuEcsAnimatorControlComponent()
  76. {
  77. animatorInfo = new AnimatorInfo()
  78. {
  79. animationID = initialAnimationID,
  80. blendFactor = 0,
  81. speedFactor = 1
  82. },
  83. transitionSpeed = 0,
  84. startNormalizedTime = 0
  85. });
  86. AddComponent(entity, new GpuEcsAnimatorControlStateComponent()
  87. {
  88. state = GpuEcsAnimatorControlStates.Start
  89. });
  90. AddComponent<GpuEcsAnimatorTransitionInfoComponent>(entity);
  91. AddComponent<GpuEcsAnimatorStateComponent>(entity);
  92. DynamicBuffer<GpuEcsAttachmentAnchorDataBufferElement> anchorDataBuffer = AddBuffer<GpuEcsAttachmentAnchorDataBufferElement>(entity);
  93. DynamicBuffer<GpuEcsCurrentAttachmentAnchorBufferElement> currentAnchorTransformBuffer = AddBuffer<GpuEcsCurrentAttachmentAnchorBufferElement>(entity);
  94. if (authoring.attachmentAnchorData != null && authoring.nbrOfAttachmentAnchors > 0)
  95. {
  96. int anchorDataLength = authoring.attachmentAnchorData.anchorTransforms.Length;
  97. NativeArray<GpuEcsAttachmentAnchorDataBufferElement> anchors = new NativeArray<GpuEcsAttachmentAnchorDataBufferElement>(anchorDataLength, Allocator.Temp);
  98. for (int i = 0; i < anchorDataLength; i++)
  99. anchors[i] = new GpuEcsAttachmentAnchorDataBufferElement() { anchorTransform = authoring.attachmentAnchorData.anchorTransforms[i] };
  100. anchorDataBuffer.AddRange(anchors);
  101. anchors.Dispose();
  102. NativeArray<GpuEcsCurrentAttachmentAnchorBufferElement> currentAnchorTransforms = new NativeArray<GpuEcsCurrentAttachmentAnchorBufferElement>(authoring.nbrOfAttachmentAnchors, Allocator.Temp);
  103. currentAnchorTransformBuffer.AddRange(currentAnchorTransforms);
  104. currentAnchorTransforms.Dispose();
  105. }
  106. AddBuffer<GpuEcsAnimatorEventBufferElement>(entity);
  107. }
  108. }
  109. }