A community-maintained Python framework for creating mathematical animations.
Made set_fill_by_value() support gradients on different axes. (#2383)
Leo Xub91c6fd50df1
diff --git a/manim/mobject/three_dimensions.py b/manim/mobject/three_dimensions.pyindex b45995dd7..b54335c99 100644--- a/manim/mobject/three_dimensions.py+++ b/manim/mobject/three_dimensions.py@@ -165,16 +165,23 @@ class Surface(VGroup, metaclass=ConvertToOpenGL):face.set_fill(colors[c_index], opacity=opacity)return self- def set_fill_by_value(self, axes: "Mobject", colors: Union[Iterable[Color], Color]):- """Sets the color of each mobject of a parametric surface to a color relative to its z-value+ def set_fill_by_value(+ self,+ axes: "Mobject",+ colors: Union[Iterable[Color], Color],+ axis: int = 2,+ ):+ """Sets the color of each mobject of a parametric surface to a color relative to its axis-valueParameters----------axes :- The axes for the parametric surface, which will be used to map z-values to colors.+ The axes for the parametric surface, which will be used to map axis-values to colors.colors :- A list of colors, ordered from lower z-values to higher z-values. If a list of tuples is passed+ A list of colors, ordered from lower axis-values to higher axis-values. If a list of tuples is passedcontaining colors paired with numbers, then those numbers will be used as the pivots.+ axis :+ The chosen axis to use for the color mapping. (0 = x, 1 = y, 2 = z)Returns-------@@ -203,16 +210,19 @@ class Surface(VGroup, metaclass=ConvertToOpenGL):u_range=[0, 5],)surface_plane.set_style(fill_opacity=1)- surface_plane.set_fill_by_value(axes=axes, colors=[(RED, -0.4), (YELLOW, 0), (GREEN, 0.4)])+ surface_plane.set_fill_by_value(axes=axes, colors=[(RED, -0.4), (YELLOW, 0), (GREEN, 0.4)], axis = 1)self.add(axes, surface_plane)"""++ ranges = [axes.x_range, axes.y_range, axes.z_range]+if type(colors[0]) is tuple:new_colors, pivots = [[i for i, j in colors], [j for i, j in colors]]else:new_colors = colors- pivot_min = axes.z_range[0]- pivot_max = axes.z_range[1]+ pivot_min = ranges[axis][0]+ pivot_max = ranges[axis][1]pivot_frequency = (pivot_max - pivot_min) / (len(new_colors) - 1)pivots = np.arange(start=pivot_min,@@ -221,15 +231,15 @@ class Surface(VGroup, metaclass=ConvertToOpenGL):)for mob in self.family_members_with_points():- z_value = axes.point_to_coords(mob.get_midpoint())[2]- if z_value <= pivots[0]:+ axis_value = axes.point_to_coords(mob.get_midpoint())[axis]+ if axis_value <= pivots[0]:mob.set_color(new_colors[0])- elif z_value >= pivots[-1]:+ elif axis_value >= pivots[-1]:mob.set_color(new_colors[-1])else:for i, pivot in enumerate(pivots):- if pivot > z_value:- color_index = (z_value - pivots[i - 1]) / (+ if pivot > axis_value:+ color_index = (axis_value - pivots[i - 1]) / (pivots[i] - pivots[i - 1])color_index = min(color_index, 1)diff --git a/tests/test_graphical_units/control_data/threed/Y_Direction.npz b/tests/test_graphical_units/control_data/threed/Y_Direction.npznew file mode 100644index 000000000..540e7062eBinary files /dev/null and b/tests/test_graphical_units/control_data/threed/Y_Direction.npz differdiff --git a/tests/test_graphical_units/test_threed.py b/tests/test_graphical_units/test_threed.pyindex b0df275fb..c730a0364 100644--- a/tests/test_graphical_units/test_threed.py+++ b/tests/test_graphical_units/test_threed.py@@ -132,3 +132,28 @@ def test_SurfaceColorscale(scene):)trig_plane.set_fill_by_value(axes=axes, colors=[BLUE, GREEN, YELLOW, ORANGE, RED])scene.add(axes, trig_plane)+++@frames_comparison(base_scene=ThreeDScene)+def test_Y_Direction(scene):+ resolution_fa = 42+ scene.set_camera_orientation(phi=75 * DEGREES, theta=-120 * DEGREES)+ axes = ThreeDAxes(x_range=(0, 5, 1), y_range=(0, 5, 1), z_range=(-1, 1, 0.5))++ def param_surface(u, v):+ x = u+ y = v+ z = np.sin(x) * np.cos(y)+ return z++ surface_plane = Surface(+ lambda u, v: axes.c2p(u, v, param_surface(u, v)),+ resolution=(resolution_fa, resolution_fa),+ v_range=[0, 5],+ u_range=[0, 5],+ )+ surface_plane.set_style(fill_opacity=1)+ surface_plane.set_fill_by_value(+ axes=axes, colors=[(RED, -0.4), (YELLOW, 0), (GREEN, 0.4)], axis=1+ )+ scene.add(axes, surface_plane)