Skip to content

How to use checkerboard pattern for Surface class #2404

@1Dung

Description

@1Dung

Describe the error

Hi, I'm using manimGL v1.7.2. I recently learned about the Surface class in Manim, I really want to represent surfaces by a checkerboard model (like the old version before). I copied some code to create the checkerboard model to try to reproduce it in the new version. But the rendered image looks weird, is there something I forgot to set up?

Code and Error

Code:
class Test(InteractiveScene):
def construct(self):
# Try add surface by checkerboard colors
frame = self.camera.frame
frame.reorient(
-87, 92, 179, (np.float32(-0.15), np.float32(0.0), np.float32(-0.0)), 2.67
)
sphere = ParametricSurface(
uv_func=lambda u, v: np.array(
[
np.sin(u) * np.cos(v),
np.sin(u) * np.sin(v),
np.cos(u),
]
),
u_range=[0, PI],
v_range=[0, TAU],
resolution=(10, 10),
)

    # From def get_u_values_and_v_values(self) in class ParametricSurface
    res = tuplify(sphere.resolution)
    if len(res) == 1:
        u_res = v_res = res[0]
    else:
        u_res, v_res = res
    u_min = sphere.u_range[0]
    u_max = sphere.u_range[-1]
    v_min = sphere.v_range[0]
    v_max = sphere.v_range[-1]
    u_values = np.linspace(u_min, u_max, u_res + 1)
    v_values = np.linspace(v_min, v_max, v_res + 1)

    # From def setup_in_uv_space(self) in class ParametricSurface
    faces = VGroup()
    for i in range(len(u_values) - 1):
        for j in range(len(v_values) - 1):
            u1, u2 = u_values[i : i + 2]
            v1, v2 = v_values[j : j + 2]
            face = VMobject()  # Old version: face = ThreeDVMobject()
            face.set_points_as_corners(
                [
                    [u1, v1, 0],
                    [u2, v1, 0],
                    [u2, v2, 0],
                    [u1, v2, 0],
                    [u1, v1, 0],
                ]
            )
            face.apply_function(lambda p: sphere.uv_func(p[0], p[1]))
            faces.add(face)
            face.u_index = i
            face.v_index = j
            face.u1 = u1
            face.u2 = u2
            face.v1 = v1
            face.v2 = v2

    # From def set_fill_by_checkerboard(self, *colors, opacity=None):
    faces.set_fill(color=sphere.color, opacity=sphere.opacity)
    faces.set_stroke(
        color=WHITE,
        width=1,
        opacity=1,
    )
    checkerboard_colors = [BLUE_D, BLUE_E]
    n_colors = len(checkerboard_colors)
    for face in faces:
        c_index = (face.u_index + face.v_index) % n_colors
        face.set_fill(checkerboard_colors[c_index], opacity=1)
    self.add(sphere, faces)

Error:

Environment

Image

OS System: Window 11
manim version: v1.7.2
python version: 3.11.8

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions