GpuEcsAnimatorBehaviour.cs 5.8 KB

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