Lesson 72 +10 XP

AR Foundation

AR Foundation

AR Foundation is a framework that allows you to build Augmented Reality games for Android (using Google's ARCore) and iOS (using Apple's ARKit) simultaneously.

Core Architecture

AR Foundation provides interfaces to standard mobile AR features:

  • AR Session: Controls the lifecycle of the AR experience (starts/stops the device camera and motion tracking).
  • AR Session Origin: Positions AR content in the physical world (similar to XR Origin).

Main Features

  • Plane Detection: Detects flat surfaces (floors, tables) in the real world, generating colliders so virtual objects can rest on them.
  • Face Tracking: Tracks eyes, nose, and mouth, letting you apply virtual masks.
  • Image Tracking: Detects predefined 2D images (like posters or book covers) and anchors 3D models to them.
  • Point Clouds: Gathers feature points detected by the camera to map environment structures.

Placing Virtual Objects

To spawn an object on a detected plane, you use Raycasting:

  1. Cast an ARRaycast using the ARRaycastManager component.
  2. If the ray hits a physical plane, it returns a hit point.
  3. Spawn your 3D prefab at the hit location, aligning it with the plane's surface normal.

TL;DR

  • AR Foundation wraps ARCore (Android) and ARKit (iOS) APIs.
  • AR Session manages device camera inputs and sensor tracking.
  • AR Session Origin maps virtual scale to physical environments.
  • Use Plane Detection and ARRaycasts to place virtual assets on real-world surfaces.