Instant Tracker iOS Tutorial

1. Overview
2. iOS Development
2.2 Create Instants
2.3 Start / Stop Tracker
2.4 Use Tracking Information
2.5 Create Instant Target Data


1. Overview

Develop Instant Tracker on iOS Platform. Refer to Instant Tracker Introduction for detailed information.

Refer to Tracker Coordinate System to better understand 3D coordinate system of Instant Tracker.

Prerequisites
Instant Tracker Introduction
Tracker Coordinate System

2. iOS Development

Start developing on xCode using Java. Refer to Requirements & Supports to find out which devices are supported.


2.2 Create Instants

GameViewController.swift

var cameraDevice:MasCameraDevice = MasCameraDevice()
var sensorDevice:MasSensorDevice = MasSensorDevice()
var trackingManager:MasTrackerManager = MasTrackerManager()

2.3 Start / Stop Tracker

To start / stop Tracker, refer to the following code.

GameViewController.swift

@objc func pauseAR()
{
    ...
    trackingManager.start(.TRACKER_TYPE_INSTANT)
}

@objc func resumeAR()
{
    trackingManager.stopTracker()
    ...
}

2.4 Use Tracking Information

To augment an object using Tracking results, refer to the following code.

You must use Convert Matrix for Metal.('metalConverter')

GameViewController.swift

func renderer(_ renderer: SCNSceneRenderer, willRenderScene scene: SCNScene, atTime time: TimeInterval) {
    if backgroundCameraQuad == nil {
        backgroundCameraQuad = BackgroundCameraQuad(device: renderer.device, pixelFormat: renderer.colorPixelFormat)
    }
    
    let commandEncoder:MTLRenderCommandEncoder = renderer.currentRenderCommandEncoder!

    let trackingState:MasTrackingState = trackingManager.updateTrackingState()
    let result:MasTrackingResult = trackingState.getTrackingResult()

    let backgroundImage:MasTrackedImage = trackingState.getImage()
    
    var backgroundProjectionMatrix:matrix_float4x4 = cameraDevice.getBackgroundPlaneProjectionMatrix()
    
    let projectionMatrix:matrix_float4x4 = cameraDevice.getProjectionMatrix()
    
    if let cameraQuad = backgroundCameraQuad {
        if(backgroundImage.getData() != nil) {
            cameraQuad.setProjectionMatrix(projectionMatrix: backgroundProjectionMatrix)
            cameraQuad.draw(commandEncoder: commandEncoder, image: backgroundImage)
        }
    }
    
    let trackingCount:Int32 = result.getCount()
    
    if trackingCount > 0 {
        for i in stride(from: 0, to: trackingCount, by: 1) {
            let trackable:MasTrackable = result.getTrackable(i)
            let projectionSCNMatrix4 = SCNMatrix4.init(projectionMatrix)
            let poseSCNMatrix4 = SCNMatrix4.init(trackable.getPose())
            cameraNode.camera?.projectionTransform = projectionSCNMatrix4
            foxNode.transform = SCNMatrix4Mult(foxTransform, poseSCNMatrix4)
        }
    }
    else {
        foxNode.scale = SCNVector3Make(0.0, 0.0, 0.0)
    }
}

2.5 Create Instant Target Data

You can create new data only when it is not in Tracking state. Refer to the following code.

GameViewController.swift

@IBAction func clickStartButton(_ sender: Any) {
    let button:UIButton = sender as! UIButton
    
    if button.titleLabel?.text == "START" {
        
        trackingManager.findSurface()
        button.setTitle("STOP", for: .normal)
    } else if button.titleLabel?.text == "STOP"{
        trackingManager.quitFindingSurface()
        button.setTitle("START", for: .normal)
    }
}