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 RotateAndColor(Animation):
    """自定义动画：旋转的同时做颜色插值。"""
    def __init__(self, mobject, angle=TAU, start_color=BLUE,
                 end_color=RED, about_point=None, **kwargs):
        self.base = mobject.copy()
        self.angle = angle
        self.start_color = start_color
        self.end_color = end_color
        self.about_point = about_point
        super().__init__(mobject, **kwargs)
    def interpolate_mobject(self, alpha):
        self.mobject.become(self.base)
        self.mobject.rotate(alpha * self.angle, about_point=self.about_point or self.base.get_center())
        col = interpolate_color(self.start_color, self.end_color, alpha)
        self.mobject.set_stroke(col, width=7)
        if hasattr(self.mobject, "set_fill"):
            self.mobject.set_fill(col, opacity=0.35)
class RotateColor3D(ThreeDScene):
    def construct(self):
        self.set_camera_orientation(phi=70 * DEGREES, theta=-45 * DEGREES)
        self.add_fixed_in_frame_mobjects(head("三维版：太阳自转与地球公转", "ThreeDScene + RotateAndColor"))
        axes = ThreeDAxes(x_length=7, y_length=7, z_length=4)
        sun = Sphere(radius=0.9, resolution=(18, 18)).set_fill(YELLOW, opacity=0.9)
        earth = Sphere(radius=0.38, resolution=(14, 14)).set_fill(BLUE, opacity=0.9).shift(RIGHT * 2.6)
        orbit = Circle(radius=2.6, color=GREY_B).set_stroke(width=2, opacity=0.6)
        self.add(axes, sun, orbit, earth)
        self.play(RotateAndColor(sun, angle=PI, start_color=YELLOW, end_color=RED, run_time=3))
        self.play(Rotate(earth, angle=TAU, axis=OUT, about_point=ORIGIN, run_time=3))
        self.add_fixed_in_frame_mobjects(foot("三维对象同样使用 interpolatemobject 逐帧插值"))
        self.wait(1.6)
