lmfilament guide

This guide explains how lmfilament is put together and how a scene script flows from text to a picture. For commands and rules see the skill page; for signatures see the reference.

Overview

lmfilament drives Google Filament from LMScript with one static executable. It embeds Filament 1.78.1, including the material compiler, the engine and the geometry tools, and exposes them to an _lmcode_ block through one context object named fil. The same host pattern is used by lmhtml and lmcad: the outer LMScript program holds the block as a value, run_lmcode compiles it, starts the host and returns an envelope with ok and result.

What is real and what is a preview

AreaStatus
Material compilation from mat source to a filamat packageReal; all upstream sample materials compile
Engine, scene, camera, light, renderable and material-instance objectsReal Filament objects
Procedural meshes, OBJ import and export, tangent framesReal
Filament pixelsNot available: a fully static binary cannot load a GPU driver, so Filament runs on its noop backend and render_frames drives the render loop without output
Pictures from Scene.render_pngA small CPU preview rasterizer: Lambert shading from the scene lights, base color from material parameters or the albedo texture, unlit vertex colors, optional shadow map

Architecture

  1. lmscript reads the .lms file, resolves cli="lmfilament" and, through install="github:lmctl-ai/lmfilament@latest", the release binary in its tool cache.
  2. The compiler type-checks the block against the schema embedded in the host: 7 host types with 62 methods, the context fil, and the 206 standard signatures.
  3. run_lmcode passes the compiled program to the host with the --lmcode protocol. The host creates a Filament engine, which logs that the resolved backend is Noop.
  4. The host answers with one result frame between LMCODE_RETURN_BEGIN and LMCODE_RETURN_END; run_lmcode turns it into the envelope the script tests with r.ok.

Domain model

Material
Filament material source compiled by the embedded material compiler, from an inline string or a file. Parameters are set by name with set_float, set_float3, set_float4 and set_texture. The examples set baseColor before each add to give each renderable its own color from one compiled material.
Mesh
Geometry from a procedural factory (box, sphere, icosphere, cylinder, cone, capsule, plane, ring, triangle, text, tube_between) or from an OBJ file. Meshes carry UVs with v = 0 at the top row of the image.
Texture
An image created empty, as a checker, or loaded from PNG, JPEG, BMP or TGA. Pixel, filter, wrap and repeat shape it; save_png writes it. It is uploaded to Filament when its material is added to a scene.
Scene
Holds renderables, lights, a camera and a skybox color. add and add_at place a mesh with a material; add_part returns a Part handle.
Part and Node
A Part is one renderable that can be translated, rotated, scaled, hidden and parented. A Node is an empty transform from Scene.node, backed by the Filament TransformManager; parts and nodes parented to it move with it, which is how the scene-graph and model-viewer examples pose a robot.

Workflow

  1. Create output directories in the outer script with mkdir.
  2. Compile materials with fil.material or fil.material_file.
  3. Create textures, set filter and wrap, then bind them with set_texture.
  4. Create the scene, set skybox and camera, add meshes with materials, add lights, optionally call shadows(true).
  5. Call render_frames to drive the Filament loop, then render_png for the preview picture.
  6. Return facts with lmcode_return_put, test r.ok and assert in the outer script.

For animation, the homepage clip examples write 240 frames and then encode them with ffmpeg into MP4 and GIF files; the outer script computes the time value and the block moves and hides parts with translate and visible.

Textures in the preview

The CPU preview samples the albedo image: the sampler named albedo, baseColor, color or emissive, else the first one, with mip-mapped trilinear or nearest filtering. Other maps such as roughness, metallic, normal and ao are uploaded to Filament but not used for shading. Set filtering and wrapping before the material is added to a scene.

Shadows

Scene.shadows(true) enables the shadow map of the preview rasterizer. The repository shadows example describes it as an orthographic depth pass along the light, 2048 x 2048, with 3 x 3 PCF. The cascaded shadow maps of Filament itself need a GPU.

Examples in the repository

The examples directory holds 38 scripts: ports of upstream Filament C++ samples (hellotriangle, hellopbr, suzanne, lightbulb, shadowtest, material_sandbox, texturedquad, heightfield and others), Magnum Python examples (primitives, scene graph, model viewer, text), scenes from lmblender and lmcad (molecules, DNA helix, Cornell box, Saturn, truss bridge) and texture demonstrations. Materials are inlined, so most run from any directory; the two Suzanne examples and texturedquad need files from a Filament checkout. The examples strobecolor, point_sprites and compile-sample-materials produce no picture.

Limits

See also