From 65e7943ff7582d64fdc5b012f05e5679c3d6e248 Mon Sep 17 00:00:00 2001 From: german2020 Date: Sun, 27 Aug 2023 13:51:54 +0800 Subject: [PATCH] fix aspect_ratio other than 16:9 issue --- manimlib/config.py | 18 ++++++++++++++++-- manimlib/constants.py | 10 ++++++---- manimlib/utils/shaders.py | 3 +++ 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/manimlib/config.py b/manimlib/config.py index 1de7bbd7cd..51e8468c58 100644 --- a/manimlib/config.py +++ b/manimlib/config.py @@ -13,7 +13,6 @@ from manimlib.logger import log from manimlib.utils.dict_ops import merge_dicts_recursively from manimlib.utils.init_config import init_customization -from manimlib.constants import FRAME_HEIGHT from typing import TYPE_CHECKING if TYPE_CHECKING: @@ -473,7 +472,7 @@ def get_camera_config(args: Namespace, custom_config: dict) -> dict: "pixel_width": width, "pixel_height": height, "frame_config": { - "frame_shape": ((width / height) * FRAME_HEIGHT, FRAME_HEIGHT), + "frame_shape": ((width / height) * get_frame_height(), get_frame_height()), }, "fps": fps, }) @@ -520,3 +519,18 @@ def get_configuration(args: Namespace) -> dict: "embed_exception_mode": custom_config["embed_exception_mode"], "embed_error_sound": custom_config["embed_error_sound"], } + +def get_frame_height(): + return 8.0 + +def get_aspect_ratio(): + cam_config = get_camera_config(parse_cli(), get_custom_config()) + return cam_config['pixel_width'] / cam_config['pixel_height'] + +def get_default_pixel_width(): + cam_config = get_camera_config(parse_cli(), get_custom_config()) + return cam_config['pixel_width'] + +def get_default_pixel_height(): + cam_config = get_camera_config(parse_cli(), get_custom_config()) + return cam_config['pixel_height'] \ No newline at end of file diff --git a/manimlib/constants.py b/manimlib/constants.py index ac0345ec6d..e883173a9f 100644 --- a/manimlib/constants.py +++ b/manimlib/constants.py @@ -1,6 +1,8 @@ from __future__ import annotations import numpy as np +from manimlib.config import get_aspect_ratio,get_default_pixel_width,get_default_pixel_height,get_frame_height + from typing import TYPE_CHECKING if TYPE_CHECKING: from typing import List @@ -8,15 +10,15 @@ # Sizes relevant to default camera frame -ASPECT_RATIO: float = 16.0 / 9.0 -FRAME_HEIGHT: float = 8.0 +ASPECT_RATIO: float = get_aspect_ratio() +FRAME_HEIGHT: float = get_frame_height() FRAME_WIDTH: float = FRAME_HEIGHT * ASPECT_RATIO FRAME_SHAPE: tuple[float, float] = (FRAME_WIDTH, FRAME_HEIGHT) FRAME_Y_RADIUS: float = FRAME_HEIGHT / 2 FRAME_X_RADIUS: float = FRAME_WIDTH / 2 -DEFAULT_PIXEL_HEIGHT: int = 1080 -DEFAULT_PIXEL_WIDTH: int = 1920 +DEFAULT_PIXEL_HEIGHT: int = get_default_pixel_height() +DEFAULT_PIXEL_WIDTH: int = get_default_pixel_width() DEFAULT_FPS: int = 30 SMALL_BUFF: float = 0.1 diff --git a/manimlib/utils/shaders.py b/manimlib/utils/shaders.py index 7cc1ba5c72..6157d835bd 100644 --- a/manimlib/utils/shaders.py +++ b/manimlib/utils/shaders.py @@ -9,6 +9,7 @@ from manimlib.config import parse_cli from manimlib.config import get_configuration +from manimlib.constants import ASPECT_RATIO from manimlib.utils.directories import get_shader_dir from manimlib.utils.file_ops import find_file @@ -112,6 +113,8 @@ def get_shader_code_from_file(filename: str) -> str | None: with open(filepath, "r") as f: result = f.read() + + result = re.sub(r"\s+ASPECT_RATIO\s+=\s+[\s0-9/.]+", f" ASPECT_RATIO = {ASPECT_RATIO}", result) # To share functionality between shaders, some functions are read in # from other files an inserted into the relevant strings before