using System; using System.Collections.Generic; namespace UnityEngine.Animations.Rigging { /// /// Utility functions for constraints. /// public static class ConstraintsUtils { /// /// Creates a list of Transforms all parented to one another in between two GameObjects. /// /// The root Transform. /// The tip Transform. /// public static Transform[] ExtractChain(Transform root, Transform tip) { if (!tip.IsChildOf(root)) return new Transform[0]{}; var chain = new List(); Transform tmp = tip; while (tmp != root) { chain.Add(tmp); tmp = tmp.parent; } chain.Add(root); chain.Reverse(); return chain.ToArray(); } /// /// Calculates the distances in between every Transforms in the specified Transform chain. /// /// The Transform chain. /// An array of distances. public static float[] ExtractLengths(Transform[] chain) { float[] lengths = new float[chain.Length]; lengths[0] = 0f; // Evaluate lengths as distance between each transform in the chain. for (int i = 1; i < chain.Length; ++i) { lengths[i] = chain[i].localPosition.magnitude; } return lengths; } /// /// Calculates the interpolant values for each Transform using distance as a measure /// such that first Transform is at 0 and last Transform is at 1. /// /// The Transform chain. /// An array of interpolants. public static float[] ExtractSteps(Transform[] chain) { float[] lengths = ExtractLengths(chain); float totalLength = 0f; Array.ForEach(lengths, (length) => totalLength += length); float[] steps = new float[lengths.Length]; // Evaluate weights and steps based on curve. float cumulativeLength = 0.0f; for (int i = 0; i < lengths.Length; ++i) { cumulativeLength += lengths[i]; float t = cumulativeLength / totalLength; steps[i] = t; } return steps; } /// /// Prepends RigConstraint data property to specified property name. /// /// Property name. /// Return a complete property name. public static string ConstructConstraintDataPropertyName(string property) { return "m_Data." + property; } /// /// Builds a unique property name for a custom property. /// /// Associated component. /// Property name. /// Returns a custom property name. public static string ConstructCustomPropertyName(Component component, string property) { return component.transform.GetInstanceID() + "/" + component.GetType() + "/" + property; } } }