Skip to content

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.

  • Scene owns entities and the typed entity API. One scene can appear in many panes (SceneView(name)) and many layouts.
  • Layout is a base view tree plus an OverlayContainer. URLs name layouts (/?view=<name>), never scenes.
  • Visualizer wires everything together and exposes the public viz.* 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__ builds Transport -> LayoutHost (+add_scene("")) -> ThemeHost/InteractionHost; OverlayContainer gets a back-reference to its owning LayoutHost, then _register_routes().
  • URL -> layout — / serves the "" layout, /?view=<name> serves layouts[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_entity turns MVs into SceneEntity (and refines a raw Conic to its specific 2D entity so the viewer can serialize it; Quadric3D is left alone and renders via the analytic ray path).
  • Insert a detached subtree — a VizGroup/VizSceneObject tree composed before any scene exists is added via viz.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 via set_layout (or declaratively in SceneView(overlay=[...])); LayoutHost.register walks the tree and calls each Control.register_handlers (registers (id, event) handlers).
  • ParamsView — a composite view (views/params_view.py) that renders a dict or a @dataclass as one child ControlView per key (title in the left column, control on the right). Nested dataclasses render as nested GroupViews (sharing the parent on_change via dotted event.key), and extra_rows=[RowSpec(…)] (top-level or per-GroupUI) adds non-parameter rows; ControlUI.full_width rows render without a key column. The children are ordinary control views, so their handlers register on the same (id, event) registry; the new Control.hide_label flag makes each render label-less. See viz-controls-and-interactions.md.
  • Inbound control event — control:* -> LayoutHost.dispatch_control_event -> resolve_control tree-walk (layouts + dialogs) -> control.handle_event -> Dispatch(event, value, push) -> push + fire the handler.
  • Banner/dialog/editor — OverlayContainer.show_* registers (id, event) handlers and sends banner_*/dialog_*/editor_* wire messages; the *_closed/accept/close routes call OverlayContainer._on_*.
  • Add/remove an overlay view — viz.add(view) mounts a View in the global overlay and pushes a granular overlay_define (only that view, not the whole layout); viz.remove_view(id) removes it via overlay_remove. Per-scene overlays (scene=…) re-sync the layout instead. Every View carries a stable id (auto v0…) so it can be addressed at runtime.
  • Layout re-push — the frontend reconciles the whole view tree by stable view id across view_layout re-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 in destroy() when it still points at that view. A scene pane newly introduced by a re-push fetches its state with a scene_sync_request round-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/ Ellipsoid rotation → quaternion); Point/HPoint at 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/horizontal where a second axis fixes the frame) → quaternion; rotation (Box/Ellipsoid) → quaternion; everything else → shape params. The scene-graph node (VizSceneObject.__init__) derives its transform from this decomposition, and set_entity re-derives it and diffs shape vs placement with a configurable epsilon (default 1e-9): transform on placement change, content on shape change, full on 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 node transform, whose rotation is a quaternion [x, y, z, w] (Transform.to_dict()). serialize_entity() emits the same transform for direct callers. The glTF exporter reads the node transform (position + quaternion) rather than re-deriving it from geometry.
  • Transform location — Transform and the argument-coercion helpers live in geometry/transform.py; the pure matrix/quaternion math in geometry/transforms.py; viz keeps thin re-export shims (viz/_transforms.py, viz/_types.py) and re-exports Transform publicly. Transform.rotation is a quaternion (single source of truth), the matrix() is computed lazily, and Transform overloads @/* to apply to Point (R·S·p + t) and Direction (R·S·d, no translation).
  • Frustum is 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

  1. Add the SceneEntity (or reuse SceneObject for non-geometry drawables).
  2. Route it through Scene.add_viz — it already handles color/opacity/ style/label/parent_id/attach_to; note _resolve_scene_entity for MV analysis (multivector -> SceneEntity).
  3. Serialize it in the node layer (VizSceneObject/SceneObject in scene.py / _nodes.py).
  4. Add a frontend renderer + a test (round-trip + flush).
  5. Export an update<Kind>(mesh, ent, prev) from the renderer module (return false to 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 flat entityRequiresRebuild field list. Wire it into factory.js::updateEntityMesh.

New control (*View)

  1. Control dataclass in _controls.py with _value_type + _fields() (scalar controls) or set_value/get_value overrides (Table). No central switch — serialize() merges _fields().
  2. *View(ControlView) in views.py whose __init__ builds self.control = <Control>(...) (keep the constructor signature; the views expose explicit typed forwarders — @property reads plus set_value/undo/redo methods; ControlView.set_value sets and pushes control_update).
  3. Control.handle_event(event, payload) -> Dispatch for any new event or model mutation (see Table.handle_event). The on_* fields are the handler declaration — Control.register_handlers maps on_change -> "change", etc.
  4. Frontend factory + sendEvent(id, event, {value}); add a server _EVENT_MSG_MAP entry for a new message name.
  5. Tests: serialization round-trip, handler registration, dispatch.

New overlay object (Dialog/Banner pattern)

Follow the OverlayContainer pattern exactly:

  1. State in OverlayContainer.__init__: self._things: dict[str|None, dict[str, Any]] = {} + a counter.
  2. Add _next_*_id, _register_*, show_*/remove_*/clear_* (+ _async variants using await self._transport.send_async), _push_* serializers, and _on_close/_on_accept handlers.
  3. Register handlers via self._transport.register(id, handler, event=...); use self._layout (the owning LayoutHost) to register dialog content and resolve control ids.
  4. Add the inbound route in Visualizer._register_routes -> an OverlayContainer._on_* method; add Visualizer.show_*/remove_* forwarders.
  5. Test the wire round-trip + handler lifecycle.

