basilisk-engine 0.1.6__py3-none-any.whl → 0.1.8__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 (88) hide show
  1. basilisk/__init__.py +14 -13
  2. basilisk/audio/__init__.py +0 -0
  3. basilisk/audio/sound.py +32 -0
  4. basilisk/bsk_assets/cube.obj +48 -48
  5. basilisk/collisions/broad/broad_aabb.py +102 -102
  6. basilisk/collisions/broad/broad_bvh.py +137 -137
  7. basilisk/collisions/collider.py +95 -95
  8. basilisk/collisions/collider_handler.py +224 -224
  9. basilisk/collisions/narrow/contact_manifold.py +95 -95
  10. basilisk/collisions/narrow/dataclasses.py +34 -34
  11. basilisk/collisions/narrow/deprecated.py +46 -46
  12. basilisk/collisions/narrow/epa.py +91 -91
  13. basilisk/collisions/narrow/gjk.py +66 -66
  14. basilisk/collisions/narrow/graham_scan.py +24 -24
  15. basilisk/collisions/narrow/helper.py +29 -29
  16. basilisk/collisions/narrow/line_intersections.py +106 -106
  17. basilisk/collisions/narrow/sutherland_hodgman.py +75 -75
  18. basilisk/config.py +2 -2
  19. basilisk/draw/draw.py +100 -100
  20. basilisk/draw/draw_handler.py +179 -179
  21. basilisk/draw/font_renderer.py +28 -28
  22. basilisk/engine.py +202 -200
  23. basilisk/generic/abstract_bvh.py +15 -15
  24. basilisk/generic/abstract_custom.py +133 -133
  25. basilisk/generic/collisions.py +72 -72
  26. basilisk/generic/input_validation.py +66 -66
  27. basilisk/generic/math.py +6 -6
  28. basilisk/generic/matrices.py +35 -35
  29. basilisk/generic/meshes.py +72 -72
  30. basilisk/generic/quat.py +142 -142
  31. basilisk/generic/quat_methods.py +7 -7
  32. basilisk/generic/raycast_result.py +23 -23
  33. basilisk/generic/vec3.py +143 -143
  34. basilisk/input/mouse.py +61 -61
  35. basilisk/input/path.py +14 -14
  36. basilisk/mesh/cube.py +33 -33
  37. basilisk/mesh/mesh.py +230 -230
  38. basilisk/mesh/mesh_from_data.py +130 -130
  39. basilisk/mesh/model.py +271 -271
  40. basilisk/mesh/narrow_aabb.py +89 -89
  41. basilisk/mesh/narrow_bvh.py +91 -91
  42. basilisk/mesh/narrow_primative.py +23 -23
  43. basilisk/nodes/helper.py +28 -28
  44. basilisk/nodes/node.py +682 -682
  45. basilisk/nodes/node_handler.py +95 -95
  46. basilisk/particles/particle_handler.py +63 -63
  47. basilisk/particles/particle_renderer.py +87 -87
  48. basilisk/physics/impulse.py +112 -112
  49. basilisk/physics/physics_body.py +43 -43
  50. basilisk/physics/physics_engine.py +35 -35
  51. basilisk/render/batch.py +86 -86
  52. basilisk/render/camera.py +206 -206
  53. basilisk/render/chunk.py +99 -99
  54. basilisk/render/chunk_handler.py +154 -154
  55. basilisk/render/frame.py +101 -99
  56. basilisk/render/framebuffer.py +130 -105
  57. basilisk/render/image.py +87 -87
  58. basilisk/render/image_handler.py +122 -122
  59. basilisk/render/light.py +96 -96
  60. basilisk/render/light_handler.py +58 -58
  61. basilisk/render/material.py +219 -219
  62. basilisk/render/material_handler.py +135 -135
  63. basilisk/render/post_process.py +132 -132
  64. basilisk/render/shader.py +109 -109
  65. basilisk/render/shader_handler.py +79 -79
  66. basilisk/render/sky.py +120 -120
  67. basilisk/scene.py +264 -256
  68. basilisk/shaders/batch.frag +276 -276
  69. basilisk/shaders/batch.vert +115 -115
  70. basilisk/shaders/crt.frag +31 -31
  71. basilisk/shaders/draw.frag +21 -21
  72. basilisk/shaders/draw.vert +21 -21
  73. basilisk/shaders/filter.frag +22 -22
  74. basilisk/shaders/frame.frag +12 -12
  75. basilisk/shaders/frame.vert +13 -13
  76. basilisk/shaders/geometry.frag +8 -8
  77. basilisk/shaders/geometry.vert +41 -41
  78. basilisk/shaders/normal.frag +59 -59
  79. basilisk/shaders/normal.vert +96 -96
  80. basilisk/shaders/particle.frag +71 -71
  81. basilisk/shaders/particle.vert +84 -84
  82. basilisk/shaders/sky.frag +9 -9
  83. basilisk/shaders/sky.vert +13 -13
  84. {basilisk_engine-0.1.6.dist-info → basilisk_engine-0.1.8.dist-info}/METADATA +39 -45
  85. basilisk_engine-0.1.8.dist-info/RECORD +103 -0
  86. {basilisk_engine-0.1.6.dist-info → basilisk_engine-0.1.8.dist-info}/WHEEL +1 -1
  87. basilisk_engine-0.1.6.dist-info/RECORD +0 -101
  88. {basilisk_engine-0.1.6.dist-info → basilisk_engine-0.1.8.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()
@@ -1,133 +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()
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 or detination.size != self.size:
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
133
  return image