2022-03-22 11:31:52 -07:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-04-12 19:19:59 +08:00
|
|
|
import moderngl
|
2018-12-24 12:37:51 -08:00
|
|
|
import numpy as np
|
2022-04-12 19:19:59 +08:00
|
|
|
import OpenGL.GL as gl
|
2022-02-13 19:32:53 +08:00
|
|
|
from PIL import Image
|
2018-03-31 15:11:35 -07:00
|
|
|
|
2023-01-25 10:19:44 -08:00
|
|
|
from manimlib.camera.camera_frame import CameraFrame
|
2022-04-12 19:19:59 +08:00
|
|
|
from manimlib.constants import BLACK
|
2022-05-14 17:47:31 -07:00
|
|
|
from manimlib.constants import DEFAULT_FPS
|
2022-04-12 19:19:59 +08:00
|
|
|
from manimlib.constants import DEFAULT_PIXEL_HEIGHT, DEFAULT_PIXEL_WIDTH
|
2023-01-25 10:19:44 -08:00
|
|
|
from manimlib.constants import FRAME_WIDTH
|
2018-12-24 12:37:51 -08:00
|
|
|
from manimlib.mobject.mobject import Mobject
|
2020-06-02 16:18:44 -07:00
|
|
|
from manimlib.mobject.mobject import Point
|
2022-04-12 19:19:59 +08:00
|
|
|
from manimlib.utils.color import color_to_rgba
|
2020-02-04 15:27:21 -08:00
|
|
|
|
2022-02-14 21:34:56 +08:00
|
|
|
from typing import TYPE_CHECKING
|
2022-02-16 21:08:25 +08:00
|
|
|
|
2022-02-14 21:34:56 +08:00
|
|
|
if TYPE_CHECKING:
|
2023-01-27 10:12:53 -08:00
|
|
|
from typing import Optional
|
2022-12-17 13:16:48 -08:00
|
|
|
from manimlib.typing import ManimColor, Vect3
|
2023-01-23 17:03:46 -08:00
|
|
|
from manimlib.window import Window
|
2022-12-14 16:41:19 -08:00
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2016-02-23 22:29:32 -08:00
|
|
|
class Camera(object):
|
2022-12-14 16:41:19 -08:00
|
|
|
def __init__(
|
|
|
|
self,
|
2023-01-27 10:12:53 -08:00
|
|
|
window: Optional[Window] = None,
|
|
|
|
background_image: Optional[str] = None,
|
2022-12-27 14:53:55 -08:00
|
|
|
frame_config: dict = dict(),
|
2022-12-14 16:41:19 -08:00
|
|
|
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,
|
2018-04-06 13:58:59 -07:00
|
|
|
# Points in vectorized mobjects with norm greater
|
|
|
|
# than this value will be rescaled.
|
2022-12-14 16:41:19 -08:00
|
|
|
max_allowable_norm: float = FRAME_WIDTH,
|
|
|
|
image_mode: str = "RGBA",
|
|
|
|
n_channels: int = 4,
|
|
|
|
pixel_array_dtype: type = np.uint8,
|
2022-12-27 14:53:55 -08:00
|
|
|
light_source_position: Vect3 = np.array([-10, 10, 10]),
|
2020-06-08 14:09:31 -07:00
|
|
|
# Although vector graphics handle antialiasing fine
|
|
|
|
# without multisampling, for 3d scenes one might want
|
|
|
|
# to set samples to be greater than 0.
|
2022-12-14 16:41:19 -08:00
|
|
|
samples: int = 0,
|
|
|
|
):
|
|
|
|
self.background_image = background_image
|
2023-01-23 17:03:46 -08:00
|
|
|
self.window = window
|
2023-01-23 14:02:06 -08:00
|
|
|
self.default_pixel_shape = (pixel_width, pixel_height)
|
2022-12-14 16:41:19 -08:00
|
|
|
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.samples = samples
|
2016-02-23 22:29:32 -08:00
|
|
|
|
2022-02-13 19:32:53 +08:00
|
|
|
self.rgb_max_val: float = np.iinfo(self.pixel_array_dtype).max
|
2022-04-12 19:19:59 +08:00
|
|
|
self.background_rgba: list[float] = list(color_to_rgba(
|
2022-12-14 16:41:19 -08:00
|
|
|
background_color, background_opacity
|
2022-04-12 19:19:59 +08:00
|
|
|
))
|
2023-01-25 14:13:56 -08:00
|
|
|
self.uniforms = dict()
|
2022-12-14 16:41:19 -08:00
|
|
|
self.init_frame(**frame_config)
|
2023-01-27 10:12:53 -08:00
|
|
|
self.init_context()
|
|
|
|
self.init_fbo()
|
2020-06-03 10:38:57 -07:00
|
|
|
self.init_light_source()
|
2020-02-04 15:27:21 -08:00
|
|
|
|
2022-12-14 16:41:19 -08:00
|
|
|
def init_frame(self, **config) -> None:
|
|
|
|
self.frame = CameraFrame(**config)
|
2020-02-04 15:27:21 -08:00
|
|
|
|
2023-01-27 10:12:53 -08:00
|
|
|
def init_context(self) -> None:
|
|
|
|
if self.window is None:
|
2023-01-28 15:40:58 -08:00
|
|
|
self.ctx: moderngl.Context = moderngl.create_standalone_context()
|
2020-02-11 19:51:19 -08:00
|
|
|
else:
|
2023-01-28 15:40:58 -08:00
|
|
|
self.ctx: moderngl.Context = self.window.ctx
|
2020-06-08 14:09:31 -07:00
|
|
|
|
2023-01-18 15:36:00 -08:00
|
|
|
self.ctx.enable(moderngl.PROGRAM_POINT_SIZE)
|
2023-01-25 13:45:18 -08:00
|
|
|
self.ctx.enable(moderngl.BLEND)
|
2023-01-18 15:36:00 -08:00
|
|
|
|
2023-01-27 10:12:53 -08:00
|
|
|
def init_fbo(self) -> None:
|
|
|
|
# This is the buffer used when writing to a video/image file
|
|
|
|
self.fbo_for_files = self.get_fbo(self.samples)
|
|
|
|
|
2023-01-20 16:31:09 -08:00
|
|
|
# This is the frame buffer we'll draw into when emitting frames
|
2023-01-24 12:04:43 -08:00
|
|
|
self.draw_fbo = self.get_fbo(samples=0)
|
2020-02-11 19:51:19 -08:00
|
|
|
|
2023-01-27 10:12:53 -08:00
|
|
|
if self.window is None:
|
|
|
|
self.window_fbo = None
|
|
|
|
self.fbo = self.fbo_for_files
|
|
|
|
else:
|
|
|
|
self.window_fbo = self.ctx.detect_framebuffer()
|
|
|
|
self.fbo = self.window_fbo
|
|
|
|
|
|
|
|
self.fbo.use()
|
|
|
|
|
2022-02-13 19:32:53 +08:00
|
|
|
def init_light_source(self) -> None:
|
2020-06-03 10:38:57 -07:00
|
|
|
self.light_source = Point(self.light_source_position)
|
|
|
|
|
2023-01-25 22:34:11 -08:00
|
|
|
def use_window_fbo(self, use: bool = True):
|
|
|
|
assert(self.window is not None)
|
|
|
|
if use:
|
|
|
|
self.fbo = self.window_fbo
|
|
|
|
else:
|
|
|
|
self.fbo = self.fbo_for_files
|
|
|
|
|
2020-02-04 15:27:21 -08:00
|
|
|
# Methods associated with the frame buffer
|
2022-02-13 19:32:53 +08:00
|
|
|
def get_fbo(
|
|
|
|
self,
|
|
|
|
samples: int = 0
|
|
|
|
) -> moderngl.Framebuffer:
|
2023-01-24 12:04:43 -08:00
|
|
|
return self.ctx.framebuffer(
|
|
|
|
color_attachments=self.ctx.texture(
|
2023-01-23 14:02:06 -08:00
|
|
|
self.default_pixel_shape,
|
2020-06-08 14:09:31 -07:00
|
|
|
components=self.n_channels,
|
|
|
|
samples=samples,
|
|
|
|
),
|
2023-01-24 12:04:43 -08:00
|
|
|
depth_attachment=self.ctx.depth_renderbuffer(
|
2023-01-23 14:02:06 -08:00
|
|
|
self.default_pixel_shape,
|
2020-06-08 14:09:31 -07:00
|
|
|
samples=samples
|
|
|
|
)
|
2020-02-13 10:49:43 -08:00
|
|
|
)
|
2018-05-21 12:11:46 -07:00
|
|
|
|
2022-02-13 19:32:53 +08:00
|
|
|
def clear(self) -> None:
|
2020-06-08 14:09:31 -07:00
|
|
|
self.fbo.clear(*self.background_rgba)
|
2020-02-04 15:27:21 -08:00
|
|
|
|
2022-02-13 19:32:53 +08:00
|
|
|
def get_raw_fbo_data(self, dtype: str = 'f1') -> bytes:
|
2023-01-20 16:31:09 -08:00
|
|
|
# Copy blocks from fbo into draw_fbo using Blit
|
|
|
|
gl.glBindFramebuffer(gl.GL_READ_FRAMEBUFFER, self.fbo.glo)
|
|
|
|
gl.glBindFramebuffer(gl.GL_DRAW_FRAMEBUFFER, self.draw_fbo.glo)
|
2023-01-25 22:34:11 -08:00
|
|
|
src_viewport = self.fbo.viewport
|
2023-01-23 11:54:01 -08:00
|
|
|
gl.glBlitFramebuffer(
|
2023-01-23 17:03:46 -08:00
|
|
|
*src_viewport,
|
2023-01-23 11:54:01 -08:00
|
|
|
*self.draw_fbo.viewport,
|
|
|
|
gl.GL_COLOR_BUFFER_BIT, gl.GL_LINEAR
|
|
|
|
)
|
2023-01-20 16:31:09 -08:00
|
|
|
return self.draw_fbo.read(
|
|
|
|
viewport=self.draw_fbo.viewport,
|
2020-02-13 10:40:21 -08:00
|
|
|
components=self.n_channels,
|
|
|
|
dtype=dtype,
|
|
|
|
)
|
2016-02-23 22:29:32 -08:00
|
|
|
|
2022-02-14 21:22:18 +08:00
|
|
|
def get_image(self) -> Image.Image:
|
2020-02-04 15:27:21 -08:00
|
|
|
return Image.frombytes(
|
2020-02-13 10:40:21 -08:00
|
|
|
'RGBA',
|
|
|
|
self.get_pixel_shape(),
|
2020-02-04 15:27:21 -08:00
|
|
|
self.get_raw_fbo_data(),
|
|
|
|
'raw', 'RGBA', 0, -1
|
2017-09-26 17:41:45 -07:00
|
|
|
)
|
|
|
|
|
2022-02-13 19:32:53 +08:00
|
|
|
def get_pixel_array(self) -> np.ndarray:
|
2020-02-04 15:27:21 -08:00
|
|
|
raw = self.get_raw_fbo_data(dtype='f4')
|
|
|
|
flat_arr = np.frombuffer(raw, dtype='f4')
|
2023-01-24 12:04:43 -08:00
|
|
|
arr = flat_arr.reshape([*reversed(self.draw_fbo.size), self.n_channels])
|
2022-12-29 20:17:54 -08:00
|
|
|
arr = arr[::-1]
|
2020-02-04 15:27:21 -08:00
|
|
|
# Convert from float
|
|
|
|
return (self.rgb_max_val * arr).astype(self.pixel_array_dtype)
|
|
|
|
|
|
|
|
# Needed?
|
2022-02-13 19:32:53 +08:00
|
|
|
def get_texture(self) -> moderngl.Texture:
|
2020-02-04 15:27:21 -08:00
|
|
|
texture = self.ctx.texture(
|
|
|
|
size=self.fbo.size,
|
|
|
|
components=4,
|
|
|
|
data=self.get_raw_fbo_data(),
|
|
|
|
dtype='f4'
|
|
|
|
)
|
|
|
|
return texture
|
2016-02-23 22:29:32 -08:00
|
|
|
|
2020-02-04 15:27:21 -08:00
|
|
|
# Getting camera attributes
|
2023-01-24 12:04:43 -08:00
|
|
|
def get_pixel_size(self) -> float:
|
2023-01-23 14:02:06 -08:00
|
|
|
return self.frame.get_shape()[0] / self.get_pixel_shape()[0]
|
|
|
|
|
2022-02-13 19:32:53 +08:00
|
|
|
def get_pixel_shape(self) -> tuple[int, int]:
|
2023-01-23 17:04:44 -08:00
|
|
|
return self.draw_fbo.size
|
2018-01-31 17:17:58 -08:00
|
|
|
|
2022-02-13 19:32:53 +08:00
|
|
|
def get_pixel_width(self) -> int:
|
2020-02-04 15:27:21 -08:00
|
|
|
return self.get_pixel_shape()[0]
|
2018-01-31 17:17:58 -08:00
|
|
|
|
2022-02-13 19:32:53 +08:00
|
|
|
def get_pixel_height(self) -> int:
|
2020-02-04 15:27:21 -08:00
|
|
|
return self.get_pixel_shape()[1]
|
2018-01-31 17:17:58 -08:00
|
|
|
|
2023-01-24 12:04:43 -08:00
|
|
|
def get_aspect_ratio(self):
|
|
|
|
pw, ph = self.get_pixel_shape()
|
|
|
|
return pw / ph
|
|
|
|
|
2022-02-13 19:32:53 +08:00
|
|
|
def get_frame_height(self) -> float:
|
2020-02-04 15:27:21 -08:00
|
|
|
return self.frame.get_height()
|
2018-03-09 10:32:19 -08:00
|
|
|
|
2022-02-13 19:32:53 +08:00
|
|
|
def get_frame_width(self) -> float:
|
2020-02-04 15:27:21 -08:00
|
|
|
return self.frame.get_width()
|
2018-01-31 17:17:58 -08:00
|
|
|
|
2022-02-13 19:32:53 +08:00
|
|
|
def get_frame_shape(self) -> tuple[float, float]:
|
2020-02-11 19:51:19 -08:00
|
|
|
return (self.get_frame_width(), self.get_frame_height())
|
|
|
|
|
2022-02-13 19:32:53 +08:00
|
|
|
def get_frame_center(self) -> np.ndarray:
|
2020-02-04 15:27:21 -08:00
|
|
|
return self.frame.get_center()
|
2016-11-23 17:50:25 -08:00
|
|
|
|
2022-02-13 19:32:53 +08:00
|
|
|
def get_location(self) -> tuple[float, float, float]:
|
2021-11-08 21:46:35 -08:00
|
|
|
return self.frame.get_implied_camera_location()
|
|
|
|
|
2022-02-13 19:32:53 +08:00
|
|
|
def resize_frame_shape(self, fixed_dimension: bool = False) -> None:
|
2020-06-08 14:09:31 -07:00
|
|
|
"""
|
|
|
|
Changes frame_shape to match the aspect ratio
|
|
|
|
of the pixels, where fixed_dimension determines
|
|
|
|
whether frame_height or frame_width
|
|
|
|
remains fixed while the other changes accordingly.
|
|
|
|
"""
|
|
|
|
frame_height = self.get_frame_height()
|
|
|
|
frame_width = self.get_frame_width()
|
2023-01-24 12:04:43 -08:00
|
|
|
aspect_ratio = self.get_aspect_ratio()
|
2022-02-13 19:32:53 +08:00
|
|
|
if not fixed_dimension:
|
2020-06-08 14:09:31 -07:00
|
|
|
frame_height = frame_width / aspect_ratio
|
|
|
|
else:
|
|
|
|
frame_width = aspect_ratio * frame_height
|
2023-01-24 12:04:43 -08:00
|
|
|
self.frame.set_height(frame_height, stretch=true)
|
|
|
|
self.frame.set_width(frame_width, stretch=true)
|
2020-06-08 14:09:31 -07:00
|
|
|
|
2020-02-04 15:27:21 -08:00
|
|
|
# Rendering
|
2022-12-27 14:53:55 -08:00
|
|
|
def capture(self, *mobjects: Mobject) -> None:
|
2023-01-26 20:38:38 -08:00
|
|
|
self.clear()
|
2023-01-25 14:13:56 -08:00
|
|
|
self.refresh_uniforms()
|
2023-01-25 22:34:11 -08:00
|
|
|
self.fbo.use()
|
2020-06-26 19:29:34 -07:00
|
|
|
for mobject in mobjects:
|
2023-01-25 14:13:56 -08:00
|
|
|
mobject.render(self.ctx, self.uniforms)
|
2020-06-29 18:17:18 -07:00
|
|
|
|
2023-01-25 14:13:56 -08:00
|
|
|
def refresh_uniforms(self) -> None:
|
2021-01-18 16:39:29 -08:00
|
|
|
frame = self.frame
|
2023-01-23 14:41:17 -08:00
|
|
|
view_matrix = frame.get_view_matrix()
|
2023-01-16 19:34:20 -08:00
|
|
|
light_pos = self.light_source.get_location()
|
2023-01-18 13:44:41 -08:00
|
|
|
cam_pos = self.frame.get_implied_camera_location()
|
2021-01-18 16:39:29 -08:00
|
|
|
|
2023-01-25 14:13:56 -08:00
|
|
|
self.uniforms.update(
|
2023-01-23 17:10:18 -08:00
|
|
|
frame_shape=frame.get_shape(),
|
|
|
|
pixel_size=self.get_pixel_size(),
|
2023-01-23 14:41:17 -08:00
|
|
|
view=tuple(view_matrix.T.flatten()),
|
2023-01-18 13:59:50 -08:00
|
|
|
camera_position=tuple(cam_pos),
|
|
|
|
light_position=tuple(light_pos),
|
|
|
|
focal_distance=frame.get_focal_distance(),
|
|
|
|
)
|
2020-06-08 20:27:07 -07:00
|
|
|
|
2020-06-04 15:41:20 -07:00
|
|
|
|
2020-06-26 23:05:25 -07:00
|
|
|
# Mostly just defined so old scenes don't break
|
2020-06-04 15:41:20 -07:00
|
|
|
class ThreeDCamera(Camera):
|
2022-12-28 18:52:05 -08:00
|
|
|
def __init__(self, samples: int = 4, **kwargs):
|
|
|
|
super().__init__(samples=samples, **kwargs)
|