> For the complete documentation index, see [llms.txt](https://docs.multiset.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.multiset.ai/webxr-sdk/api-reference/needleadapter.md).

# NeedleAdapter

```typescript
import { NeedleAdapter } from '@multisetai/vps/needle';
```

A Needle Engine `Behaviour` that owns `XRSessionManager` and wires it to Needle's render loop. It replaces Needle's built-in WebXR session on `awake()`. Use `NeedleAdapter` programmatically for custom logic; use `MultisetVPS` when you want Inspector-driven setup.

### Constructor

```typescript
new NeedleAdapter(options: INeedleAdapterOptions)
```

### Options

| Parameter               | Type                                                                    | Default  | Description                                                                                                           |
| ----------------------- | ----------------------------------------------------------------------- | -------- | --------------------------------------------------------------------------------------------------------------------- |
| `client`                | `MultisetClient`                                                        | Required | Authorized HTTP client                                                                                                |
| `sessionOptions`        | `Omit<IXRSessionOptions, 'client'>`                                     | —        | All `XRSessionManager` options except `client`. See [XRSessionManager](/webxr-sdk/api-reference/xrsessionmanager.md). |
| `showMesh`              | `boolean`                                                               | `false`  | Download and display the map mesh after localization                                                                  |
| `showGizmo`             | `boolean`                                                               | `false`  | Display a transform gizmo at the map origin                                                                           |
| `showObjectMeshes`      | `boolean`                                                               | `false`  | Download and display 3D outline meshes for detected objects                                                           |
| `useDefaultButton`      | `boolean`                                                               | `true`   | Mount the built-in AR button in `awake()`                                                                             |
| `buttonContainer`       | `HTMLElement`                                                           | —        | Container element for the AR button                                                                                   |
| `onButtonCreated`       | `(button: HTMLButtonElement) => void`                                   | —        | Called when the button is created                                                                                     |
| `onXRFrame`             | `(event: IXRFrameEvent) => void`                                        | —        | Called every XR frame                                                                                                 |
| `onLocalizationSuccess` | `(result: ILocalizeAndMapDetails, worldFromMap: THREE.Matrix4) => void` | —        | Called after successful localization                                                                                  |
| `onObjectMeshLoaded`    | `(objectCode: string) => void`                                          | —        | Called when an object mesh finishes loading                                                                           |

### Static Methods

| Method                        | Returns            | Description                                |
| ----------------------------- | ------------------ | ------------------------------------------ |
| `NeedleAdapter.isSupported()` | `Promise<boolean>` | Check if the browser supports immersive-ar |

### Instance Methods

| Method                               | Returns                                    | Description                                                                                                               |
| ------------------------------------ | ------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------- |
| `startSession()`                     | `Promise<void>`                            | Start the AR session from a user gesture handler                                                                          |
| `stopSession()`                      | `void`                                     | Stop the active session                                                                                                   |
| `localizeFrame()`                    | `Promise<ILocalizeAndMapDetails \| null>`  | Capture and localize one frame                                                                                            |
| `trackObjects()`                     | `Promise<IObjectTrackingResponse \| null>` | Capture and run object detection                                                                                          |
| `isActive()`                         | `boolean`                                  | Whether an AR session is running                                                                                          |
| `clearObjectMeshes()`                | `void`                                     | Remove all loaded object meshes                                                                                           |
| `getSession()`                       | `XRSessionManager`                         | Access the underlying session manager                                                                                     |
| `dispose()`                          | `void`                                     | End session, remove listeners, release resources                                                                          |
| `registerAnchor(anchor: IMapAnchor)` | `void`                                     | Wire a runtime-spawned `MapAnchor` to this adapter. Replays the last localization result immediately if one is available. |

### Properties

| Property       | Type      | Description                                     |
| -------------- | --------- | ----------------------------------------------- |
| `isLocalizing` | `boolean` | Whether localization or tracking is in progress |

### Event Listeners

`NeedleAdapter` supports multiple subscribers per event, unlike the single-callback pattern on `XRSessionManager`.

| Method                           | Description                                                           |
| -------------------------------- | --------------------------------------------------------------------- |
| `addLocalizationListener(fn)`    | Subscribe to `(result, worldFromMap)` on each successful localization |
| `removeLocalizationListener(fn)` | Unsubscribe                                                           |
| `addSessionStartListener(fn)`    | Subscribe to session start                                            |
| `removeSessionStartListener(fn)` | Unsubscribe                                                           |
| `addSessionEndListener(fn)`      | Subscribe to session end                                              |
| `removeSessionEndListener(fn)`   | Unsubscribe                                                           |

Always call the matching `remove*` method in `onDestroy()` to avoid memory leaks.

### Needle Lifecycle

| Method        | Called when                                                                                                   |
| ------------- | ------------------------------------------------------------------------------------------------------------- |
| `awake()`     | Component added to scene. Disables Needle's built-in WebXR, creates `XRSessionManager`, mounts the AR button. |
| `onDestroy()` | Component removed. Disposes session, world, removes all listeners.                                            |


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.multiset.ai/webxr-sdk/api-reference/needleadapter.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
