-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcamera_matrix_util.gd
172 lines (127 loc) · 4.65 KB
/
camera_matrix_util.gd
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
tool
var matrix: PoolRealArray = PoolRealArray(
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
)
func set_identity() -> void:
matrix = PoolRealArray(
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
)
static func get_fovy(p_fovx: float, p_aspect: float) -> float:
return rad2deg(atan(p_aspect * tan(deg2rad(p_fovx) * 0.5)) * 2.0)
func get_endpoints() -> PoolVector3Array:
# Near Plane
var near_plane: Plane = Plane(matrix[3] + matrix[2], matrix[7] + matrix[6], matrix[11] + matrix[10], -matrix[15] - matrix[14]).normalized()
# Far Plane
var far_plane: Plane = Plane(matrix[2] - matrix[3], matrix[6] - matrix[7], matrix[10] - matrix[11], matrix[15] - matrix[14]).normalized()
# Right Plane
var right_plane: Plane = Plane(matrix[0] - matrix[3], matrix[4] - matrix[7], matrix[8] - matrix[11], -matrix[15] + matrix[12]).normalized()
# Top Plane
var top_plane: Plane = Plane(matrix[1] - matrix[3], matrix[5] - matrix[7], matrix[9] - matrix[11], -matrix[15] + matrix[13]).normalized()
var near_endpoint: Vector3 = near_plane.intersect_3(right_plane, top_plane)
var far_endpoint: Vector3 = far_plane.intersect_3(right_plane, top_plane)
var points_8: PoolVector3Array = PoolVector3Array()
points_8.push_back(Vector3(near_endpoint.x, near_endpoint.y, near_endpoint.z))
points_8.push_back(Vector3(near_endpoint.x, -near_endpoint.y, near_endpoint.z))
points_8.push_back(Vector3(-near_endpoint.x, near_endpoint.y, near_endpoint.z))
points_8.push_back(Vector3(-near_endpoint.x, -near_endpoint.y, near_endpoint.z))
points_8.push_back(Vector3(far_endpoint.x, far_endpoint.y, far_endpoint.z))
points_8.push_back(Vector3(far_endpoint.x, -far_endpoint.y, far_endpoint.z))
points_8.push_back(Vector3(-far_endpoint.x, far_endpoint.y, far_endpoint.z))
points_8.push_back(Vector3(-far_endpoint.x, -far_endpoint.y, far_endpoint.z))
return points_8
func get_projection_planes(p_transform: Transform) -> Array:
var planes: Array = []
var new_plane: Plane
# Near Plane
new_plane = Plane(
matrix[3] + matrix[2],
matrix[7] + matrix[6],
matrix[11] + matrix[10],
matrix[15] + matrix[14]
)
new_plane.normal = -new_plane.normal
new_plane = new_plane.normalized()
planes.push_back(p_transform.xform(new_plane))
# Far Plane
new_plane = Plane(
matrix[3] - matrix[2],
matrix[7] - matrix[6],
matrix[11] - matrix[10],
matrix[15] - matrix[14]
)
new_plane.normal = -new_plane.normal
new_plane = new_plane.normalized()
planes.push_back(p_transform.xform(new_plane))
# Left Plane
new_plane = Plane(
matrix[3] + matrix[0],
matrix[7] + matrix[4],
matrix[11] + matrix[8],
matrix[15] + matrix[12]
)
new_plane.normal = -new_plane.normal
new_plane = new_plane.normalized()
planes.push_back(p_transform.xform(new_plane))
# Top Plane
new_plane = Plane(
matrix[3] - matrix[1],
matrix[7] - matrix[5],
matrix[11] - matrix[9],
matrix[15] - matrix[13]
)
new_plane.normal = -new_plane.normal
new_plane = new_plane.normalized()
planes.push_back(p_transform.xform(new_plane))
# Right Plane
new_plane = Plane(
matrix[3] - matrix[0],
matrix[7] - matrix[4],
matrix[11] - matrix[8],
matrix[15] - matrix[12]
)
new_plane.normal = -new_plane.normal
new_plane = new_plane.normalized()
planes.push_back(p_transform.xform(new_plane))
# Bottom Plane
new_plane = Plane(
matrix[3] + matrix[1],
matrix[7] + matrix[5],
matrix[11] + matrix[9],
matrix[15] + matrix[13]
)
new_plane.normal = -new_plane.normal
new_plane = new_plane.normalized()
planes.push_back(p_transform.xform(new_plane))
return planes
func set_perspective(
p_fovy_degrees: float, p_aspect: float, p_z_near: float, p_z_far: float, p_flip_fov: float
) -> void:
if p_flip_fov:
p_fovy_degrees = get_fovy(p_fovy_degrees, 1.0 / p_aspect)
var sine: float
var cotangent: float
var delta_z: float
var radians: float = p_fovy_degrees / 2.0 * PI / 180.0
delta_z = p_z_far - p_z_near
sine = sin(radians)
if (delta_z == 0) || (sine == 0) || (p_aspect == 0):
return
cotangent = cos(radians) / sine
set_identity()
matrix[0] = cotangent / p_aspect
matrix[5] = cotangent
matrix[10] = -(p_z_far + p_z_near) / delta_z
matrix[11] = -1
matrix[14] = -2 * p_z_near * p_z_far / delta_z
matrix[15] = 0
func set_orthogonal(
p_left: float, p_right: float, p_bottom: float, p_top: float, p_znear: float, p_zfar: float
) -> void:
set_identity()
matrix[0] = 2.0 / (p_right - p_left)
matrix[12] = -((p_right + p_left) / (p_right - p_left))
matrix[5] = 2.0 / (p_top - p_bottom)
matrix[13] = -((p_top + p_bottom) / (p_top - p_bottom))
matrix[10] = -2.0 / (p_zfar - p_znear)
matrix[14] = -((p_zfar + p_znear) / (p_zfar - p_znear))
matrix[15] = 1.0