Invariants + AI-agent orientation

  • Every id is globally unique; (id, event) is the single handler key.
  • Every View has a stable id (auto v0…, overridable via id=); its _serialize() emits self.id — there is no per-serialization id generator.
  • viz.remove_view(id) removes a mounted overlay view (global overlay = a granular overlay_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_views index and no ControlHost.
  • Visualizer.add is polymorphic: a View -> 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.js keeps a single _viewRegistry: Map<view_id, View> of every live view. _sceneRoutes (scene → panes) and _viewById (view_id → ThreeJsView, the target of the view_camera/view_viewport/view_background_image dispatch) 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 when node.id matches an existing view of the same type, refreshing it via update(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 — appendChild moves the DOM node, so a reordered layout re-parents the expensive WebGL panes instead of recreating them. The stateful table/file_chooser/log leaves are deliberately recreated (their destroy() unregisters file-browser/log state).
  • Identity = View.id. The serialized node.id is the stable key. Reusing the same Python View object across set_layout keeps the frontend view alive; constructing a new View gets 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), or scene_update (entities) — and re-push view_layout only for structural layout changes. A new per-pane runtime knob must follow this pattern (a view_* 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 via ImageData(codec=…).
  • source: "url" — the frontend loads the URL at runtime: an arbitrary URL via THREE.TextureLoader, or a camera feed (/stream/{id}) served as MJPEG (_camera_stream.py, drawn into a texture by makeStreamTexture).
  • source: "tiled" — an HTTP tile pyramid (_image_pyramid.py, /image/{id}/{tile_version}/{level}/{x}/{y}, format=jpeg|png|raw|zlib): uint8 tiles stream as JPEG/PNG (drawn to a canvas) while float32/uint16 tiles stream as lossless zlib-compressed raw, assembled client-side into a float texture so u_value_min/u_value_max and brightness/contrast use the full dynamic range. tile_version is a per-image_id cache key bumped on each re-registration, so a same-id runtime swap fetches fresh URLs instead of stale browser-cached tiles (the route also serves Cache-Control: no-store). ImageData auto-tiles arrays over tile_max_dim/tile_max_bytes (defaults 4096 px / 32 MB) and ImageCanvas._sync_image auto-registers the pyramid with the Visualizer (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" (VizDepthImage in _nodes.py) — the content payload (frame / depth / image / segmentation / shader / mesh / uniforms) is produced by DepthImage._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-keyed ImageData binary frames (<eid>.depth / <eid>.image / <eid>.seg) — so only the needed resolution is transmitted. DepthImageStyle.codec reuses EImageCodec (None = auto per frame; JPEG only for a uint8 color image, depth/segmentation always lossless). Scene.add_depth_image pushes the frames live (bytes first); Scene.depth_image_frames() re-sends them on connect. Unlike VizImage, 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.js build a decimated PlaneGeometry whose vertex shader samples the nearest-filtered uDepth and displaces position.z (setting vUv/vDepth/vValid); the fragment shader colors by u_render_mode (colormap / uImage0 / plain u_color), discards missing depth, and discards non-selected segmentation ids via tangaSegmentSelected(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.py defines LightSpec (color + intensity), DirectionalLightSpec (a direction toward the light + LightSpec), and SceneLighting (ambient + a tuple of directional lights). SceneConfig.lighting: SceneLighting | None (None = frontend default) serializes into the scene_config.lighting wire block ({"ambient": {color, intensity}, "lights": [{direction: [x,y,z], color, intensity}]}); Visualizer.set_scene_lighting(...) sets it and re-pushes scene_config. Lighting is in world space; a directional direction is a unit vector toward the light (the frontend normalizes non-unit input).
  • Shared shader module — renderers/shader-lighting.js is the single source of truth for the light preamble (lightPreamble), wire parsing (parseLighting), and uniform uploads (setLightUniforms, DEFAULT_LIGHTING); sdf.js/sdf/glsl.js and depth-shader.js import it.
  • Standard renderer — three-view.js::_applyLighting replaces the hardcoded lights with AmbientLight + DirectionalLights built from the config, and _applyShaderLighting re-lights existing SDF/depth materials on a scene_config re-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 defining vec3 surfaceColor(vec3 baseColor, vec3 n, vec3 p) + optional uniforms), SurfaceTexture (an ImageData + a sampler uniform name + UV wrap/repeat/offset), and SurfaceMaterial(shader=…, textures=[…]). The seven solid styles carry a surface: SurfaceMaterial | None field; Scene.add_viz(..., surface=…) (reached via Visualizer.add/new/__call__ and VizSceneHandle.add/new) normalizes a shader/texture/list into a SurfaceMaterial.
  • Semantics — no surface → the standard MeshPhongMaterial; 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 declared uniform sampler. Solid-surface entities default to opacity 1.0; the surface material honors the entity's opacity — the standard texture shader multiplies u_opacity by the texture alpha channel, and a custom shader receives the entity opacity as u_opacity.
  • Image delivery — texture pixels are ImageData binary frames (encode_image_frame; codec auto-selects JPEG for uint8 1/3-channel). Scene.surface_texture_frames() feeds Visualizer._image_frames_for, and _push_surface_texture_frames pushes them live on add.
  • Frontend — surface-shader.js assembles the GLSL (varyings vUv/ vNormal/vWorldPosition/vLocalPos, the shader-lighting.js light preamble, a tangaLight(albedo, n, p) helper, and the user surfaceColor). surface-material.js builds the ShaderMaterial and binds textures via makeEncodedTexture + image-frames.js. The seven solid renderers branch on ent.style?.surface.
  • Lighting — the standard texture shader and custom surface shaders consume the shared shader-lighting.js uniforms (uLightCount/uLightDir[]/ uLightColor[]/uAmbientColor), re-lit by three-view.js::_applyShaderLighting.
  • Granular uniform updates — Scene.set_surface_uniform(id, name, value) / _send_surface_uniforms pushes a surface_update message (and stores the value back into the node's style.surface.shader.uniforms); surface-material.js::applySurfaceMaterialUniforms applies it in place (no mesh rebuild). Exposed as Visualizer.set_surface_uniform(entity_id, …) and VizObjectRef.set_surface_uniform(name, value).
  • Errors — three-view.js installs renderer.debug.onShaderError (from renderers/shader-error.js) and forwards a formatted driver log as a source: "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 of CameraConfig2d/CameraConfig3d carrying intrinsics (fx/fy/cx/cy/width/ height), pose, clipping, and a fit policy ("fit" letterbox / "fill" stretch). pinhole_camera(K, R, t, image_size=…) builds it.
  • Coordinate frames + calibration types (pytanga.geometry / pytanga.viz.camera) — a plain numeric Matrix (with a runtime-checkable MatrixProvider protocol), CoordinateFrame/OpenCVFrame axis conventions (OpenCV x-right/y-down/z-forward is a 180° rotation about +x, preserving the right-handed internal world), and CameraCalibration(K, R, t, image_size, frame=OpenCVFrame(), units=…) which converts read-in calibration to the standard frame + units and builds a PinholeCamera. Frames satisfy MatrixProvider, so set_transform(frame) remaps OpenCV-frame data through the scene graph.
  • pinhole-framing.js — a single pure module that maps the intrinsics + pane aspect + fit to 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 by applyPinhole(camera, aspect) in view_mode.js on switch and resize.
  • CameraView (presentation) — bundles the camera, a lock set (validated against CameraLock), a navigation mode ("orbit" default / "2d" dolly zoom + screen-space pan, no orbit), a per-pane controls button mapping, an optional viewport (ViewportConfig: zoom/pan + limits), and an optional background_image; serialized in the scene_view node as one camera_view field.
  • Viewport navigation — a pane's 2D viewport ({zoom, pan}) is a presentation-layer transform folded into the pinhole crop window by pinhole-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: as zoom grows its {hx, hy} letterbox grows toward 1, so the image fills the pane (the letterbox bars shrink) and then crops at the edges — the standard image-viewer zoom. Set at runtime via Visualizer.set_viewport(view, …) (per-pane view_viewport message, mirroring view_camera) or set_viewport(scene_name=…) / VizSceneHandle.set_viewport (scene-wide SceneConfig.viewport default).
  • Image background — CameraView.background_image mounts 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; a source: "tiled" background fetches /image/… tiles and a /stream/… url streams MJPEG. At runtime Visualizer.set_background_image(view, image) sends the new pixel frame followed by a granular view_background_image message — no view_layout re-push, so other panes are untouched. On reconnect the quad is rebuilt from the re-sent frame (the pane's clear_all tears it down); an unchanged background keeps its previous texture on a layout re-push, and set_layout re-sends a frame only when its ImageData.version (a per-instance uuid) changed — one binary frame per layer (url/tiled layers send none). background_image is an ImageView (a bare ImageData is 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 full images[] 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_uniform drive it via a granular view_background_uniforms message (no pixel re-send, no layout re-push). Once a layout is registered, the background ImageView is 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 granular view_background_uniforms / view_background_image updates 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 granular view_visibility message (no layout re-push, no rebuild); Scene.new/add (and VizSceneHandle.new / Visualizer.add) accept visible_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_visible set a node's visible flag and emit a granular visible aspect patch on the object_update channel (frontend sets THREE.Object3D.visible), so toggling visibility never re-serializes geometry/style. Action objects add ActSceneObject.set_enabled/enable/disable, which flips InteractionConfig.enabled and re-pushes the existing interaction aspect.
  • Frustum — pytanga.geometry.Frustum (a viz-only entity, no MV) with Frustum.from_camera(camera) and FrustumStyle; serialized to explicit corners and rendered by renderers/frustum.js.
  • CoordinateMapper — a pixel↔world mapping protocol (to_world(u, v) → Point, to_pixel(point) → (u, v)) in pytanga.viz.camera, with a PlanarMapper (identity: Point(u, v, 0)) and a CalibratedPlaneMapper (pinhole ray ∩ a plane ⟂ the optical axis at a fixed depth). It maps image pixels only — screen-pixel sizing lives on the frontend in screenWorldScale (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.0 for PlanarMapper, depth / fx for CalibratedPlaneMapper), 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/+v on the annotation plane, so LabelMeStore can 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 default PlanarMapper preserves the historical "pixel == world XY" behavior, while CalibratedPlaneMapper lets labelme shapes round-trip through a calibrated 3D scene. LabelMeStore is a pure data store: iter_objects(doc) and add_shapes(handle, doc) return/emit plain geometry (Point, Line, Circle, Ellipse, Rectangle2D, PointPath), never interactive ActSceneObject composites — the label apps wrap those entities into acts via their own _act_from_entity. The single fixed depth is a display/editing approximation, not a true reprojection of arbitrary 3D geometry.
  • Screen-space markers — point markers tagged userData.isScreenSpace (see renderers/utils.js::tagScreenSpaceMarker) keep the entity's exact world position and are resized once per frame by ThreeJsView._updateScreenSpaceMarkers (screenWorldScale × an optional userData._hoverScale multiplier). The marker leaf owns scale and _hoverScale; the wrapper Group carries only the entity transform, and interaction hover (interaction.js::_screenSpaceLeaf) only sets/clears _hoverScale — it never mutates scale. Flat overlay markers (circle/icon) layer above coplanar bodies or a standard point with renderOrder = 1 + polygonOffset (factor/units −1), never a world-space z lift (which would introduce zoom-dependent parallax under a pinhole camera).

Test commands

uv run pytest py/tests/viz -q   # fast
uv run pytest -q                # full
uv run mkdocs build --strict    # docs

Common pitfalls

  • Circular imports — scene.py <-> _scene_handle.py; import VizSceneHandle and view classes lazily inside methods.
  • Scene._host is a back-reference to the Visualizer, used only for ActSceneObject init and VizSceneHandle creation.
  • OverlayContainer.configure(...) wires transport/layout post-construction (the layout host back-reference).
  • set_layout vs add_layout — both take (root, name=""); add_layout raises if the name is taken; set_layout also registers handlers + injects _push callbacks and re-syncs the layout.