Manim實現閃光軌跡特效
在動畫制作中,軌跡特效常常用于增強視覺效果,而帶有閃光效果的軌跡更是能夠吸引觀眾的注意力。
本文將介紹如何使用Manim動畫庫實現閃光軌跡特效。
1. 實現原理
下面的GlowingTracedPath類參考了Manim中的TracePath類,增加了閃光和漸變色的效果。
class GlowingTracedPath(VMobject):
def __init__(
self,
traced_point_func: Callable,
stroke_width: float = 2,
stroke_color: ParsableManimColor | None = WHITE,
dissipating_time: float | None = None,
gradient_colors: list[ParsableManimColor] | None = None, #
glow_speed: int = 2,
**kwargs,
):
"""
構造函數
Parameters:
traced_point_func - 回調函數,用于獲取被追蹤點的當前位置
stroke_width - 軌跡的寬度,默認為 2
stroke_color - 軌跡的基本顏色,默認為白色
dissipating_time - 軌跡的消散時間
gradient_colors - 顏色漸變列表
glow_speed - 閃光速度,控制軌跡閃光效果的快慢
**kwargs - 用于傳遞額外的關鍵字參數到父類VMobject的構造函數中
"""
super().__init__(
stroke_color=stroke_color,
stroke_width=stroke_width,
**kwargs,
)
self.glow_speed = glow_speed
if not gradient_colors is None:
self.set_color_by_gradient(gradient_colors)
self.set_stroke(opacity=0.2)
self.traced_point_func = traced_point_func
self.dissipating_time = dissipating_time
self.time = 1
self.add_updater(self.update_path)
def update_path(self, mob, dt):
# 更新軌跡中的點
new_point = self.traced_point_func()
if not self.has_points():
self.start_new_path(new_point)
self.time += dt
t = self.time * self.glow_speed # 控制脈沖速度
intensity = 0.25 * np.sin(t) + 0.75 # 0.5-1之間波動
# 動態調整透明度
self.set_stroke(
opacity=intensity, # 透明度波動
)
self.add_line_to(new_point)
# 軌跡消散
if self.dissipating_time:
if self.time - 1 > self.dissipating_time:
nppcc = self.n_points_per_curve
self.set_points(self.points[nppcc:])
初始化函數(__init__)的參數,代碼中已經有詳細的注釋,不再贅述。
至于更新軌跡的update_path方法,它的關鍵邏輯有:
- 獲取新點:通過
traced_point_func獲取被追蹤點的當前位置,并將其添加到軌跡的末尾 - 動態調整透明度:使用正弦函數
np.sin生成一個在0.5到1之間波動的強度值intensity,并通過set_stroke方法動態調整軌跡的透明度,從而實現閃爍效果 - 軌跡消散:如果設置了
dissipating_time參數,當軌跡的持續時間超過該值時,會移除軌跡的起始部分,使軌跡逐漸消失
此外,GlowingTracedPath類還會通過set_color_by_gradient方法為軌跡設置顏色漸變效果。
2. 使用示例
接下來我們通過幾個示例來看看如何使用這個特效。
2.1. 漸變色軌跡
基于上面我們實現的GlowingTracedPath類,實現一個漸變色的軌跡非常簡單。
a = Dot(RIGHT * 2)
b = GlowingTracedPath(
a.get_center,
gradient_colors=[BLUE, RED, YELLOW],
)
self.add(a, b)
self.play(a.animate(path_arc=PI / 2).shift(LEFT * 2), run_time=2)
self.play(a.animate(path_arc=-PI / 2).shift(LEFT * 2), run_time=2)
代碼中,軌跡b追蹤點a的位置,形成一個由參數gradient_colors指定的漸變色軌跡。

2.2. 軌跡逐漸消失
上面的示例中,軌跡不會消失,通過參數dissipating_time,可以指定軌跡存在的時間(單位:秒)。
b = GlowingTracedPath(
a.get_center,
gradient_colors=[BLUE, RED, YELLOW],
dissipating_time=0.5,
)
將軌跡b的dissipating_time設置為0.5,也就是新產生的軌跡會在0.5秒后逐漸消失。

2.3. 軌跡閃爍
最后,來看看軌跡的閃光效果,下面的通過設置兩種不同的glow_speed(閃爍的速度),
來比較不同速度下的閃爍效果。
a1 = Dot()
a2 = Dot()
b1 = GlowingTracedPath(
a1.get_center,
gradient_colors=[BLUE, RED, YELLOW],
glow_speed=8,
)
b2 = GlowingTracedPath(
a2.get_center,
gradient_colors=[BLUE, RED, YELLOW],
glow_speed=16,
)
self.add(a1, b1, a2, b2)
heart1 = ImplicitFunction(
lambda x, y: (x**2 + y**2 - 1) ** 3 - x**2 * y**3,
color=PINK,
x_range=[-1.5, 1.5],
y_range=[-1.2, 1.8],
).shift(LEFT * 1.5)
heart2 = ImplicitFunction(
lambda x, y: (x**2 + y**2 - 1) ** 3 - x**2 * y**3,
color=PINK,
x_range=[-1.5, 1.5],
y_range=[-1.2, 1.8],
).shift(RIGHT * 1.5)
self.play(MoveAlongPath(a1, heart1), MoveAlongPath(a2, heart2), run_time=4)

左邊的心形glow_speed=8,右邊的心形glow_speed=16,所以右邊的閃爍速度會快一些。
3. 總結
通過GlowingTracedPath類,我們可以輕松地在Manim中實現帶有閃光效果的軌跡。
這個類通過動態調整透明度和顏色漸變,結合軌跡的實時更新,創造出了引人注目的視覺效果。
無論是用于數學動畫還是其他創意項目,這種特效都能為你的作品增添獨特的魅力。

浙公網安備 33010602011771號