forked from vakdev/VakScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworld_to_screen.py
36 lines (30 loc) · 1.66 KB
/
world_to_screen.py
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
#ext
from pyMeow import r_bytes
from numpy import matmul, array, frombuffer, float32
#own
from data import Offsets
class World:
def __init__(self, process, base_address, width, height):
self.process = process
self.base_address = base_address
self.width = width
self.height = height
self.view_proj_matrix = Offsets.view_proj_matrix
def get_view_proj_matrix(self):
data = r_bytes(self.process, self.base_address + self.view_proj_matrix, 0x128)
view_matrix = frombuffer(data[:64], dtype=float32).reshape(4, 4)
proj_matrix = frombuffer(data[64:128], dtype=float32).reshape(4, 4)
view_proj_matrix = matmul(view_matrix, proj_matrix)
return view_proj_matrix
def world_to_screen(self, view_proj_matrix, x, y, z):
clip_coords = matmul(array([x, y, z, 1.0]), view_proj_matrix.reshape(4, 4))
if clip_coords[3] <= 0.: clip_coords[3] = 0.1
clip_coords /= clip_coords[3]
return (self.width / 2.0 * clip_coords[0]) + (clip_coords[0] + self.width / 2.0), -(self.height / 2.0 * clip_coords[1]) + (clip_coords[1] + self.height / 2.0)
def world_to_screen_limited(self, view_proj_matrix, x, y, z):
clip_coords = matmul(array([x, y, z, 1.0]), view_proj_matrix.reshape(4, 4))
if clip_coords[3] <= 0.: clip_coords[3] = 0.1
clip_coords /= clip_coords[3]
out_x, out_y = (self.width / 2.0 * clip_coords[0]) + (clip_coords[0] + self.width / 2.0), -(self.height / 2.0 * clip_coords[1]) + (clip_coords[1] + self.height / 2.0)
if 0 <= out_x <= self.width and 0 <= out_y <= self.height:
return out_x, out_y