Skip to content

ImGui as an in-VR panel

Field can float a live Dear ImGui panel inside a 3D scene: a clickable control surface hanging in space in front of you. ImGui draws into a texture, the texture is mapped onto a quad, the quad is rendered once per eye into a side-by-side stereo image, and a controller ray is hit-tested against it so buttons and sliders respond to it. On a headset the same stereo image feeds the eye swapchains; on the desktop it previews in the window.

For a smaller scene with an editable post-processing pass, start with Stereo RTT composite: desktop preview, an FXAA toggle and optional OpenXR output in five boxes.

For desktop OpenXR scenes on Vision Pro over Wi-Fi, use Linux → CloudXR → AVP. The separate native RTT receiver presents a stereo video surface; its eye matrices can come from an uplinked head pose. Those are different transports, with different presentation and input paths.

A single eye of the VR panel: a readable Dear ImGui window titled "Field · VR Panel" with body text, a Click me button, a checkbox, a gain slider, and a ray-hit UV readout

That is one eye of the running demo, cropped for legibility. The panel title, body text, Click me button, show grid checkbox, gain slider, and the live ray hit u=0.500 v=0.500 readout are all drawn by ImGui into a texture and sampled onto the quad.

Try it

Run the interactive desktop preview (move the mouse to aim the ray, left-click to press):

./gradlew runImGuiVrPanelDemo

The ImGuiVrPanelDemo helper wires the pipeline in build(). Run the automated smoke to exercise it on a real Vulkan window and check that a ray aimed at the panel centre lands exactly at its centre:

uv run --project . --group backdoor python tools/run_field_tests.py --id examples.four.imgui-vr-panel-smoke

Edit the panel in a box

Open the ImGui VR panel starting point for a renderer and a child panel box. Its code shows the texture, quad, per-eye modules and controller mapping; its vertex and fragment properties hold the shaders. Native targets are cached across reruns, while the named UI callback and eye modules are replaced, so editing the button or slider does not create another window.

The panel box registers this UI on its imgui texture. Change the label, add a control, or use gain.get() in your own scene:

imgui.addUi("panel") {
    gui {
        ImGui.setNextWindowSize(1960f, 1960f)
        ImGui.setNextWindowPos(44f, 44f)
    }
    window("Field · VR Panel") {
        ImGui.text("Dear ImGui, rendered to a texture,")
        ImGui.text("mapped onto a quad, shown in both eyes.")
        ImGui.separator()
        if (ImGui.button("  Click me  ")) clicks++
        ImGui.sameLine()
        ImGui.text("clicks: $clicks")
        ImGui.checkbox("show grid", showGrid)
        ImGui.sliderFloat("gain", gain.data, 0f, 1f)
        ImGui.separator()
        if (hitActive) {
            ImGui.text("ray hit  u=%.3f  v=%.3f".format(hitU, hitV))
        } else {
            ImGui.text("ray hit  (off panel)")
        }
    }
}

The same box places an ImGuiPanel 1.5 metres in front of the eyes and handles the ray explicitly:

fun updateRayHit() {
    val ctrl = sbs.controllerState()
    if (!ctrl.active || !ctrl.poseValid) {
        hitActive = false
        imgui.injectPointer(imgui.width * 2f, imgui.height * 2f, false)
        return
    }
    val (origin, dir) = ImGuiPanel.controllerRay(ctrl)
    val hit = panel.interact(origin, dir, ctrl.selectPressed || ctrl.triggerPressed)
    if (hit != null && hit.onPanel) {
        hitActive = true
        hitU = hit.u
        hitV = hit.v
    } else {
        hitActive = false
    }
}

updateRayHit() runs alongside the per-eye MVP update. The two eyes have separate draw modules, but use the same panel texture and controller. The transcript keeps this wiring editable rather than hiding it inside the standalone demo.

The pipeline

The whole thing is one chain:

Dear ImGui frame
      │  drawn into
      ▼
render-to-texture (a 2048×2048 panel image)
      │  mapped onto
      ▼
a world-space quad, 1.5 m in front of your eyes
      │  rendered once per eye by the side-by-side stereo target
      ▼
one packed side-by-side image
      ├─ blitted to the desktop window (what you see without a headset)
      └─ copied into the OpenXR eye swapchains (what you see with one)

Because the quad is drawn once per eye, you get correct stereo depth: the panel sits at a distance in the scene rather than flat on the screen. Here is the full packed side-by-side frame, the same pixels a headset would show, one panel per eye:

The full side-by-side stereo frame: the ImGui panel rendered twice, left eye and right eye, against a black background

Each eye looks tall and narrow because the two eye views share the window's width, so each is horizontally squished by half; the headset (or the crop above) stretches it back out.

When using OpenXR, let Renderer.run() drive the frame loop. Field samples the current predicted head and controller poses before rendering the stereo image, then submits that image with the same poses and display time. Keep custom stereo rendering inside this loop so the image and the headset's reprojection agree about where your head was.

Aiming and clicking

A controller ray drives the panel. Each frame Field:

  1. reads the controller pose (a real controller in XR; the mouse on the desktop),
  2. casts a ray from it and intersects it with the panel plane,
  3. converts the hit point into the panel's UV coordinates, and
  4. injects that UV into ImGui as the mouse position, with the trigger as the left button.

So hovering the ray over a button highlights it and pressing the trigger clicks it. When the ray misses the panel, the pointer is parked off-screen so nothing hovers.

If you build your own

Put the panel in the scene with panel.faceToward(position, eye): it turns the panel to face the eye, upright, so the text reads correctly whichever way the camera looks. The demo places its panel this way.

The developer-facing writeup (the ImGuiPanel helper API, the render-graph wiring, and the capture gotchas) lives in dev_docs/imgui_vr_panel.md.

API reference