XR

XR Features

Galacean XR currently supports the following features:

FeatureDescription
Anchor TrackingAnchor tracking
Plane TrackingPlane tracking
Image TrackingImage tracking
Hit TestCollision detection

Anchor Tracking

PropertyDescription
trackingAnchors(Read-only) Get anchors requested for tracking
trackedAnchors(Read-only) Get successfully tracked anchors
MethodDescription
addAnchorAdd a specific anchor
removeAnchorRemove a specific anchor
clearAnchorsRemove all anchors
addChangedListenerAdd listener for anchor changes
removeChangedListenerRemove anchor change listener

Example code to add an anchor in XR space:

const anchorTracking = xrManager.getFeature(XRAnchorTracking);  
const position = new Vector3();  
const rotation = new Quaternion();  
// Add an anchor  
const anchor = anchorTracking.addAnchor(position, rotation);  
// Remove this anchor  
anchorTracking.removeAnchor(anchor);  
// Listen for anchor changes  
anchorTracking.addChangedListener(  
  (added: readonly XRAnchor[], updated: readonly XRAnchor[], removed: readonly XRAnchor[]) => {  
    // Handle added, updated, and removed anchors here  
  }  
);  

Plane Tracking

PropertyDescription
detectionMode(Read-only) Plane detection type (horizontal, vertical, or all)
trackedPlanes(Read-only) Get successfully tracked planes
MethodDescription
addChangedListenerAdd listener for plane changes
removeChangedListenerRemove plane change listener

Note: Plane detection type must be specified when enabling this feature.

// Set plane detection mode to "All" during initialization  
xrManager.addFeature(XRPlaneTracking, XRPlaneMode.EveryThing);  

You can detect real-world planes and mark them with transparent grids and coordinate systems.

Image Tracking

PropertyDescription
trackingImages(Read-only) Array of images requested for tracking (contains name, source, and size)
trackedImages(Read-only) Get successfully tracked images
MethodDescription
addChangedListenerAdd listener for image changes
removeChangedListenerRemove image change listener

Note: Image tracking requires pre-defined reference images. In the engine, these are represented by XRReferenceImage objects:

PropertyDescription
nameName of the reference image (unique identifier)
imageSourceSource of the image (typically an HtmlImageElement)
physicalWidthPhysical size of the image in the real world (default in meters, e.g., 0.08 means 8 cm)

In WebXR, the same image will only be tracked once.

const image = new Image();  
image.onload = () => {  
  // Create reference image  
  const refImage = new XRReferenceImage("test", image, 0.08);  
  // Enable image tracking and specify reference image  
  xrManager.addFeature(XRImageTracking, [refImage]);  
};  
image.src = "Image URL";  

Hit Test

MethodDescription
hitTestPerform collision detection by casting a ray against real-world planes
screenHitTestPerform collision detection using screen space coordinates
const pointer = engine.inputManager.pointers[0];  
// Get plane hit point  
if (pointer) {  
  const hitTest = xrManager.getFeature(XRHitTest);  
  const { position } = pointer;  
  // Perform screen-to-world collision detection  
  const result = hitTest.screenHitTest(position.x, position.y, TrackableType.Plane);  
}  

Was this page helpful?