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