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 math
from random import sample
import moderngl
import numpy as np
@ -26,14 +27,24 @@ from typing import TYPE_CHECKING
if TYPE_CHECKING:
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):
CONFIG = {
"frame_shape": (FRAME_WIDTH, FRAME_HEIGHT),
"center_point": ORIGIN,
"focal_dist_to_height": 2,
}
def __init__(
self,
frame_shape: tuple[float, float] = (FRAME_WIDTH, FRAME_HEIGHT),
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:
super().init_uniforms()
@ -165,38 +176,49 @@ class CameraFrame(Mobject):
class Camera(object):
CONFIG = {
"background_image": None,
"frame_config": {},
"pixel_width": DEFAULT_PIXEL_WIDTH,
"pixel_height": DEFAULT_PIXEL_HEIGHT,
"fps": DEFAULT_FPS,
# Note: frame height and width will be resized to match
# the pixel aspect ratio
"background_color": BLACK,
"background_opacity": 1,
def __init__(
self,
ctx: moderngl.Context | None = None,
background_image: str | None = None,
frame_config: dict = dict,
pixel_width: int = DEFAULT_PIXEL_WIDTH,
pixel_height: int = DEFAULT_PIXEL_HEIGHT,
fps: int = DEFAULT_FPS,
# Note: frame height and width will be resized to match the pixel aspect ratio
background_color: ManimColor = BLACK,
background_opacity: float = 1.0,
# Points in vectorized mobjects with norm greater
# than this value will be rescaled.
"max_allowable_norm": FRAME_WIDTH,
"image_mode": "RGBA",
"n_channels": 4,
"pixel_array_dtype": 'uint8',
"light_source_position": [-10, 10, 10],
max_allowable_norm: float = FRAME_WIDTH,
image_mode: str = "RGBA",
n_channels: int = 4,
pixel_array_dtype: type = np.uint8,
light_source_position: Sequence[float] = [-10, 10, 10],
# 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
# without multisampling, for 3d scenes one might want
# 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.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_shaders()
self.init_textures()
@ -207,8 +229,8 @@ class Camera(object):
# mobjects
self.mob_to_render_groups = {}
def init_frame(self) -> None:
self.frame = CameraFrame(**self.frame_config)
def init_frame(self, **config) -> None:
self.frame = CameraFrame(**config)
def init_context(self, ctx: moderngl.Context | None = None) -> None:
if ctx is None:
@ -517,7 +539,5 @@ class Camera(object):
# Mostly just defined so old scenes don't break
class ThreeDCamera(Camera):
CONFIG = {
"samples": 4,
"anti_alias_width": 0,
}
def __init__(self, samples: int = 4, anti_alias_width: float = 0, **kwargs):
super().__init__(samples=samples, anti_alias_width=anti_alias_width, **kwargs)