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.resize_points(len(points))
|
||||||
self.data["points"][:] = 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):
|
def get_points(self):
|
||||||
return self.data["points"]
|
return self.data["points"]
|
||||||
|
|
||||||
|
@ -748,6 +753,7 @@ class Mobject(object):
|
||||||
submob.fade_to(color, alpha)
|
submob.fade_to(color, alpha)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
# TODO, get rid of this
|
||||||
def fade(self, darkness=0.5, family=True):
|
def fade(self, darkness=0.5, family=True):
|
||||||
if family:
|
if family:
|
||||||
for submob in self.submobjects:
|
for submob in self.submobjects:
|
||||||
|
@ -1147,7 +1153,7 @@ class Mobject(object):
|
||||||
def push_self_into_submobjects(self):
|
def push_self_into_submobjects(self):
|
||||||
copy = self.deepcopy()
|
copy = self.deepcopy()
|
||||||
copy.set_submobjects([])
|
copy.set_submobjects([])
|
||||||
self.reset_points()
|
self.resize_points(0)
|
||||||
self.add(copy)
|
self.add(copy)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
@ -1329,15 +1335,12 @@ class Mobject(object):
|
||||||
self.shader_wrapper.refresh_id()
|
self.shader_wrapper.refresh_id()
|
||||||
return self
|
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
|
# If possible, try to populate an existing array, rather
|
||||||
# than recreating it each frame
|
# than recreating it each frame
|
||||||
arr = getattr(self, name)
|
if self.shader_data.size != size:
|
||||||
if arr.size != size:
|
self.shader_data = resize_array(self.shader_data, size)
|
||||||
new_arr = resize_array(arr, size)
|
return self.shader_data
|
||||||
setattr(self, name, new_arr)
|
|
||||||
return new_arr
|
|
||||||
return arr
|
|
||||||
|
|
||||||
def get_shader_wrapper(self):
|
def get_shader_wrapper(self):
|
||||||
self.shader_wrapper.vert_data = self.get_shader_data()
|
self.shader_wrapper.vert_data = self.get_shader_data()
|
||||||
|
@ -1364,8 +1367,9 @@ class Mobject(object):
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def get_shader_data(self):
|
def get_shader_data(self):
|
||||||
# May be different for subclasses
|
data = self.get_resized_shader_data_array(self.get_num_points())
|
||||||
return self.shader_data
|
data["point"] = self.data["points"]
|
||||||
|
return data
|
||||||
|
|
||||||
def get_shader_uniforms(self):
|
def get_shader_uniforms(self):
|
||||||
return {
|
return {
|
||||||
|
|
|
@ -4,10 +4,8 @@ from PIL import Image
|
||||||
|
|
||||||
from manimlib.constants import *
|
from manimlib.constants import *
|
||||||
from manimlib.mobject.mobject import Mobject
|
from manimlib.mobject.mobject import Mobject
|
||||||
from manimlib.utils.bezier import interpolate
|
|
||||||
from manimlib.utils.bezier import inverse_interpolate
|
from manimlib.utils.bezier import inverse_interpolate
|
||||||
from manimlib.utils.images import get_full_raster_image_path
|
from manimlib.utils.images import get_full_raster_image_path
|
||||||
from manimlib.utils.iterables import listify
|
|
||||||
|
|
||||||
|
|
||||||
class ImageMobject(Mobject):
|
class ImageMobject(Mobject):
|
||||||
|
@ -28,36 +26,25 @@ class ImageMobject(Mobject):
|
||||||
self.texture_paths = {"Texture": path}
|
self.texture_paths = {"Texture": path}
|
||||||
super().__init__(**kwargs)
|
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):
|
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
|
size = self.image.size
|
||||||
self.set_width(2 * size[0] / size[1], stretch=True)
|
self.set_width(2 * size[0] / size[1], stretch=True)
|
||||||
self.set_height(self.height)
|
self.set_height(self.height)
|
||||||
|
|
||||||
def init_colors(self):
|
def set_opacity(self, opacity, family=True):
|
||||||
self.set_opacity(self.opacity)
|
# TODO, account for opacity coming in as an array
|
||||||
|
mobs = self.get_family() if family else [self]
|
||||||
def set_opacity(self, alpha, family=True):
|
for mob in mobs:
|
||||||
opacity = listify(alpha)
|
mob.data["opacity"][:, 0] = opacity
|
||||||
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)
|
|
||||||
return self
|
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):
|
def point_to_rgb(self, point):
|
||||||
x0, y0 = self.get_corner(UL)[:2]
|
x0, y0 = self.get_corner(UL)[:2]
|
||||||
x1, y1 = self.get_corner(DR)[:2]
|
x1, y1 = self.get_corner(DR)[:2]
|
||||||
|
@ -75,8 +62,7 @@ class ImageMobject(Mobject):
|
||||||
return np.array(rgb) / 255
|
return np.array(rgb) / 255
|
||||||
|
|
||||||
def get_shader_data(self):
|
def get_shader_data(self):
|
||||||
data = self.get_blank_shader_data_array(len(self.points))
|
data = super().get_shader_data()
|
||||||
data["point"] = self.points
|
data["im_coords"] = self.data["im_coords"]
|
||||||
data["im_coords"] = self.im_coords
|
data["opacity"] = self.data["opacity"]
|
||||||
data["opacity"] = self.opacity
|
|
||||||
return data
|
return data
|
||||||
|
|
|
@ -173,7 +173,7 @@ class ParametricSurface(Mobject):
|
||||||
# For shaders
|
# For shaders
|
||||||
def get_shader_data(self):
|
def get_shader_data(self):
|
||||||
s_points, du_points, dv_points = self.get_surface_points_and_nudged_points()
|
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["point"] = s_points
|
||||||
data["du_point"] = du_points
|
data["du_point"] = du_points
|
||||||
data["dv_point"] = dv_points
|
data["dv_point"] = dv_points
|
||||||
|
@ -274,7 +274,7 @@ class TexturedSurface(ParametricSurface):
|
||||||
result["num_textures"] = self.num_textures
|
result["num_textures"] = self.num_textures
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def fill_in_shader_color_info(self, data):
|
def fill_in_shader_color_info(self, shader_data):
|
||||||
data["im_coords"] = self.data["im_coords"]
|
shader_data["im_coords"] = self.data["im_coords"]
|
||||||
data["opacity"] = self.data["opacity"]
|
shader_data["opacity"] = self.data["opacity"]
|
||||||
return data
|
return shader_data
|
||||||
|
|
Loading…
Add table
Reference in a new issue