OffsetOnTransform.cs
1.42 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System;
using UnityEngine;
namespace VRM
{
[Serializable]
public struct OffsetOnTransform
{
public Transform Transform;
public Matrix4x4 OffsetRotation;
public Matrix4x4 WorldMatrix
{
get
{
if (Transform == null) return Matrix4x4.identity;
return Transform.localToWorldMatrix * OffsetRotation;
}
}
public Vector3 WorldForward
{
get
{
var m = WorldMatrix;
return m.GetColumn(2); // zaxis
}
}
Matrix4x4 m_initialLocalMatrix;
public void Setup()
{
if (Transform == null) return;
m_initialLocalMatrix = Transform.parent.worldToLocalMatrix * Transform.localToWorldMatrix;
}
public Matrix4x4 InitialWorldMatrix
{
get
{
return Transform.parent.localToWorldMatrix * m_initialLocalMatrix;
}
}
public static OffsetOnTransform Create(Transform transform)
{
var coordinate = new OffsetOnTransform
{
Transform = transform
};
if (transform != null)
{
coordinate.OffsetRotation = transform.worldToLocalMatrix.RotationToWorldAxis();
}
return coordinate;
}
}
}