Moebius Strip in Manim

huangapple go评论82阅读模式
英文:

Moebius Strip in Manim

问题

我正在尝试制作一个彩色的莫比乌斯带,但我总是在带的开头和结尾之间出现颜色差异。

这里是一个最小工作示例(MWE)以及它生成的图像。

from manim import *

BG_COLOR = "#F7F5E7"

config.background_color = BG_COLOR

def mobius_func(u, v):
    x = (1 + v / 2 * np.cos(u / 2)) * np.cos(u)
    y = (1 + v / 2 * np.cos(u / 2)) * np.sin(u)
    z = v / 2 * np.sin(u / 2)
    return np.array((x, y, z))

class Logo(ThreeDScene):
    def construct(self):
        self.set_camera_orientation(
            phi=50 * DEGREES, theta=330 * DEGREES, run_time=2, zoom=2.2
        )

        mobius = Surface(
            mobius_func,
            u_range=[0, 2 * PI],
            v_range=[-1, 1],
            resolution=(64, 16),
        )
        mobius.set_color(RED)
        self.add(mobius)

问题的原因是什么,以及如何解决它?

英文:

I'm trying to make a colored Mobius strip, but I always end up with a color difference between the beginning and the end of the strip.

Here is a MWE and the image it produces.

from manim import *

BG_COLOR = "#F7F5E7"

config.background_color = BG_COLOR


def mobius_func(u, v):
    x = (1 + v / 2 * np.cos(u / 2)) * np.cos(u)
    y = (1 + v / 2 * np.cos(u / 2)) * np.sin(u)
    z = v / 2 * np.sin(u / 2)
    return np.array((x, y, z))


class Logo(ThreeDScene):
    def construct(self):

        self.set_camera_orientation(
            phi=50 * DEGREES, theta=330 * DEGREES, run_time=2, zoom=2.2
        )

        mobius = Surface(
            mobius_func,
            u_range=[0, 2 * PI],
            v_range=[-1, 1],
            resolution=(64, 16),
        )
        mobius.set_color(RED)
        self.add(mobius)

Moebius Strip in Manim

What causes this problem and how can I fix it ?

答案1

得分: 1

To render a Möbius strip correctly, you need a rendering engine that supports the hidden surface removal (such as z buffering) and face culling. But Manim does not support them. So in short, you can't solve that problem.

Your best bet will be to hack Manim like this. (Of course, it's not recommended for a long-term project because it may not work on versions other than 0.17.2.)

import numpy as np
import manim

def new_get_shaded_rgb(
    rgb: np.ndarray,
    point: np.ndarray,
    unit_normal_vect: np.ndarray,
    light_source: np.ndarray,
) -> np.ndarray:
    to_sun = manim.utils.space_ops.normalize(light_source - point)
    factor = 0.5 * np.dot(unit_normal_vect, to_sun) ** 3
    factor = abs(factor) # This patch will give the same color for a back or front face.
    result = rgb + factor
    return result
manim.camera.three_d_camera.get_shaded_rgb = new_get_shaded_rgb
英文:

To render a Möbius strip correctly, you need a rendering engine that supports the hidden surface removal(such as z buffering) and face culling. But Manim does not supports them. So in short, you can't solve that problem.

Your best bet will be to hack Manim like this.(Of course, it's not recommended for a long term project, because it maybe will not work on versions other than 0.17.2.)

import numpy as np
import manim

def new_get_shaded_rgb(
    rgb: np.ndarray,
    point: np.ndarray,
    unit_normal_vect: np.ndarray,
    light_source: np.ndarray,
) -> np.ndarray:
    to_sun = manim.utils.space_ops.normalize(light_source - point)
    factor = 0.5 * np.dot(unit_normal_vect, to_sun) ** 3
    factor = abs(factor) # This patch will give the same color for a back or front face.
    result = rgb + factor
    return result
manim.camera.three_d_camera.get_shaded_rgb = new_get_shaded_rgb

huangapple
  • 本文由 发表于 2023年2月14日 05:38:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75441432.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定