Skip to content

Commit 08607e2

Browse files
committed
Beginning to port shaders example
1 parent 71da505 commit 08607e2

File tree

4 files changed

+243
-202
lines changed

4 files changed

+243
-202
lines changed

manim/mobject/opengl_mobject.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ class OpenGLMobject:
3838
"""
3939
Mathematical Object
4040
"""
41+
shader_dtype=[
42+
("point", np.float32, (3,)),
43+
]
44+
shader_folder = ""
4145

4246
def __init__(
4347
self,
@@ -50,16 +54,12 @@ def __init__(
5054
# Positive shadow up to 1 makes a side opposite the light darker
5155
shadow=0.0,
5256
# For shaders
53-
shader_folder="",
5457
render_primitive=moderngl.TRIANGLE_STRIP,
5558
texture_paths=None,
5659
depth_test=False,
5760
# If true, the mobject will not get rotated according to camera position
5861
is_fixed_in_frame=False,
5962
# Must match in attributes of vert shader
60-
shader_dtype=[
61-
("point", np.float32, (3,)),
62-
],
6363
# Event listener
6464
listen_to_events=False,
6565
**kwargs
@@ -74,14 +74,12 @@ def __init__(
7474
# Positive shadow up to 1 makes a side opposite the light darker
7575
self.shadow = shadow
7676
# For shaders
77-
self.shader_folder = shader_folder
7877
self.render_primitive = render_primitive
7978
self.texture_paths = texture_paths
8079
self.depth_test = depth_test
8180
# If true, the mobject will not get rotated according to camera position
8281
self.is_fixed_in_frame = is_fixed_in_frame
8382
# Must match in attributes of vert shader
84-
self.shader_dtype = shader_dtype
8583
# Event listener
8684
self.listen_to_events = listen_to_events
8785

@@ -817,7 +815,7 @@ def space_out_submobjects(self, factor=1.5, **kwargs):
817815
def move_to(
818816
self, point_or_mobject, aligned_edge=ORIGIN, coor_mask=np.array([1, 1, 1])
819817
):
820-
if isinstance(point_or_mobject, Mobject):
818+
if isinstance(point_or_mobject, OpenGLMobject):
821819
target = point_or_mobject.get_bounding_box_point(aligned_edge)
822820
else:
823821
target = point_or_mobject
@@ -1384,11 +1382,11 @@ def set_color_by_xyz_func(
13841382

13851383
def init_shader_data(self):
13861384
# TODO, only call this when needed?
1387-
self.shader_data = np.zeros(len(self.get_points()), dtype=self.shader_dtype)
1385+
self.shader_data = np.zeros(len(self.get_points()), dtype=self.__class__.shader_dtype)
13881386
self.shader_indices = None
13891387
self.shader_wrapper = ShaderWrapper(
13901388
vert_data=self.shader_data,
1391-
shader_folder=self.shader_folder,
1389+
shader_folder=self.__class__.shader_folder,
13921390
texture_paths=self.texture_paths,
13931391
depth_test=self.depth_test,
13941392
render_primitive=self.render_primitive,
@@ -1404,8 +1402,9 @@ def get_shader_wrapper(self):
14041402
vert_indices=self.get_shader_vert_indices(),
14051403
uniforms = self.get_shader_uniforms(),
14061404
depth_test = self.depth_test,
1405+
texture_paths=self.texture_paths,
14071406
render_primitive=self.render_primitive,
1408-
shader_folder=self.shader_folder,
1407+
shader_folder=self.__class__.shader_folder,
14091408
)
14101409
return self.shader_wrapper
14111410

manim/mobject/types/opengl_surface.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ def index_dot(index):
184184
# For shaders
185185
def get_shader_data(self):
186186
s_points, du_points, dv_points = self.get_surface_points_and_nudged_points()
187-
shader_data = np.zeros(len(s_points), dtype=OpenGLSurface.shader_dtype)
187+
shader_data = np.zeros(len(s_points), dtype=self.__class__.shader_dtype)
188188
if "points" not in self.locked_data_keys:
189189
shader_data["point"] = s_points
190190
shader_data["du_point"] = du_points
@@ -218,22 +218,20 @@ class OpenGLTexturedSurface(OpenGLSurface):
218218
("im_coords", np.float32, (2,)),
219219
("opacity", np.float32, (1,)),
220220
]
221+
shader_folder = "textured_surface"
221222

222223
def __init__(
223224
self, uv_surface, image_file, dark_image_file=None, shader_folder=None, **kwargs
224225
):
225226
if not isinstance(uv_surface, OpenGLSurface):
226227
raise Exception("uv_surface must be of type OpenGLSurface")
227-
shader_folder = (
228-
shader_folder if shader_folder is not None else "textured_surface"
229-
)
230228
# Set texture information
231229
if dark_image_file is None:
232230
dark_image_file = image_file
233231
self.num_textures = 1
234232
else:
235233
self.num_textures = 2
236-
self.texture_paths = {
234+
texture_paths = {
237235
"LightTexture": get_full_raster_image_path(image_file),
238236
"DarkTexture": get_full_raster_image_path(dark_image_file),
239237
}
@@ -244,7 +242,7 @@ def __init__(
244242
self.v_range = uv_surface.v_range
245243
self.resolution = uv_surface.resolution
246244
self.gloss = self.uv_surface.gloss
247-
super().__init__(**kwargs)
245+
super().__init__(texture_paths=texture_paths, **kwargs)
248246

249247
def init_data(self):
250248
super().init_data()

0 commit comments

Comments
 (0)