DEV Community

Akira Game
Akira Game

Posted on

AR Game ~ Geospatial API ~

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)

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

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

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
Enter fullscreen mode Exit fullscreen mode

Image description

Add required libraries to your app

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

Image description

Enable Geospatial capabilities in the session configuration

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

Image description

Image description

Create ARCore Extensions

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

Image description

Create XR Origin

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

Image description

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

Add AR Occlusion Manager onto Main Camera Object

This component can occlude virtual objects with real world exists like buildings and trees.

Image description

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.

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;
}
Enter fullscreen mode Exit fullscreen mode

Get location information

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

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"));
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

Execution of the Geospatial API

This video shows the sample game that used Geospatial API.

Next step

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

Top comments (0)