-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxtrixHelper
More file actions
110 lines (93 loc) · 3.41 KB
/
MaxtrixHelper
File metadata and controls
110 lines (93 loc) · 3.41 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using UnityEngine;
using System.Collections;
public class MatrixTest : MonoBehaviour
{
public Transform target;
public Vector3 relativePos;
public Matrix4x4 matrix = new Matrix4x4();
MatrixHelper maxtrixHelper = new MatrixHelper();
void Start()
{
}
void Update()
{
matrix.SetColumn(0, transform.right);
matrix.SetColumn(1, transform.up);
matrix.SetColumn(2, transform.forward);
Vector3 p = transform.position;
matrix.SetColumn(3, new Vector4(p.x, p.y, p.z, 1));
if (target)
{
maxtrixHelper.position = transform.position;
maxtrixHelper.rotation = transform.eulerAngles;
maxtrixHelper.Apply();
// relativePos = transform.worldToLocalMatrix.MultiplyPoint3x4(target.position);
relativePos = maxtrixHelper.worldToLocalMatrix.MultiplyPoint(target.position);
}
}
public class MatrixHelper
{
public Vector3 position;
public Vector3 rotation;
public Matrix4x4 worldToLocalMatrix;
public Matrix4x4 localToWorldMatrix;
public Matrix4x4 PositionMatrix
{
get
{
Matrix4x4 matrix = new Matrix4x4();
matrix.SetRow(0, new Vector4(1f, 0f, 0f, position.x));
matrix.SetRow(1, new Vector4(0f, 1f, 0f, position.y));
matrix.SetRow(2, new Vector4(0f, 0f, 1f, position.z));
matrix.SetRow(3, new Vector4(0f, 0f, 0f, 1f));
return matrix;
}
}
public Matrix4x4 RotationMatrix
{
get
{
float radX = rotation.x * Mathf.Deg2Rad;
float radY = rotation.y * Mathf.Deg2Rad;
float radZ = rotation.z * Mathf.Deg2Rad;
float sinX = Mathf.Sin(radX);
float cosX = Mathf.Cos(radX);
float sinY = Mathf.Sin(radY);
float cosY = Mathf.Cos(radY);
float sinZ = Mathf.Sin(radZ);
float cosZ = Mathf.Cos(radZ);
Matrix4x4 matrix = new Matrix4x4();
matrix.SetColumn(0, new Vector4(
cosY * cosZ,
cosX * sinZ + sinX * sinY * cosZ,
sinX * sinZ - cosX * sinY * cosZ,
0f
));
matrix.SetColumn(1, new Vector4(
-cosY * sinZ,
cosX * cosZ - sinX * sinY * sinZ,
sinX * cosZ + cosX * sinY * sinZ,
0f
));
matrix.SetColumn(2, new Vector4(
sinY,
-sinX * cosY,
cosX * cosY,
0f
));
matrix.SetColumn(3, new Vector4(0f, 0f, 0f, 1f));
return matrix;
}
}
public void Apply()
{
localToWorldMatrix =RotationMatrix*PositionMatrix;
worldToLocalMatrix = localToWorldMatrix.inverse;
}
}
}
//Codes from http://catlikecoding.com/unity/tutorials/rendering/part-1/
//也可以直接用Unity自带的这个简便来写
// Matrix4x4 matrix4X4=new Matrix4x4();
// matrix4X4.SetTRS(transform.position,Quaternion.LookRotation(transform.forward), Vector3.one);
// relativePos = matrix4X4.inverse.MultiplyPoint(target.position);