basilisk-engine 0.1.35__py3-none-any.whl → 0.1.37__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of basilisk-engine might be problematic. Click here for more details.
- basilisk/__init__.py +26 -26
- basilisk/audio/sound.py +27 -27
- basilisk/bsk_assets/cube.obj +48 -48
- basilisk/collisions/broad/broad_aabb.py +102 -102
- basilisk/collisions/broad/broad_bvh.py +137 -137
- basilisk/collisions/collider.py +95 -95
- basilisk/collisions/collider_handler.py +225 -225
- basilisk/collisions/narrow/contact_manifold.py +95 -95
- basilisk/collisions/narrow/dataclasses.py +34 -34
- basilisk/collisions/narrow/deprecated.py +46 -46
- basilisk/collisions/narrow/epa.py +91 -91
- basilisk/collisions/narrow/gjk.py +66 -66
- basilisk/collisions/narrow/graham_scan.py +24 -24
- basilisk/collisions/narrow/helper.py +29 -29
- basilisk/collisions/narrow/line_intersections.py +106 -106
- basilisk/collisions/narrow/sutherland_hodgman.py +75 -75
- basilisk/config.py +54 -4
- basilisk/draw/draw.py +100 -100
- basilisk/draw/draw_handler.py +178 -175
- basilisk/draw/font_renderer.py +28 -28
- basilisk/engine.py +165 -165
- basilisk/generic/abstract_bvh.py +15 -15
- basilisk/generic/abstract_custom.py +133 -133
- basilisk/generic/collisions.py +70 -70
- basilisk/generic/input_validation.py +82 -74
- basilisk/generic/math.py +6 -6
- basilisk/generic/matrices.py +35 -35
- basilisk/generic/meshes.py +72 -72
- basilisk/generic/quat.py +142 -142
- basilisk/generic/quat_methods.py +7 -7
- basilisk/generic/raycast_result.py +26 -26
- basilisk/generic/vec3.py +143 -143
- basilisk/input_output/IO_handler.py +91 -91
- basilisk/input_output/clock.py +49 -49
- basilisk/input_output/keys.py +43 -43
- basilisk/input_output/mouse.py +90 -89
- basilisk/input_output/path.py +14 -14
- basilisk/mesh/cube.py +33 -33
- basilisk/mesh/mesh.py +233 -233
- basilisk/mesh/mesh_from_data.py +150 -150
- basilisk/mesh/model.py +271 -271
- basilisk/mesh/narrow_aabb.py +89 -89
- basilisk/mesh/narrow_bvh.py +91 -91
- basilisk/mesh/narrow_primative.py +23 -23
- basilisk/nodes/helper.py +28 -28
- basilisk/nodes/node.py +704 -695
- basilisk/nodes/node_handler.py +97 -97
- basilisk/particles/particle_handler.py +64 -64
- basilisk/particles/particle_renderer.py +93 -92
- basilisk/physics/impulse.py +112 -112
- basilisk/physics/physics_body.py +43 -43
- basilisk/physics/physics_engine.py +35 -35
- basilisk/render/batch.py +103 -103
- basilisk/render/bloom.py +108 -0
- basilisk/render/camera.py +260 -260
- basilisk/render/chunk.py +108 -106
- basilisk/render/chunk_handler.py +167 -165
- basilisk/render/frame.py +109 -95
- basilisk/render/framebuffer.py +203 -192
- basilisk/render/image.py +120 -120
- basilisk/render/image_handler.py +120 -120
- basilisk/render/light.py +96 -96
- basilisk/render/light_handler.py +58 -58
- basilisk/render/material.py +232 -221
- basilisk/render/material_handler.py +133 -133
- basilisk/render/post_process.py +139 -139
- basilisk/render/shader.py +134 -134
- basilisk/render/shader_handler.py +85 -83
- basilisk/render/sky.py +120 -120
- basilisk/scene.py +291 -290
- basilisk/shaders/batch.frag +289 -276
- basilisk/shaders/batch.vert +117 -115
- basilisk/shaders/bloom_downsample.frag +43 -0
- basilisk/shaders/bloom_frame.frag +25 -0
- basilisk/shaders/bloom_upsample.frag +34 -0
- basilisk/shaders/crt.frag +31 -31
- basilisk/shaders/draw.frag +25 -22
- basilisk/shaders/draw.vert +25 -25
- basilisk/shaders/filter.frag +22 -22
- basilisk/shaders/frame.frag +12 -12
- basilisk/shaders/frame.vert +13 -13
- basilisk/shaders/geometry.frag +10 -8
- basilisk/shaders/geometry.vert +41 -41
- basilisk/shaders/normal.frag +62 -59
- basilisk/shaders/normal.vert +96 -96
- basilisk/shaders/particle.frag +76 -71
- basilisk/shaders/particle.vert +86 -84
- basilisk/shaders/sky.frag +23 -9
- basilisk/shaders/sky.vert +13 -13
- basilisk_engine-0.1.37.dist-info/METADATA +89 -0
- basilisk_engine-0.1.37.dist-info/RECORD +110 -0
- {basilisk_engine-0.1.35.dist-info → basilisk_engine-0.1.37.dist-info}/WHEEL +1 -1
- basilisk/input/__init__.py +0 -0
- basilisk/input/mouse.py +0 -62
- basilisk/input/path.py +0 -14
- basilisk_engine-0.1.35.dist-info/METADATA +0 -45
- basilisk_engine-0.1.35.dist-info/RECORD +0 -109
- {basilisk_engine-0.1.35.dist-info → basilisk_engine-0.1.37.dist-info}/top_level.txt +0 -0
basilisk/render/chunk.py
CHANGED
|
@@ -1,107 +1,109 @@
|
|
|
1
|
-
from .batch import Batch
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
class Chunk():
|
|
5
|
-
chunk_handler: ...
|
|
6
|
-
"""Back refrence to the parent chunk handler"""
|
|
7
|
-
position: tuple
|
|
8
|
-
"""The position of the chunk. Used as a key in the chunk handler"""
|
|
9
|
-
batch: Batch
|
|
10
|
-
"""Batched mesh of the chunk"""
|
|
11
|
-
nodes: set
|
|
12
|
-
"""Set conaining references to all nodes in the chunk"""
|
|
13
|
-
static: bool
|
|
14
|
-
"""Type of node that the chunk recognizes"""
|
|
15
|
-
|
|
16
|
-
def __init__(self, chunk_handler, position: tuple, static: bool, shader=None) -> None:
|
|
17
|
-
"""
|
|
18
|
-
Basilisk chunk object.
|
|
19
|
-
Contains references to all nodes in the chunk.
|
|
20
|
-
Handles batching for its own nodes
|
|
21
|
-
"""
|
|
22
|
-
|
|
23
|
-
# Back references
|
|
24
|
-
self.chunk_handler = chunk_handler
|
|
25
|
-
|
|
26
|
-
# Chunk Attrbiutes
|
|
27
|
-
self.position = position
|
|
28
|
-
self.static = static
|
|
29
|
-
self.shader = shader
|
|
30
|
-
|
|
31
|
-
# Create empty batch
|
|
32
|
-
self.batch = Batch(self)
|
|
33
|
-
|
|
34
|
-
# Create empty set for chunk's nodes
|
|
35
|
-
self.nodes = set()
|
|
36
|
-
|
|
37
|
-
def render(self) -> None:
|
|
38
|
-
"""
|
|
39
|
-
Renders the chunk mesh
|
|
40
|
-
"""
|
|
41
|
-
|
|
42
|
-
if self.batch.vao: self.batch.vao.render()
|
|
43
|
-
|
|
44
|
-
def update(self) -> bool:
|
|
45
|
-
"""
|
|
46
|
-
Batches all the node meshes in the chunk
|
|
47
|
-
"""
|
|
48
|
-
|
|
49
|
-
# Check if there are no nodes in the chunk
|
|
50
|
-
if not self.nodes: return False
|
|
51
|
-
# Batch the chunk nodes, return success bit
|
|
52
|
-
return self.batch.batch()
|
|
53
|
-
|
|
54
|
-
def node_update_callback(self, node):
|
|
55
|
-
if not self.batch.vbo: return
|
|
56
|
-
|
|
57
|
-
data = node.get_data()
|
|
58
|
-
self.batch.vbo.write(data, node.data_index * data.shape[1] * 4)
|
|
59
|
-
|
|
60
|
-
def add(self, node):
|
|
61
|
-
"""
|
|
62
|
-
Adds an existing node to the chunk. Updates the node's chunk reference
|
|
63
|
-
"""
|
|
64
|
-
|
|
65
|
-
self.nodes.add(node)
|
|
66
|
-
node.chunk = self
|
|
67
|
-
|
|
68
|
-
return node
|
|
69
|
-
|
|
70
|
-
def remove(self, node):
|
|
71
|
-
"""
|
|
72
|
-
Removes a node from the chunk
|
|
73
|
-
"""
|
|
74
|
-
|
|
75
|
-
if node == None: return
|
|
76
|
-
|
|
77
|
-
self.nodes.remove(node)
|
|
78
|
-
if self.batch and self.batch.vbo:
|
|
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
|
-
|
|
1
|
+
from .batch import Batch
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class Chunk():
|
|
5
|
+
chunk_handler: ...
|
|
6
|
+
"""Back refrence to the parent chunk handler"""
|
|
7
|
+
position: tuple
|
|
8
|
+
"""The position of the chunk. Used as a key in the chunk handler"""
|
|
9
|
+
batch: Batch
|
|
10
|
+
"""Batched mesh of the chunk"""
|
|
11
|
+
nodes: set
|
|
12
|
+
"""Set conaining references to all nodes in the chunk"""
|
|
13
|
+
static: bool
|
|
14
|
+
"""Type of node that the chunk recognizes"""
|
|
15
|
+
|
|
16
|
+
def __init__(self, chunk_handler, position: tuple, static: bool, shader=None) -> None:
|
|
17
|
+
"""
|
|
18
|
+
Basilisk chunk object.
|
|
19
|
+
Contains references to all nodes in the chunk.
|
|
20
|
+
Handles batching for its own nodes
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
# Back references
|
|
24
|
+
self.chunk_handler = chunk_handler
|
|
25
|
+
|
|
26
|
+
# Chunk Attrbiutes
|
|
27
|
+
self.position = position
|
|
28
|
+
self.static = static
|
|
29
|
+
self.shader = shader
|
|
30
|
+
|
|
31
|
+
# Create empty batch
|
|
32
|
+
self.batch = Batch(self)
|
|
33
|
+
|
|
34
|
+
# Create empty set for chunk's nodes
|
|
35
|
+
self.nodes = set()
|
|
36
|
+
|
|
37
|
+
def render(self) -> None:
|
|
38
|
+
"""
|
|
39
|
+
Renders the chunk mesh
|
|
40
|
+
"""
|
|
41
|
+
|
|
42
|
+
if self.batch.vao: self.batch.vao.render()
|
|
43
|
+
|
|
44
|
+
def update(self) -> bool:
|
|
45
|
+
"""
|
|
46
|
+
Batches all the node meshes in the chunk
|
|
47
|
+
"""
|
|
48
|
+
|
|
49
|
+
# Check if there are no nodes in the chunk
|
|
50
|
+
if not self.nodes: return False
|
|
51
|
+
# Batch the chunk nodes, return success bit
|
|
52
|
+
return self.batch.batch()
|
|
53
|
+
|
|
54
|
+
def node_update_callback(self, node):
|
|
55
|
+
if not self.batch.vbo: return
|
|
56
|
+
|
|
57
|
+
data = node.get_data()
|
|
58
|
+
self.batch.vbo.write(data, node.data_index * data.shape[1] * 4)
|
|
59
|
+
|
|
60
|
+
def add(self, node):
|
|
61
|
+
"""
|
|
62
|
+
Adds an existing node to the chunk. Updates the node's chunk reference
|
|
63
|
+
"""
|
|
64
|
+
|
|
65
|
+
self.nodes.add(node)
|
|
66
|
+
node.chunk = self
|
|
67
|
+
|
|
68
|
+
return node
|
|
69
|
+
|
|
70
|
+
def remove(self, node):
|
|
71
|
+
"""
|
|
72
|
+
Removes a node from the chunk
|
|
73
|
+
"""
|
|
74
|
+
|
|
75
|
+
if node == None: return
|
|
76
|
+
|
|
77
|
+
self.nodes.remove(node)
|
|
78
|
+
if self.batch and self.batch.vbo:
|
|
79
|
+
self.batch.vbo.clear()
|
|
80
|
+
self.batch.batch()
|
|
81
|
+
|
|
82
|
+
return node
|
|
83
|
+
|
|
84
|
+
def get_shader(self):
|
|
85
|
+
"""
|
|
86
|
+
Gets the bsk.Shader of the chunks nodes' shader
|
|
87
|
+
"""
|
|
88
|
+
|
|
89
|
+
shader = self.shader
|
|
90
|
+
|
|
91
|
+
if shader: return shader
|
|
92
|
+
return self.chunk_handler.scene.shader
|
|
93
|
+
|
|
94
|
+
def swap_shader(self, shader):
|
|
95
|
+
"""
|
|
96
|
+
Swaps the batches shader to the given shader
|
|
97
|
+
"""
|
|
98
|
+
|
|
99
|
+
self.batch.swap_shader(shader)
|
|
100
|
+
|
|
101
|
+
def __repr__(self) -> str:
|
|
102
|
+
return f'<Basilisk Chunk | {self.position}, {len(self.nodes)} nodes, {"static" if self.static else "dynamic"}>'
|
|
103
|
+
|
|
104
|
+
def __del__(self) -> None:
|
|
105
|
+
"""
|
|
106
|
+
Deletes the batch if this chunk is deleted
|
|
107
|
+
"""
|
|
108
|
+
|
|
107
109
|
del self.batch
|
basilisk/render/chunk_handler.py
CHANGED
|
@@ -1,166 +1,168 @@
|
|
|
1
|
-
import numpy as np
|
|
2
|
-
import moderngl as mgl
|
|
3
|
-
from .chunk import Chunk
|
|
4
|
-
from ..nodes.node import Node
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
class ChunkHandler():
|
|
8
|
-
engine: ...
|
|
9
|
-
"""Back reference to the parent engine"""
|
|
10
|
-
scene: ...
|
|
11
|
-
"""Back reference to the parent scene"""
|
|
12
|
-
ctx: mgl.Context
|
|
13
|
-
"""Back reference to the parent context"""
|
|
14
|
-
program: mgl.Program
|
|
15
|
-
"""Reference to the shader program used by batches"""
|
|
16
|
-
chunks: list[dict]
|
|
17
|
-
"""List containing two dictionaries for dynamic and static chunks repsectivly"""
|
|
18
|
-
updated_chunks: set
|
|
19
|
-
"""Set containing recently updated chunks"""
|
|
20
|
-
|
|
21
|
-
def __init__(self, scene) -> None:
|
|
22
|
-
# Reference to the scene hadlers and variables
|
|
23
|
-
"""
|
|
24
|
-
Handles all the chunking of all the nodes in the scene
|
|
25
|
-
"""
|
|
26
|
-
|
|
27
|
-
# Back references
|
|
28
|
-
self.scene = scene
|
|
29
|
-
self.engine = scene.engine
|
|
30
|
-
self.ctx = scene.engine.ctx
|
|
31
|
-
self.program = scene.engine.shader.program
|
|
32
|
-
|
|
33
|
-
# List for the dynamic and static chunk dictionaries | [dyanmic: dict, static: dict]
|
|
34
|
-
self.shader_groups = {None : ({}, {})}
|
|
35
|
-
# self.chunks = [{}, {}]
|
|
36
|
-
self.updated_chunks = set()
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
def render(self) -> None:
|
|
40
|
-
"""
|
|
41
|
-
Renders all the chunk batches in the camera's range
|
|
42
|
-
Includes some view culling, but not frustum culling.
|
|
43
|
-
"""
|
|
44
|
-
|
|
45
|
-
# Gets a rectanglur prism of chunks in the cameras view
|
|
46
|
-
render_range_x, render_range_y, render_range_z = self.get_render_range()
|
|
47
|
-
|
|
48
|
-
chunk_keys = [(x, y, z) for x in range(*render_range_x) for y in range(*render_range_y) for z in range(*render_range_z)]
|
|
49
|
-
|
|
50
|
-
# Loop through all chunks in view and render
|
|
51
|
-
for shader, group in self.shader_groups.items():
|
|
52
|
-
if shader == None: shader = self.engine.shader
|
|
53
|
-
for chunk in chunk_keys:
|
|
54
|
-
# Render the chunk if it exists
|
|
55
|
-
if chunk in group[0]: group[0][chunk].render()
|
|
56
|
-
if chunk in group[1]: group[1][chunk].render()
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
def update(self) -> None:
|
|
60
|
-
"""
|
|
61
|
-
Updates all the chunks that have been updated since the last frame.
|
|
62
|
-
"""
|
|
63
|
-
|
|
64
|
-
self.program = self.scene.engine.shader.program
|
|
65
|
-
|
|
66
|
-
# Loop through the set of updated chunk keys and update the chunk
|
|
67
|
-
removes = []
|
|
68
|
-
|
|
69
|
-
for chunk in self.updated_chunks:
|
|
70
|
-
if chunk.update(): continue
|
|
71
|
-
removes.append(chunk)
|
|
72
|
-
|
|
73
|
-
# Remove any empty chunks
|
|
74
|
-
for chunk in removes:
|
|
75
|
-
del self.shader_groups[chunk.shader][chunk.static][chunk.position]
|
|
76
|
-
|
|
77
|
-
# Clears the set of updated chunks so that they are not updated unless they are updated again
|
|
78
|
-
self.updated_chunks.clear()
|
|
79
|
-
|
|
80
|
-
def update_all(self):
|
|
81
|
-
self.program = self.scene.engine.shader.program
|
|
82
|
-
for shader in self.shader_groups.values():
|
|
83
|
-
for chunk in shader[0].values():
|
|
84
|
-
self.updated_chunks.add(chunk)
|
|
85
|
-
for chunk in shader[1].values():
|
|
86
|
-
self.updated_chunks.add(chunk)
|
|
87
|
-
|
|
88
|
-
def add(self, node: Node) -> Node:
|
|
89
|
-
"""
|
|
90
|
-
Adds an existing node to its chunk. Updates the node's chunk reference
|
|
91
|
-
"""
|
|
92
|
-
|
|
93
|
-
# The key of the chunk the node will be added to
|
|
94
|
-
chunk_size = self.engine.config.chunk_size
|
|
95
|
-
chunk_key = (int(node.x // chunk_size), int(node.y // chunk_size), int(node.z // chunk_size))
|
|
96
|
-
shader = node.shader
|
|
97
|
-
|
|
98
|
-
if shader not in self.shader_groups:
|
|
99
|
-
self.shader_groups[shader] = ({}, {})
|
|
100
|
-
|
|
101
|
-
# Ensure that the chunk exists
|
|
102
|
-
if chunk_key not in self.shader_groups[shader][node.static]:
|
|
103
|
-
chunk = Chunk(self, chunk_key, node.static, shader)
|
|
104
|
-
self.shader_groups[shader][node.static][chunk_key] = chunk
|
|
105
|
-
|
|
106
|
-
# Add the node to the chunk
|
|
107
|
-
self.shader_groups[shader][node.static][chunk_key].add(node)
|
|
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
|
-
for chunk in group[
|
|
1
|
+
import numpy as np
|
|
2
|
+
import moderngl as mgl
|
|
3
|
+
from .chunk import Chunk
|
|
4
|
+
from ..nodes.node import Node
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class ChunkHandler():
|
|
8
|
+
engine: ...
|
|
9
|
+
"""Back reference to the parent engine"""
|
|
10
|
+
scene: ...
|
|
11
|
+
"""Back reference to the parent scene"""
|
|
12
|
+
ctx: mgl.Context
|
|
13
|
+
"""Back reference to the parent context"""
|
|
14
|
+
program: mgl.Program
|
|
15
|
+
"""Reference to the shader program used by batches"""
|
|
16
|
+
chunks: list[dict]
|
|
17
|
+
"""List containing two dictionaries for dynamic and static chunks repsectivly"""
|
|
18
|
+
updated_chunks: set
|
|
19
|
+
"""Set containing recently updated chunks"""
|
|
20
|
+
|
|
21
|
+
def __init__(self, scene) -> None:
|
|
22
|
+
# Reference to the scene hadlers and variables
|
|
23
|
+
"""
|
|
24
|
+
Handles all the chunking of all the nodes in the scene
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
# Back references
|
|
28
|
+
self.scene = scene
|
|
29
|
+
self.engine = scene.engine
|
|
30
|
+
self.ctx = scene.engine.ctx
|
|
31
|
+
self.program = scene.engine.shader.program
|
|
32
|
+
|
|
33
|
+
# List for the dynamic and static chunk dictionaries | [dyanmic: dict, static: dict]
|
|
34
|
+
self.shader_groups = {None : ({}, {})}
|
|
35
|
+
# self.chunks = [{}, {}]
|
|
36
|
+
self.updated_chunks = set()
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def render(self) -> None:
|
|
40
|
+
"""
|
|
41
|
+
Renders all the chunk batches in the camera's range
|
|
42
|
+
Includes some view culling, but not frustum culling.
|
|
43
|
+
"""
|
|
44
|
+
|
|
45
|
+
# Gets a rectanglur prism of chunks in the cameras view
|
|
46
|
+
render_range_x, render_range_y, render_range_z = self.get_render_range()
|
|
47
|
+
|
|
48
|
+
chunk_keys = [(x, y, z) for x in range(*render_range_x) for y in range(*render_range_y) for z in range(*render_range_z)]
|
|
49
|
+
|
|
50
|
+
# Loop through all chunks in view and render
|
|
51
|
+
for shader, group in self.shader_groups.items():
|
|
52
|
+
if shader == None: shader = self.engine.shader
|
|
53
|
+
for chunk in chunk_keys:
|
|
54
|
+
# Render the chunk if it exists
|
|
55
|
+
if chunk in group[0]: group[0][chunk].render()
|
|
56
|
+
if chunk in group[1]: group[1][chunk].render()
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def update(self) -> None:
|
|
60
|
+
"""
|
|
61
|
+
Updates all the chunks that have been updated since the last frame.
|
|
62
|
+
"""
|
|
63
|
+
|
|
64
|
+
self.program = self.scene.engine.shader.program
|
|
65
|
+
|
|
66
|
+
# Loop through the set of updated chunk keys and update the chunk
|
|
67
|
+
removes = []
|
|
68
|
+
|
|
69
|
+
for chunk in self.updated_chunks:
|
|
70
|
+
if chunk.update(): continue
|
|
71
|
+
removes.append(chunk)
|
|
72
|
+
|
|
73
|
+
# Remove any empty chunks
|
|
74
|
+
for chunk in removes:
|
|
75
|
+
del self.shader_groups[chunk.shader][chunk.static][chunk.position]
|
|
76
|
+
|
|
77
|
+
# Clears the set of updated chunks so that they are not updated unless they are updated again
|
|
78
|
+
self.updated_chunks.clear()
|
|
79
|
+
|
|
80
|
+
def update_all(self):
|
|
81
|
+
self.program = self.scene.engine.shader.program
|
|
82
|
+
for shader in self.shader_groups.values():
|
|
83
|
+
for chunk in shader[0].values():
|
|
84
|
+
self.updated_chunks.add(chunk)
|
|
85
|
+
for chunk in shader[1].values():
|
|
86
|
+
self.updated_chunks.add(chunk)
|
|
87
|
+
|
|
88
|
+
def add(self, node: Node) -> Node:
|
|
89
|
+
"""
|
|
90
|
+
Adds an existing node to its chunk. Updates the node's chunk reference
|
|
91
|
+
"""
|
|
92
|
+
|
|
93
|
+
# The key of the chunk the node will be added to
|
|
94
|
+
chunk_size = self.engine.config.chunk_size
|
|
95
|
+
chunk_key = (int(node.x // chunk_size), int(node.y // chunk_size), int(node.z // chunk_size))
|
|
96
|
+
shader = node.shader
|
|
97
|
+
|
|
98
|
+
if shader not in self.shader_groups:
|
|
99
|
+
self.shader_groups[shader] = ({}, {})
|
|
100
|
+
|
|
101
|
+
# Ensure that the chunk exists
|
|
102
|
+
if chunk_key not in self.shader_groups[shader][node.static]:
|
|
103
|
+
chunk = Chunk(self, chunk_key, node.static, shader)
|
|
104
|
+
self.shader_groups[shader][node.static][chunk_key] = chunk
|
|
105
|
+
|
|
106
|
+
# Add the node to the chunk
|
|
107
|
+
self.shader_groups[shader][node.static][chunk_key].add(node)
|
|
108
|
+
|
|
109
|
+
node.chunk = self.shader_groups[shader][node.static][chunk_key]
|
|
110
|
+
|
|
111
|
+
# Update the chunk
|
|
112
|
+
self.updated_chunks.add(self.shader_groups[shader][node.static][chunk_key])
|
|
113
|
+
|
|
114
|
+
return Node
|
|
115
|
+
|
|
116
|
+
def remove(self, node: Node) -> None:
|
|
117
|
+
"""
|
|
118
|
+
Removes a node from the its chunk
|
|
119
|
+
"""
|
|
120
|
+
|
|
121
|
+
if node == None: return
|
|
122
|
+
|
|
123
|
+
# Remove the node
|
|
124
|
+
chunk = node.chunk
|
|
125
|
+
chunk.remove(node)
|
|
126
|
+
node.chunk = None
|
|
127
|
+
|
|
128
|
+
# Update the chunk
|
|
129
|
+
self.updated_chunks.add(chunk)
|
|
130
|
+
|
|
131
|
+
def get_render_range(self) -> tuple:
|
|
132
|
+
"""
|
|
133
|
+
Returns a rectangluar prism of chunks that are in the camera's view.
|
|
134
|
+
Tuple return is in form ((x1, x2), (y1, y2), (z1, z2))
|
|
135
|
+
"""
|
|
136
|
+
|
|
137
|
+
cam_position = self.scene.camera.position # glm.vec3(x, y, z)
|
|
138
|
+
fov = 40 # The range in which a direction will not be culled
|
|
139
|
+
|
|
140
|
+
# Default to a cube of chunks around the camera extending view_distance chunks in each direction
|
|
141
|
+
chunk_size = self.engine.config.chunk_size
|
|
142
|
+
render_distance = self.engine.config.render_distance
|
|
143
|
+
render_range_x = [int(cam_position.x // chunk_size - render_distance), int(cam_position.x // chunk_size + render_distance + 1)]
|
|
144
|
+
render_range_y = [int(cam_position.y // chunk_size - render_distance), int(cam_position.y // chunk_size + render_distance + 1)]
|
|
145
|
+
render_range_z = [int(cam_position.z // chunk_size - render_distance), int(cam_position.z // chunk_size + render_distance + 1)]
|
|
146
|
+
|
|
147
|
+
# Remove chunks that the camera is facing away from
|
|
148
|
+
render_range_x[1] -= render_distance * (180 - fov < self.scene.camera.yaw < 180 + fov) - 1
|
|
149
|
+
render_range_x[0] += render_distance * (-fov < self.scene.camera.yaw < fov or self.scene.camera.yaw > 360 - fov) - 1
|
|
150
|
+
|
|
151
|
+
render_range_y[0] += render_distance * (self.scene.camera.pitch > 25) - 1
|
|
152
|
+
render_range_y[1] -= render_distance * (self.scene.camera.pitch < -25) - 1
|
|
153
|
+
|
|
154
|
+
render_range_z[1] -= render_distance * (270 - fov < self.scene.camera.yaw < 270 + fov) - 1
|
|
155
|
+
render_range_z[0] += render_distance * (90 - fov < self.scene.camera.yaw < 90 + fov) - 1
|
|
156
|
+
|
|
157
|
+
return (render_range_x, render_range_y, render_range_z)
|
|
158
|
+
|
|
159
|
+
def swap_default(self, shader):
|
|
160
|
+
"""
|
|
161
|
+
Swaps the shader of the default chunks
|
|
162
|
+
"""
|
|
163
|
+
|
|
164
|
+
group = self.shader_groups[None]
|
|
165
|
+
for chunk in group[0].values():
|
|
166
|
+
chunk.swap_shader(shader)
|
|
167
|
+
for chunk in group[1].values():
|
|
166
168
|
chunk.swap_shader(shader)
|