UpdateBoundsSystem.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #if ENABLE_ENTITIES
  2. using System.Collections.Generic;
  3. using Unity.Collections;
  4. using Unity.Entities;
  5. using Unity.Jobs;
  6. using Unity.Mathematics;
  7. using UnityEngine.Jobs;
  8. using UnityEngine.U2D.Common;
  9. using UnityEngine.Scripting;
  10. namespace UnityEngine.U2D.Animation
  11. {
  12. [Preserve]
  13. [UnityEngine.ExecuteAlways]
  14. [UpdateInGroup(typeof(PresentationSystemGroup))]
  15. [UpdateAfter(typeof(DeformSpriteSystem))]
  16. internal class UpdateBoundsSystem : ComponentSystem
  17. {
  18. EntityQuery m_ComponentGroup;
  19. protected override void OnCreateManager()
  20. {
  21. m_ComponentGroup = GetEntityQuery(typeof(SpriteSkin));
  22. }
  23. struct Bounds
  24. {
  25. public float4 center;
  26. public float4 extents;
  27. }
  28. struct CalculateBoundsJob : IJobParallelFor
  29. {
  30. [ReadOnly, DeallocateOnJobCompletion]
  31. public NativeArray<float4x4> worldToLocalArray;
  32. [ReadOnly, DeallocateOnJobCompletion]
  33. public NativeArray<float4x4> rootLocalToWorldArray;
  34. public NativeArray<Bounds> boundsArray;
  35. public void Execute(int i)
  36. {
  37. var matrix = math.mul(worldToLocalArray[i], rootLocalToWorldArray[i]);
  38. var center = boundsArray[i].center;
  39. var extents = boundsArray[i].extents;
  40. var p0 = math.mul(matrix, center + new float4(-extents.x, -extents.y, extents.z, extents.w));
  41. var p1 = math.mul(matrix, center + new float4(-extents.x, extents.y, extents.z, extents.w));
  42. var p2 = math.mul(matrix, center + extents);
  43. var p3 = math.mul(matrix, center + new float4(extents.x, -extents.y, extents.z, extents.w));
  44. var min = math.min(p0, math.min(p1, math.min(p2, p3)));
  45. var max = math.max(p0, math.max(p1, math.max(p2, p3)));
  46. extents = (max - min) * 0.5f;
  47. center = min + extents;
  48. boundsArray[i] = new Bounds()
  49. {
  50. center = center,
  51. extents = extents
  52. };
  53. }
  54. }
  55. protected override void OnUpdate()
  56. {
  57. var entityLength = m_ComponentGroup.CalculateLength();
  58. var worldToLocalArray = new NativeArray<float4x4>(entityLength, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
  59. var rootLocalToWorldArray = new NativeArray<float4x4>(entityLength, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
  60. var boundsArray = new NativeArray<Bounds>(entityLength, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
  61. var counter = 0;
  62. Entities.ForEach((Entity entity, SpriteSkin spriteSkin) =>
  63. {
  64. if (spriteSkin.isValid && spriteSkin.spriteRenderer.enabled)
  65. {
  66. worldToLocalArray[counter] = spriteSkin.transform.worldToLocalMatrix;
  67. rootLocalToWorldArray[counter] = spriteSkin.rootBone.localToWorldMatrix;
  68. var unityBounds = spriteSkin.bounds;
  69. boundsArray[counter] = new Bounds()
  70. {
  71. center = new float4(unityBounds.center, 1),
  72. extents = new float4(unityBounds.extents, 0),
  73. };
  74. }
  75. counter++;
  76. });
  77. var jobHandle = new CalculateBoundsJob()
  78. {
  79. worldToLocalArray = worldToLocalArray,
  80. rootLocalToWorldArray = rootLocalToWorldArray,
  81. boundsArray = boundsArray
  82. }.Schedule(entityLength, 32);
  83. jobHandle.Complete();
  84. counter = 0;
  85. Entities.With(m_ComponentGroup).ForEach((Entity entity, SpriteSkin spriteSkin) =>
  86. {
  87. if (spriteSkin.isValid && spriteSkin.spriteRenderer.enabled)
  88. {
  89. var center = boundsArray[counter].center;
  90. var extents = boundsArray[counter].extents;
  91. var bounds = new UnityEngine.Bounds();
  92. bounds.center = new Vector3(center.x, center.y, center.z);
  93. bounds.extents = new Vector3(extents.x, extents.y, extents.z);
  94. InternalEngineBridge.SetLocalAABB(spriteSkin.spriteRenderer, bounds);
  95. }
  96. counter++;
  97. });
  98. boundsArray.Dispose();
  99. return;
  100. }
  101. }
  102. }
  103. #endif