Sphere类用于创建三维球体对象,它提供了丰富的参数和方法来定制球体的外观和行为。

球体在制作三维动画时,具有广泛的应用场景。

比如:

  • 展示几何概念:通过创建不同大小、颜色和透明度的球体,可以直观地展示几何中的体积、表面积等概念
  • 物理模拟:在模拟物理现象(如重力、碰撞等)时,可以使用Sphere类来代表球体物体
  • 天文模拟:在展示天文现象(如行星运动、星系结构等)时,Sphere类可以用于创建星球、恒星等天体模型

1. 主要参数

Sphere类的主要参数包括:

参数名称 类型 说明
center Point3D 球体的中心位置
radius float 球体的半径
resolution int 球体的细分程度。这个参数通常用于控制球体表面网格的密度或光滑程度。
u_range [float] 定义了球体在u方向上的参数化范围
v_range [float] 定义了球体在v方向上的参数化范围

resolution参数设置较大时,会生成更精细、更光滑的球体表面,但也会增加计算量和内存使用。

2. 使用示例

Sphere类的使用比较简单,下面的示例重点展示其参数的不同使用方式。

2.1. 基本球体

这个示例创建了一个位于原点、半径为1的基本球体。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from manim import *
import numpy as np
from numpy import array, pi, cos, sin
run_time = 1
data = [[1, 2], [3, 4]]
def dot(pos, **kwargs):
return Dot(pos, **kwargs)
class N100_04(Scene):
def construct(self):
sc = self
s = Sphere(radius=1, center=np.array([1, 0, -1]))
self.play(FadeIn(s))
self.play(s.animate.move_to(np.array([-1.5, -1, 1])))
self.play(s.animate.rotate(PI / 2, axis=OUT))
self.play(s.animate.scale(1.5))
self.wait(0.5)
self.wait(1.10)

2.2. 位置和颜色

在这个示例中,创建了一个球体,并对其位置和颜色进行了自定义。

球心放置在三维空间中的特定点(2, -1, 1),并且球体被填充为红色。

1
2
3
4
5
6
7
8
9
10
11
12
from manim import *
class Demo(Scene):
def construct(self):
s = Sphere(
radius=1.5,
center=np.array([2, -1, 1]),
)
s.set_color(RED)
_obj = s
self.play(FadeIn(_obj), run_time=1.0)
self.wait(3.00)
self.wait(1.60)

2.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
from manim import *
class Demo(Scene):
def construct(self):
s1 = Sphere(
radius=0.5,
resolution=(32, 32),
center=np.array([-1, -1, 1]),
fill_opacity=0.2,
)
s1.set_color(BLUE)
s2 = Sphere(
radius=0.5,
resolution=(8, 8),
fill_opacity=0.6,
)
s2.set_color(YELLOW)
s3 = Sphere(
radius=0.5,
resolution=(4, 4),
center=np.array([1, 1, -1]),
fill_opacity=1,
)
s3.set_color(RED)
_obj = s3
self.play(FadeIn(_obj), run_time=1.0)
self.wait(3.00)
self.wait(1.60)

2.4. 球体动画

在这个示例中,不仅创建了一个球体,还为其添加了一系列动画效果。

球体首先以淡入效果出现,然后移动到三维空间中的另一个位置,接着绕一个轴旋转,最后进行缩放。

1
2
3
4
5
6
7
8
9
10
11
12
from manim import *
class Demo(Scene):
def construct(self):
s = Sphere(
radius=1,
center=np.array([1, 0, -1]),
)
self.play(FadeIn(s))
self.play(s.animate.move_to(np.array([-1.5, -1, 1])))
self.play(s.animate.rotate(PI / 2, axis=OUT))
self.play(s.animate.scale(1.5))
self.wait(3.00)

下载 Python 脚本

3. 附件