Kill CONFIG in camera.py

This commit is contained in:
Grant Sanderson
2022-12-14 16:41:19 -08:00
parent a0a17be6ea
commit 2f8fe689d9

View File

@ -2,6 +2,7 @@ from __future__ import annotations
import itertools as it import itertools as it
import math import math
from random import sample
import moderngl import moderngl
import numpy as np import numpy as np
@ -26,14 +27,24 @@ from typing import TYPE_CHECKING
if TYPE_CHECKING: if TYPE_CHECKING:
from manimlib.shader_wrapper import ShaderWrapper from manimlib.shader_wrapper import ShaderWrapper
from manimlib.constants import ManimColor
from typing import Sequence
np_vector = np.ndarray[int, np.dtype[np.float64]]
class CameraFrame(Mobject): class CameraFrame(Mobject):
CONFIG = { def __init__(
"frame_shape": (FRAME_WIDTH, FRAME_HEIGHT), self,
"center_point": ORIGIN, frame_shape: tuple[float, float] = (FRAME_WIDTH, FRAME_HEIGHT),
"focal_dist_to_height": 2, center_point: np_vector = ORIGIN,
} focal_dist_to_height: float = 2.0,
**kwargs,
):
self.frame_shape = frame_shape
self.center_point = center_point
self.focal_dist_to_height = focal_dist_to_height
super().__init__(**kwargs)
def init_uniforms(self) -> None: def init_uniforms(self) -> None:
super().init_uniforms() super().init_uniforms()
@ -165,38 +176,49 @@ class CameraFrame(Mobject):
class Camera(object): class Camera(object):
CONFIG = { def __init__(
"background_image": None, self,
"frame_config": {}, ctx: moderngl.Context | None = None,
"pixel_width": DEFAULT_PIXEL_WIDTH, background_image: str | None = None,
"pixel_height": DEFAULT_PIXEL_HEIGHT, frame_config: dict = dict,
"fps": DEFAULT_FPS, pixel_width: int = DEFAULT_PIXEL_WIDTH,
# Note: frame height and width will be resized to match pixel_height: int = DEFAULT_PIXEL_HEIGHT,
# the pixel aspect ratio fps: int = DEFAULT_FPS,
"background_color": BLACK, # Note: frame height and width will be resized to match the pixel aspect ratio
"background_opacity": 1, background_color: ManimColor = BLACK,
background_opacity: float = 1.0,
# Points in vectorized mobjects with norm greater # Points in vectorized mobjects with norm greater
# than this value will be rescaled. # than this value will be rescaled.
"max_allowable_norm": FRAME_WIDTH, max_allowable_norm: float = FRAME_WIDTH,
"image_mode": "RGBA", image_mode: str = "RGBA",
"n_channels": 4, n_channels: int = 4,
"pixel_array_dtype": 'uint8', pixel_array_dtype: type = np.uint8,
"light_source_position": [-10, 10, 10], light_source_position: Sequence[float] = [-10, 10, 10],
# Measured in pixel widths, used for vector graphics # Measured in pixel widths, used for vector graphics
"anti_alias_width": 1.5, anti_alias_width: float = 1.5,
# Although vector graphics handle antialiasing fine # Although vector graphics handle antialiasing fine
# without multisampling, for 3d scenes one might want # without multisampling, for 3d scenes one might want
# to set samples to be greater than 0. # to set samples to be greater than 0.
"samples": 0, samples: int = 0,
} **kwargs
):
self.background_image = background_image
self.pixel_width = pixel_width
self.pixel_height = pixel_height
self.fps = fps
self.max_allowable_norm = max_allowable_norm
self.image_mode = image_mode
self.n_channels = n_channels
self.pixel_array_dtype = pixel_array_dtype
self.light_source_position = light_source_position
self.anti_alias_width = anti_alias_width
self.samples = samples
def __init__(self, ctx: moderngl.Context | None = None, **kwargs):
digest_config(self, kwargs, locals())
self.rgb_max_val: float = np.iinfo(self.pixel_array_dtype).max self.rgb_max_val: float = np.iinfo(self.pixel_array_dtype).max
self.background_rgba: list[float] = list(color_to_rgba( self.background_rgba: list[float] = list(color_to_rgba(
self.background_color, self.background_opacity background_color, background_opacity
)) ))
self.init_frame() self.init_frame(**frame_config)
self.init_context(ctx) self.init_context(ctx)
self.init_shaders() self.init_shaders()
self.init_textures() self.init_textures()
@ -207,8 +229,8 @@ class Camera(object):
# mobjects # mobjects
self.mob_to_render_groups = {} self.mob_to_render_groups = {}
def init_frame(self) -> None: def init_frame(self, **config) -> None:
self.frame = CameraFrame(**self.frame_config) self.frame = CameraFrame(**config)
def init_context(self, ctx: moderngl.Context | None = None) -> None: def init_context(self, ctx: moderngl.Context | None = None) -> None:
if ctx is None: if ctx is None:
@ -517,7 +539,5 @@ class Camera(object):
# Mostly just defined so old scenes don't break # Mostly just defined so old scenes don't break
class ThreeDCamera(Camera): class ThreeDCamera(Camera):
CONFIG = { def __init__(self, samples: int = 4, anti_alias_width: float = 0, **kwargs):
"samples": 4, super().__init__(samples=samples, anti_alias_width=anti_alias_width, **kwargs)
"anti_alias_width": 0,
}