-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrustum.py
43 lines (30 loc) · 1.03 KB
/
frustum.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
37
38
39
40
41
42
43
from settings import *
"""
Allows only chunks in player's
frustum (FOV) to be rendered
"""
class Frustum:
def __init__(self, camera):
self.cam: Camera = camera
self.factor_y = 1.0 / math.cos(half_y := V_FOV * 0.5)
self.tan_y = math.tan(half_y)
self.factor_x = 1.0 / math.cos(half_x := H_FOV * 0.5)
self.tan_x = math.tan(half_x)
def is_on_frustum(self, chunk):
# vector to center of sphere
sphere_vec = chunk.center - self.cam.position
# check if outside the NEAR and FAR planes
sz = glm.dot(sphere_vec, self.cam.forward)
if not (NEAR - CHUNK_SPHERE_RADIUS <= sz <= FAR + CHUNK_SPHERE_RADIUS):
return False
# check if outside TOP and BOTTOM planes
sy = glm.dot(sphere_vec, self.cam.up)
dist = self.factor_y * CHUNK_SPHERE_RADIUS + sz * self.tan_y
if not (-dist <= sy <= dist):
return False
# check if outside LEFT and RIGHT planes
sx = glm.dot(sphere_vec, self.cam.right)
dist = self.factor_x * CHUNK_SPHERE_RADIUS + sz * self.tan_x
if not (-dist <= sx <= dist):
return False
return True