basilisk-engine 0.1.38__py3-none-any.whl → 0.1.40__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.

Files changed (97) hide show
  1. basilisk/__init__.py +26 -26
  2. basilisk/audio/sound.py +27 -27
  3. basilisk/bsk_assets/cube.obj +48 -48
  4. basilisk/collisions/broad/broad_aabb.py +102 -102
  5. basilisk/collisions/broad/broad_bvh.py +137 -137
  6. basilisk/collisions/collider.py +95 -95
  7. basilisk/collisions/collider_handler.py +226 -226
  8. basilisk/collisions/narrow/contact_manifold.py +95 -95
  9. basilisk/collisions/narrow/dataclasses.py +34 -34
  10. basilisk/collisions/narrow/deprecated.py +46 -46
  11. basilisk/collisions/narrow/epa.py +91 -91
  12. basilisk/collisions/narrow/gjk.py +66 -66
  13. basilisk/collisions/narrow/graham_scan.py +24 -24
  14. basilisk/collisions/narrow/helper.py +29 -29
  15. basilisk/collisions/narrow/line_intersections.py +106 -106
  16. basilisk/collisions/narrow/sutherland_hodgman.py +75 -75
  17. basilisk/config.py +53 -53
  18. basilisk/draw/draw.py +100 -100
  19. basilisk/draw/draw_handler.py +178 -178
  20. basilisk/draw/font_renderer.py +28 -28
  21. basilisk/engine.py +165 -165
  22. basilisk/generic/abstract_bvh.py +15 -15
  23. basilisk/generic/abstract_custom.py +133 -133
  24. basilisk/generic/collisions.py +70 -70
  25. basilisk/generic/input_validation.py +82 -82
  26. basilisk/generic/math.py +18 -7
  27. basilisk/generic/matrices.py +35 -35
  28. basilisk/generic/meshes.py +72 -72
  29. basilisk/generic/quat.py +142 -142
  30. basilisk/generic/quat_methods.py +7 -7
  31. basilisk/generic/raycast_result.py +26 -26
  32. basilisk/generic/vec3.py +143 -143
  33. basilisk/input/__init__.py +0 -0
  34. basilisk/input/mouse.py +62 -0
  35. basilisk/input/path.py +14 -0
  36. basilisk/input_output/IO_handler.py +91 -91
  37. basilisk/input_output/clock.py +49 -49
  38. basilisk/input_output/keys.py +43 -43
  39. basilisk/input_output/mouse.py +90 -90
  40. basilisk/input_output/path.py +14 -14
  41. basilisk/mesh/cube.py +33 -33
  42. basilisk/mesh/mesh.py +233 -233
  43. basilisk/mesh/mesh_from_data.py +150 -150
  44. basilisk/mesh/model.py +271 -271
  45. basilisk/mesh/narrow_aabb.py +89 -89
  46. basilisk/mesh/narrow_bvh.py +91 -91
  47. basilisk/mesh/narrow_primative.py +23 -23
  48. basilisk/nodes/helper.py +28 -28
  49. basilisk/nodes/node.py +709 -704
  50. basilisk/nodes/node_handler.py +97 -97
  51. basilisk/particles/particle_handler.py +64 -64
  52. basilisk/particles/particle_renderer.py +93 -93
  53. basilisk/physics/impulse.py +112 -112
  54. basilisk/physics/physics_body.py +43 -43
  55. basilisk/physics/physics_engine.py +35 -35
  56. basilisk/render/batch.py +103 -103
  57. basilisk/render/bloom.py +107 -107
  58. basilisk/render/camera.py +260 -260
  59. basilisk/render/chunk.py +108 -108
  60. basilisk/render/chunk_handler.py +167 -167
  61. basilisk/render/frame.py +110 -110
  62. basilisk/render/framebuffer.py +202 -202
  63. basilisk/render/image.py +120 -120
  64. basilisk/render/image_handler.py +120 -120
  65. basilisk/render/light.py +96 -96
  66. basilisk/render/light_handler.py +58 -58
  67. basilisk/render/material.py +232 -232
  68. basilisk/render/material_handler.py +133 -133
  69. basilisk/render/post_process.py +146 -146
  70. basilisk/render/shader.py +134 -134
  71. basilisk/render/shader_handler.py +85 -85
  72. basilisk/render/sky.py +120 -120
  73. basilisk/scene.py +290 -290
  74. basilisk/shaders/batch.frag +289 -289
  75. basilisk/shaders/batch.vert +117 -117
  76. basilisk/shaders/bloom_downsample.frag +42 -42
  77. basilisk/shaders/bloom_frame.frag +24 -24
  78. basilisk/shaders/bloom_upsample.frag +33 -33
  79. basilisk/shaders/crt.frag +31 -31
  80. basilisk/shaders/draw.frag +25 -25
  81. basilisk/shaders/draw.vert +25 -25
  82. basilisk/shaders/filter.frag +22 -22
  83. basilisk/shaders/frame.frag +12 -12
  84. basilisk/shaders/frame.vert +13 -13
  85. basilisk/shaders/geometry.frag +10 -10
  86. basilisk/shaders/geometry.vert +41 -41
  87. basilisk/shaders/normal.frag +62 -62
  88. basilisk/shaders/normal.vert +96 -96
  89. basilisk/shaders/particle.frag +76 -76
  90. basilisk/shaders/particle.vert +86 -86
  91. basilisk/shaders/sky.frag +23 -23
  92. basilisk/shaders/sky.vert +13 -13
  93. {basilisk_engine-0.1.38.dist-info → basilisk_engine-0.1.40.dist-info}/METADATA +89 -89
  94. basilisk_engine-0.1.40.dist-info/RECORD +114 -0
  95. {basilisk_engine-0.1.38.dist-info → basilisk_engine-0.1.40.dist-info}/WHEEL +1 -1
  96. basilisk_engine-0.1.38.dist-info/RECORD +0 -111
  97. {basilisk_engine-0.1.38.dist-info → basilisk_engine-0.1.40.dist-info}/top_level.txt +0 -0
