material_shaders¶
Lighting-model shader functions backing the Three.js-style material system.
Each function follows the shader calling convention used throughout Algan (see
SHADER_FIXED_PARAM_COUNT below): the first nine parameters are fixed
(memory, vertex_location, vertex_normal, albedo_color,
camera_location, light_origin, light_color, light_intensity,
ambient_light_intensity) and any further parameters are the material’s
animatable properties. The renderer registers those extra parameters as
animatable attributes on the mob (see
set_shader()).
Channel layout¶
albedo_color arrives as 4 channels [R, G, B, glow] (the mob’s color
without its trailing opacity channel) and the return value is written back into
those same 4 channels, so every shader does its RGB maths on [..., :3] and
re-attaches the passthrough glow channel. light_color is likewise
4 channels (opacity pre-multiplied); only its RGB is used.
These are intentionally simplified, real-time-friendly approximations of the Three.js GLSL materials – enough to reproduce their look and respond to the same properties, evaluated per vertex in PyTorch. These functions do not themselves sample texture or environment images. The renderer’s default fragment path does have UV samplers for color, normal, roughness and metalness maps, together with its environment-lighting/transport paths. Limitations noted on a function here refer to that vertex-shader implementation, not to the whole renderer.
Functions
- basic_material_shader(memory, vertex_location, vertex_normal, albedo_color, camera_location, light_origin, light_color, light_intensity, ambient_light_intensity)[source]¶
MeshBasicMaterial: unlit, returns the flat base color unchanged.
- Parameters:
light_intensity (float)
ambient_light_intensity (float)
- depth_shader(memory, vertex_location, vertex_normal, albedo_color, camera_location, light_origin, light_color, light_intensity, ambient_light_intensity, near=0.1, far=100.0)[source]¶
MeshDepthMaterial: grayscale by camera distance, near=bright, far=dark.
Approximates Three.js depth packing with a simple linear luminance ramp.
- Parameters:
light_intensity (float)
ambient_light_intensity (float)
near (float)
far (float)
- lambert_shader(memory, vertex_location, vertex_normal, albedo_color, camera_location, light_origin, light_color, light_intensity, ambient_light_intensity, emissive=(0.0, 0.0, 0.0), emissive_intensity=1.0, flat_shading=0.0, env_map_intensity=1.0)[source]¶
MeshLambertMaterial: Lambertian (diffuse-only) lighting plus emissive.
- Parameters:
light_intensity (float)
ambient_light_intensity (float)
emissive_intensity (float)
flat_shading (float)
env_map_intensity (float)
- manim_shader(memory, vertex_location, vertex_normal, albedo_color, camera_location, light_origin, light_color, light_intensity, ambient_light_intensity, flat_shading=0.0)[source]¶
Shade a surface with Manim’s default 3-D lighting model.
Implements Manim’s
get_shaded_rgb: each light contributes an offset of0.5 * (n . to_light) ** 3– halved when the surface faces away from the light, so back-facing surfaces darken at half the rate front-facing ones brighten. There is no ambient term, no specular lobe and no distance falloff;ambient_light_intensityis accepted for signature parity but unused, because Manim’s model has none.Under Manim’s own rig – the single white
PointLightof intensity 1 thatuse_manim_defaults()installs, with decay 0 and distance 0 – the light-color factor below is exactly(1, 1, 1)and the offset reproduces Manim’s scalar one exactly. Multiplying bylight_color * light_intensityis a strict generalisation to colored and multi-light rigs, which Manim does not have; the per-light offsets simply sum.Manim adds its offset to display-referred sRGB values. Under the default linear working space this shader therefore encodes the base color to sRGB, adds the offsets there, clamps the sum to
[0, 1]and decodes back to linear light; under the display-referred setting it adds and clamps directly. Exact Manim fidelity further assumes exposure 1 and tonemapping off – which is whatuse_manim_defaults()sets; any other exposure or tonemap curve maps the result as Manim never would.- Parameters:
memory – Scratch-tensor provider supplied by the renderer. Unused here.
vertex_location – Location of the vertex to shade, shape
(*, 3); the renderer’s triangle path passes(*, 3, 3)corners.vertex_normal – Surface normal at the vertex; need not be normalized. Shape
(*, 3).albedo_color – Base color with its trailing glow channel, shape
(*, 4), which is also the shape of the return value.camera_location – Camera position, shape
(*, 3). Accepted for signature parity; Manim’s model is view-independent, so this shader ignores it.light_origin – Position of the light source, shape
(*, 3).light_color – Color of the light with its trailing opacity channel, shape
(*, 4); only its RGB is used.light_intensity (float) – Multiplier on the light’s contribution. Defaults to whatever the renderer passes (1 for Algan’s stock rig).
ambient_light_intensity (float) – Accepted for signature parity; unused, since the model has no ambient term.
flat_shading (float) – Blend of the interpolated normal toward the flat per-face normal, from 0 (smooth, the default) to 1 (flat).
- matcap_shader(memory, vertex_location, vertex_normal, albedo_color, camera_location, light_origin, light_color, light_intensity, ambient_light_intensity, flat_shading=0.0)[source]¶
MeshMatcapMaterial: the matcap image is not sampled (no UV pipeline); this approximates a default matcap with a view-facing diffuse term plus a rim highlight, tinted by the base color.
- Parameters:
light_intensity (float)
ambient_light_intensity (float)
flat_shading (float)
- normal_shader(memory, vertex_location, vertex_normal, albedo_color, camera_location, light_origin, light_color, light_intensity, ambient_light_intensity, flat_shading=0.0)[source]¶
MeshNormalMaterial: RGB encodes the surface normal (
n * 0.5 + 0.5).Three.js uses view-space normals; only the camera location (not its orientation) is available here, so this uses world-space normals.
- Parameters:
light_intensity (float)
ambient_light_intensity (float)
flat_shading (float)
- phong_shader(memory, vertex_location, vertex_normal, albedo_color, camera_location, light_origin, light_color, light_intensity, ambient_light_intensity, emissive=(0.0, 0.0, 0.0), emissive_intensity=1.0, specular=(0.0666, 0.0666, 0.0666), shininess=30.0, flat_shading=0.0, env_map_intensity=1.0)[source]¶
MeshPhongMaterial: Blinn-Phong diffuse + specular highlight + emissive.
Twin of
shading_taichi._stage_phong; read its docstring for why the specular lobe carries three.js’s0.25 * (shininess * 0.5 + 1)normalization and its Fresnel term but not its1/pi(the diffuse lobe drops the same factor, so the ratio between them is three.js’s exactly).- Parameters:
light_intensity (float)
ambient_light_intensity (float)
emissive_intensity (float)
shininess (float)
flat_shading (float)
env_map_intensity (float)
- physical_shader(memory, vertex_location, vertex_normal, albedo_color, camera_location, light_origin, light_color, light_intensity, ambient_light_intensity, roughness=1.0, metalness=0.0, emissive=(0.0, 0.0, 0.0), emissive_intensity=1.0, env_map_intensity=1.0, flat_shading=0.0, ior=1.5, specular_intensity=1.0, specular_color=(1.0, 1.0, 1.0), clearcoat=0.0, clearcoat_roughness=0.0, sheen=0.0, sheen_roughness=1.0, sheen_color=(0.0, 0.0, 0.0), transmission=0.0, iridescence=0.0, attenuation_sigma=(0.0, 0.0, 0.0), sigma_s=(0.0, 0.0, 0.0), g=0.0)[source]¶
MeshPhysicalMaterial: MeshStandard plus clearcoat, sheen, ior-driven specular and (approximate) transmission.
iordrives the dielectric base reflectivityF0 = ((ior - 1) / (ior + 1))^2, scaled byspecular_intensityand tinted byspecular_color(the KHR specular workflow). A second GGX lobe adds theclearcoat.sheenadds a soft inverted-Fresnel rim. Thetransmissionandiridescenceparameters are approximated (no refraction / thin-film spectral model in a per-vertex pass). Volumetric absorption is not approximated here at all: it is applied along the refracted path in the renderer, driven by this parameter’s packed slot.- Parameters:
light_intensity (float)
ambient_light_intensity (float)
roughness (float)
metalness (float)
emissive_intensity (float)
env_map_intensity (float)
flat_shading (float)
ior (float)
specular_intensity (float)
clearcoat (float)
clearcoat_roughness (float)
sheen (float)
sheen_roughness (float)
transmission (float)
iridescence (float)
g (float)
- smith_geometry(n_dot_v, n_dot_l, roughness)[source]¶
Smith geometry term with Schlick-GGX, direct-lighting k remapping.
- standard_shader(memory, vertex_location, vertex_normal, albedo_color, camera_location, light_origin, light_color, light_intensity, ambient_light_intensity, roughness=1.0, metalness=0.0, emissive=(0.0, 0.0, 0.0), emissive_intensity=1.0, env_map_intensity=1.0, flat_shading=0.0)[source]¶
MeshStandardMaterial: metalness/roughness Cook-Torrance PBR + emissive.
Implements the GGX NDF, Smith geometry and Schlick Fresnel terms. The image-based environment reflection of the GLSL material is approximated by a constant ambient term scaled by
env_map_intensity.- Parameters:
light_intensity (float)
ambient_light_intensity (float)
roughness (float)
metalness (float)
emissive_intensity (float)
env_map_intensity (float)
flat_shading (float)
- toon_shader(memory, vertex_location, vertex_normal, albedo_color, camera_location, light_origin, light_color, light_intensity, ambient_light_intensity, emissive=(0.0, 0.0, 0.0), emissive_intensity=1.0, num_bands=3.0, flat_shading=0.0)[source]¶
MeshToonMaterial: diffuse lighting quantized into flat bands (cel shading).
The Three.js
gradientMapis approximated by an evennum_bands-step ramp.- Parameters:
light_intensity (float)
ambient_light_intensity (float)
emissive_intensity (float)
num_bands (float)
flat_shading (float)