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