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();
0 commit comments