mirror of
https://github.com/3b1b/manim.git
synced 2025-07-28 04:23:16 +08:00
Move Texture handling and vao creation outside of Camera
This commit is contained in:
@ -4,6 +4,7 @@ import os
|
||||
import re
|
||||
from functools import lru_cache
|
||||
import moderngl
|
||||
from PIL import Image
|
||||
|
||||
from manimlib.utils.directories import get_shader_dir
|
||||
from manimlib.utils.file_ops import find_file
|
||||
@ -14,6 +15,34 @@ if TYPE_CHECKING:
|
||||
from typing import Sequence, Optional
|
||||
|
||||
|
||||
ID_TO_TEXTURE: dict[int, moderngl.Texture] = dict()
|
||||
|
||||
|
||||
@lru_cache()
|
||||
def image_path_to_texture(path: str, ctx: moderngl.Context) -> moderngl.Texture:
|
||||
im = Image.open(path).convert("RGBA")
|
||||
return ctx.texture(
|
||||
size=im.size,
|
||||
components=len(im.getbands()),
|
||||
data=im.tobytes(),
|
||||
)
|
||||
|
||||
|
||||
def get_texture_id(texture: moderngl.Texture) -> int:
|
||||
tid = 0
|
||||
while tid in ID_TO_TEXTURE:
|
||||
tid += 1
|
||||
ID_TO_TEXTURE[tid] = texture
|
||||
texture.use(location=tid)
|
||||
return tid
|
||||
|
||||
|
||||
def release_texture(texture_id: int):
|
||||
texture = ID_TO_TEXTURE.pop(texture_id, None)
|
||||
if texture is not None:
|
||||
texture.release()
|
||||
|
||||
|
||||
@lru_cache()
|
||||
def get_shader_program(
|
||||
ctx: moderngl.context.Context,
|
||||
|
Reference in New Issue
Block a user