-
Notifications
You must be signed in to change notification settings - Fork 68
/
frustum.py
35 lines (26 loc) · 1.08 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
from settings import *
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 sphere center
sphere_vec = chunk.center - self.cam.position
# 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
# outside the 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
# outside the 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