basilisk-engine 0.1.3__py3-none-any.whl → 0.1.4__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 (86) hide show
  1. basilisk/__init__.py +13 -12
  2. basilisk/bsk_assets/cube.obj +48 -48
  3. basilisk/collisions/broad/broad_aabb.py +102 -102
  4. basilisk/collisions/broad/broad_bvh.py +137 -137
  5. basilisk/collisions/collider.py +95 -95
  6. basilisk/collisions/collider_handler.py +225 -225
  7. basilisk/collisions/narrow/contact_manifold.py +90 -90
  8. basilisk/collisions/narrow/dataclasses.py +32 -32
  9. basilisk/collisions/narrow/deprecated.py +46 -46
  10. basilisk/collisions/narrow/epa.py +91 -91
  11. basilisk/collisions/narrow/gjk.py +66 -66
  12. basilisk/collisions/narrow/graham_scan.py +24 -24
  13. basilisk/collisions/narrow/helper.py +29 -29
  14. basilisk/collisions/narrow/line_intersections.py +106 -106
  15. basilisk/collisions/narrow/sutherland_hodgman.py +75 -75
  16. basilisk/config.py +2 -2
  17. basilisk/draw/draw.py +100 -100
  18. basilisk/draw/draw_handler.py +179 -179
  19. basilisk/draw/font_renderer.py +28 -28
  20. basilisk/engine.py +199 -195
  21. basilisk/generic/abstract_bvh.py +15 -15
  22. basilisk/generic/abstract_custom.py +133 -133
  23. basilisk/generic/collisions.py +70 -70
  24. basilisk/generic/input_validation.py +66 -66
  25. basilisk/generic/math.py +6 -6
  26. basilisk/generic/matrices.py +33 -33
  27. basilisk/generic/meshes.py +72 -72
  28. basilisk/generic/quat.py +137 -137
  29. basilisk/generic/quat_methods.py +7 -7
  30. basilisk/generic/raycast_result.py +23 -23
  31. basilisk/generic/vec3.py +143 -143
  32. basilisk/input/mouse.py +61 -61
  33. basilisk/input/path.py +14 -0
  34. basilisk/mesh/cube.py +33 -33
  35. basilisk/mesh/mesh.py +230 -230
  36. basilisk/mesh/mesh_from_data.py +130 -132
  37. basilisk/mesh/model.py +271 -271
  38. basilisk/mesh/narrow_aabb.py +89 -89
  39. basilisk/mesh/narrow_bvh.py +91 -91
  40. basilisk/mesh/narrow_primative.py +23 -23
  41. basilisk/nodes/helper.py +28 -28
  42. basilisk/nodes/node.py +680 -680
  43. basilisk/nodes/node_handler.py +95 -95
  44. basilisk/particles/particle_handler.py +63 -63
  45. basilisk/particles/particle_renderer.py +87 -87
  46. basilisk/physics/impulse.py +112 -112
  47. basilisk/physics/physics_body.py +43 -43
  48. basilisk/physics/physics_engine.py +35 -35
  49. basilisk/render/batch.py +86 -86
  50. basilisk/render/camera.py +204 -204
  51. basilisk/render/chunk.py +99 -99
  52. basilisk/render/chunk_handler.py +154 -154
  53. basilisk/render/frame.py +99 -182
  54. basilisk/render/framebuffer.py +106 -0
  55. basilisk/render/image.py +87 -75
  56. basilisk/render/image_handler.py +122 -122
  57. basilisk/render/light.py +96 -96
  58. basilisk/render/light_handler.py +58 -58
  59. basilisk/render/material.py +219 -219
  60. basilisk/render/material_handler.py +135 -135
  61. basilisk/render/post_process.py +133 -0
  62. basilisk/render/shader.py +109 -109
  63. basilisk/render/shader_handler.py +79 -79
  64. basilisk/render/sky.py +120 -120
  65. basilisk/scene.py +256 -250
  66. basilisk/shaders/batch.frag +276 -276
  67. basilisk/shaders/batch.vert +115 -115
  68. basilisk/shaders/crt.frag +32 -0
  69. basilisk/shaders/draw.frag +21 -21
  70. basilisk/shaders/draw.vert +21 -21
  71. basilisk/shaders/filter.frag +22 -22
  72. basilisk/shaders/frame.frag +12 -12
  73. basilisk/shaders/frame.vert +13 -13
  74. basilisk/shaders/geometry.frag +8 -8
  75. basilisk/shaders/geometry.vert +41 -41
  76. basilisk/shaders/normal.frag +59 -59
  77. basilisk/shaders/normal.vert +96 -96
  78. basilisk/shaders/particle.frag +71 -71
  79. basilisk/shaders/particle.vert +84 -84
  80. basilisk/shaders/sky.frag +9 -9
  81. basilisk/shaders/sky.vert +13 -13
  82. {basilisk_engine-0.1.3.dist-info → basilisk_engine-0.1.4.dist-info}/METADATA +38 -45
  83. basilisk_engine-0.1.4.dist-info/RECORD +101 -0
  84. {basilisk_engine-0.1.3.dist-info → basilisk_engine-0.1.4.dist-info}/WHEEL +1 -1
  85. basilisk_engine-0.1.3.dist-info/RECORD +0 -97
  86. {basilisk_engine-0.1.3.dist-info → basilisk_engine-0.1.4.dist-info}/top_level.txt +0 -0
