DeformSpriteSystem.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #if ENABLE_ENTITIES
  2. using System.Collections.Generic;
  3. using Unity.Entities;
  4. using Unity.Mathematics;
  5. using Unity.Burst;
  6. using Unity.Collections;
  7. using Unity.Jobs;
  8. using UnityEngine.Scripting;
  9. namespace UnityEngine.U2D.Animation
  10. {
  11. [Preserve]
  12. [UnityEngine.ExecuteAlways]
  13. [UpdateInGroup(typeof(PresentationSystemGroup))]
  14. [UpdateAfter(typeof(PrepareSkinningSystem))]
  15. class DeformSpriteSystem : JobComponentSystem
  16. {
  17. List<SpriteComponent> m_UniqueSpriteComponents = new List<SpriteComponent>(16);
  18. EntityQuery m_ComponentGroup;
  19. protected override void OnCreateManager()
  20. {
  21. m_ComponentGroup = GetEntityQuery(typeof(WorldToLocal), typeof(SpriteComponent), typeof(Vertex), typeof(BoneTransform));
  22. }
  23. [BurstCompile]
  24. public struct SkinJob : IJobParallelFor
  25. {
  26. // these are readonly per sprite and shared
  27. [ReadOnly]
  28. [DeallocateOnJobCompletion]
  29. public NativeArray<Entity> entities;
  30. [ReadOnly]
  31. public NativeSlice<float3> vertices;
  32. [ReadOnly]
  33. public NativeSlice<BoneWeight> boneWeights;
  34. [ReadOnly]
  35. public NativeSlice<float4x4> bindPoses;
  36. [ReadOnly]
  37. [DeallocateOnJobCompletion]
  38. public NativeArray<WorldToLocal> localToWorldArray;
  39. // these are calculated per renderer and per instance
  40. [NativeDisableParallelForRestriction]
  41. public BufferFromEntity<BoneTransform> boneTransformArray;
  42. [NativeDisableParallelForRestriction]
  43. public BufferFromEntity<Vertex> deformedArray;
  44. public void Execute(int i)
  45. {
  46. var rootTransformId = localToWorldArray[i].Value;
  47. var boneTransforms = boneTransformArray[entities[i]].Reinterpret<float4x4>().AsNativeArray();
  48. var deformableVertices = deformedArray[entities[i]].Reinterpret<float3>().AsNativeArray();
  49. SpriteSkinUtility.Deform(rootTransformId, vertices, boneWeights, boneTransforms, bindPoses, deformableVertices);
  50. }
  51. }
  52. protected override JobHandle OnUpdate(JobHandle inputDeps)
  53. {
  54. m_UniqueSpriteComponents.Clear();
  55. EntityManager.GetAllUniqueSharedComponentData(m_UniqueSpriteComponents);
  56. var spriteComponentCount = m_UniqueSpriteComponents.Count;
  57. var returnHandle = inputDeps;
  58. for (var i = 0; i < spriteComponentCount; i++)
  59. {
  60. var spriteComponent = m_UniqueSpriteComponents[i];
  61. var sprite = spriteComponent.Value;
  62. var entityCount = 0;
  63. if (sprite != null)
  64. {
  65. m_ComponentGroup.SetFilter(spriteComponent);
  66. var filteredEntities = m_ComponentGroup.ToEntityArray(Allocator.TempJob);
  67. entityCount = filteredEntities.Length;
  68. if (entityCount > 0)
  69. {
  70. var skinJob = new SkinJob
  71. {
  72. entities = filteredEntities,
  73. vertices = sprite.GetVertexAttribute<Vector3>(UnityEngine.Rendering.VertexAttribute.Position).SliceWithStride<float3>(),
  74. boneWeights = sprite.GetVertexAttribute<BoneWeight>(UnityEngine.Rendering.VertexAttribute.BlendWeight),
  75. bindPoses = new NativeSlice<Matrix4x4>(sprite.GetBindPoses()).SliceWithStride<float4x4>(),
  76. localToWorldArray = m_ComponentGroup.ToComponentDataArray<WorldToLocal>(Allocator.TempJob),
  77. boneTransformArray = GetBufferFromEntity<BoneTransform>(),
  78. deformedArray = GetBufferFromEntity<Vertex>()
  79. };
  80. returnHandle = skinJob.Schedule(entityCount, 4, returnHandle);
  81. }
  82. else
  83. filteredEntities.Dispose();
  84. }
  85. }
  86. var system = World.GetOrCreateSystem<EndPresentationEntityCommandBufferSystem>();
  87. system.AddJobHandleForProducer(returnHandle);
  88. return returnHandle;
  89. }
  90. }
  91. }
  92. #endif