|
1 | | -# arjs-plugin-artoolkit |
2 | | -```markdown |
3 | 1 | # @ar-js-org/arjs-plugin-artoolkit |
4 | 2 |
|
5 | | -Minimal ARToolKit detection plugin scaffold for AR.js core. |
| 3 | +ARToolKit marker detection plugin for AR.js core with WebAssembly support. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- Web Worker-based detection — marker detection runs off the main thread |
| 8 | +- ImageBitmap support — zero-copy frame transfer (browser) |
| 9 | +- Cross-platform — browser Workers and Node.js worker_threads (stub path) |
| 10 | +- ARToolKit integration — square pattern markers |
| 11 | +- Event-driven API — marker found/updated/lost + raw getMarker forwarding |
| 12 | +- Filtering — only forwards PATTERN_MARKER events above a minimum confidence |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +```bash |
| 17 | +npm install @ar-js-org/arjs-plugin-artoolkit |
| 18 | +``` |
| 19 | + |
| 20 | +## Configuration (module + assets) |
| 21 | + |
| 22 | +To ensure the Worker can import ARToolKit in the browser, pass an explicit ESM URL (recommended): |
| 23 | + |
| 24 | +```js |
| 25 | +const plugin = new ArtoolkitPlugin({ |
| 26 | + worker: true, |
| 27 | + artoolkitModuleUrl: '/node_modules/@ar-js-org/artoolkit5-js/dist/ARToolkit.js', // explicit ESM |
| 28 | + cameraParametersUrl: '/path/to/camera_para.dat', // optional override |
| 29 | + wasmBaseUrl: '/node_modules/@ar-js-org/artoolkit5-js/dist/', // optional; if your build needs it |
| 30 | + minConfidence: 0.6 // forward only confident detections |
| 31 | +}); |
| 32 | +``` |
| 33 | + |
| 34 | +CDN fallback (if you don’t serve node_modules): |
| 35 | +- Set `artoolkitModuleUrl` to a CDN ESM endpoint, e.g. a jsDelivr/UNPKG URL for the package’s ESM bundle. |
| 36 | + |
| 37 | +Notes: |
| 38 | +- The previous “loader.js” and manual WASM placement flow is no longer used. |
| 39 | +- If your ARToolKit build fetches auxiliary assets (WASM, data), set `wasmBaseUrl` accordingly. |
6 | 40 |
|
7 | 41 | ## Usage |
8 | 42 |
|
9 | | -Register with the Engine plugin manager: |
| 43 | +### Register and enable |
10 | 44 |
|
11 | 45 | ```js |
12 | 46 | import { ArtoolkitPlugin } from '@ar-js-org/arjs-plugin-artoolkit'; |
13 | 47 |
|
14 | | -engine.pluginManager.register('artoolkit', new ArtoolkitPlugin({ /* options */ })); |
| 48 | +const plugin = new ArtoolkitPlugin({ |
| 49 | + worker: true, |
| 50 | + lostThreshold: 5, // frames before a marker is considered lost |
| 51 | + frameDurationMs: 100, // expected ms per frame (affects lost timing) |
| 52 | + artoolkitModuleUrl: '/node_modules/@ar-js-org/artoolkit5-js/dist/ARToolkit.js', |
| 53 | + cameraParametersUrl: '/data/camera_para.dat', |
| 54 | + minConfidence: 0.6 |
| 55 | +}); |
| 56 | + |
| 57 | +engine.pluginManager.register('artoolkit', plugin); |
15 | 58 | await engine.pluginManager.enable('artoolkit'); |
16 | 59 | ``` |
17 | 60 |
|
18 | | -The plugin emits events on the engine event bus: |
19 | | -- `ar:markerFound` |
20 | | -- `ar:markerUpdated` |
21 | | -- `ar:markerLost` |
| 61 | +### Events |
| 62 | + |
| 63 | +The plugin emits the following events on your engine’s event bus: |
| 64 | + |
| 65 | +```js |
| 66 | +// Marker first detected |
| 67 | +engine.eventBus.on('ar:markerFound', ({ id, poseMatrix, confidence, corners }) => { |
| 68 | + // poseMatrix is Float32Array(16) |
| 69 | +}); |
| 70 | + |
| 71 | +// Marker updated (tracking) |
| 72 | +engine.eventBus.on('ar:markerUpdated', (data) => { |
| 73 | + // same shape as markerFound |
| 74 | +}); |
| 75 | + |
| 76 | +// Marker lost |
| 77 | +engine.eventBus.on('ar:markerLost', ({ id }) => {}); |
| 78 | + |
| 79 | +// Worker lifecycle |
| 80 | +engine.eventBus.on('ar:workerReady', () => {}); |
| 81 | +engine.eventBus.on('ar:workerError', (error) => {}); |
| 82 | + |
| 83 | +// Raw ARToolKit getMarker (filtered: PATTERN_MARKER only, above minConfidence) |
| 84 | +engine.eventBus.on('ar:getMarker', (payload) => { |
| 85 | + // payload = { type, matrix: number[16], marker: { idPatt, cfPatt, idMatrix?, cfMatrix?, vertex? } } |
| 86 | +}); |
| 87 | +``` |
| 88 | + |
| 89 | +### Sending frames |
| 90 | + |
| 91 | +```js |
| 92 | +// Create ImageBitmap from a <video> or <canvas> |
| 93 | +const imageBitmap = await createImageBitmap(video); |
| 94 | + |
| 95 | +// Emit an engine update; the plugin transfers the ImageBitmap to the worker |
| 96 | +engine.eventBus.emit('engine:update', { |
| 97 | + id: frameId, |
| 98 | + timestamp: Date.now(), |
| 99 | + imageBitmap, |
| 100 | + width: imageBitmap.width, |
| 101 | + height: imageBitmap.height |
| 102 | +}); |
| 103 | + |
| 104 | +// The ImageBitmap is transferred and cannot be reused; the worker will close it. |
| 105 | +``` |
| 106 | + |
| 107 | +### Loading a pattern marker |
| 108 | + |
| 109 | +```js |
| 110 | +const { markerId, size } = await plugin.loadMarker('/examples/simple-marker/data/patt.hiro', 1); |
| 111 | +``` |
| 112 | + |
| 113 | +## Examples |
| 114 | + |
| 115 | +A complete webcam-based example is available under `examples/simple-marker/`. |
| 116 | + |
| 117 | +Serve from the repository root so that ES modules and node_modules paths resolve: |
| 118 | + |
| 119 | +```bash |
| 120 | +# From repository root |
| 121 | +npx http-server -p 8080 |
| 122 | +# or |
| 123 | +python3 -m http.server 8080 |
| 124 | +``` |
| 125 | + |
| 126 | +Open: |
| 127 | +- http://localhost:8080/examples/simple-marker/index.html |
| 128 | + |
| 129 | +The example demonstrates: |
| 130 | +- Webcam capture with getUserMedia |
| 131 | +- ImageBitmap creation and frame submission |
| 132 | +- Event handling and console output |
| 133 | +- Raw `ar:getMarker` payloads for debugging |
| 134 | + |
| 135 | +## API Reference |
| 136 | + |
| 137 | +### ArtoolkitPlugin options |
| 138 | + |
| 139 | +```ts |
| 140 | +{ |
| 141 | + worker?: boolean; // Enable worker (default: true) |
| 142 | + lostThreshold?: number; // Frames before 'lost' (default: 5) |
| 143 | + frameDurationMs?: number; // ms per frame (default: 200) |
| 144 | + sweepIntervalMs?: number; // Lost-sweep interval (default: 100) |
| 145 | + artoolkitModuleUrl?: string; // ESM URL for ARToolKit (recommended) |
| 146 | + cameraParametersUrl?: string;// Camera params file URL |
| 147 | + wasmBaseUrl?: string; // Base URL for ARToolKit assets (if required) |
| 148 | + minConfidence?: number; // Minimum confidence to forward getMarker (default: 0.6) |
| 149 | +} |
| 150 | +``` |
| 151 | + |
| 152 | +### Methods |
| 153 | + |
| 154 | +- `async init(core)` — initialize with engine core |
| 155 | +- `async enable()` — start worker and subscribe to frames |
| 156 | +- `async disable()` — stop worker and timers |
| 157 | +- `dispose()` — alias for disable |
| 158 | +- `getMarkerState(markerId)` — current tracked state |
| 159 | +- `async loadMarker(patternUrl: string, size = 1)` — load and track a pattern |
| 160 | + |
| 161 | +## Troubleshooting |
22 | 162 |
|
23 | | -This repo contains a skeleton. Detection and worker/WASM integration will be implemented in follow-up work. |
24 | | -``` |
| 163 | +- “Failed to resolve module specifier” in the Worker: |
| 164 | + - Provide `artoolkitModuleUrl` or serve `/node_modules` from your dev server |
| 165 | +- Worker not starting: |
| 166 | + - Serve via HTTP/HTTPS; ensure ES modules and Workers are supported |
| 167 | +- No detections: |
| 168 | + - Confirm camera started, correct marker pattern, sufficient lighting |
| 169 | + - Adjust `minConfidence` to reduce/raise filtering |
0 commit comments