basilisk/render/frame.py CHANGED
@@ -1,111 +1,111 @@
1
- import numpy as np
2
- import moderngl as mgl
3
- from .shader import Shader
4
- from .framebuffer import Framebuffer
5
- from .post_process import PostProcess
6
- from .bloom import Bloom
7
-
8
-
9
- class Frame:
10
- shader: Shader=None
11
- vbo: mgl.Buffer=None
12
- vao: mgl.VertexArray=None
13
- framebuffer: mgl.Framebuffer=None
14
-
15
- def __init__(self, engine, scale: float=1.0, linear_filter: bool=False) -> None:
16
- """
17
- Basilisk render destination.
18
- Can be used to render to the screen or for headless rendering
19
- """
20
-
21
- self.engine = engine
22
- self.ctx = engine.ctx
23
-
24
- # Load framebuffer
25
- self.framebuffer = Framebuffer(self.engine, scale=scale, n_color_attachments=2, linear_filter=linear_filter)
26
- self.ping_pong_buffer = Framebuffer(self.engine, scale=scale, n_color_attachments=2, linear_filter=linear_filter)
27
-
28
- self.framebuffer.texture.repeat_x = False
29
- self.framebuffer.texture.repeat_y = False
30
-
31
- # Load Shaders
32
- self.shader = Shader(self.engine, self.engine.root + '/shaders/frame.vert', self.engine.root + '/shaders/bloom_frame.frag')
33
- self.engine.shader_handler.add(self.shader)
34
-
35
- # Load VAO
36
- self.vbo = self.ctx.buffer(np.array([[-1, -1, 0, 0, 0], [1, -1, 0, 1, 0], [1, 1, 0, 1, 1], [-1, 1, 0, 0, 1], [-1, -1, 0, 0, 0], [1, 1, 0, 1, 1]], dtype='f4'))
37
- self.vao = self.ctx.vertex_array(self.shader.program, [(self.vbo, '3f 2f', 'in_position', 'in_uv')], skip_errors=True)
38
-
39
- self.bloom = Bloom(self)
40
-
41
- # TEMP TESTING
42
- self.post_processes = []
43
-
44
-
45
- def render(self) -> None:
46
- """
47
- Renders the current frame to the screen
48
- """
49
-
50
- self.ctx.enable(mgl.BLEND)
51
- self.ctx.blend_func = mgl.ADDITIVE_BLENDING
52
-
53
- if self.engine.event_resize: self.bloom.generate_bloom_buffers()
54
-
55
- self.bloom.render()
56
-
57
- for process in self.post_processes:
58
- self.ping_pong_buffer = process.apply([('screenTexture', self.framebuffer)], self.ping_pong_buffer)
59
-
60
- temp = self.framebuffer
61
- self.framebuffer = self.ping_pong_buffer
62
- self.ping_pong_buffer = temp
63
-
64
- self.ctx.screen.use()
65
- self.shader.bind(self.framebuffer.texture, 'screenTexture', 0)
66
- self.shader.bind(self.bloom.texture, 'bloomTexture', 1)
67
- self.vao.render()
68
-
69
- self.ctx.disable(mgl.BLEND)
70
-
71
- def use(self) -> None:
72
- """
73
- Uses the frame as a render target
74
- """
75
-
76
- self.framebuffer.use()
77
-
78
- def add_post_process(self, post_process: PostProcess) -> PostProcess:
79
- """
80
- Add a post process to the frames post process stack
81
- """
82
-
83
- self.post_processes.append(post_process)
84
- return post_process
85
-
86
- def save(self, destination: str=None) -> None:
87
- """
88
- Saves the frame as an image to the given file destination
89
- """
90
-
91
- self.framebuffer.save(destination)
92
-
93
- def clear(self):
94
- self.framebuffer.clear()
95
-
96
- def resize(self) -> None:
97
- """
98
- Resize the frame to the given size. None for window size
99
- """
100
-
101
- self.framebuffer.resize()
102
- self.ping_pong_buffer.resize()
103
- self.generate_bloom_buffers()
104
-
105
- def __del__(self) -> None:
106
- """
107
- Releases memory used by the frame
108
- """
109
-
110
- if self.vbo: self.vbo.release()
1
+ import numpy as np
2
+ import moderngl as mgl
3
+ from .shader import Shader
4
+ from .framebuffer import Framebuffer
5
+ from .post_process import PostProcess
6
+ from .bloom import Bloom
7
+
8
+
9
+ class Frame:
10
+ shader: Shader=None
11
+ vbo: mgl.Buffer=None
12
+ vao: mgl.VertexArray=None
13
+ framebuffer: mgl.Framebuffer=None
14
+
15
+ def __init__(self, engine, scale: float=1.0, linear_filter: bool=False) -> None:
16
+ """
17
+ Basilisk render destination.
18
+ Can be used to render to the screen or for headless rendering
19
+ """
20
+
21
+ self.engine = engine
22
+ self.ctx = engine.ctx
23
+
24
+ # Load framebuffer
25
+ self.framebuffer = Framebuffer(self.engine, scale=scale, n_color_attachments=2, linear_filter=linear_filter)
26
+ self.ping_pong_buffer = Framebuffer(self.engine, scale=scale, n_color_attachments=2, linear_filter=linear_filter)
27
+
28
+ self.framebuffer.texture.repeat_x = False
29
+ self.framebuffer.texture.repeat_y = False
30
+
31
+ # Load Shaders
32
+ self.shader = Shader(self.engine, self.engine.root + '/shaders/frame.vert', self.engine.root + '/shaders/bloom_frame.frag')
33
+ self.engine.shader_handler.add(self.shader)
34
+
35
+ # Load VAO
36
+ self.vbo = self.ctx.buffer(np.array([[-1, -1, 0, 0, 0], [1, -1, 0, 1, 0], [1, 1, 0, 1, 1], [-1, 1, 0, 0, 1], [-1, -1, 0, 0, 0], [1, 1, 0, 1, 1]], dtype='f4'))
37
+ self.vao = self.ctx.vertex_array(self.shader.program, [(self.vbo, '3f 2f', 'in_position', 'in_uv')], skip_errors=True)
38
+
39
+ self.bloom = Bloom(self)
40
+
41
+ # TEMP TESTING
42
+ self.post_processes = []
43
+
44
+
45
+ def render(self) -> None:
46
+ """
47
+ Renders the current frame to the screen
48
+ """
49
+
50
+ self.ctx.enable(mgl.BLEND)
51
+ self.ctx.blend_func = mgl.ADDITIVE_BLENDING
52
+
53
+ if self.engine.event_resize: self.bloom.generate_bloom_buffers()
54
+
55
+ self.bloom.render()
56
+
57
+ for process in self.post_processes:
58
+ self.ping_pong_buffer = process.apply([('screenTexture', self.framebuffer)], self.ping_pong_buffer)
59
+
60
+ temp = self.framebuffer
61
+ self.framebuffer = self.ping_pong_buffer
62
+ self.ping_pong_buffer = temp
63
+
64
+ self.ctx.screen.use()
65
+ self.shader.bind(self.framebuffer.texture, 'screenTexture', 0)
66
+ self.shader.bind(self.bloom.texture, 'bloomTexture', 1)
67
+ self.vao.render()
68
+
69
+ self.ctx.disable(mgl.BLEND)
70
+
71
+ def use(self) -> None:
72
+ """
73
+ Uses the frame as a render target
74
+ """
75
+
76
+ self.framebuffer.use()
77
+
78
+ def add_post_process(self, post_process: PostProcess) -> PostProcess:
79
+ """
80
+ Add a post process to the frames post process stack
81
+ """
82
+
83
+ self.post_processes.append(post_process)
84
+ return post_process
85
+
86
+ def save(self, destination: str=None) -> None:
87
+ """
88
+ Saves the frame as an image to the given file destination
89
+ """
90
+
91
+ self.framebuffer.save(destination)
92
+
93
+ def clear(self):
94
+ self.framebuffer.clear()
95
+
96
+ def resize(self) -> None:
97
+ """
98
+ Resize the frame to the given size. None for window size
99
+ """
100
+
101
+ self.framebuffer.resize()
102
+ self.ping_pong_buffer.resize()
103
+ self.generate_bloom_buffers()
104
+
105
+ def __del__(self) -> None:
106
+ """
107
+ Releases memory used by the frame
108
+ """
109
+
110
+ if self.vbo: self.vbo.release()
111
111
  if self.vao: self.vao.release()
