import { Nullable , Observer , Quaternion , Scene , StringDictionary , Vector3 , VideoTexture , Tools } from "babylonjs" ; import { ArUcoMetaMarkerTracker } from "./arUcoMetaMarkerTracker" ; import { TrackedNode } from "../shared/trackedNode" ; export class ArUcoMetaMarkerObjectTracker { private _scene : Scene ; private _runTrackingObserver : Nullable < Observer < Scene > > = null ; private _tracker : ArUcoMetaMarkerTracker ; private _trackedNodes : { [ id ] : TrackedNode ; } = { } ; public constructor ( scene ) { this . _scene = scene ; } public async addTrackableObjectAsync ( upperLeftId , upperRightId , lowerLeftId , lowerRightId , widthInMarkerSides ) { const marker = await this . _tracker . addMarker ( upperLeftId , upperRightId , lowerLeftId , lowerRightId , widthInMarkerSides ) ; const node = new TrackedNode ( upperLeftId + "_" + upperRightId + "_" + lowerLeftId + "_" + lowerRightId , this . _scene , true ) ; node . rotationQuaternion = Quaternion . Identity ( ) ; this . _trackedNodes [ marker . id ] = node ; return node ; } public startTracking ( ) { let running = false ; this . _runTrackingObserver = this . _scene . onAfterRenderObservable . add ( ( ) => { if ( ! running ) { running = true ; this . _tracker . updateAsync ( ) . then ( ( ) => { for ( let id in this . _trackedNodes ) { const marker = this . _tracker . markers [ id ] ; const node = this . _trackedNodes [ id ] ; node . setTracking ( marker . position , marker . rotation , marker . confidence > 0.98 ) ; } running = false ; } ) ; } } ) ; } public stopTracking ( ) { this . _scene . onAfterRenderObservable . remove ( this . _runTrackingObserver ) ; this . _runTrackingObserver = null ; } public static async CreateAsync ( videoTexture , scene , videoScale = 1.0 ) { let objectTracker = new ArUcoMetaMarkerObjectTracker ( scene ) ; objectTracker . _tracker = await ArUcoMetaMarkerTracker . CreateAsync ( videoTexture ) ; await objectTracker . setCalibrationAsync ( videoTexture , videoScale ) ; return objectTracker ; } private setCalibrationAsync ( videoTexture , scalar ) { return this . _tracker . setCalibrationAsync ( Math . round ( scalar * videoTexture . getSize ( ) . width ) , Math . round ( scalar * videoTexture . getSize ( ) . height ) ) ; } }