@@ -1,136 +1,136 @@
1
- import moderngl as mgl
2
- from ..render.image_handler import ImageHandler
3
- from ..render.material import Material
4
- import numpy as np
5
-
6
-
7
- class MaterialHandler():
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
- materials: list[Material]
15
- """List containing all the materials in the scene"""
16
- data_texture: mgl.Texture
17
- """ModernGL texture containing all the material data for materials in the scene"""
18
-
19
- def __init__(self, scene) -> None:
20
- """
21
- Handles all the materials introduced to a scene.
22
- Writes material information to the GPU
23
- """
24
-
25
- # Back references
26
- self.scene = scene
27
- self.engine = scene.engine
28
- self.ctx = scene.engine.ctx
29
-
30
- # Initialize data
31
- self.materials = []
32
- self.data_texture = None
33
- self.set_base()
34
-
35
- self.image_handler = ImageHandler(scene)
36
-
37
- def add(self, material: Material) -> None:
38
- """
39
- Adds the given material to the handler if it is not already present
40
- """
41
-
42
- write = False
43
-
44
- if isinstance(material, Material): material = [material]
45
-
46
- for mtl in material:
47
- # Check that the material is not already in the scene
48
- if mtl in self.materials: continue
49
- # Update the material's handler
50
- mtl.material_handler = self
51
- # Add images
52
- if mtl.texture: self.image_handler.add(mtl.texture)
53
- if mtl.normal: self.image_handler.add(mtl.normal)
54
-
55
- # Add the material
56
- self.materials.append(mtl)
57
-
58
- write = True
59
-
60
-
61
- # Write materials
62
- if write: self.write(regenerate=True)
63
-
64
- def generate_material_texture(self) -> None:
65
- """
66
- Generates the texture that is used to write material data to the GPU
67
- """
68
-
69
- # Check that there are materials to write
70
- if len(self.materials) == 0: return
71
-
72
- # Release existing data texture
73
- if self.data_texture: self.data_texture.release()
74
-
75
- # Create empty texture data
76
- material_data = np.zeros(shape=(len(self.materials), 25), dtype="f4")
77
-
78
- # Get data from the materials
79
- for i, mtl in enumerate(self.materials):
80
- mtl.index = i
81
- material_data[i] = mtl.get_data()
82
-
83
- # Create texture from data
84
- material_data = np.ravel(material_data)
85
- self.data_texture = self.ctx.texture((1, len(material_data)), components=1, dtype='f4', data=material_data)
86
-
87
- def write(self, regenerate=False) -> None:
88
- """
89
- Writes all material data to relavent shaders
90
- """
91
-
92
- if regenerate: self.generate_material_texture()
93
-
94
- if not self.data_texture: return
95
-
96
- for shader in self.engine.scene.shader_handler.shaders:
97
- if 'materialsTexture' not in shader.uniforms: continue
98
-
99
- shader.program['materialsTexture'] = 9
100
- self.data_texture.use(location=9)
101
-
102
- def get(self, identifier: str | int) -> any:
103
- """
104
- Gets the basilisk material with the given name or index
105
- Args:
106
- identifier: str | int
107
- The name string or index of the desired material
108
- """
109
-
110
- # Simply use index if given
111
- if isinstance(identifier, int): return self.materials[identifier]
112
-
113
- # Else, search the list for an image material the given name
114
- for material in self.materials:
115
- if material.name != identifier: continue
116
- return material
117
-
118
- # No matching material found
119
- return None
120
-
121
- def set_base(self):
122
- """
123
- Creates a base material
124
- """
125
-
126
- self.base = Material('Base')
127
- self.materials.append(self.base)
128
- self.generate_material_texture()
129
- self.write()
130
-
131
- def __del__(self) -> None:
132
- """
133
- Releases the material data texture
134
- """
135
-
1
+ import moderngl as mgl
2
+ from ..render.image_handler import ImageHandler
3
+ from ..render.material import Material
4
+ import numpy as np
5
+
6
+
7
+ class MaterialHandler():
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
+ materials: list[Material]
15
+ """List containing all the materials in the scene"""
16
+ data_texture: mgl.Texture
17
+ """ModernGL texture containing all the material data for materials in the scene"""
18
+
19
+ def __init__(self, scene) -> None:
20
+ """
21
+ Handles all the materials introduced to a scene.
22
+ Writes material information to the GPU
23
+ """
24
+
25
+ # Back references
26
+ self.scene = scene
27
+ self.engine = scene.engine
28
+ self.ctx = scene.engine.ctx
29
+
30
+ # Initialize data
31
+ self.materials = []
32
+ self.data_texture = None
33
+ self.set_base()
34
+
35
+ self.image_handler = ImageHandler(scene)
36
+
37
+ def add(self, material: Material) -> None:
38
+ """
39
+ Adds the given material to the handler if it is not already present
40
+ """
41
+
42
+ write = False
43
+
44
+ if isinstance(material, Material): material = [material]
45
+
46
+ for mtl in material:
47
+ # Check that the material is not already in the scene
48
+ if mtl in self.materials: continue
49
+ # Update the material's handler
50
+ mtl.material_handler = self
51
+ # Add images
52
+ if mtl.texture: self.image_handler.add(mtl.texture)
53
+ if mtl.normal: self.image_handler.add(mtl.normal)
54
+
55
+ # Add the material
56
+ self.materials.append(mtl)
57
+
58
+ write = True
59
+
60
+
61
+ # Write materials
62
+ if write: self.write(regenerate=True)
63
+
64
+ def generate_material_texture(self) -> None:
65
+ """
66
+ Generates the texture that is used to write material data to the GPU
67
+ """
68
+
69
+ # Check that there are materials to write
70
+ if len(self.materials) == 0: return
71
+
72
+ # Release existing data texture
73
+ if self.data_texture: self.data_texture.release()
74
+
75
+ # Create empty texture data
76
+ material_data = np.zeros(shape=(len(self.materials), 25), dtype="f4")
77
+
78
+ # Get data from the materials
79
+ for i, mtl in enumerate(self.materials):
80
+ mtl.index = i
81
+ material_data[i] = mtl.get_data()
82
+
83
+ # Create texture from data
84
+ material_data = np.ravel(material_data)
85
+ self.data_texture = self.ctx.texture((1, len(material_data)), components=1, dtype='f4', data=material_data)
86
+
87
+ def write(self, regenerate=False) -> None:
88
+ """
89
+ Writes all material data to relavent shaders
90
+ """
91
+
92
+ if regenerate: self.generate_material_texture()
93
+
94
+ if not self.data_texture: return
95
+
96
+ for shader in self.engine.scene.shader_handler.shaders:
97
+ if 'materialsTexture' not in shader.uniforms: continue
98
+
99
+ shader.program['materialsTexture'] = 9
100
+ self.data_texture.use(location=9)
101
+
102
+ def get(self, identifier: str | int) -> any:
103
+ """
104
+ Gets the basilisk material with the given name or index
105
+ Args:
106
+ identifier: str | int
107
+ The name string or index of the desired material
108
+ """
109
+
110
+ # Simply use index if given
111
+ if isinstance(identifier, int): return self.materials[identifier]
112
+
113
+ # Else, search the list for an image material the given name
114
+ for material in self.materials:
115
+ if material.name != identifier: continue
116
+ return material
117
+
118
+ # No matching material found
119
+ return None
120
+
121
+ def set_base(self):
122
+ """
123
+ Creates a base material
124
+ """
125
+
126
+ self.base = Material('Base')
127
+ self.materials.append(self.base)
128
+ self.generate_material_texture()
129
+ self.write()
130
+
131
+ def __del__(self) -> None:
132
+ """
133
+ Releases the material data texture
134
+ """
135
+
136
136
  if self.data_texture: self.data_texture.release()
