/*
* @author Valentin Simonov / http://va.lent.in/
*/
using System.Text;
using UnityEngine;
namespace TouchScript.Utils
{
///
/// Utility methods to work with Transforms.
///
public static class TransformUtils
{
private static StringBuilder sb;
///
/// Converts a global position of a transform to local position in its parent's coordinate system.
///
/// The transform.
/// The global position.
/// Local position in transform parent's coordinate system.
public static Vector3 GlobalToLocalPosition(Transform transform, Vector3 global)
{
if (transform.parent == null) return global;
return transform.parent.InverseTransformPoint(global);
}
///
/// Converts a global direction of a transform to local direction in its parent's coordinate system.
///
/// The transform.
/// The global direction.
/// Local direction in transform parent's coordinate system.
public static Vector3 GlobalToLocalDirection(Transform transform, Vector3 global)
{
if (transform.parent == null) return global;
return transform.parent.InverseTransformDirection(global);
}
///
/// Converts a global vector of a transform to local vector in its parent's coordinate system. The difference from is that this vector has length.
///
/// The transform.
/// The global vector.
/// Local vector in transform parent's coordinate system.
public static Vector3 GlobalToLocalVector(Transform transform, Vector3 global)
{
var parent = transform.parent;
if (parent == null) return global;
var scale = parent.localScale;
var vector = GlobalToLocalVector(parent, global);
vector = Quaternion.Inverse(parent.localRotation) * vector;
vector.x = Mathf.Approximately(scale.x, 0) ? 0 : vector.x / scale.x;
vector.y = Mathf.Approximately(scale.y, 0) ? 0 : vector.y / scale.y;
vector.z = Mathf.Approximately(scale.z, 0) ? 0 : vector.z / scale.z;
return vector;
}
///
/// Returns the string path of the transform in the hierarchy, i.g. "GameObject/ChildGameObject".
///
/// The transform.
/// The path in the hierarchy.
public static string GetHeirarchyPath(Transform transform)
{
initStringBuilder();
if (transform == null) return null;
while (transform != null)
{
sb.Insert(0, transform.name);
sb.Insert(0, "/");
transform = transform.parent;
}
return sb.ToString();
}
private static void initStringBuilder()
{
if (sb == null) sb = new StringBuilder();
sb.Length = 0;
}
}
}