Skip to content

Commit

Permalink
Reorganize how shader files sit in the shaders folder
Browse files Browse the repository at this point in the history
  • Loading branch information
3b1b committed Jan 6, 2021
1 parent 6486938 commit 065df65
Show file tree
Hide file tree
Showing 34 changed files with 56 additions and 59 deletions.
8 changes: 4 additions & 4 deletions example_scenes.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,10 @@ def construct(self):
# https://commons.wikimedia.org/wiki/File:The_earth_at_night.jpg
# and make sure they are available in whatever folder manim
# looks for images, then uncomment the lines below
surfaces = [
TexturedSurface(surface, "EarthTextureMap", "NightEarthTextureMap")
for surface in [sphere, torus1, torus2]
]
# surfaces = [
# TexturedSurface(surface, "EarthTextureMap", "NightEarthTextureMap")
# for surface in [sphere, torus1, torus2]
# ]

for mob in surfaces:
mob.mesh = SurfaceMesh(mob)
Expand Down
11 changes: 5 additions & 6 deletions manimlib/mobject/mobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ class Mobject(object):
# Positive shadow up to 1 makes a side opposite the light darker
"shadow": 0.0,
# For shaders
"vert_shader_file": "",
"geom_shader_file": "",
"frag_shader_file": "",
"shader_folder": "",
# "vert_shader_file": "",
# "geom_shader_file": "",
# "frag_shader_file": "",
"render_primitive": moderngl.TRIANGLE_STRIP,
"texture_paths": None,
"depth_test": False,
Expand Down Expand Up @@ -1222,9 +1223,7 @@ def init_shader_data(self):
self.shader_indices = None
self.shader_wrapper = ShaderWrapper(
vert_data=self.shader_data,
vert_file=self.vert_shader_file,
geom_file=self.geom_shader_file,
frag_file=self.frag_shader_file,
shader_folder=self.shader_folder,
texture_paths=self.texture_paths,
depth_test=self.depth_test,
render_primitive=self.render_primitive,
Expand Down
4 changes: 1 addition & 3 deletions manimlib/mobject/types/dot_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ class DotCloud(PMobject):
CONFIG = {
"color": GREY_C,
"opacity": 1,
"vert_shader_file": "true_dot_vert.glsl",
"geom_shader_file": "true_dot_geom.glsl",
"frag_shader_file": "true_dot_frag.glsl",
"shader_folder": "true_dot",
"render_primitive": moderngl.POINTS,
"shader_dtype": [
('point', np.float32, (3,)),
Expand Down
3 changes: 1 addition & 2 deletions manimlib/mobject/types/image_mobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ class ImageMobject(Mobject):
CONFIG = {
"height": 4,
"opacity": 1,
"vert_shader_file": "image_vert.glsl",
"frag_shader_file": "image_frag.glsl",
"shader_folder": "image",
"shader_dtype": [
('point', np.float32, (3,)),
('im_coords', np.float32, (2,)),
Expand Down
6 changes: 2 additions & 4 deletions manimlib/mobject/types/surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ class ParametricSurface(Mobject):
"epsilon": 1e-5,
"render_primitive": moderngl.TRIANGLES,
"depth_test": True,
"vert_shader_file": "surface_vert.glsl",
"frag_shader_file": "surface_frag.glsl",
"shader_folder": "surface",
"shader_dtype": [
('point', np.float32, (3,)),
('du_point', np.float32, (3,)),
Expand Down Expand Up @@ -201,8 +200,7 @@ def init_points(self):

class TexturedSurface(ParametricSurface):
CONFIG = {
"vert_shader_file": "textured_surface_vert.glsl",
"frag_shader_file": "textured_surface_frag.glsl",
"shader_folder": "textured_surface",
"shader_dtype": [
('point', np.float32, (3,)),
('du_point', np.float32, (3,)),
Expand Down
22 changes: 10 additions & 12 deletions manimlib/mobject/types/vectorized_mobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@ class VMobject(Mobject):
"n_points_per_curve": 3,
"long_lines": False,
# For shaders
"stroke_vert_shader_file": "quadratic_bezier_stroke_vert.glsl",
"stroke_geom_shader_file": "quadratic_bezier_stroke_geom.glsl",
"stroke_frag_shader_file": "quadratic_bezier_stroke_frag.glsl",
"fill_vert_shader_file": "quadratic_bezier_fill_vert.glsl",
"fill_geom_shader_file": "quadratic_bezier_fill_geom.glsl",
"fill_frag_shader_file": "quadratic_bezier_fill_frag.glsl",
"stroke_shader_folder": "quadratic_bezier_stroke",
# "stroke_vert_shader_file": "quadratic_bezier_stroke_vert.glsl",
# "stroke_geom_shader_file": "quadratic_bezier_stroke_geom.glsl",
# "stroke_frag_shader_file": "quadratic_bezier_stroke_frag.glsl",
"fill_shader_folder": "quadratic_bezier_fill",
# "fill_vert_shader_file": "quadratic_bezier_fill_vert.glsl",
# "fill_geom_shader_file": "quadratic_bezier_fill_geom.glsl",
# "fill_frag_shader_file": "quadratic_bezier_fill_frag.glsl",
# Could also be Bevel, Miter, Round
"joint_type": "auto",
"render_primitive": moderngl.TRIANGLES,
Expand Down Expand Up @@ -903,16 +905,12 @@ def init_shader_data(self):
self.fill_shader_wrapper = ShaderWrapper(
vert_data=self.fill_data,
vert_indices=np.zeros(0, dtype='i4'),
vert_file=self.fill_vert_shader_file,
geom_file=self.fill_geom_shader_file,
frag_file=self.fill_frag_shader_file,
shader_folder=self.fill_shader_folder,
render_primitive=self.render_primitive,
)
self.stroke_shader_wrapper = ShaderWrapper(
vert_data=self.stroke_data,
vert_file=self.stroke_vert_shader_file,
geom_file=self.stroke_geom_shader_file,
frag_file=self.stroke_frag_shader_file,
shader_folder=self.stroke_shader_folder,
render_primitive=self.render_primitive,
)

Expand Down
3 changes: 1 addition & 2 deletions manimlib/scene/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
from manimlib.animation.transform import MoveToTarget
from manimlib.mobject.mobject import Point
from manimlib.camera.camera import Camera
from manimlib.config import get_custom_defaults
from manimlib.constants import *
from manimlib.constants import DEFAULT_WAIT_TIME
from manimlib.mobject.mobject import Mobject
from manimlib.scene.scene_file_writer import SceneFileWriter
from manimlib.utils.config_ops import digest_config
Expand Down
47 changes: 26 additions & 21 deletions manimlib/shader_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import os
import warnings
import logging
import re
import moderngl
import numpy as np
import copy

from manimlib.utils.directories import get_shader_dir
from manimlib.utils.file_ops import seek_full_path_from_defaults

# Mobjects that should be rendered with
# the same shader will be organized and
Expand All @@ -18,9 +19,7 @@ class ShaderWrapper(object):
def __init__(self,
vert_data=None,
vert_indices=None,
vert_file=None,
geom_file=None,
frag_file=None,
shader_folder=None,
uniforms=None, # A dictionary mapping names of uniform variables
texture_paths=None, # A dictionary mapping names to filepaths for textures.
depth_test=False,
Expand All @@ -29,9 +28,7 @@ def __init__(self,
self.vert_data = vert_data
self.vert_indices = vert_indices
self.vert_attributes = vert_data.dtype.names
self.vert_file = vert_file
self.geom_file = geom_file
self.frag_file = frag_file
self.shader_folder = shader_folder
self.uniforms = uniforms or dict()
self.texture_paths = texture_paths or dict()
self.depth_test = depth_test
Expand All @@ -53,8 +50,7 @@ def copy(self):
def is_valid(self):
return all([
self.vert_data is not None,
self.vert_file,
self.frag_file,
self.shader_folder,
])

def get_id(self):
Expand All @@ -66,9 +62,7 @@ def get_program_id(self):
def create_id(self):
# A unique id for a shader
return "|".join(map(str, [
self.vert_file,
self.geom_file,
self.frag_file,
self.shader_folder,
self.uniforms,
self.texture_paths,
self.depth_test,
Expand All @@ -79,13 +73,18 @@ def refresh_id(self):
self.id = self.create_id()

def create_program_id(self):
return "|".join(map(str, [self.vert_file, self.geom_file, self.frag_file]))
return self.shader_folder

def get_program_code(self):
def get_code(name):
return get_shader_code_from_file(
os.path.join(self.shader_folder, f"{name}.glsl")
)

return {
"vertex_shader": get_shader_code_from_file(self.vert_file),
"geometry_shader": get_shader_code_from_file(self.geom_file),
"fragment_shader": get_shader_code_from_file(self.frag_file),
"vertex_shader": get_code("vert"),
"geometry_shader": get_code("geom"),
"fragment_shader": get_code("frag"),
}

def combine_with(self, *shader_wrappers):
Expand All @@ -111,10 +110,14 @@ def get_shader_code_from_file(filename):
if not filename:
return None

filepath = os.path.join(get_shader_dir(), filename)
if not os.path.exists(filepath):
warnings.warn(f"No file at {filepath}")
return
try:
filepath = seek_full_path_from_defaults(
filename,
directories=[get_shader_dir(), "/"],
extensions=[],
)
except IOError:
return None

with open(filepath, "r") as f:
result = f.read()
Expand All @@ -125,6 +128,8 @@ def get_shader_code_from_file(filename):
# Replace "#INSERT " lines with relevant code
insertions = re.findall(r"^#INSERT .*\.glsl$", result, flags=re.MULTILINE)
for line in insertions:
inserted_code = get_shader_code_from_file(line.replace("#INSERT ", ""))
inserted_code = get_shader_code_from_file(
os.path.join("inserts", line.replace("#INSERT ", ""))
)
result = result.replace(line, inserted_code)
return result
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 3 additions & 2 deletions manimlib/utils/file_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ def guarantee_existence(path):
return os.path.abspath(path)


def seek_full_path_from_defaults(file_name, default_dir, extensions):
def seek_full_path_from_defaults(file_name, directories, extensions):
possible_paths = [file_name]
possible_paths += [
os.path.join(default_dir, file_name + extension)
os.path.join(directory, file_name + extension)
for directory in directories
for extension in ["", *extensions]
]
for path in possible_paths:
Expand Down
4 changes: 2 additions & 2 deletions manimlib/utils/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
def get_full_raster_image_path(image_file_name):
return seek_full_path_from_defaults(
image_file_name,
default_dir=get_raster_image_dir(),
directories=[get_raster_image_dir()],
extensions=[".jpg", ".png", ".gif"]
)


def get_full_vector_image_path(image_file_name):
return seek_full_path_from_defaults(
image_file_name,
default_dir=get_vector_image_dir(),
directories=[get_vector_image_dir()],
extensions=[".svg", ".xdv"],
)

Expand Down
2 changes: 1 addition & 1 deletion manimlib/utils/sounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ def play_finish_sound():
def get_full_sound_file_path(sound_file_name):
return seek_full_path_from_defaults(
sound_file_name,
default_dir=os.path.join("assets", "sounds"),
directories=[os.path.join("assets", "sounds")],
extensions=[".wav", ".mp3"]
)

0 comments on commit 065df65

Please sign in to comment.