本文将详细介绍如何使用Manim框架来实现一个逼真的闪电特效。

1. 实现原理

闪电特效通过LightningAnimation类实现,该类继承自Manim的基础Animation类,

主要基于以下几个核心原理:

1.1. 折线生成与随机偏移

闪电的基本形状是通过在起点和终点之间创建多个带有随机偏移的转折点来实现的:

1
2
3
4
5
6
7
8
9
10
11
12
13
from manim import *
# 初始化闪电的转折点
def initialize_points(self):
# 在起点和终点之间均匀分布转折点
for i in range(1, self.num_points + 1):
t = i / (self.num_points + 1)
# 基础位置
self.points[i] = self.start_point + t * self.vector
# 添加随机偏移
perpendicular = np.array([-self.direction[1], self.direction[0], 0])
offset_strength = self.distance * 0.1
random_offset = np.random.uniform(-offset_strength, offset_strength)
self.points[i] += random_offset * perpendicular

下载 Python 脚本

这段代码首先在起点和终点之间均匀分布转折点,然后对每个转折点添加垂直于闪电方向的随机偏移,从而模拟闪电不规则的锯齿形状。

1.2. 分叉闪电系统

为了增强真实感,代码实现了分叉闪电系统:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from manim import *
# 初始化分叉点
for f in range(self.num_forks):
# 随机选择分叉点在主闪电上的位置
fork_start_index = np.random.randint(2, self.num_points - 2)
fork_start_point = self.points[fork_start_index]

# 计算分叉方向
fork_direction = self.direction + np.array(
[np.random.uniform(-0.5, 0.5), np.random.uniform(-0.5, 0.5), 0]
)
fork_direction = fork_direction / np.linalg.norm(fork_direction)

# 计算分叉终点和转折点
# ...省略部分代码...

系统会随机选择主闪电上的点作为分叉起点,然后生成带有随机方向和长度的分叉闪电。每个分叉也有自己的随机转折点,使其看起来更加自然。

1.3. 动态变化效果

闪电效果的动态变化主要通过interpolate_mobject方法实现,该方法在动画过程中不断更新闪电的形状、颜色和透明度:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from manim import *
def interpolate_mobject(self, alpha):
# 计算时间参数,使用正弦波来创造闪烁效果
time = self.speed * alpha

# 动态调整转折点位置
# ...省略部分代码...

# 动态调整透明度,创造闪烁效果
alpha_value = self.alpha_range[0] + (
self.alpha_range[1] - self.alpha_range[0]
) * (0.5 + 0.5 * np.sin(time * 20))
self.lightning_line.set_stroke(opacity=alpha_value)

# 动态调整粗细
thickness = self.thickness_range[0] + (
self.thickness_range[1] - self.thickness_range[0]
) * (0.5 + 0.5 * np.sin(time * 15))
self.lightning_line.set_stroke(width=thickness)

# ...省略部分代码...

下载 Python 脚本

代码使用正弦函数创造闪电的闪烁和粗细变化效果,结合随机因素使每次播放的闪电效果都略有不同,增强了视觉真实感。

1.4. LightningAnimation 类的参数

LightningAnimation 类提供了丰富的参数选项,可以灵活调整闪电效果的各种特性。

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
from manim import *
class LightningAnimation(Animation):
"""
闪电特效动画类
通过创建多个带有尖锐转折的折线,并动态调整折线的形状、颜色和透明度来模拟闪电的效果
包含主闪电和分叉闪电效果
"""

def __init__(
self,
start_point,
end_point,
num_points=10,
thickness_range=(0.1, 0.5),
color=YELLOW,
alpha_range=(0.5, 1),
speed=1,
num_forks=3,
fork_probability=0.3,
**kwargs
):
"""
初始化闪电动画

参数:
- start_point: 闪电起点坐标
- end_point: 闪电终点坐标
- num_points: 闪电中的转折点数量
- thickness_range: 闪电粗细范围 (最小值, 最大值)
- color: 闪电颜色
- alpha_range: 透明度范围 (最小值, 最大值)
- speed: 动画速度
- num_forks: 分叉闪电的数量
- fork_probability: 分叉闪电出现的概率
"""
pass # 省略... ...

