Sphere
class is used to create 3D sphere objects that provide a rich set of parameters and methods to customize the appearance and behavior of the sphere.
Spheres have a wide range of application scenarios when creating 3D animations.
For example:
- Demonstrate geometric concepts: Concepts such as volume and surface area in geometry can be visualized by creating spheres of different sizes, colors, and transparency.
- physical simulation: When modeling physical phenomena (e.g., gravity, collisions, etc.), you can use the Sphere class to represent spherical objects
- astronomical simulation: The Sphere class can be used to create models of celestial bodies such as planets and stars when demonstrating astronomical phenomena such as planetary motions and galaxy structures.
1. Main parameters
Sphere
The main parameters of the class include:
Parameter name | typology | clarification |
---|---|---|
center | Point3D | Center of the sphere |
radius | float | Radius of the sphere |
resolution | int | The degree of subdivision of the sphere. This parameter is often used to control the density or smoothness of the mesh on the surface of the sphere. |
u_range | [float] | defines the parameterized range of the sphere in the u-direction |
v_range | [float] | defines the parameterized range of the sphere in the v-direction |
resolution
Larger parameter settings produce finer, smoother sphere surfaces, but also increase computation and memory usage.
2. Examples of use
Sphere
class is relatively simple to use, the following example focuses on the different ways its parameters can be used.
2.1 Basic sphere
This example creates a basic sphere at the origin with radius 1.
s = Sphere(radius=1)
2.2. location and color
In this example, a sphere is created and its position and color are customized.
The center of the sphere is placed at a specific point in 3D space (2, -1, 1) and the sphere is filled with red color.
s = Sphere(
radius=1.5,
center=([2, -1, 1]),
)
s.set_color(RED)
2.3 Resolution and transparency
This example shows how to adjust the resolution and transparency of the sphere.
By increasing the resolution, the surface of the sphere looks smoother; by setting the transparency, the sphere takes on a transparent effect, making objects or parts of the background visible behind it.
# High resolution with high transparency
s1 = Sphere(
radius=0.5,
resolution=(32, 32), center=([-1, -1, 1]), s1 = Sphere(
center=([-1, -1, 1]),
fill_opacity=0.2, )
)
s1.set_color(BLUE)
# Medium resolution, medium transparency
s2 = Sphere(
radius=0.5,
resolution=(8, 8), fill_opacity=0.6,
fill_opacity=0.6, )
)
s2.set_color(YELLOW)
# low resolution, opaque
s3 = Sphere(
radius=0.5,
resolution=(4, 4), center=([1, 1, -1]), s2.set_color(YELLOW)
fill_opacity=1, )
)
s3.set_color(RED)
2.4. Sphere animation
In this example, not only is a sphere created, but a series of animation effects are added to it.
The sphere first appears as a fade-in effect, then moves to another position in 3D space, then rotates around an axis, and finally scales.
s = Sphere(
radius=1,
center=([1, 0, -1]),
)
(FadeIn(s))
(.move_to(([-1.5, -1, 1])))
((PI / 2, axis=OUT))
((1.5))
3. Annexes
The code in the article is just an extract of the key parts, the complete code is shared on a web disk (),
Download at.Full Code (Access code: 6872)