mirror of
https://github.com/3b1b/manim.git
synced 2025-08-05 16:49:03 +00:00
Setup Image/Surface for new data formatting
This commit is contained in:
parent
9314dfd933
commit
96fbac85ad
3 changed files with 34 additions and 44 deletions
|
@ -102,6 +102,11 @@ class Mobject(object):
|
|||
self.resize_points(len(points))
|
||||
self.data["points"][:] = points
|
||||
|
||||
def append_points(self, new_points):
|
||||
self.resize_points(self.get_num_points() + len(new_points))
|
||||
self.data["points"][-len(new_points):] = new_points
|
||||
return self
|
||||
|
||||
def get_points(self):
|
||||
return self.data["points"]
|
||||
|
||||
|
@ -748,6 +753,7 @@ class Mobject(object):
|
|||
submob.fade_to(color, alpha)
|
||||
return self
|
||||
|
||||
# TODO, get rid of this
|
||||
def fade(self, darkness=0.5, family=True):
|
||||
if family:
|
||||
for submob in self.submobjects:
|
||||
|
@ -1147,7 +1153,7 @@ class Mobject(object):
|
|||
def push_self_into_submobjects(self):
|
||||
copy = self.deepcopy()
|
||||
copy.set_submobjects([])
|
||||
self.reset_points()
|
||||
self.resize_points(0)
|
||||
self.add(copy)
|
||||
return self
|
||||
|
||||
|
@ -1329,15 +1335,12 @@ class Mobject(object):
|
|||
self.shader_wrapper.refresh_id()
|
||||
return self
|
||||
|
||||
def get_blank_shader_data_array(self, size, name="shader_data"):
|
||||
def get_resized_shader_data_array(self, size):
|
||||
# If possible, try to populate an existing array, rather
|
||||
# than recreating it each frame
|
||||
arr = getattr(self, name)
|
||||
if arr.size != size:
|
||||
new_arr = resize_array(arr, size)
|
||||
setattr(self, name, new_arr)
|
||||
return new_arr
|
||||
return arr
|
||||
if self.shader_data.size != size:
|
||||
self.shader_data = resize_array(self.shader_data, size)
|
||||
return self.shader_data
|
||||
|
||||
def get_shader_wrapper(self):
|
||||
self.shader_wrapper.vert_data = self.get_shader_data()
|
||||
|
@ -1364,8 +1367,9 @@ class Mobject(object):
|
|||
return result
|
||||
|
||||
def get_shader_data(self):
|
||||
# May be different for subclasses
|
||||
return self.shader_data
|
||||
data = self.get_resized_shader_data_array(self.get_num_points())
|
||||
data["point"] = self.data["points"]
|
||||
return data
|
||||
|
||||
def get_shader_uniforms(self):
|
||||
return {
|
||||
|
|
|
@ -4,10 +4,8 @@ from PIL import Image
|
|||
|
||||
from manimlib.constants import *
|
||||
from manimlib.mobject.mobject import Mobject
|
||||
from manimlib.utils.bezier import interpolate
|
||||
from manimlib.utils.bezier import inverse_interpolate
|
||||
from manimlib.utils.images import get_full_raster_image_path
|
||||
from manimlib.utils.iterables import listify
|
||||
|
||||
|
||||
class ImageMobject(Mobject):
|
||||
|
@ -28,36 +26,25 @@ class ImageMobject(Mobject):
|
|||
self.texture_paths = {"Texture": path}
|
||||
super().__init__(**kwargs)
|
||||
|
||||
def init_data(self):
|
||||
self.data = {
|
||||
"points": np.array([UL, DL, UR, DR]),
|
||||
"im_coords": np.array([(0, 0), (0, 1), (1, 0), (1, 1)]),
|
||||
"opacity": np.array([[self.opacity]], dtype=np.float32),
|
||||
}
|
||||
|
||||
def init_points(self):
|
||||
self.points = np.array([UL, DL, UR, DR])
|
||||
self.im_coords = np.array([(0, 0), (0, 1), (1, 0), (1, 1)])
|
||||
size = self.image.size
|
||||
self.set_width(2 * size[0] / size[1], stretch=True)
|
||||
self.set_height(self.height)
|
||||
|
||||
def init_colors(self):
|
||||
self.set_opacity(self.opacity)
|
||||
|
||||
def set_opacity(self, alpha, family=True):
|
||||
opacity = listify(alpha)
|
||||
diff = 4 - len(opacity)
|
||||
opacity += [opacity[-1]] * diff
|
||||
self.opacity = np.array(opacity).reshape((4, 1))
|
||||
|
||||
if family:
|
||||
for sm in self.submobjects:
|
||||
sm.set_opacity(alpha)
|
||||
|
||||
def fade(self, darkness=0.5, family=True):
|
||||
self.set_opacity(1 - darkness, family)
|
||||
def set_opacity(self, opacity, family=True):
|
||||
# TODO, account for opacity coming in as an array
|
||||
mobs = self.get_family() if family else [self]
|
||||
for mob in mobs:
|
||||
mob.data["opacity"][:, 0] = opacity
|
||||
return self
|
||||
|
||||
def interpolate_color(self, mobject1, mobject2, alpha):
|
||||
# TODO, transition between actual images?
|
||||
self.opacity = interpolate(
|
||||
mobject1.opacity, mobject2.opacity, alpha
|
||||
)
|
||||
|
||||
def point_to_rgb(self, point):
|
||||
x0, y0 = self.get_corner(UL)[:2]
|
||||
x1, y1 = self.get_corner(DR)[:2]
|
||||
|
@ -75,8 +62,7 @@ class ImageMobject(Mobject):
|
|||
return np.array(rgb) / 255
|
||||
|
||||
def get_shader_data(self):
|
||||
data = self.get_blank_shader_data_array(len(self.points))
|
||||
data["point"] = self.points
|
||||
data["im_coords"] = self.im_coords
|
||||
data["opacity"] = self.opacity
|
||||
data = super().get_shader_data()
|
||||
data["im_coords"] = self.data["im_coords"]
|
||||
data["opacity"] = self.data["opacity"]
|
||||
return data
|
||||
|
|
|
@ -173,7 +173,7 @@ class ParametricSurface(Mobject):
|
|||
# For shaders
|
||||
def get_shader_data(self):
|
||||
s_points, du_points, dv_points = self.get_surface_points_and_nudged_points()
|
||||
data = self.get_blank_shader_data_array(len(s_points))
|
||||
data = self.get_resized_shader_data_array(len(s_points))
|
||||
data["point"] = s_points
|
||||
data["du_point"] = du_points
|
||||
data["dv_point"] = dv_points
|
||||
|
@ -274,7 +274,7 @@ class TexturedSurface(ParametricSurface):
|
|||
result["num_textures"] = self.num_textures
|
||||
return result
|
||||
|
||||
def fill_in_shader_color_info(self, data):
|
||||
data["im_coords"] = self.data["im_coords"]
|
||||
data["opacity"] = self.data["opacity"]
|
||||
return data
|
||||
def fill_in_shader_color_info(self, shader_data):
|
||||
shader_data["im_coords"] = self.data["im_coords"]
|
||||
shader_data["opacity"] = self.data["opacity"]
|
||||
return shader_data
|
||||
|
|
Loading…
Add table
Reference in a new issue