HTML, SVG and 3D in a component

A component can hold HTML elements, inline SVG and a 3D scene. All three read the same signals, and the component’s <style> reaches all three: a class toggled by a button can change the button’s colour and a mesh’s colour in the same update.

Run the one-scene example. It draws three turning cubes on a ground plane, with a panel of HTML buttons over the scene. Each button toggles a CSS class on one cube. Every 15 seconds a class on the page switches a custom property that colours the ground. Moving the pointer over any of the cubes changes the middle cube’s colour, through a :hover rule on their group.

Scene components

A scene is written in the template with four components: <Scene>, <Group>, <Mesh> and <Camera>. Groups nest. Meshes and groups can be repeated in keyed {for} blocks and added or removed by {if} blocks, as elements can.

<gpu-surface name="view" class="view"></gpu-surface>
<Scene scene={scene} surface={surface} camera={eye}>
  <Camera handle={eye} projection={lens} transform={looking()} />
  <Group class="spin" transform={turning(time)}>
    <Mesh geometry={cube.clone()} class="one" class:lit={lit1.get()} transform={at(-2.0, 0.0, 0.0)} />
  </Group>
</Scene>

The application creates the scene, the geometry and the camera in Rust and passes them in as props. A node’s position, rotation and scale come from its transform prop, which can read signals. When one of those signals changes, vapors rewrites that node’s transform and nothing else.

What CSS reaches

Rules in the component’s <style> match scene nodes by class and by :hover. They set a mesh’s color and a node’s visibility. Custom properties and var() work as they do for elements. A declaration a scene node does not read, such as transform, display or background-color, is refused, and the message names what to use instead.

A mesh can be drawn by an application material instead of a flat colour: a WGSL function the application supplies, which receives the fragment’s world position, its position in the scene’s viewport, the viewport’s size, the mesh’s CSS colour and its shade. The material is fixed when the mesh mounts.

SVG

Inline <svg> draws <path>, <rect> and <circle>. fill, stroke and stroke-width can be set from CSS or as attributes, and currentColor takes the colour of the surrounding text. Elements and events lists what SVG supports and refuses.

SVG cannot yet be placed on a 3D object.

How a frame is drawn

  • Elements and SVG are drawn by the 2D painter in one render pass.
  • The 3D scene is drawn by the scene renderer into its own texture, which a gpu-surface element sizes and places in the page’s layout. The painter draws that texture into the page as it draws an image. HTML placed over the surface is drawn over the scene.
  • Meshes are opaque, depth-tested against each other, and shaded per vertex. There is no lighting model and no transparency.
  • Both passes can draw at 4 samples per pixel (MSAA). The racer on the home page draws its page and its world at 4 samples. The one-scene example draws at 1 sample unless its address ends &samples=4. On an Intel UHD 630 at 1100 × 720, 4 samples added 1.34 ms of GPU time a frame to the one-scene example (median of three runs), and cut the mean colour error on a cube’s edges from 29 to 10 against a 64-sample reference.

The racer’s world (track, ground and car) is drawn through the same scene renderer, with the ground as an application material. Its speed and lap display is a component drawn over the scene.

Checking every frame

vapors-perf --scene-run drives the racer or the one-scene example through its own input over a 60-second scripted run on a fixed clock, and times each frame from the start of its update to the moment the GPU finishes. It fails naming the first frame that takes longer than 16.7 ms, the first frame included. It refuses to run on a busy machine, refuses a frame whose GPU time was not read back, and refuses the driver’s default shader cache, so a cold run starts from an empty cache. Each refusal was shown failing in a deliberately broken build before it was relied on.

Coming soon, in this order

  1. One pass. The scene drawn inside the painter’s pass and sharing its depth buffer, removing the scene’s separate texture and the step that copies it into the page.
  2. SVG on 3D objects. SVG shapes placed on a mesh’s surface and styled by the same CSS.
  3. The judged run. A scene with 3D objects, SVG on one of them and HTML over them, run for 60 seconds by --scene-run with no frame over 16.7 ms.

Performance gives the recorded frame times for the racer, with the hardware and settings they were measured on.