AR Game ~ Geospatial API ~
Tags: gamedev, unity3d, api, location
Published: 2024-06-11 13:54:38

Table of contents

- Background
- What is Geospatial API
- Implementation of Geospatial API
- Execution of the Geospatial API
- Next Step

# Background
I will develop AR Game with Unity, AR foundation and so on. To learn AR development, I am researching about AR and the software related it. This blog shows the research and the process of developing AR game. If you have a question, I am happy to answer it.

# What is Geospatial API
I will develop AR game with Geospatial API. Currently, there are several types of AR. I have written the article about these types. If you want, please see it. 

[What is Augmented Reality (AR)](https://dev.to/takeda1411123/ar-game-what-is-augmented-reality-ar--24m0)

Previous AR technology made it difficult to place AR objects in specific locations, and objects could only be placed around the device.
The Geospatial API uses not only your smartphone's GPS information but also Google Street View data and images from your smartphone's camera to perform precise positioning, allowing you to place AR objects anywhere in the world.
Additionally, this API allows nearby buildings to create an AR world, which can provide a more immersive experience. 


[Geospatial API](https://developers.google.com/ar/develop/geospatial)

# Implementation of Geospatial API
This post will show how to implement AR using **Geospatial API**. 

## Prerequisites
- Installed Unity
- Installed AR Foundation
- Installed AR core or AR Kit

## Enable the ARCore API
It is necessary to enable the ARCore API in a GCP project to use Geospatial API.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s698wnjrg0rhxsttkbgs.png)

## Add required libraries to your app

### Install ARCore Extension
To use the ARCore Geospatial API, your project must support AR Foundation and the ARCore Extensions for AR Foundation.

1. Access Package manager
2. Click "Add package from git URL"

URL
```
https://github.com/google-ar/arcore-unity-extensions.git
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/izcbm21za2bdtbs4x17f.png)

### Add required libraries to your app
You must add libraries to enable Geospatial features in your app.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0yj7vtsdmu8l19q2uqzm.png)

### Enable Geospatial capabilities in the session configuration

1. Create "ARCore Extensions Config"
2. Change Geospatial Mode from disabled to enabled


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iz0yjp7w2a9atganw00i.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o6a6l589jg3fkc6oezal.png)

## Create ARCore Extensions
Create ARCore Extensions object and attach ARCore Extensions Config on it. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iprguizqv8y3cwk4v28f.png)

## Create XR Origin 
Create XR Origin (AR) object, which manage user' movement and input.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mym6x9jvg3cgtt2izjx3.png)

### Add Component
Add these below components

1. **AR Earth Manger** for location 
2. **AR Anchor Manager** for putting objects in a specific place
3. **AR Streetscape Geometry Manager** for getting  
Geometry information for buildings and other urban environments
4. AR Plane Manager for tracking and managing a plane

## Set AR Camera Manager on Main Camera Object
This component can estimate the light in an AR scene based on light information obtained from camera images. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/managobgf0ccv2pxnv9t.png)


## Add AR Occlusion Manager onto Main Camera Object
This component can occlude virtual objects with real world exists like buildings and trees. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0a5mxjpx9jnxnqsfouws.png)

## Get started with Code

### Start location Service
You can start the location service with "Input.location.Start()". Once started, the service can get the user's location information etc. 

```C#
example 

_waitingForLocationService = true;
if (!Input.location.isEnabledByUser)
{
  _waitingForLocationService = false;
  yield break;
}
Debug.Log("Starting location service.");
Input.location.Start();

while (Input.location.status == LocationServiceStatus.Initializing)
{
  yield return null;
}
```

### Get location information
You can get location information with AR Earth Manger as above.

```C#
var pose = earthTrackingState == TrackingState.Tracking 
EarthManager.CameraGeospatialPose : new GeospatialPose();

InfoText.text = string.Format(
                "Latitude/Longitude: {1}°, {2}°{0}" +
                "Horizontal Accuracy: {3}m{0}" +
                "Altitude: {4}m{0}" +
                "Vertical Accuracy: {5}m{0}" +
                "Eun Rotation: {6}{0}" +
                "Orientation Yaw Accuracy: {7}°",
                Environment.NewLine,
                pose.Latitude.ToString("F6"),
                pose.Longitude.ToString("F6"),
                pose.HorizontalAccuracy.ToString("F6"),
                pose.Altitude.ToString("F2"),
                pose.VerticalAccuracy.ToString("F2"),
                pose.EunRotation.ToString("F1"),
                pose.OrientationYawAccuracy.ToString("F1"));
```

### Set AR Contents
AR contents can be put by specifying the latitude, longtude, and altitude. 

```
anchor = anchorManager.AddAnchor(latitude, longitude, altitude, rotation);
instance = Instantiate(prefab, anchor.transform);
```


# Execution of the Geospatial API

This video shows the sample game that used Geospatial API.

{% youtube https://www.youtube.com/watch?v=hHFIj8Zwc9g %}

# Next step
As next step, I will research ChatGPT API for the automated creation of character dialog.