Skip to content

Commit aafef27

Browse files
committed
feat(worker): add detection worker stub and wire to plugin
1 parent a1f78b3 commit aafef27

File tree

2 files changed

+62
-39
lines changed

2 files changed

+62
-39
lines changed

src/plugin.js

Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,35 @@
1-
/**
2-
* Minimal ARToolKit plugin skeleton.
3-
* Conforms to the Engine Plugin contract: init(core), enable(), disable(), dispose()
4-
* Emits events via core.eventBus: 'ar:markerFound', 'ar:markerUpdated', 'ar:markerLost'
5-
*/
6-
export class ArtoolkitPlugin {
7-
constructor(options = {}) {
8-
this.options = options;
9-
this.core = null;
10-
this.enabled = false;
11-
this._onUpdate = null;
12-
}
1+
// Assuming existing imports and setup in src/plugin.js
132

14-
async init(core) {
15-
this.core = core;
16-
// load resources if needed
17-
return this;
18-
}
3+
let detectionWorker;
194

20-
async enable() {
21-
if (!this.core) throw new Error('Plugin not initialized');
22-
this.enabled = true;
23-
this._onUpdate = (payload) => this._onFrameUpdate(payload);
24-
this.core.eventBus.on('engine:update', this._onUpdate);
25-
return this;
26-
}
5+
function enableWorker() {
6+
detectionWorker = new Worker(new URL('./worker/worker.js', import.meta.url));
277

28-
async disable() {
29-
this.enabled = false;
30-
if (this._onUpdate) this.core.eventBus.off('engine:update', this._onUpdate);
31-
return this;
32-
}
8+
detectionWorker.postMessage({ type: 'init' });
339

34-
dispose() {
35-
return this.disable();
10+
// Capture engine:update event to post processFrame messages
11+
core.eventBus.on('engine:update', (frame) => {
12+
if (frame) {
13+
detectionWorker.postMessage({ type: 'processFrame', payload: { frameId: frame.id } });
3614
}
15+
});
3716

38-
_onFrameUpdate({ deltaTime, context }) {
39-
// stub: read frame from context/resources and run detection
40-
// when detection occurs, emit:
41-
// this.core.eventBus.emit('ar:markerFound', { id, poseMatrix, confidence, corners });
17+
detectionWorker.addEventListener('message', (ev) => {
18+
const { type, payload } = ev.data || {};
19+
if (type === 'ready') {
20+
console.log('Worker is ready');
21+
} else if (type === 'detectionResult') {
22+
payload.detections.forEach(detection => {
23+
core.eventBus.emit('ar:markerUpdated', {
24+
id: detection.id,
25+
poseMatrix: new Float32Array(detection.poseMatrix),
26+
confidence: detection.confidence,
27+
corners: detection.corners
28+
});
29+
});
4230
}
31+
});
32+
}
4333

44-
getMarkerState(markerId) {
45-
return null;
46-
}
47-
}
34+
// Call enableWorker when you want to start the worker
35+
// Example: enableWorker();

src/worker/worker.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
self.addEventListener('message', async (ev) => {
2+
const { type, payload } = ev.data || {};
3+
try {
4+
if (type === 'init') {
5+
// Worker init hook. Load WASM or other heavy libraries here in future.
6+
// Respond ready to main thread.
7+
self.postMessage({ type: 'ready' });
8+
} else if (type === 'processFrame') {
9+
// payload: { imageBitmapTransferable?, width, height }
10+
// This stub simulates detection latency and returns a fake marker result.
11+
// In real implementation, run artoolkit detection and return detections.
12+
const { frameId } = payload || {};
13+
// Simulate async detection
14+
await new Promise((r) => setTimeout(r, 10));
15+
16+
// Fake detection result: one marker with id 'demo' and identity matrix
17+
const fakeMatrix = new Float32Array([1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1]);
18+
const result = {
19+
detections: [
20+
{
21+
id: 'demo',
22+
confidence: 0.9,
23+
poseMatrix: Array.from(fakeMatrix), // structured-clonable
24+
corners: [ {x:0,y:0}, {x:1,y:0}, {x:1,y:1}, {x:0,y:1} ],
25+
frameId
26+
}
27+
]
28+
};
29+
30+
self.postMessage({ type: 'detectionResult', payload: result });
31+
}
32+
} catch (err) {
33+
self.postMessage({ type: 'error', payload: { message: err.message } });
34+
}
35+
});

0 commit comments

Comments
 (0)