@@ -0,0 +1,133 @@
1
+ import numpy as np
2
+ import moderngl as mgl
3
+ from .shader import Shader
4
+ from .image import Image
5
+ from .framebuffer import Framebuffer
6
+
7
+
8
+ class PostProcess:
9
+ engine: ...
10
+ """Reference to the parent engine"""
11
+ ctx: mgl.Context
12
+ """Reference to the parent context"""
13
+ shader: Shader
14
+ """Shader object used by the post process"""
15
+ vao: mgl.VertexArray
16
+ """Screenspace render vao"""
17
+
18
+ def __init__(self, engine, shader_path: str=None, size: tuple=None, components: int=4, filter=(mgl.LINEAR, mgl.LINEAR)) -> None:
19
+ """
20
+ Object to apply post processing to a texture
21
+ """
22
+
23
+ # Reference attributes
24
+ self.engine = engine
25
+ self.ctx = engine.ctx
26
+
27
+ # Size of the destination
28
+ self.size = size if size else engine.win_size
29
+ self.components = components
30
+
31
+ # Load default fragment if none given
32
+ if not (isinstance(shader_path, str) or isinstance(shader_path, type(None))):
33
+ raise ValueError(f'PostProces.apply: Invalid post process source type {type(shader_path)}. Expected string path destination of fragment shader.')
34
+ frag = shader_path if shader_path else self.engine.root + f'/shaders/frame.frag'
35
+
36
+ # Load Shaders
37
+ self.shader = Shader(self.engine, self.engine.root + f'/shaders/frame.vert', frag)
38
+ self.engine.scene.shader_handler.add(self.shader)
39
+
40
+ # Load VAO
41
+ 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'))
42
+ self.vao = self.ctx.vertex_array(self.shader.program, [(self.vbo, '3f 2f', 'in_position', 'in_uv')], skip_errors=True)
43
+
44
+ # Temporary render destination
45
+ self.fbo = Framebuffer(engine, self.size, components)
46
+
47
+ # Filter settings
48
+ self.filter = filter
49
+ self.fbo.texture.filter = self.filter
50
+
51
+
52
+ def apply(self, source: mgl.Texture | Image | Framebuffer, destination: mgl.Texture | Image | Framebuffer=None) -> mgl.Texture | Image | Framebuffer:
53
+ """
54
+ Applies a post process shader to a texture source.
55
+ Returns the modified texture or renders to the destination if given
56
+ """
57
+
58
+ if isinstance(source, Framebuffer): return self._apply_to_framebuffer(source, destination)
59
+ elif isinstance(source, mgl.Texture): return self._apply_to_texture(source, destination)
60
+ elif isinstance(source, Image): return self._apply_to_image(source, destination)
61
+
62
+ raise ValueError(f'PostProces.apply: Invalid postprocess source type {type(source)}')
63
+
64
+ def _render_post_process(self, source: mgl.Texture):
65
+ # Clear and use the fbo as a render target
66
+ self.fbo.use()
67
+ self.fbo.clear()
68
+
69
+ # Load the source texture to the shader
70
+ self.shader.program['screenTexture'] = 0
71
+ source.use(location=0)
72
+
73
+ # Apply the post process
74
+ self.vao.render()
75
+
76
+
77
+ def _apply_to_framebuffer(self, source: Framebuffer, detination: Framebuffer=None) -> Framebuffer:
78
+ """
79
+ Applies a post process to a bsk.Framebuffer
80
+ """
81
+
82
+ # Create a blank framebuffer
83
+ if not detination:
84
+ fbo = Framebuffer(self.engine, self.size, self.components)
85
+ old_filter = None
86
+ fbo.texture.filter = self.filter
87
+ else:
88
+ fbo = detination
89
+ old_filter = fbo.filter
90
+ fbo.texture.filter = self.filter
91
+
92
+ # Load the source texture to the shader
93
+ self.shader.program['screenTexture'] = 0
94
+ source.texture.use(location=0)
95
+
96
+ fbo.use()
97
+ fbo.clear()
98
+ # Apply the post process
99
+ self.vao.render()
100
+
101
+ # Reset filter if needed
102
+ if old_filter: fbo.texture.filter = old_filter
103
+
104
+ # Return the fbo for the user
105
+ return fbo
106
+
107
+ def _apply_to_texture(self, source: mgl.Texture, destination: mgl.Texture) -> mgl.Texture:
108
+ """
109
+ Applies a post process to a mgl.Texture
110
+ """
111
+
112
+ # Render the post processs with the given texture
113
+ self._render_post_process(source)
114
+
115
+ # Make a deep copy of the modified texture
116
+ texture = self.ctx.texture(self.fbo.size, self.fbo.components, self.fbo.data)
117
+
118
+ return texture
119
+
120
+ def _apply_to_image(self, source: Image, destination: Image) -> Image:
121
+ """
122
+ Applies a post process to a bsk.Image
123
+ """
124
+
125
+ # Create a texture from the image data
126
+ texture = self.ctx.texture(self.fbo.size, self.fbo.components, source.data)
127
+
128
+ # Render the post processs with the given texture
129
+ self._render_post_process(source)
130
+
131
+ # Make an image from the texture
132
+ image = Image()
133
+ return image
basilisk/render/shader.py CHANGED
@@ -1,110 +1,110 @@
1
- import moderngl as mgl
2
- import random
3
-
4
- attribute_mappings = {
5
- 'in_position' : [0, 1, 2],
6
- 'in_uv' : [3, 4],
7
- 'in_normal' : [5, 6, 7],
8
- 'in_tangent' : [8, 9, 10],
9
- 'in_bitangent' : [11, 12, 13],
10
- 'obj_position' : [14, 15, 16],
11
- 'obj_rotation' : [17, 18, 19, 20],
12
- 'obj_scale' : [21, 22, 23],
13
- 'obj_material' : [24],
14
- }
15
-
16
-
17
- class Shader:
18
- program: mgl.Program=None
19
- """Shader program for the vertex and fragment shader"""
20
- vertex_shader: str
21
- """String representation of the vertex shader"""
22
- fragment_shader: str
23
- """String representation of the vertex shader"""
24
- uniforms: list[str]=[]
25
- """List containg the names of all uniforms in the shader"""
26
- attribute_indices: list[int]
27
- """List of indices that map all possible shader attributes to the ones used byu the shader"""
28
- fmt: str
29
- """String representation of the format for building vaos"""
30
- attributes: list[str]
31
- """List representation of the attributes for building vaos"""
32
-
33
- def __init__(self, engine, vert: str=None, frag: str=None) -> None:
34
- """
35
- Basilisk shader object. Contains shader program and shader attrbibute/uniform information
36
- Args:
37
- vert: str=None
38
- Path to the vertex shader. Defaults to internal if none is given
39
- frag: str=None
40
- Path to the fragment shader. Defaults to internal if none is given
41
- """
42
-
43
- self.engine = engine
44
- self.ctx = engine.ctx
45
-
46
- # Default class attributes values
47
- self.uniforms = []
48
- self.attribute_indices = []
49
- self.fmt = ''
50
- self.attributes = []
51
-
52
- # Default vertex and fragment shaders
53
- if vert == None: vert = self.engine.root + '/shaders/batch.vert'
54
- if frag == None: frag = self.engine.root + '/shaders/batch.frag'
55
-
56
- # Read the shaders
57
- with open(vert) as file:
58
- self.vertex_shader = file.read()
59
- with open(frag) as file:
60
- self.fragment_shader = file.read()
61
-
62
- # Hash value for references
63
- if vert == None and frag == None:
64
- self.hash = hash((self.vertex_shader, self.fragment_shader, 'default'))
65
- else:
66
- self.hash = hash((self.vertex_shader, self.fragment_shader))
67
-
68
- # Create a string of all lines in both shaders
69
- lines = f'{self.vertex_shader}\n{self.fragment_shader}'.split('\n')
70
-
71
- # Parse through shader to find uniforms and attributes
72
- for line in lines:
73
- tokens = line.strip().split(' ')
74
-
75
- # Add uniforms
76
- if tokens[0] == 'uniform' and len(tokens) > 2:
77
- self.uniforms.append(tokens[-1][:-1])
78
-
79
- # Add attributes
80
- if tokens[0] == 'layout' and len(tokens) > 2 and 'in' in line:
81
- self.attributes.append(tokens[-1][:-1])
82
-
83
- if tokens[-1][:-1] not in attribute_mappings: continue
84
- indices = attribute_mappings[tokens[-1][:-1]]
85
- self.attribute_indices.extend(indices)
86
- self.fmt += f'{len(indices)}f '
87
-
88
- # Create a program with shaders
89
- self.program = self.ctx.program(vertex_shader=self.vertex_shader, fragment_shader=self.fragment_shader)
90
-
91
- def set_main(self):
92
- """
93
- Selects a shader for use
94
- """
95
-
96
- self.engine.scene.shader_handler.add(self)
97
- self.engine.scene.node_handler.chunk_handler.update_all()
98
-
99
- def write(self, name: str, value) -> None:
100
- """
101
- Writes a uniform to the shader program
102
- """
103
-
104
- self.program[name].write(value)
105
-
106
- def __del__(self) -> int:
107
- if self.program: self.program.release()
108
-
109
- def __hash__(self) -> int:
1
+ import moderngl as mgl
2
+ import random
3
+
4
+ attribute_mappings = {
5
+ 'in_position' : [0, 1, 2],
6
+ 'in_uv' : [3, 4],
7
+ 'in_normal' : [5, 6, 7],
8
+ 'in_tangent' : [8, 9, 10],
9
+ 'in_bitangent' : [11, 12, 13],
10
+ 'obj_position' : [14, 15, 16],
11
+ 'obj_rotation' : [17, 18, 19, 20],
12
+ 'obj_scale' : [21, 22, 23],
13
+ 'obj_material' : [24],
14
+ }
15
+
16
+
17
+ class Shader:
18
+ program: mgl.Program=None
19
+ """Shader program for the vertex and fragment shader"""
20
+ vertex_shader: str
21
+ """String representation of the vertex shader"""
22
+ fragment_shader: str
23
+ """String representation of the vertex shader"""
24
+ uniforms: list[str]=[]
25
+ """List containg the names of all uniforms in the shader"""
26
+ attribute_indices: list[int]
27
+ """List of indices that map all possible shader attributes to the ones used byu the shader"""
28
+ fmt: str
29
+ """String representation of the format for building vaos"""
30
+ attributes: list[str]
31
+ """List representation of the attributes for building vaos"""
32
+
33
+ def __init__(self, engine, vert: str=None, frag: str=None) -> None:
34
+ """
35
+ Basilisk shader object. Contains shader program and shader attrbibute/uniform information
36
+ Args:
37
+ vert: str=None
38
+ Path to the vertex shader. Defaults to internal if none is given
39
+ frag: str=None
40
+ Path to the fragment shader. Defaults to internal if none is given
41
+ """
42
+
43
+ self.engine = engine
44
+ self.ctx = engine.ctx
45
+
46
+ # Default class attributes values
47
+ self.uniforms = []
48
+ self.attribute_indices = []
49
+ self.fmt = ''
50
+ self.attributes = []
51
+
52
+ # Default vertex and fragment shaders
53
+ if vert == None: vert = self.engine.root + '/shaders/batch.vert'
54
+ if frag == None: frag = self.engine.root + '/shaders/batch.frag'
55
+
56
+ # Read the shaders
57
+ with open(vert) as file:
58
+ self.vertex_shader = file.read()
59
+ with open(frag) as file:
60
+ self.fragment_shader = file.read()
61
+
62
+ # Hash value for references
63
+ if vert == None and frag == None:
64
+ self.hash = hash((self.vertex_shader, self.fragment_shader, 'default'))
65
+ else:
66
+ self.hash = hash((self.vertex_shader, self.fragment_shader))
67
+
68
+ # Create a string of all lines in both shaders
69
+ lines = f'{self.vertex_shader}\n{self.fragment_shader}'.split('\n')
70
+
71
+ # Parse through shader to find uniforms and attributes
72
+ for line in lines:
73
+ tokens = line.strip().split(' ')
74
+
75
+ # Add uniforms
76
+ if tokens[0] == 'uniform' and len(tokens) > 2:
77
+ self.uniforms.append(tokens[-1][:-1])
78
+
79
+ # Add attributes
80
+ if tokens[0] == 'layout' and len(tokens) > 2 and 'in' in line:
81
+ self.attributes.append(tokens[-1][:-1])
82
+
83
+ if tokens[-1][:-1] not in attribute_mappings: continue
84
+ indices = attribute_mappings[tokens[-1][:-1]]
85
+ self.attribute_indices.extend(indices)
86
+ self.fmt += f'{len(indices)}f '
87
+
88
+ # Create a program with shaders
89
+ self.program = self.ctx.program(vertex_shader=self.vertex_shader, fragment_shader=self.fragment_shader)
90
+
91
+ def set_main(self):
92
+ """
93
+ Selects a shader for use
94
+ """
95
+
96
+ self.engine.scene.shader_handler.add(self)
97
+ self.engine.scene.node_handler.chunk_handler.update_all()
98
+
99
+ def write(self, name: str, value) -> None:
100
+ """
101
+ Writes a uniform to the shader program
102
+ """
103
+
104
+ self.program[name].write(value)
105
+
106
+ def __del__(self) -> int:
107
+ if self.program: self.program.release()
108
+
109
+ def __hash__(self) -> int:
110
110
  return self.hash