@@ -1,203 +1,203 @@
1
- import numpy as np
2
- import moderngl as mgl
3
- from PIL import Image
4
- from ..render.shader import Shader
5
- from ..generic.input_validation import validate_int
6
-
7
-
8
- class Framebuffer:
9
- engine: ...
10
- """Reference to the parent engine"""
11
- size: tuple[int] | None=None
12
- """The dimensions of the framebuffer (x, y). Defaults to window size if None"""
13
- scale: float=1.0
14
- """Scaling factor applied to the size. Best for use with default size"""
15
- texture_filter: tuple[int]=(mgl.NEAREST, mgl.NEAREST)
16
- """The filter applied to the texture when rendering"""
17
- fbo: mgl.Framebuffer=None
18
- """The core framebuffer the object provides abstraction for."""
19
- texture: mgl.Texture=None
20
- """The color texture of the framebuffer"""
21
- depth: mgl.Texture=None
22
- """The depth texture of the framebuffer"""
23
- _color_attachments = None
24
- """"""
25
- _depth_attachment = None
26
- """"""
27
-
28
- def __init__(self, engine: ..., size: tuple[int]=None, n_color_attachments: int=1, scale: float=1.0, linear_filter: bool=True) -> None:
29
- """
30
- Abstraction of the MGL framebuffer.
31
- Has the given number of color attachements (4-component) and 1 depth attachment.
32
- All textures are of uniform size.
33
- """
34
-
35
- self.engine = engine
36
- self.ctx = engine.ctx
37
- self._size = size
38
- self.scale = scale
39
- self.texture_filter = (mgl.LINEAR, mgl.LINEAR) if linear_filter else (mgl.NEAREST, mgl.NEAREST)
40
- self.n_attachments = n_color_attachments
41
-
42
- self.load_pipeline()
43
- self.generate_fbo()
44
-
45
- self.engine.fbos.append(self)
46
-
47
- def generate_fbo(self):
48
- """
49
- Generates fresh depth texture and color textures and creates an FBO
50
- """
51
-
52
- # Release existing memory
53
- self.__del__()
54
-
55
- # Create textures
56
- self._color_attachments = [self.ctx.texture(self.size, components=4, dtype='f4') for i in range(self.n_attachments)]
57
- for tex in self._color_attachments: tex.filter = self.texture_filter
58
- self._depth_attachment = self.ctx.depth_texture(self.size)
59
-
60
- # Create the internal fbo
61
- self.fbo = self.ctx.framebuffer(self._color_attachments, self._depth_attachment)
62
-
63
- # Texture attributes
64
- self.texture.repeat_x = False
65
- self.texture.repeat_y = False
66
-
67
- # Set the show attachment to default
68
- self._show = -1
69
- self.show = 0
70
-
71
- def resize(self, new_size: tuple[int]=None) -> None:
72
- """
73
- Update set size framebuffers with the given size.
74
- """
75
-
76
- # Check that we are not updating the size to the existing size and
77
- if self._size and self._size == new_size: return
78
-
79
- # If we have a set size, update with the given size
80
- if self._size and new_size: self._size = new_size
81
-
82
- # Update the textures and fbo
83
- self.generate_fbo()
84
-
85
- def load_pipeline(self) -> None:
86
- """
87
- Loads the shader, vbo, and vao used to display the fbo
88
- """
89
-
90
- # Load Shaders
91
- self.shader = Shader(self.engine, self.engine.root + '/shaders/frame.vert', self.engine.root + '/shaders/frame.frag')
92
- self.engine.shader_handler.add(self.shader)
93
-
94
- # Load VAO
95
- self.vbo = self.ctx.buffer(np.array([[-1, -1, 0, 0, 0], [1, -1, 0, 1, 0], [1, 1, 0, 1, 1], [-1, 1, 0, 0, 1], [-1, -1, 0, 0, 0], [1, 1, 0, 1, 1]], dtype='f4'))
96
- self.vao = self.ctx.vertex_array(self.shader.program, [(self.vbo, '3f 2f', 'in_position', 'in_uv')], skip_errors=True)
97
-
98
- def render(self, render_target=None, show: int=None) -> None:
99
- """
100
- Render the fbo to the screen
101
- If the fbo has multiple attachments, show will specifiy which is shown
102
- Depth is considered the last show element
103
- """
104
-
105
- if not isinstance(show, type(None)): self.show = show
106
-
107
- target = render_target if render_target else self.engine.frame
108
- target.use()
109
-
110
- self.vao.render()
111
-
112
- def use(self) -> None:
113
- """
114
- Select this framebuffer for use
115
- """
116
-
117
- self.fbo.use()
118
-
119
- def clear(self) -> None:
120
- """
121
- Clear all data currently in the textures (set to black)
122
- """
123
-
124
- self.fbo.clear()
125
-
126
- def save(self, destination: str=None) -> None:
127
- """
128
- Saves the frame as an image to the given file destination
129
- """
130
-
131
- path = destination if destination else 'screenshot'
132
-
133
- data = self.fbo.read(components=3, alignment=1)
134
- img = Image.frombytes('RGB', self.size, data).transpose(Image.FLIP_TOP_BOTTOM)
135
- img.save(f'{path}.png')
136
-
137
-
138
- @property
139
- def size(self) -> tuple[int]:
140
- """Size of the textures in the fbo in pixels (x: int, y: int)"""
141
- size = self._size if self._size else self.engine.win_size
142
- size = tuple(map((lambda x: int(x * self.scale)), size))
143
- return size
144
- @property
145
- def texture(self) -> mgl.Texture:
146
- """First color attachment in the fbo"""
147
- return self._color_attachments[0]
148
- @property
149
- def color_attachments(self) -> list[mgl.Texture]:
150
- """List of all color attachments in the fbo"""
151
- return self._color_attachments
152
- @property
153
- def depth(self) -> mgl.Texture:
154
- """Depth attachment of the fbo"""
155
- return self._depth_attachment
156
- @property
157
- def data(self) -> bytes:
158
- """Reads the data from the fbo"""
159
- return self.fbo.read()
160
- @property
161
- def show(self) -> int:
162
- return self._show
163
-
164
- @size.setter
165
- def size(self, value: tuple[int]=None) -> tuple[int]:
166
- self.resize(value)
167
- return self.size
168
-
169
- @show.setter
170
- def show(self, value: int) -> None:
171
- value = validate_int("Framebuffer", "show", value)
172
- if value == self._show: return
173
-
174
- # Verify the range
175
- if value < 0 or value > len(self.color_attachments): raise ValueError(f'Framebuffer.show: invalid color attachement to show, {value} is out of range')
176
- elif value == len(self.color_attachments): src = self.depth
177
- else: src = self.color_attachments[value]
178
-
179
- # Update value
180
- self._show = value
181
-
182
- # Bind the correct texture
183
- self.shader.program['screenTexture'] = value+1
184
- src.use(location=value+1)
185
-
186
- def __repr__(self) -> str:
187
- return f'<bsk.Framebuffer | size: {self.size}>'
188
-
189
- def __del__(self) -> None:
190
- """
191
- Releases all memory used by the fbo
192
- """
193
-
194
- if self._color_attachments: [tex.release() for tex in self._color_attachments]
195
- if self._depth_attachment: self._depth_attachment.release()
196
- if self.fbo: self.fbo.release()
197
- if self.vbo: self.vbo.release()
198
- if self.vao: self.vao.release()
199
- if self.shader:
200
- if self.shader in self.engine.shader_handler.shaders: self.engine.shader_handler.shaders.remove(self.shader)
201
- self.shader.__del__()
202
-
1
+ import numpy as np
2
+ import moderngl as mgl
3
+ from PIL import Image
4
+ from ..render.shader import Shader
5
+ from ..generic.input_validation import validate_int
6
+
7
+
8
+ class Framebuffer:
9
+ engine: ...
10
+ """Reference to the parent engine"""
11
+ size: tuple[int] | None=None
12
+ """The dimensions of the framebuffer (x, y). Defaults to window size if None"""
13
+ scale: float=1.0
14
+ """Scaling factor applied to the size. Best for use with default size"""
15
+ texture_filter: tuple[int]=(mgl.NEAREST, mgl.NEAREST)
16
+ """The filter applied to the texture when rendering"""
17
+ fbo: mgl.Framebuffer=None
18
+ """The core framebuffer the object provides abstraction for."""
19
+ texture: mgl.Texture=None
20
+ """The color texture of the framebuffer"""
21
+ depth: mgl.Texture=None
22
+ """The depth texture of the framebuffer"""
23
+ _color_attachments = None
24
+ """"""
25
+ _depth_attachment = None
26
+ """"""
27
+
28
+ def __init__(self, engine: ..., size: tuple[int]=None, n_color_attachments: int=1, scale: float=1.0, linear_filter: bool=True) -> None:
29
+ """
30
+ Abstraction of the MGL framebuffer.
31
+ Has the given number of color attachements (4-component) and 1 depth attachment.
32
+ All textures are of uniform size.
33
+ """
34
+
35
+ self.engine = engine
36
+ self.ctx = engine.ctx
37
+ self._size = size
38
+ self.scale = scale
39
+ self.texture_filter = (mgl.LINEAR, mgl.LINEAR) if linear_filter else (mgl.NEAREST, mgl.NEAREST)
40
+ self.n_attachments = n_color_attachments
41
+
42
+ self.load_pipeline()
43
+ self.generate_fbo()
44
+
45
+ self.engine.fbos.append(self)
46
+
47
+ def generate_fbo(self):
48
+ """
49
+ Generates fresh depth texture and color textures and creates an FBO
50
+ """
51
+
52
+ # Release existing memory
53
+ self.__del__()
54
+
55
+ # Create textures
56
+ self._color_attachments = [self.ctx.texture(self.size, components=4, dtype='f4') for i in range(self.n_attachments)]
57
+ for tex in self._color_attachments: tex.filter = self.texture_filter
58
+ self._depth_attachment = self.ctx.depth_texture(self.size)
59
+
60
+ # Create the internal fbo
61
+ self.fbo = self.ctx.framebuffer(self._color_attachments, self._depth_attachment)
62
+
63
+ # Texture attributes
64
+ self.texture.repeat_x = False
65
+ self.texture.repeat_y = False
66
+
67
+ # Set the show attachment to default
68
+ self._show = -1
69
+ self.show = 0
70
+
71
+ def resize(self, new_size: tuple[int]=None) -> None:
72
+ """
73
+ Update set size framebuffers with the given size.
74
+ """
75
+
76
+ # Check that we are not updating the size to the existing size and
77
+ if self._size and self._size == new_size: return
78
+
79
+ # If we have a set size, update with the given size
80
+ if self._size and new_size: self._size = new_size
81
+
82
+ # Update the textures and fbo
83
+ self.generate_fbo()
84
+
85
+ def load_pipeline(self) -> None:
86
+ """
87
+ Loads the shader, vbo, and vao used to display the fbo
88
+ """
89
+
90
+ # Load Shaders
91
+ self.shader = Shader(self.engine, self.engine.root + '/shaders/frame.vert', self.engine.root + '/shaders/frame.frag')
92
+ self.engine.shader_handler.add(self.shader)
93
+
94
+ # Load VAO
95
+ self.vbo = self.ctx.buffer(np.array([[-1, -1, 0, 0, 0], [1, -1, 0, 1, 0], [1, 1, 0, 1, 1], [-1, 1, 0, 0, 1], [-1, -1, 0, 0, 0], [1, 1, 0, 1, 1]], dtype='f4'))
96
+ self.vao = self.ctx.vertex_array(self.shader.program, [(self.vbo, '3f 2f', 'in_position', 'in_uv')], skip_errors=True)
97
+
98
+ def render(self, render_target=None, show: int=None) -> None:
99
+ """
100
+ Render the fbo to the screen
101
+ If the fbo has multiple attachments, show will specifiy which is shown
102
+ Depth is considered the last show element
103
+ """
104
+
105
+ if not isinstance(show, type(None)): self.show = show
106
+
107
+ target = render_target if render_target else self.engine.frame
108
+ target.use()
109
+
110
+ self.vao.render()
111
+
112
+ def use(self) -> None:
113
+ """
114
+ Select this framebuffer for use
115
+ """
116
+
117
+ self.fbo.use()
118
+
119
+ def clear(self) -> None:
120
+ """
121
+ Clear all data currently in the textures (set to black)
122
+ """
123
+
124
+ self.fbo.clear()
125
+
126
+ def save(self, destination: str=None) -> None:
127
+ """
128
+ Saves the frame as an image to the given file destination
129
+ """
130
+
131
+ path = destination if destination else 'screenshot'
132
+
133
+ data = self.fbo.read(components=3, alignment=1)
134
+ img = Image.frombytes('RGB', self.size, data).transpose(Image.FLIP_TOP_BOTTOM)
135
+ img.save(f'{path}.png')
136
+
137
+
138
+ @property
139
+ def size(self) -> tuple[int]:
140
+ """Size of the textures in the fbo in pixels (x: int, y: int)"""
141
+ size = self._size if self._size else self.engine.win_size
142
+ size = tuple(map((lambda x: int(x * self.scale)), size))
143
+ return size
144
+ @property
145
+ def texture(self) -> mgl.Texture:
146
+ """First color attachment in the fbo"""
147
+ return self._color_attachments[0]
148
+ @property
149
+ def color_attachments(self) -> list[mgl.Texture]:
150
+ """List of all color attachments in the fbo"""
151
+ return self._color_attachments
152
+ @property
153
+ def depth(self) -> mgl.Texture:
154
+ """Depth attachment of the fbo"""
155
+ return self._depth_attachment
156
+ @property
157
+ def data(self) -> bytes:
158
+ """Reads the data from the fbo"""
159
+ return self.fbo.read()
160
+ @property
161
+ def show(self) -> int:
162
+ return self._show
163
+
164
+ @size.setter
165
+ def size(self, value: tuple[int]=None) -> tuple[int]:
166
+ self.resize(value)
167
+ return self.size
168
+
169
+ @show.setter
170
+ def show(self, value: int) -> None:
171
+ value = validate_int("Framebuffer", "show", value)
172
+ if value == self._show: return
173
+
174
+ # Verify the range
175
+ if value < 0 or value > len(self.color_attachments): raise ValueError(f'Framebuffer.show: invalid color attachement to show, {value} is out of range')
176
+ elif value == len(self.color_attachments): src = self.depth
177
+ else: src = self.color_attachments[value]
178
+
179
+ # Update value
180
+ self._show = value
181
+
182
+ # Bind the correct texture
183
+ self.shader.program['screenTexture'] = value+1
184
+ src.use(location=value+1)
185
+
186
+ def __repr__(self) -> str:
187
+ return f'<bsk.Framebuffer | size: {self.size}>'
188
+
189
+ def __del__(self) -> None:
190
+ """
191
+ Releases all memory used by the fbo
192
+ """
193
+
194
+ if self._color_attachments: [tex.release() for tex in self._color_attachments]
195
+ if self._depth_attachment: self._depth_attachment.release()
196
+ if self.fbo: self.fbo.release()
197
+ if self.vbo: self.vbo.release()
198
+ if self.vao: self.vao.release()
199
+ if self.shader:
200
+ if self.shader in self.engine.shader_handler.shaders: self.engine.shader_handler.shaders.remove(self.shader)
201
+ self.shader.__del__()
202
+
203
203
  if self in self.engine.fbos: self.engine.fbos.remove(self)