参数主要含义:

  1. 位置参数:
  • start_point : 闪电的起点坐标,使用 NumPy 数组表示 (x, y, z)
  • end_point : 闪电的终点坐标,同样使用 NumPy 数组表示 (x, y, z)
  1. 形状与复杂度参数:
  • num_points : 闪电中的转折点数量,默认为 10
    • 数值越大,闪电的路径越复杂,锯齿状越明显
    • 数值越小,闪电越接近直线
  • num_forks : 分叉闪电的数量,默认为 3
    • 决定了闪电会产生多少个分支
  • fork_probability : 分叉闪电出现的概率,默认为 0.3
    • 控制每次动画播放时,实际显示的分叉数量
    • 值为 0 时不会显示分叉,值为 1 时所有分叉都会显示
  1. 视觉效果参数:
  • thickness_range : 闪电粗细范围,格式为 (最小值, 最大值),默认为 (0.1, 0.5)
    • 控制闪电的最大和最小宽度
    • 闪电在动画过程中会在此范围内动态变化
  • color : 闪电颜色,默认为 YELLOW
    • 可以使用 Manim 提供的颜色常量,如 YELLOW、WHITE、BLUE 等
  • alpha_range : 透明度范围,格式为 (最小值, 最大值),默认为 (0.5, 1)
    • 控制闪电的透明度变化范围
    • 值越小,闪电越透明
  1. 动画参数:
  • speed : 动画速度,默认为 1
    • 控制闪电闪烁和动态变化的速率
    • 数值越大,闪电变化越快

2. 使用示例

LightningAnimation类提供了丰富的参数,可以灵活调整闪电效果。下面是几个不同场景的使用示例:

2.1. 示例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
41
42
43
44
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 LightningAnimation(Animation):
"""自定义动画:带随机转折与分叉的闪烁闪电。"""
def __init__(self, start_point, end_point, num_points=12,
color=YELLOW, seed=3, **kwargs):
line = VMobject().set_stroke(color, width=5)
super().__init__(line, **kwargs)
self.start_point = np.array(start_point, dtype=float)
self.end_point = np.array(end_point, dtype=float)
self.num_points = num_points
self.color = color
rng = np.random.default_rng(seed)
self.offsets = rng.uniform(-0.55, 0.55, num_points)
def interpolate_mobject(self, alpha):
vec = self.end_point - self.start_point
perp = np.array([-vec[1], vec[0], 0.0])
perp /= np.linalg.norm(perp)
flick = 0.5 + 0.5 * np.sin(alpha * 22)
pts = [self.start_point]
for i in range(1, self.num_points + 1):
t = i / (self.num_points + 1)
pts.append(self.start_point + t * vec
+ perp * self.offsets[i - 1] * flick)
pts.append(self.end_point)
self.mobject.set_points_as_corners(pts).set_stroke(
self.color, width=5, opacity=0.45 + 0.55 * flick)
class LightningLong(Scene):
def construct(self):
self.camera.background_color = "#0d0d1a"
self.add(head("闪电特效:长距离带分叉", "LightningAnimation / set_points_as_corners"))
main = LightningAnimation([-5, 2.6, 0], [5, -2.6, 0], num_points=16, seed=5, run_time=4.0)
fork = LightningAnimation([0.2, 0.1, 0], [2.6, -2.3, 0], num_points=7, color=BLUE, seed=9, run_time=4.0)
fork2 = LightningAnimation([-1.2, 0.5, 0], [-3.2, -2.0, 0], num_points=6, color=BLUE, seed=13, run_time=4.0)
self.add(main.mobject, fork.mobject, fork2.mobject)
self.play(main, fork, fork2, run_time=4.0)
self.play(FadeIn(foot("转折点加垂直方向随机偏移,正弦函数控制明暗闪烁")), run_time=0.4)
self.wait(1.4)

下载 Python 脚本

