在数学动画的世界里,旋转与变色特效无疑是最能吸引观众眼球的元素之一。
今天,就让我们一起探索如何使用Manim框架来实现自定义的旋转变色特效吧!
1. 实现原理
Manim的动画魔法源于Animation类的interpolate_mobject方法。
这个方法通过alpha参数(0到1之间)控制动画进度,我们只需重写这个方法就能创造自定义特效:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| from manim import * class RotateAndColor(Animation): def __init__( self, mobject, angle=TAU, start_color=BLUE, end_color=RED, about_point=ORIGIN, **kwargs ): """ 构造函数。
Parameters: mobject - 这是要进行动画的对象,例如一个几何形状、文本或其他任何 Manim 支持的对象 angle - 指定旋转的总角度,默认值为 TAU 即 360度 start_color - 动画开始时对象的颜色,默认蓝色 end_color - 动画结束时对象的颜色,默认为红色 **kwargs - 用于传递额外的关键字参数到父类Animation的构造函数中 """
super().__init__(mobject, **kwargs)
self.angle = angle self.start_color = start_color self.end_color = end_color self.about_point = about_point
def interpolate_mobject(self, alpha): """ 实现动画效果的核心方法。
Parameters: alpha - 是一个介于 0 和 1 之间的参数,表示动画的进度 """ pass
|
下载 Python 脚本
接下来,实现同一个动画周期内同时控制旋转和颜色:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| from manim import * def interpolate_mobject(self, alpha): """ 实现动画效果的核心方法。
Parameters: alpha - 是一个介于 0 和 1 之间的参数,表示动画的进度 """ new_color = interpolate_color(self.start_color, self.end_color, alpha)
current_angle = alpha * self.angle
self.mobject.become(self.starting_mobject)
self.mobject.rotate(current_angle, about_point=self.about_point)
self.mobject.set_color(new_color)
|
下载 Python 脚本
上面的代码中已经加了详细的注释,总得来说,RotateAndColor类实现了一个同时进行旋转和颜色变化的动画效果。
它通过继承Animation类,利用Manim的动画系统,实现了对对象的旋转和颜色变化。
通过interpolate_mobject方法,根据动画进度alpha,动态计算旋转角度和颜色,从而实现平滑的动画效果。
2. 应用示例
实现了这个自定义的旋转变色动画(RotateAndColor)后,下面来实际使用看看效果如何。
2.1. 基础使用
在基础使用示例中,我们构造一个正方体,先快速旋转3圈,同时颜色由蓝变绿;
再慢速旋转半圈,同时颜色又绿变紫。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| from manim import * import numpy as np def head(cn, en): t = Text(cn, font="STLiti", font_size=27).to_edge(UP) s = Text(en, font="Consolas", font_size=20, color=YELLOW).next_to(t, DOWN, buff=0.10) return VGroup(t, s) def foot(cn): return Text(cn, font="STLiti", font_size=20, color=YELLOW).to_edge(DOWN) class RotateAndColor(Animation): """自定义动画:旋转的同时做颜色插值。""" def __init__(self, mobject, angle=TAU, start_color=BLUE, end_color=RED, about_point=None, **kwargs): self.base = mobject.copy() self.angle = angle self.start_color = start_color self.end_color = end_color self.about_point = about_point super().__init__(mobject, **kwargs) def interpolate_mobject(self, alpha): self.mobject.become(self.base) self.mobject.rotate(alpha * self.angle, about_point=self.about_point or self.base.get_center()) col = interpolate_color(self.start_color, self.end_color, alpha) self.mobject.set_stroke(col, width=7) if hasattr(self.mobject, "set_fill"): self.mobject.set_fill(col, opacity=0.35) class RotateColorBasic(Scene): def construct(self): self.add(head("旋转变色:自定义 Animation", "RotateAndColor / interpolate_mobject")) sq = Square(side_length=2.2, color=BLUE).set_fill(BLUE, opacity=0.3).shift(LEFT * 2.6) tri = RegularPolygon(n=5, color=BLUE).scale(1.1).shift(RIGHT * 2.6) self.add(sq, tri) self.play(RotateAndColor(sq, angle=TAU, start_color=BLUE, end_color=RED, run_time=3)) self.play(RotateAndColor(tri, angle=-TAU, start_color=GREEN, end_color=YELLOW, run_time=3)) self.play(FadeIn(foot("become 还原初始状态,避免逐帧累积旋转")), run_time=0.4) self.wait(1.0)
|
下载 Python 脚本
2.2. 模拟公转自转
下面再构造一个略微复杂点的示例,构造两个球形模拟太阳和地球,太阳自转同时变色;
地球绕着太阳公转,且在绕的过程中逐渐变色(可用来模拟白天黑夜)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| from manim import * import numpy as np def head(cn, en): t = Text(cn, font="STLiti", font_size=27).to_edge(UP) s = Text(en, font="Consolas", font_size=20, color=YELLOW).next_to(t, DOWN, buff=0.10) return VGroup(t, s) def foot(cn): return Text(cn, font="STLiti", font_size=20, color=YELLOW).to_edge(DOWN) class RotateAndColor(Animation): """自定义动画:旋转的同时做颜色插值。""" def __init__(self, mobject, angle=TAU, start_color=BLUE, end_color=RED, about_point=None, **kwargs): self.base = mobject.copy() self.angle = angle self.start_color = start_color self.end_color = end_color self.about_point = about_point super().__init__(mobject, **kwargs) def interpolate_mobject(self, alpha): self.mobject.become(self.base) self.mobject.rotate(alpha * self.angle, about_point=self.about_point or self.base.get_center()) col = interpolate_color(self.start_color, self.end_color, alpha) self.mobject.set_stroke(col, width=7) if hasattr(self.mobject, "set_fill"): self.mobject.set_fill(col, opacity=0.35) class RotateColor3D(ThreeDScene): def construct(self): self.set_camera_orientation(phi=70 * DEGREES, theta=-45 * DEGREES) self.add_fixed_in_frame_mobjects(head("三维版:太阳自转与地球公转", "ThreeDScene + RotateAndColor")) axes = ThreeDAxes(x_length=7, y_length=7, z_length=4) sun = Sphere(radius=0.9, resolution=(18, 18)).set_fill(YELLOW, opacity=0.9) earth = Sphere(radius=0.38, resolution=(14, 14)).set_fill(BLUE, opacity=0.9).shift(RIGHT * 2.6) orbit = Circle(radius=2.6, color=GREY_B).set_stroke(width=2, opacity=0.6) self.add(axes, sun, orbit, earth) self.play(RotateAndColor(sun, angle=PI, start_color=YELLOW, end_color=RED, run_time=3)) self.play(Rotate(earth, angle=TAU, axis=OUT, about_point=ORIGIN, run_time=3)) self.add_fixed_in_frame_mobjects(foot("三维对象同样使用 interpolatemobject 逐帧插值")) self.wait(1.6)
|
下载 Python 脚本
3. 总结
通过上面介绍的方法,相信您已经掌握了制作专业级数学动画的核心技巧。
感兴趣的话,尝试修改示例代码中的参数,或者将旋转变色效果与缩放、位移等动画组合使用,创造出属于您的独特视觉效果!
主观感受,仅供参考。本文采用 本文采用 CC BY-NC-SA 4.0 许可 协议,
完整转载请注明来自
数学动画引擎。