Skip to content

Commit 5e726c0

Browse files
committed
Add example_scenes/opengl.py
1 parent 21bee39 commit 5e726c0

File tree

4 files changed

+87
-1
lines changed

4 files changed

+87
-1
lines changed
69.1 KB
Loading
108 KB
Loading

example_scenes/opengl.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
from manim import *
2+
from manim.opengl import *
3+
import os
4+
from pathlib import Path
5+
6+
7+
class SurfaceExample(Scene):
8+
def construct(self):
9+
# surface_text = Text("For 3d scenes, try using surfaces")
10+
# surface_text.fix_in_frame()
11+
# surface_text.to_edge(UP)
12+
# self.add(surface_text)
13+
# self.wait(0.1)
14+
15+
torus1 = OpenGLTorus(r1=1, r2=1)
16+
torus2 = OpenGLTorus(r1=3, r2=1)
17+
sphere = OpenGLSphere(radius=3, resolution=torus1.resolution)
18+
# You can texture a surface with up to two images, which will
19+
# be interpreted as the side towards the light, and away from
20+
# the light. These can be either urls, or paths to a local file
21+
# in whatever you've set as the image directory in
22+
# the custom_config.yml file
23+
24+
script_location = Path(os.path.realpath(__file__)).parent
25+
day_texture = (
26+
script_location / "assets" / "1280px-Whole_world_-_land_and_oceans.jpg"
27+
)
28+
night_texture = script_location / "assets" / "1280px-The_earth_at_night.jpg"
29+
30+
surfaces = [
31+
OpenGLTexturedSurface(surface, day_texture, night_texture)
32+
for surface in [sphere, torus1, torus2]
33+
]
34+
35+
for mob in surfaces:
36+
mob.shift(IN)
37+
mob.mesh = OpenGLSurfaceMesh(mob)
38+
mob.mesh.set_stroke(BLUE, 1, opacity=0.5)
39+
40+
# Set perspective
41+
frame = self.renderer.camera
42+
frame.set_euler_angles(
43+
theta=-30 * DEGREES,
44+
phi=70 * DEGREES,
45+
)
46+
47+
surface = surfaces[0]
48+
49+
self.play(
50+
FadeIn(surface),
51+
ShowCreation(surface.mesh, lag_ratio=0.01, run_time=3),
52+
)
53+
for mob in surfaces:
54+
mob.add(mob.mesh)
55+
surface.save_state()
56+
self.play(Rotate(surface, PI / 2), run_time=2)
57+
for mob in surfaces[1:]:
58+
mob.rotate(PI / 2)
59+
60+
self.play(Transform(surface, surfaces[1]), run_time=3)
61+
62+
self.play(
63+
Transform(surface, surfaces[2]),
64+
# Move camera frame during the transition
65+
frame.animate.increment_phi(-10 * DEGREES),
66+
frame.animate.increment_theta(-20 * DEGREES),
67+
run_time=3,
68+
)
69+
# Add ambient rotation
70+
frame.add_updater(lambda m, dt: m.increment_theta(-0.1 * dt))
71+
72+
# Play around with where the light is
73+
# light_text = Text("You can move around the light source")
74+
# light_text.move_to(surface_text)
75+
# light_text.fix_in_frame()
76+
77+
# self.play(FadeTransform(surface_text, light_text))
78+
light = self.camera.light_source
79+
self.add(light)
80+
light.save_state()
81+
self.play(light.animate.move_to(3 * IN), run_time=5)
82+
self.play(light.animate.shift(10 * OUT), run_time=5)
83+
84+
# drag_text = Text("Try moving the mouse while pressing d or s")
85+
# drag_text.move_to(light_text)
86+
# drag_text.fix_in_frame()

manim/mobject/opengl_mobject.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1147,7 +1147,7 @@ def align_to(self, mobject_or_point, direction=ORIGIN):
11471147
return self
11481148

11491149
def get_group_class(self):
1150-
return Group
1150+
return OpenGLGroup
11511151

11521152
# Alignment
11531153

0 commit comments

Comments
 (0)