2.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
39
40
41
42
43
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 LightningAnimation(Animation):
"""自定义动画:带随机转折与分叉的闪烁闪电。"""
def __init__(self, start_point, end_point, num_points=12,
color=YELLOW, seed=3, **kwargs):
line = VMobject().set_stroke(color, width=5)
super().__init__(line, **kwargs)
self.start_point = np.array(start_point, dtype=float)
self.end_point = np.array(end_point, dtype=float)
self.num_points = num_points
self.color = color
rng = np.random.default_rng(seed)
self.offsets = rng.uniform(-0.55, 0.55, num_points)
def interpolate_mobject(self, alpha):
vec = self.end_point - self.start_point
perp = np.array([-vec[1], vec[0], 0.0])
perp /= np.linalg.norm(perp)
flick = 0.5 + 0.5 * np.sin(alpha * 22)
pts = [self.start_point]
for i in range(1, self.num_points + 1):
t = i / (self.num_points + 1)
pts.append(self.start_point + t * vec
+ perp * self.offsets[i - 1] * flick)
pts.append(self.end_point)
self.mobject.set_points_as_corners(pts).set_stroke(
self.color, width=5, opacity=0.45 + 0.55 * flick)
class LightningReverse(Scene):
def construct(self):
self.camera.background_color = "#0d0d1a"
self.add(head("闪电特效:反向与更多转折", "num_points = 12"))
main = LightningAnimation([5, 2.6, 0], [-5, -2.6, 0], num_points=12, color=YELLOW, seed=17, run_time=4.0)
fork = LightningAnimation([-0.5, 0.2, 0], [-3.0, -2.2, 0], num_points=6, color=TEAL, seed=21, run_time=4.0)
self.add(main.mobject, fork.mobject)
self.play(main, fork, run_time=4.0)
self.play(FadeIn(foot("起点终点互换,同一套算法即可生成不同走向的闪电")), run_time=0.4)
self.wait(1.4)

下载 Python 脚本

2.3. 示例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
36
37
38
39
40
41
42
43
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 LightningAnimation(Animation):
"""自定义动画:带随机转折与分叉的闪烁闪电。"""
def __init__(self, start_point, end_point, num_points=12,
color=YELLOW, seed=3, **kwargs):
line = VMobject().set_stroke(color, width=5)
super().__init__(line, **kwargs)
self.start_point = np.array(start_point, dtype=float)
self.end_point = np.array(end_point, dtype=float)
self.num_points = num_points
self.color = color
rng = np.random.default_rng(seed)
self.offsets = rng.uniform(-0.55, 0.55, num_points)
def interpolate_mobject(self, alpha):
vec = self.end_point - self.start_point
perp = np.array([-vec[1], vec[0], 0.0])
perp /= np.linalg.norm(perp)
flick = 0.5 + 0.5 * np.sin(alpha * 22)
pts = [self.start_point]
for i in range(1, self.num_points + 1):
t = i / (self.num_points + 1)
pts.append(self.start_point + t * vec
+ perp * self.offsets[i - 1] * flick)
pts.append(self.end_point)
self.mobject.set_points_as_corners(pts).set_stroke(
self.color, width=5, opacity=0.45 + 0.55 * flick)
class LightningVertical(Scene):
def construct(self):
self.camera.background_color = "#0d0d1a"
self.add(head("闪电特效:短距离垂直闪电", "num_points = 8"))
main = LightningAnimation([0, 3.2, 0], [0, -1.6, 0], num_points=8, color=YELLOW, seed=31, run_time=3.8)
fork = LightningAnimation([0, 0.6, 0], [1.8, -1.4, 0], num_points=5, color=BLUE, seed=37, run_time=3.8)
self.add(main.mobject, fork.mobject)
self.play(main, fork, run_time=3.8)
self.play(FadeIn(foot("缩短距离、减少转折点,得到更紧凑的放电效果")), run_time=0.4)
self.wait(1.6)

下载 Python 脚本

这些示例展示了如何通过调整参数来创建不同形状、颜色和特性的闪电效果,以适应不同的场景需求。

3. 总结

3.1. 特效特点

  1. 高度可定制:通过调整参数可以创建各种不同类型的闪电效果
  2. 真实感强:结合随机因素和动态变化,模拟真实闪电的不规则性和闪烁效果
  3. 层次丰富:主闪电和分叉闪电系统共同作用,创造出复杂而自然的闪电效果
  4. 易于集成:作为Manim的Animation子类,可以方便地与其他Manim动画元素结合使用

3.2. 使用场景

  1. 天气现象模拟:在气象相关的可视化中展示雷电现象
  2. 科幻与奇幻场景:为魔法效果、能量释放或科幻场景增添视觉冲击力
  3. 强调与过渡:作为场景转换或重点内容强调的动态效果
  4. 教育演示:在物理课程中演示电场放电等相关概念
  5. 艺术创作:用于抽象动画和视觉艺术作品

通过这个LightningAnimation类,我们可以在Manim项目中轻松实现逼真的闪电特效,为动画作品增添生动的视觉元素。