Viz architecture¶
Canonical overview of the Tanga viewer (py/pytanga/viz/) after the
scene/layout/overlay remodel. It documents the ownership hierarchy, the
data flows, and the extension recipes (new entities, new *View
controls, new overlay objects like Dialog/Banner). Written for developers
and for AI agents: read the invariants + file map before touching code.
Big picture¶
One hierarchy, one owner per concern — no parallel global-registry dicts.
Visualizer facade + composition root + lifecycle (boot/loop)
|- Transport WebSocketTransport: send / register / route / dispatch
|- LayoutHost owns scenes AND layouts (+ register/resolve/dispatch)
| |- scenes: dict[name, Scene] entity containers (add_scene)
| `- layouts: dict[name, Layout] first-class layouts (set_layout)
| `- Layout { base: View, overlay: OverlayContainer }
|- ThemeHost / InteractionHost
`- OverlayContainer per-layout: overlays, dialogs, banners, editor
There is no ControlHost. Each *View wraps a Control that owns its own
value, serialization, and handlers (serialize / register_handlers /
handle_event); LayoutHost owns the tree walk that registers them and the
inbound dispatch that resolves them.
Sceneowns entities and the typed entity API. One scene can appear in many panes (SceneView(name)) and many layouts.Layoutis abaseview tree plus anOverlayContainer. URLs name layouts (/?view=<name>), never scenes.Visualizerwires everything together and exposes the publicviz.*API through explicit thin forwarders (no__getattr__).
Ownerships¶
| Concern | Owner | Notes |
|---|---|---|
| named scenes | LayoutHost.scenes |
add_scene(name) auto-creates Layout(name, SceneView(name)) |
| named layouts | LayoutHost.layouts |
set_layout(root, name) / add_layout(root, name) |
| base view tree | Layout.base |
SplitView / StackView / SceneView |
| view identity | View.id (auto v0…, overridable via id=) |
stable across re-serializations; remove_view(id) |
| overlays (groups/dialogs/banners/editor) | Layout.overlay (OverlayContainer) |
one per layout (shared instance today) |
| entities | Scene (add_viz/add/update/remove/clear) |
|
| styles | Scene.styles (copy of the master _global_styles) |
|
| control model (value/handlers/history) | the Control on each *View |
view.control |
(id, event) handlers |
ControlHandlerRegistry (via Transport) |
registered by Control.register_handlers |
| inbound routing | Transport.route data table -> LayoutHost.dispatch_control_event |
Data flows¶
- Boot —
Visualizer.__init__buildsTransport->LayoutHost(+add_scene("")) ->ThemeHost/InteractionHost;OverlayContainergets a back-reference to its owningLayoutHost, then_register_routes(). - URL -> layout —
/serves the""layout,/?view=<name>serveslayouts[name];add_scene(name)supplies scene-URL sugar for free. - Add an entity —
viz.add(...)->layout.scene("").add_viz(...)->Scene.add/add_object->SceneObject+VizNode->flush()->entity_*messages._resolve_scene_entityturns MVs intoSceneEntity(and refines a rawConicto its specific 2D entity so the viewer can serialize it;Quadric3Dis left alone and renders via the analytic ray path). - Insert a detached subtree — a
VizGroup/VizSceneObjecttree composed before any scene exists is added viaviz.add(group)/viz.new(group)→Scene.add_viz→Scene.add_subtree, which registers every descendant in_nodes(auto-assigning ids where omitted) and backfills each node's partial style from the scene's per-kind defaults. - Add a control — build a
*View, mount it viaset_layout(or declaratively inSceneView(overlay=[...]));LayoutHost.registerwalks the tree and calls eachControl.register_handlers(registers(id, event)handlers). ParamsView— a composite view (views/params_view.py) that renders a dict or a@dataclassas one childControlViewper key (title in the left column, control on the right). Nested dataclasses render as nestedGroupViews (sharing the parenton_changevia dottedevent.key), andextra_rows=[RowSpec(…)](top-level or per-GroupUI) adds non-parameter rows;ControlUI.full_widthrows render without a key column. The children are ordinary control views, so their handlers register on the same(id, event)registry; the newControl.hide_labelflag makes each render label-less. Seeviz-controls-and-interactions.md.- Inbound control event —
control:*->LayoutHost.dispatch_control_event->resolve_controltree-walk (layouts + dialogs) ->control.handle_event->Dispatch(event, value, push)-> push + fire the handler. - Banner/dialog/editor —
OverlayContainer.show_*registers(id, event)handlers and sendsbanner_*/dialog_*/editor_*wire messages; the*_closed/accept/closeroutes callOverlayContainer._on_*. - Add/remove an overlay view —
viz.add(view)mounts aViewin the global overlay and pushes a granularoverlay_define(only that view, not the whole layout);viz.remove_view(id)removes it viaoverlay_remove. Per-scene overlays (scene=…) re-sync the layout instead. EveryViewcarries a stableid(autov0…) so it can be addressed at runtime. - Layout re-push — the frontend reconciles the whole view tree by stable
view id across
view_layoutre-pushes. A single live-view registry (_viewRegistry) reuses each view whose id is unchanged: scene panes keep their WebGL scene/camera; simple controls keep their DOM; and the stateful content views (log_view,file_chooser_view,table_view,progress_bar_view,group) keep their runtime registries and content. Only the pure-layout containers (split/stack/toolbar/menu) are rebuilt (cheap DOM) and re-attach the reused children in the new order, so reordering a layout re-parents the expensive panes instead of destroying them. A registry- backed view only forgets its runtime-registry entry indestroy()when it still points at that view. A scene pane newly introduced by a re-push fetches its state with ascene_sync_requestround-trip.
Canonical frame + transform placement¶
Every scene entity renders in a canonical frame and its placement rides on a
single per-entity Transform (quaternion TRS) applied to the per-entity
THREE.Group that already wraps each mesh (wrapWithNodeTransform /
applyTransformToObject in scene-builder.js). There is no second, baked
placement in the renderers or the content serialization — this is what makes a
Circle-center change a cheap transform patch rather than a content
re-serialization.
- Canonical frames (three.js defaults): linear primitives (
Line,Cylinder,Cone,Direction,Parabola,Hyperbola) along +Y with their origin at the node position; planar primitives (Circle,Arc,Disk,PartialDisk,Ellipse,RegularPolygon,Rectangle2D,Plane) in the XY plane (normal +Z) centred at the node position; volumes (Sphere,Box,Ellipsoid) centred at the node position, axis-aligned (Box/Ellipsoidrotation→ quaternion);Point/HPointat the node position. - Decomposition —
_decompose.py::_entity_decompose(entity)maps an entity's placement into a(Transform, shape)pair:center/origin/vertex/point→ position;normal/axis/direction(+startDirection/dirU/dirV/horizontalwhere a second axis fixes the frame) → quaternion;rotation(Box/Ellipsoid) → quaternion; everything else → shape params. The scene-graph node (VizSceneObject.__init__) derives itstransformfrom this decomposition, andset_entityre-derives it and diffs shape vs placement with a configurable epsilon (default1e-9):transformon placement change,contenton shape change,fullon kind change. Out-of-scope kinds (PointPath/Curve/PointSet, point/plane pairs, operators, axes/grid, SDF/ray/image) return(Transform(), {})and rebuild on any change. - Wire contract — the serializer emits shape-only content (no
center/normal/axis/origin/direction/vertex/rotation/startDirection); placement rides on the nodetransform, whoserotationis a quaternion[x, y, z, w](Transform.to_dict()).serialize_entity()emits the sametransformfor direct callers. The glTF exporter reads the nodetransform(position + quaternion) rather than re-deriving it from geometry. Transformlocation —Transformand the argument-coercion helpers live ingeometry/transform.py; the pure matrix/quaternion math ingeometry/transforms.py;vizkeeps thin re-export shims (viz/_transforms.py,viz/_types.py) and re-exportsTransformpublicly.Transform.rotationis a quaternion (single source of truth), thematrix()is computed lazily, andTransformoverloads@/*to apply toPoint(R·S·p + t) andDirection(R·S·d, no translation).Frustumis re-parameterized intrinsically (origin,axis,horizontal,near,far,far_half_width,far_half_height) so it participates in transform placement and diffing like the other entities.
Extension recipes¶
New entity kind¶
- Add the
SceneEntity(or reuseSceneObjectfor non-geometry drawables). - Route it through
Scene.add_viz— it already handlescolor/opacity/style/label/parent_id/attach_to; note_resolve_scene_entityfor MV analysis (multivector ->SceneEntity). - Serialize it in the node layer (
VizSceneObject/SceneObjectinscene.py/_nodes.py). - Add a frontend renderer + a test (round-trip + flush).
- Export an
update<Kind>(mesh, ent, prev)from the renderer module (returnfalseto rebuild) for kinds whose geometry derives from content fields, so a live content change re-samples/re-builds the mesh instead of falling through to the flatentityRequiresRebuildfield list. Wire it intofactory.js::updateEntityMesh.
New control (*View)¶
Controldataclass in_controls.pywith_value_type+_fields()(scalar controls) orset_value/get_valueoverrides (Table). No central switch —serialize()merges_fields().*View(ControlView)inviews.pywhose__init__buildsself.control = <Control>(...)(keep the constructor signature; the views expose explicit typed forwarders —@propertyreads plusset_value/undo/redomethods;ControlView.set_valuesets and pushescontrol_update).Control.handle_event(event, payload) -> Dispatchfor any new event or model mutation (seeTable.handle_event). Theon_*fields are the handler declaration —Control.register_handlersmapson_change->"change", etc.- Frontend factory +
sendEvent(id, event, {value}); add a server_EVENT_MSG_MAPentry for a new message name. - Tests: serialization round-trip, handler registration, dispatch.
New overlay object (Dialog/Banner pattern)¶
Follow the OverlayContainer pattern exactly:
- State in
OverlayContainer.__init__:self._things: dict[str|None, dict[str, Any]] = {}+ a counter. - Add
_next_*_id,_register_*,show_*/remove_*/clear_*(+_asyncvariants usingawait self._transport.send_async),_push_*serializers, and_on_close/_on_accepthandlers. - Register handlers via
self._transport.register(id, handler, event=...); useself._layout(the owningLayoutHost) to register dialog content and resolve control ids. - Add the inbound route in
Visualizer._register_routes-> anOverlayContainer._on_*method; addVisualizer.show_*/remove_*forwarders. - Test the wire round-trip + handler lifecycle.
Invariants + AI-agent orientation¶
- Every
idis globally unique;(id, event)is the single handler key. - Every
Viewhas a stableid(autov0…, overridable viaid=); its_serialize()emitsself.id— there is no per-serialization id generator. viz.remove_view(id)removes a mounted overlay view (global overlay = a granularoverlay_remove; per-scene overlay = a full re-sync).- Controls live in overlays/layouts, never in
Scene. - No
add_*facades and no runtime value API; value/history/serialization/ handler registration live on the control (Control.set_value,Control.serialize,Control.register_handlers,Table.undo/redo). - Inbound dispatch walks the layout tree (
LayoutHost.resolve_control); there is no_control_viewsindex and noControlHost. Visualizer.addis polymorphic: aView-> the default layout overlay, everything else -> the main scene.
Layout reconciliation & runtime updates (frontend)¶
The frontend has one live-view registry and reuses views by their stable
id — do not build a second, parallel structure.
- One
_viewRegistry.templates/viewer.jskeeps a single_viewRegistry: Map<view_id, View>of every live view._sceneRoutes(scene → panes) and_viewById(view_id →ThreeJsView, the target of theview_camera/view_viewport/view_background_imagedispatch) are derived routing indexes recomputed from the built tree, not separate registries._controlRegistry(control value/apply, owner-scoped) is a different concern and stays separate — it is not an orphan map. If you need to look a view up by id at runtime, derive it from the tree/_viewRegistry; never add another id-indexed dict. - Reconciliation by id.
buildViewTree(templates/views/build.js) reuses a live view whennode.idmatches an existing view of the same type, refreshing it viaupdate(node)(controls) /updateFromNode(node)(scene panes); a new id constructs a new view, and an id no longer present is torn down. Containers are rebuilt (cheap flexbox DOM) and re-attach the reused children in order —appendChildmoves the DOM node, so a reordered layout re-parents the expensive WebGL panes instead of recreating them. The statefultable/file_chooser/logleaves are deliberately recreated (theirdestroy()unregisters file-browser/log state). - Identity =
View.id. The serializednode.idis the stable key. Reusing the same PythonViewobject acrossset_layoutkeeps the frontend view alive; constructing a newViewgets a new id, and the old frontend view is replaced. There is no per-serialization id generator, so never re-key on scene name or positional index. - Granular updates, not a layout re-push. For a single-value change, send a
granular message —
view_camera,view_viewport,view_background_image(per-pane, dispatched through_viewById),control_update/log_update(controls/logs), orscene_update(entities) — and re-pushview_layoutonly for structural layout changes. A new per-pane runtime knob must follow this pattern (aview_*message dispatched through_viewById), not a new full-tree channel.
File map¶
| File | Role |
|---|---|
visualizer.py |
Visualizer — facade, composition root, lifecycle |
_layout.py |
Layout, OverlayContainer, LayoutHostImpl |
scene.py |
Scene, SceneObject, SceneConfig, _resolve_scene_entity |
_hosts.py |
ThemeHost, InteractionHost, OverlayHost |
_ids.py |
generate_id() — the single id convention shared by _nodes.py and scene.py |
_nodes.py |
VizNode/VizSceneObject/VizOverlayObject/VizGroup/VizImage scene-graph nodes |
_controls.py |
Control + subclasses (value/serialize/handle_event/register_handlers), registry |
views.py |
View + SceneView/StackView/GroupView/MenuView + *View |
_ports.py |
Transport/LayoutHost protocols + ServerState |
_transport.py |
WebSocketTransport |
_scene_handle.py |
VizSceneHandle (per-scene proxy) |
image.py |
ImageData/ImageDType/ImageChannelMode value model + pil_to_numpy + read_exr/read_hdr re-exports; ImageData auto-tiles large arrays (tile_max_dim/tile_max_bytes) |
_image_io.py |
dependency-free HDR readers — read_hdr (Radiance RGBE) and read_exr (EXR NONE/RLE/ZIPS/ZIP/PIZ → float32) + register_loading_progress_handler / per-call on_progress; PIZ decode dispatches to the compiled binding_piz extension (via codegen._piz_cache) with a numpy fallback |
_image_view.py |
ImageView (plane + textures + shader/uniform state) + ImageCanvas (dedicated 2D scene) |
_image_wire.py |
binary image-frame codec (server → client), v2 with a codec byte |
surface/ |
SurfaceShader/SurfaceTexture/SurfaceMaterial + standard surface shaders |
_image_pyramid.py |
ImagePyramid — on-demand tile pyramid (level/tile geometry + encode + LRU) |
_camera_stream.py |
CameraStream — MJPEG publisher (JPEG encode + latest-frame fan-out) |
_scale.py |
Scale/nice_linear_ticks/log_ticks — data↔world + tick math |
_coordinate_system.py |
CoordinateSystem plotting helper (axes/grid/overlay/underlay specs) |
templates/nice-ticks.js |
pure tick math port of _scale.py (Node-testable) |
templates/axes-overlay-math.js |
pure world↔data + tick/px layout math (Node-testable) |
templates/axes-overlay.js |
SVG axes-frame renderer (overlay layer) |
templates/grid-underlay.js |
SVG grid renderer (underlay layer) |
templates/renderers/surface-shader.js |
surface GLSL assembly (varyings + lighting preamble + surfaceColor) |
templates/renderers/surface-material.js |
ShaderMaterial factory, texture binding, in-place uniform updates |
templates/renderers/shader-error.js |
formatShaderError + installShaderErrorHook |
Image canvas (extension recipe)¶
ImageCanvas (in _image_view.py) is a user-facing helper analogous to
CoordinateSystem: it owns a dedicated 2D scene with a y-down pixel
frame (1 world unit = 1 pixel), an ImageView (the plane + textures +
shader/uniform state), an InteractionSurface (a per-pane z = 0 interaction
plane), and an overlay VizGroup. The image is a new scene-object kind
(kind == "image", VizImage node in _nodes.py), whose pixels reach the
browser through one of three mutually exclusive ImageData sources:
source: "data"(default) — the pixel buffer travels as binary WebSocket frames (_image_wire.py,Transport.send_bytes/server.push_bytes) with a versioned codec byte (v2):raw/jpeg/zlib— auto-selecting JPEG for 8-bit 1/3-channel and lossless zlib otherwise, overridable viaImageData(codec=…).source: "url"— the frontend loads the URL at runtime: an arbitrary URL viaTHREE.TextureLoader, or a camera feed (/stream/{id}) served as MJPEG (_camera_stream.py, drawn into a texture bymakeStreamTexture).source: "tiled"— an HTTP tile pyramid (_image_pyramid.py,/image/{id}/{tile_version}/{level}/{x}/{y},format=jpeg|png|raw|zlib):uint8tiles stream as JPEG/PNG (drawn to a canvas) whilefloat32/uint16tiles stream as lossless zlib-compressed raw, assembled client-side into a float texture sou_value_min/u_value_maxand brightness/contrast use the full dynamic range.tile_versionis a per-image_idcache key bumped on each re-registration, so a same-id runtime swap fetches fresh URLs instead of stale browser-cached tiles (the route also servesCache-Control: no-store).ImageDataauto-tiles arrays overtile_max_dim/tile_max_bytes(defaults 4096 px / 32 MB) andImageCanvas._sync_imageauto-registers the pyramid with theVisualizer(via_register_pyramid).
Uniforms and overlays travel as JSON (image_update) and never re-send the
pixels. The export path stores images in an id-keyed asset store
(AnimationRecording.assets, capture_frame(include_images=False)), embedding
8-bit images as JPEG data URLs by default. At export, the bootstrap hydrates
those pixel frames through the window.__tanga frame-store API
(storeImageFrame / takeImageFrame / hasImageFrame /
registerImageFrameConsumer, from templates/image-frames.js); compressed
(compress=True) animation data is gzip-decompressed into a
window.__tangaAnimReady promise that the bootstrap top-level-awaits before
reading window.__TANGA_ANIMATION__.
AnimationRecording records each frame as a full, id-keyed entity snapshot
(capture_frame → Scene.full_state). For animated export,
AnimationRecording.to_diff_dict() reduces those full snapshots to initial
(frame 0, full) plus a per-frame deltas list of only the entities that changed
or were removed (whole-entity diffs). The animated bootstrap expands them back
to full frames[] at load via _expandFrames
(py/pytanga/viz/export/_bootstrap/_animation.py) before the id-based
_reconcileFrame plays them, so unchanged entities are stored once and playback/
scrubbing are unchanged. to_dict() still emits the full frames list for
legacy consumers.
ImageView supports up to max_layers image layers (default 8 — the WebGL 1
fragment-texture-unit floor; serialized as max_layers so the frontend can later
adopt the runtime MAX_TEXTURE_IMAGE_UNITS). Each layer is an
ImageData (any source) plus per-layer compositing uniforms — u_layer{i}_enabled,
u_layer{i}_opacity, u_layer{i}_blend (LayerBlendMode: normal / add /
multiply / screen), u_layer{i}_mode, u_layer{i}_colormap
(ImageColormap: gray / viridis / turbo for scalar layers), and
u_layer{i}_value_min/u_layer{i}_value_max (per-layer normalization, so a
uint16 depth map or uint8 segmentation normalizes independently of the primary
image). Layers are composited bottom-up by the default shader and sampled by
the primary frame's normalized UV (the plane is sized to the primary image's
pixel extent). Per-layer state lives only in the uniform dict and is projected
into the serialized images[] entries by ImageView.serialized_images() /
ImageData.layer_meta() — one canonical per-image dict whose base is identical
for data/url/tiled (tiled adds tile_size/levels/tile_version;
version is always the ImageData uuid). The per-layer state is bundled in
ImageLayerStyle (enabled/opacity/blend/channel/colormap/value_min/value_max,
all None-defaulted) and applied via ImageView.set_image/add_layer (returns
the new layer index)/set_layer (full apply, None → per-image default; image=None
clears in place) and set_layer_style (partial merge, None → keep current).
remove_layer(i) clears a non-primary layer in place (stable index — the slot
serializes as null, no re-numbering). ImageCanvas mirrors
these and drives the granular image_update message. LayerBlendMode is a
StrEnum ("normal"/"add"/"multiply"/"screen") whose .code int feeds the
wire uniform.
The shader/uniform state and the default compositing pipeline are shared with
the background image (see "Image background" below): ImageView holds images +
uniforms + fragment/vertex, ImageView._serialize_state() is the shared payload
(the scene entity _serialize() adds only the id/layer/kind/frame
envelope), and image-shader.js::buildImageLayerPipeline() (per-layer
tangaSampleLayer + tangaBlend + tangaComposite) plus
image.js::buildColorUniforms/buildLayerUniforms/applyImageUniforms are used
by both renderers/image.js (scene mesh) and renderers/image-background.js
(NDC quad).
The same 2D scene hosts interactive rectangles: Rectangle2D (a new viz-only
entity, kind == "Rectangle2D", rendered by renderers/rectangle2d.js as an
outline + optional fill) and ActRectangle2D (a composite ActSceneObject that
spawns square ActPoint handles for resize/translate). Drag-to-create is shown
in the rectangle_labeling.py example by composing a disabled left-drag binding
plus a mode flag (no bespoke draw_rectangle() helper).
Depth image (extension recipe)¶
DepthImage (depth_image.py) is a viz-only scene entity (no GA conversion)
that renders a per-pixel depth map as a 2.5D heightfield surface in the normal
3D viewer. DepthImageData bundles three numpy arrays — image (optional),
depth (required), segmentation (optional) — that may have different pixel
resolutions (only the extent matches); DepthImage adds the presentation fields
(custom shader/uniforms + initial selected_segments) and DepthImageStyle
(_styles/_entity_styles.py) holds the appearance knobs (render_mode,
colormap, mesh_scale, image_scale/segmentation_scale, codec, depth
mapping, …). A pinhole mode (DepthImageStyle.camera = a
CameraCalibration or PinholeCamera, depth_kind="axial"|"radial"|"inverse",
apply_pose=True) renders camera-frame vertices
((u−cx)/fx·z, (v−cy)/fy·z, z) instead of the orthographic heightfield; the mesh
payload carries mesh.camera (fx/fy/cx/cy/depth_kind) and, when a
calibration pose is applied, mesh.camera_to_world (16 floats).
It follows the image entity pattern end to end, with two differences:
kind == "depth_image"(VizDepthImagein_nodes.py) — the content payload (frame/depth/image/segmentation/shader/mesh/uniforms) is produced byDepthImage._build(style, entity_id), which downscales the arrays Python-side (depth → mesh resolution, image →image_scale, segmentation →segmentation_scale; nearest for depth/seg, area/bilinear for image) and wraps them as id-keyedImageDatabinary frames (<eid>.depth/<eid>.image/<eid>.seg) — so only the needed resolution is transmitted.DepthImageStyle.codecreusesEImageCodec(None= auto per frame; JPEG only for a uint8 color image, depth/segmentation always lossless).Scene.add_depth_imagepushes the frames live (bytes first);Scene.depth_image_frames()re-sends them on connect. UnlikeVizImage,VizDepthImage.serialize()adds the standard node envelope (parent_id/transform/visible), so it supports parenting/transforms.- Vertex shader + segmentation filter —
renderers/depth-shader.js+renderers/depth-image.jsbuild a decimatedPlaneGeometrywhose vertex shader samples the nearest-filtereduDepthand displacesposition.z(settingvUv/vDepth/vValid); the fragment shader colors byu_render_mode(colormap /uImage0/ plainu_color), discards missing depth, and discards non-selected segmentation ids viatangaSegmentSelected(vUv)(no color and no depth write).
Live uniform/segment changes use a granular depth_image_update message
(Scene.set_depth_image_uniform / set_depth_image_selected_segments, surfaced
as VizObjectRef.set_uniform / set_selected_segments) — no pixel re-send, no
mesh rebuild. The new renderer modules are added to
_bootstrap/_html.py::_RENDERER_FILES so the HTML-export bundle includes them.
Scene lighting¶
A single per-scene lighting configuration (ambient + directional lights)
drives all three render paths — the standard MeshPhongMaterial entities, the
ray-marched SDF renderer, and the depth surface's custom shader — so they share
one light setup.
- Python model —
_lighting.pydefinesLightSpec(color+intensity),DirectionalLightSpec(adirectiontoward the light +LightSpec), andSceneLighting(ambient + a tuple of directional lights).SceneConfig.lighting: SceneLighting | None(None= frontend default) serializes into thescene_config.lightingwire block ({"ambient": {color, intensity}, "lights": [{direction: [x,y,z], color, intensity}]});Visualizer.set_scene_lighting(...)sets it and re-pushesscene_config. Lighting is in world space; a directionaldirectionis a unit vector toward the light (the frontend normalizes non-unit input). - Shared shader module —
renderers/shader-lighting.jsis the single source of truth for the light preamble (lightPreamble), wire parsing (parseLighting), and uniform uploads (setLightUniforms,DEFAULT_LIGHTING);sdf.js/sdf/glsl.jsanddepth-shader.jsimport it. - Standard renderer —
three-view.js::_applyLightingreplaces the hardcoded lights withAmbientLight+DirectionalLights built from the config, and_applyShaderLightingre-lights existing SDF/depth materials on ascene_configre-push.
Surface material (extension recipe)¶
A solid surface entity (Plane, Sphere, Ellipsoid, Disk, Box,
Cylinder, Cone) can be textured by an image, a custom GLSL program, or both.
The contract lives in py/pytanga/viz/surface/ (Python) and
renderers/surface-shader.js + surface-material.js (frontend).
- Python model —
SurfaceShader(a GLSL fragment definingvec3 surfaceColor(vec3 baseColor, vec3 n, vec3 p)+ optional uniforms),SurfaceTexture(anImageData+ a sampleruniformname + UV wrap/repeat/offset), andSurfaceMaterial(shader=…, textures=[…]). The seven solid styles carry asurface: SurfaceMaterial | Nonefield;Scene.add_viz(..., surface=…)(reached viaVisualizer.add/new/__call__andVizSceneHandle.add/new) normalizes a shader/texture/list into aSurfaceMaterial. - Semantics — no
surface→ the standardMeshPhongMaterial; textures only → the first texture is sampled by the standard texture shader; a shader replaces the standard shader; a shader + textures binds each texture to its declareduniformsampler. Solid-surface entities default to opacity1.0; the surface material honors the entity's opacity — the standard texture shader multipliesu_opacityby the texture alpha channel, and a custom shader receives the entity opacity asu_opacity. - Image delivery — texture pixels are
ImageDatabinary frames (encode_image_frame; codec auto-selects JPEG foruint81/3-channel).Scene.surface_texture_frames()feedsVisualizer._image_frames_for, and_push_surface_texture_framespushes them live on add. - Frontend —
surface-shader.jsassembles the GLSL (varyingsvUv/vNormal/vWorldPosition/vLocalPos, theshader-lighting.jslight preamble, atangaLight(albedo, n, p)helper, and the usersurfaceColor).surface-material.jsbuilds theShaderMaterialand binds textures viamakeEncodedTexture+image-frames.js. The seven solid renderers branch onent.style?.surface. - Lighting — the standard texture shader and custom surface shaders consume
the shared
shader-lighting.jsuniforms (uLightCount/uLightDir[]/uLightColor[]/uAmbientColor), re-lit bythree-view.js::_applyShaderLighting. - Granular uniform updates —
Scene.set_surface_uniform(id, name, value)/_send_surface_uniformspushes asurface_updatemessage (and stores the value back into the node'sstyle.surface.shader.uniforms);surface-material.js::applySurfaceMaterialUniformsapplies it in place (no mesh rebuild). Exposed asVisualizer.set_surface_uniform(entity_id, …)andVizObjectRef.set_surface_uniform(name, value). - Errors —
three-view.jsinstallsrenderer.debug.onShaderError(fromrenderers/shader-error.js) and forwards a formatted driver log as asource: "shader"client-log record;Visualizer.on_shader_error(handler)filters it for apps (e.g. to show a banner).
Overlay coordinate system (extension recipe)¶
CoordinateSystem(display_mode="overlay") (2D only, no explicit size) draws the
axes as a fixed screen-space overlay frame and the grid as a scene-level
underlay behind the data. The backend emits two payload-style scene objects —
layer="overlay", kind="axes_overlay" and layer="underlay", kind="grid_underlay"
(a generalized VizOverlayObject carrying a static spec of scales, formats,
labels, and styles) — instead of world-space Axis/Grid entities. The data
group, transform, and fit_view2d camera ownership are unchanged.
The frontend layers each pane as underlay container (z:0) → WebGL canvas (z:1,
transparent) → CSS2D labels (z:2) → overlay DOM (z:3). axes-overlay.js (SVG
frame) and grid-underlay.js (SVG grid) recompute their ticks every frame from
the live ortho camera via the pure nice-ticks.js / axes-overlay-math.js
modules (ports of _scale.py). Zoom/pan happens entirely in the browser
(OrbitControls), so the overlay reads the camera rather than round-tripping.
Export bundles those modules into js/tanga-viewer.js (CDN + inline/offline)
and the export adapters run the same renderers in their render loops; a
grid_underlay present makes the scene background transparent so the grid shows
through.
Calibrated camera view (extension recipe)¶
A real camera's internal (K) + external (R, t) calibration drives a scene
pane through a clean three-layer model — camera data → camera view → pane:
PinholeCamera(camera data,type: "pinhole") — a sibling ofCameraConfig2d/CameraConfig3dcarrying intrinsics (fx/fy/cx/cy/width/ height), pose, clipping, and afitpolicy ("fit"letterbox /"fill"stretch).pinhole_camera(K, R, t, image_size=…)builds it.- Coordinate frames + calibration types (
pytanga.geometry/pytanga.viz.camera) — a plain numericMatrix(with a runtime-checkableMatrixProviderprotocol),CoordinateFrame/OpenCVFrameaxis conventions (OpenCV x-right/y-down/z-forward is a 180° rotation about +x, preserving the right-handed internal world), andCameraCalibration(K, R, t, image_size, frame=OpenCVFrame(), units=…)which converts read-in calibration to the standard frame + units and builds aPinholeCamera. Frames satisfyMatrixProvider, soset_transform(frame)remaps OpenCV-frame data through the scene graph. pinhole-framing.js— a single pure module that maps the intrinsics + pane aspect +fitto both the off-center frustum bounds and the background quad's letterbox half-extents ({hx, hy}, zoom-scaled) plus the base fit half-extents (fitHx/fitHy). One source of truth for aspect, used byapplyPinhole(camera, aspect)inview_mode.json switch and resize.CameraView(presentation) — bundles the camera, alockset (validated againstCameraLock), anavigationmode ("orbit"default /"2d"dolly zoom + screen-space pan, no orbit), a per-panecontrolsbutton mapping, an optionalviewport(ViewportConfig:zoom/pan+ limits), and an optionalbackground_image; serialized in thescene_viewnode as onecamera_viewfield.- Viewport navigation — a pane's 2D viewport (
{zoom, pan}) is a presentation-layer transform folded into the pinhole crop window bypinhole-framing.js(returning both the sub-frustum and the background crop{u0,v0,u1,v1}), so projection and image never diverge. The crop window is pane-shaped: aszoomgrows its{hx, hy}letterbox grows toward1, so the image fills the pane (the letterbox bars shrink) and then crops at the edges — the standard image-viewer zoom. Set at runtime viaVisualizer.set_viewport(view, …)(per-paneview_viewportmessage, mirroringview_camera) orset_viewport(scene_name=…)/VizSceneHandle.set_viewport(scene-wideSceneConfig.viewportdefault). - Image background —
CameraView.background_imagemounts a full-viewport NDC quad (renderers/image-background.js) that letterboxes to match the projection (and crops to the viewport window). Pixel bytes travel on the existing binary frame transport (_image_wire.py→LayoutHost.background_image_frames→ re-sent on connect), compressed with the same v2 codec; asource: "tiled"background fetches/image/…tiles and a/stream/…url streams MJPEG. At runtimeVisualizer.set_background_image(view, image)sends the new pixel frame followed by a granularview_background_imagemessage — noview_layoutre-push, so other panes are untouched. On reconnect the quad is rebuilt from the re-sent frame (the pane'sclear_alltears it down); an unchanged background keeps its previous texture on a layout re-push, andset_layoutre-sends a frame only when itsImageData.version(a per-instance uuid) changed — one binary frame per layer (url/tiled layers send none).background_imageis anImageView(a bareImageDatais auto-wrapped), so it carries the same multi-layer state as the canvas: the metadata emits a flat primary image (for framing/uImageAspect) plus a fullimages[]layer list, and the default background shader composites the layers (u_layer{i}_*) inside the letterbox/crop quad —Visualizer.register_background_shader/register_background_uniform/set_background_uniformdrive it via a granularview_background_uniformsmessage (no pixel re-send, no layout re-push). Once a layout is registered, the backgroundImageViewis bound to its pane transport (LayoutHost.set_layout→_bind_background_images), so its mutators (set_image/add_layer/set_layer/set_layer_style/set_uniform/remove_layer) push the granularview_background_uniforms/view_background_imageupdates themselves — per-layer pixel changes re-send only the changed layer's frame. - Per-pane visibility —
SceneView.set_visible(entity_id, visible)toggles an entity in one pane at runtime via a granularview_visibilitymessage (no layout re-push, no rebuild);Scene.new/add(andVizSceneHandle.new/Visualizer.add) acceptvisible_in={view_ids}to give an entity a static per-view scope so it is never constructed in panes outside that scope (default = all views). - Runtime entity visibility —
Scene.set_visible/Visualizer.set_visible(…, scene_name=…)/VizSceneHandle.set_visibleset a node'svisibleflag and emit a granularvisibleaspect patch on theobject_updatechannel (frontend setsTHREE.Object3D.visible), so toggling visibility never re-serializes geometry/style. Action objects addActSceneObject.set_enabled/enable/disable, which flipsInteractionConfig.enabledand re-pushes the existinginteractionaspect. - Frustum —
pytanga.geometry.Frustum(a viz-only entity, no MV) withFrustum.from_camera(camera)andFrustumStyle; serialized to explicit corners and rendered byrenderers/frustum.js. CoordinateMapper— a pixel↔world mapping protocol (to_world(u, v) → Point,to_pixel(point) → (u, v)) inpytanga.viz.camera, with aPlanarMapper(identity:Point(u, v, 0)) and aCalibratedPlaneMapper(pinhole ray ∩ a plane ⟂ the optical axis at a fixeddepth). It maps image pixels only — screen-pixel sizing lives on the frontend inscreenWorldScale(camera-fit.js), whose perspective branch uses the point's depth along the camera's view axis (not Euclidean distance).world_units_per_pixel()returns the world size of one image pixel at the plane (1.0forPlanarMapper,depth / fxforCalibratedPlaneMapper), used for scale-dependent distances (min radius/size, auto-close tolerance, rotate-handle offset).pixel_axes()returns the unit world directions of image pixel+u/+von the annotation plane, soLabelMeStorecan build/export oriented shapes (rectangle/ellipse) in the mapper's plane basis.LabelMeStore(mapper=…)threads it through every pixel↔world call site so the labelme loader/saver is coordinate-system-agnostic — the defaultPlanarMapperpreserves the historical "pixel == world XY" behavior, whileCalibratedPlaneMapperlets labelme shapes round-trip through a calibrated 3D scene.LabelMeStoreis a pure data store:iter_objects(doc)andadd_shapes(handle, doc)return/emit plain geometry (Point,Line,Circle,Ellipse,Rectangle2D,PointPath), never interactiveActSceneObjectcomposites — the label apps wrap those entities into acts via their own_act_from_entity. The single fixeddepthis a display/editing approximation, not a true reprojection of arbitrary 3D geometry.- Screen-space markers — point markers tagged
userData.isScreenSpace(seerenderers/utils.js::tagScreenSpaceMarker) keep the entity's exact world position and are resized once per frame byThreeJsView._updateScreenSpaceMarkers(screenWorldScale× an optionaluserData._hoverScalemultiplier). The marker leaf ownsscaleand_hoverScale; the wrapperGroupcarries only the entity transform, and interaction hover (interaction.js::_screenSpaceLeaf) only sets/clears_hoverScale— it never mutatesscale. Flat overlay markers (circle/icon) layer above coplanar bodies or a standard point withrenderOrder = 1+polygonOffset(factor/units −1), never a world-spacezlift (which would introduce zoom-dependent parallax under a pinhole camera).
Test commands¶
Common pitfalls¶
- Circular imports —
scene.py<->_scene_handle.py; importVizSceneHandleand view classes lazily inside methods. Scene._hostis a back-reference to theVisualizer, used only forActSceneObjectinit andVizSceneHandlecreation.OverlayContainer.configure(...)wirestransport/layoutpost-construction (the layout host back-reference).set_layoutvsadd_layout— both take(root, name="");add_layoutraises if the name is taken;set_layoutalso registers handlers + injects_pushcallbacks and re-syncs the layout.