A community-maintained Python framework for creating mathematical animations.
Merge pull request #109 from frozar/3D_camera_moving
Grant Sanderson921dc1a6d7eb
diff --cc example_scenes.pyindex 0fdcf4e99,4561c1fc4..d9538204b--- a/example_scenes.py+++ b/example_scenes.py@@@ -22,8 -16,15 +22,10 @@@ from topics.characters importfrom topics.functions import *from topics.number_line import *from topics.combinatorics import *-from scene import Scene-from camera import Camera-from mobject.svg_mobject import *-from mobject.tex_mobject import *--from mobject.vectorized_mobject import *+from topics.three_dimensions import *+ from topics.three_dimensions import *+# To watch one of these scenes, run the following:# python extract_scene.py file_name <SceneName> -p#@@@ -59,71 -60,68 +61,132 @@@ class WriteStuff(Scene)self.play(Write(TextMobject("Stuff").scale(3)))+ class Rotation3d(ThreeDScene):+ def construct(self):+ # STEP 1+ # Build two cube in the 3D scene, one for around the origin,+ # the other shifted along the vector RIGHT + UP + OUT+ cube_origin = Cube(fill_opacity = 0.8, stroke_width = 1.,+ side_length = 1., fill_color = WHITE)++ # RIGHT side: Red+ # UP side: Green+ # OUT side: Blue+ orientations = [IN, OUT, LEFT, RIGHT, UP, DOWN]+ for face, orient in zip(cube_origin.family_members_with_points(), orientations):+ if np.array_equal(orient, RIGHT):+ face.set_style_data(fill_color = RED)+ elif np.array_equal(orient, UP):+ face.set_style_data(fill_color = GREEN)+ elif np.array_equal(orient, OUT):+ face.set_style_data(fill_color = BLUE)++ cube_shifted = Cube(fill_opacity = 0.8, stroke_width = 1.,+ side_length = 1., fill_color = BLUE)+ shift_vec = 2*(RIGHT + UP + OUT)+ cube_shifted.shift(shift_vec)++ # STEP 2+ # Add the cubes in the 3D scene+ self.add(cube_origin)+ self.add(cube_shifted)++ # STEP 3+ # Setup the camera position+ phi, theta, distance = ThreeDCamera().get_spherical_coords()+ angle_factor = 0.9+ phi += 2*np.pi/4*angle_factor+ theta += 3*2*np.pi/8+ self.set_camera_position(phi, theta, distance)+ self.wait()++ # STEP 4+ # Animation+ # Animation 1: rotation around the Z-axis with the ORIGIN of the space+ # as center of rotation+ theta += 2*np.pi+ self.move_camera(phi, theta, distance,+ run_time = 5)++ # Animation 2: shift the space in order of to get the center of the shifted cube+ # as the next center of rotation+ cube_center = cube_shifted.get_center()+ self.move_camera(center_x = cube_center[0],+ center_y = cube_center[1],+ center_z = cube_center[2],+ run_time = 2)++ # Animation 3: rotation around the Z-axis with the center of the shifted cube+ # as center of rotation+ theta += 2*np.pi+ self.move_camera(phi, theta, distance,+ run_time = 5)++class SpinAroundCube(ThreeDScene):+ # Take a look at ThreeDSCene in three_dimensions.py.+ # This has a few methods on it like set_camera_position+ # and move_camera that will be useful. The main thing to+ # know about these is that the camera position is thought+ # of as having spherical coordinates, phi and theta.++ # In general, the nature of how this 3d camera works+ # is not always robust, you might discover little+ # quirks here or there+ def construct(self):+ axes = ThreeDAxes()+ cube = Cube(+ fill_opacity = 1,+ stroke_color = LIGHT_GREY,+ stroke_width = 1,+ )+ # The constant OUT is np.array([0, 0, 1])+ cube.next_to(ORIGIN, UP+RIGHT+OUT)+ self.add(axes, cube)++ # The camera starts positioned with phi=0, meaning it+ # is directly above the xy-plane, and theta = -TAU/4,+ # which makes the "down" direction of the screen point+ # in the negative y direction.++ # This animates a camera movement+ self.move_camera(+ # Tilted 20 degrees off xy plane (70 degrees off the vertical)+ phi = (70./360.)*TAU,+ # Positioned above the third quadrant of+ # the xy-plane+ theta = (-110./360.)*TAU,+ # pass in animation config just like a .play call+ run_time = 3+ )+ self.wait()+ # If you want the camera to slowly rotate about+ # the z-axis+ self.begin_ambient_camera_rotation()+ self.wait(4)+ self.play(FadeOut(cube))++++ text = TextMobject("Your ad here")+ text.rotate(TAU/4, axis = RIGHT)+ text.next_to(cube, OUT)+ self.play(Write(text))+ # If you want to play animations while moving the camera,+ # include them in an "added_anims" list to move_camera+ self.move_camera(+ theta = -0.2*TAU,+ added_anims = [+ text.shift, 3*OUT,+ text.set_fill, {"opacity" : 1},+ ]+ )+ self.wait(4)+++++diff --cc topics/three_dimensions.pyindex 2d38cd55e,ba9611cb8..752e48181--- a/topics/three_dimensions.py+++ b/topics/three_dimensions.py@@@ -36,21 -36,29 +36,24 @@@ class ThreeDCamera(CameraWithPerspectivdef __init__(self, *args, **kwargs):Camera.__init__(self, *args, **kwargs)self.unit_sun_vect = self.sun_vect/np.linalg.norm(self.sun_vect)- ## Lives in the phi-theta-distance space+ ## rotation_mobject lives in the phi-theta-distance spaceself.rotation_mobject = VectorizedPoint()+ ## moving_center lives in the x-y-z space+ ## It representes the center of rotation+ self.moving_center = VectorizedPoint(self.space_center)self.set_position(self.phi, self.theta, self.distance)- def get_color(self, method):- color = method()- vmobject = method.im_self+ def modified_rgb(self, vmobject, rgb):if should_shade_in_3d(vmobject):- return Color(rgb = self.get_shaded_rgb(- color_to_rgb(color),- normal_vect = self.get_unit_normal_vect(vmobject)- ))+ return self.get_shaded_rgb(rgb, self.get_unit_normal_vect(vmobject))else:- return color+ return rgb- def get_stroke_color(self, vmobject):- return self.get_color(vmobject.get_stroke_color)+ def get_stroke_rgb(self, vmobject):+ return self.modified_rgb(vmobject, vmobject.get_stroke_rgb())- def get_fill_color(self, vmobject):- return self.get_color(vmobject.get_fill_color)+ def get_fill_rgb(self, vmobject):+ return self.modified_rgb(vmobject, vmobject.get_fill_rgb())def get_shaded_rgb(self, rgb, normal_vect):brightness = np.dot(normal_vect, self.unit_sun_vect)**2