This chapter teaches you how to create immersive spatial user interfaces using pmndrs/uikit, specifically uikitml – IWSDK’s spatial UI system.
What You’ll Learn
By the end of this chapter, you’ll be able to:
Understand spatial UI design principles
Create UI layouts using UIKitML markup
Position and scale interfaces in 3D space
Handle user interactions with spatial UI elements
Implement common UI patterns for WebXR
Spatial UI Principles
Great spatial interfaces follow these principles:
World-scale: UI elements have a physical presence in 3D space
Natural interaction: Use pointing, grabbing, and gestures
Readable at distance: Text and icons scale appropriately
Contextual placement: UI appears near relevant objects
Comfortable viewing: Positioned to avoid neck strain
Introduction to Building Spatial User Interfaces in IWSDK
The unavailability of HTML in WebXR has been a big challenge for developers, since manually placing user interface elements is very cumbersome. That’s why IWSDK uses pmndrs/uikit, a GPU-accelerated UI rendering system that provides an API aligned with HTML and CSS, allowing developers to feel right at home. To make UI authoring even more natural, IWSDK uses the uikitml language, which allows developers to write user interfaces using an HTML-like syntax, including features such as CSS classes. This integration allows IWSDK developers to reuse their HTML knowledge to quickly build high-performance, GPU-accelerated user interfaces for WebXR. Furthermore, IWSDK makes use of the pre-built component collections offered by the uikit project: the Default Kit (based on shadcn) and the Horizon Kit (based on the Reality Labs Design System).
Key Features
Declarative markup: Describe UI structure, not implementation
3D layout system: Flexbox-like layouts in 3D space
Component Kits: Pre-built buttons, panels, sliders, etc.
Event system: Handle clicks, hovers, and other interactions
Theming support: Consistent styling across your application
Basic Syntax
UIKitML uses HTML-style markup with CSS properties for styling and layouting:
IWSDK includes a Vite plugin that compiles UIKitML files:
// vite.config.js
import { defineConfig } from 'vite';
import { uikitml } from '@iwsdk/vite-plugin-uikitml';
export default defineConfig({
plugins: [
uikitml({
// File extensions to process
include: ['**/*.uikitml'],
// Hot reload during development
hotReload: true,
}),
],
});
Creating Your First UIKitML File
Create src/ui/main-menu.uikitml and insert the following content, which uses the Panel, Button, ButtonIcon, and LoginIcon components from the Horizon Kit to design a panel with a button:
Adding Component Kits to Your Spatial User Interface
If you’d like to use a different or additional component kit for your uikitml file, you can configure the kits in the spatialUI feature list when creating a World:
import * as horizonKit from "@pmndrs/uikit-horizon";
World.create(document.getElementById("scene-container"), {
...
features: {
...
spatialUI: { kits: [horizonKit] },
},
})
Overview of Properties and Features Available for Building Spatial User Interfaces
When authoring a User Interface with IWSDK, developers can use almost all the features they know and love from HTML.
The following section shows all the available element types and styling methods for designing Spatial User Interfaces.
Element Types
Container Elements
Most HTML elements become containers that can hold children and text.