-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChunk.py
More file actions
66 lines (53 loc) · 1.99 KB
/
Chunk.py
File metadata and controls
66 lines (53 loc) · 1.99 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
from Constants import *
from Meshes.Chunk_Mesh import ChunkMesh
import random
from Terrain import *
class Chunk:
def __init__(self, world, position):
self.app = world.app
self.world = world
self.position = position
self.m_model = self.get_model_matrix()
self.blocks: np.array = None
self.mesh: ChunkMesh = None
self.transparent_mesh: ChunkMesh = None
self.is_empty = True
self.center = (glm.vec3(self.position) + 0.5) * CHUNK_SIZE
self.is_on_frustum = self.app.player.frustum.is_on_frustum
def get_model_matrix(self):
m_model = glm.translate(glm.mat4(), glm.vec3(self.position) * CHUNK_SIZE)
return m_model
def set_uniform(self):
self.mesh.program['m_model'].write(self.m_model)
def build_mesh(self):
self.mesh = ChunkMesh(self)
self.transparent_mesh = ChunkMesh(self, transparent=True)
def render(self):
if not self.is_empty and self.is_on_frustum(self):
self.set_uniform()
self.mesh.render()
def render_transparent(self):
if not self.is_empty and self.is_on_frustum(self):
self.set_uniform()
self.transparent_mesh.render()
def build_blocks(self):
# empty chunk
blocks = np.zeros(CHUNK_VOL, dtype='uint8')
# fill chunk
cx, cy, cz = glm.ivec3(self.position) * CHUNK_SIZE
self.generate_terrain(blocks, cx, cy, cz)
if np.any(blocks):
self.is_empty = False
return blocks
@staticmethod
@njit
def generate_terrain(blocks, cx, cy, cz):
for x in range(CHUNK_SIZE):
for z in range(CHUNK_SIZE):
wx = x + cx
wz = z + cz
world_height = get_height(wx, wz)
local_height = min(world_height - cy, CHUNK_SIZE)
for y in range(local_height):
wy = y + cy
set_block_id(blocks, x, y, z, wx, wy, wz, world_height)