ModuleUtility.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using UnityEngine;
  2. using System;
  3. using System.Collections.Generic;
  4. namespace UnityEditor.U2D.Animation
  5. {
  6. internal static class ModuleUtility
  7. {
  8. public static Vector3 GUIToWorld(Vector3 guiPosition)
  9. {
  10. return GUIToWorld(guiPosition, Vector3.forward, Vector3.zero);
  11. }
  12. public static Vector3 GUIToWorld(Vector3 guiPosition, Vector3 planeNormal, Vector3 planePos)
  13. {
  14. Vector3 worldPos = Handles.inverseMatrix.MultiplyPoint(guiPosition);
  15. if (Camera.current)
  16. {
  17. Ray ray = HandleUtility.GUIPointToWorldRay(guiPosition);
  18. planeNormal = Handles.matrix.MultiplyVector(planeNormal);
  19. planePos = Handles.matrix.MultiplyPoint(planePos);
  20. Plane plane = new Plane(planeNormal, planePos);
  21. float distance = 0f;
  22. if (plane.Raycast(ray, out distance))
  23. {
  24. worldPos = Handles.inverseMatrix.MultiplyPoint(ray.GetPoint(distance));
  25. }
  26. }
  27. return worldPos;
  28. }
  29. public static GUIContent[] ToGUIContentArray(string[] names)
  30. {
  31. return Array.ConvertAll(names, n => new GUIContent(n));
  32. }
  33. public static Color CalculateNiceColor(int index, int numColors)
  34. {
  35. numColors = Mathf.Clamp(numColors, 1, int.MaxValue);
  36. int loops = index / numColors;
  37. index = index % 360;
  38. int hueAngleStep = 360 / numColors;
  39. float hueLoopOffset = hueAngleStep * 0.5f;
  40. float hue = index * hueAngleStep + loops * hueLoopOffset;
  41. return Color.HSVToRGB(Mathf.Repeat(hue, 360f) / 360f, 1f, 1f);
  42. }
  43. public static void UpdateLocalToWorldMatrices(List<SpriteBoneData> spriteBoneDataList, Matrix4x4 rootMatrix, ref Matrix4x4[] localToWorldMatrices)
  44. {
  45. if (localToWorldMatrices == null || localToWorldMatrices.Length != spriteBoneDataList.Count)
  46. localToWorldMatrices = new Matrix4x4[spriteBoneDataList.Count];
  47. bool[] calculatedMatrix = new bool[spriteBoneDataList.Count];
  48. var processedBoneCount = 0;
  49. while (processedBoneCount < spriteBoneDataList.Count)
  50. {
  51. int oldCount = processedBoneCount;
  52. for (var i = 0; i < spriteBoneDataList.Count; ++i)
  53. {
  54. if (calculatedMatrix[i])
  55. continue;
  56. var sourceBone = spriteBoneDataList[i];
  57. if (sourceBone.parentId != -1 && !calculatedMatrix[sourceBone.parentId])
  58. continue;
  59. var localToWorldMatrix = Matrix4x4.identity;
  60. localToWorldMatrix.SetTRS(sourceBone.localPosition, sourceBone.localRotation, Vector3.one);
  61. if (sourceBone.parentId == -1)
  62. localToWorldMatrix = rootMatrix * localToWorldMatrix;
  63. else if (calculatedMatrix[sourceBone.parentId])
  64. localToWorldMatrix = localToWorldMatrices[sourceBone.parentId] * localToWorldMatrix;
  65. localToWorldMatrices[i] = localToWorldMatrix;
  66. calculatedMatrix[i] = true;
  67. processedBoneCount++;
  68. }
  69. if (oldCount == processedBoneCount)
  70. throw new ArgumentException("Invalid hierarchy detected");
  71. }
  72. }
  73. public static List<SpriteBoneData> CreateSpriteBoneData(UnityEngine.U2D.SpriteBone[] spriteBoneList, Matrix4x4 rootMatrix)
  74. {
  75. List<SpriteBoneData> spriteBoneDataList = new List<SpriteBoneData>(spriteBoneList.Length);
  76. foreach (var spriteBone in spriteBoneList)
  77. {
  78. spriteBoneDataList.Add(new SpriteBoneData()
  79. {
  80. name = spriteBone.name,
  81. parentId = spriteBone.parentId,
  82. localPosition = spriteBone.position,
  83. localRotation = spriteBone.rotation,
  84. depth = spriteBone.position.z,
  85. length = spriteBone.length
  86. });
  87. }
  88. Matrix4x4[] localToWorldMatrices = null;
  89. UpdateLocalToWorldMatrices(spriteBoneDataList, rootMatrix, ref localToWorldMatrices);
  90. for (int i = 0; i < spriteBoneDataList.Count; ++i)
  91. {
  92. var spriteBoneData = spriteBoneDataList[i];
  93. spriteBoneData.position = localToWorldMatrices[i].MultiplyPoint(Vector2.zero);
  94. spriteBoneData.endPosition = localToWorldMatrices[i].MultiplyPoint(Vector2.right * spriteBoneData.length);
  95. }
  96. return spriteBoneDataList;
  97. }
  98. }
  99. }