forked from TouchScript/TouchScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransformUtils.cs
More file actions
38 lines (32 loc) · 1.25 KB
/
Copy pathTransformUtils.cs
File metadata and controls
38 lines (32 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
* @author Valentin Simonov / http://va.lent.in/
*/
using UnityEngine;
namespace TouchScript.Utils
{
internal static class TransformUtils
{
public static Vector3 GlobalToLocalPosition(Transform transform, Vector3 global)
{
if (transform.parent == null) return global;
return transform.parent.InverseTransformPoint(global);
}
public static Vector3 GlobalToLocalDirection(Transform transform, Vector3 global)
{
if (transform.parent == null) return global;
return transform.parent.InverseTransformDirection(global);
}
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;
}
}
}