Table of contents
- Background
- Sample
- Code
- Unity
- Next Step
Background
I am 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.
I researched Geospatial API before. This post will show the sample of the game with geospatial API. Therefore, it will be my note. If you use it, please do not miss it.
Sample
If you use ARCore Extensions, you can import Geospatial Sample and try it easily.
In sample scene, you can test Geospatial API and put AR object anywhere unless localising area.
UI
Origin
This is basement of AR content and user viewpoint (camera). It used to be called AR Session origin.
In real world, it can adjust the scale of virtual objects. Also, according user movement, it can update the position and rotation of AR objects.
Session
This object is used to manage the whole lifecycle of AR experience and setting.
It can start, pose, and restart AR experience.
ARCore Extensions
This is the package that merge AR Foundation and Google ARCore extension. By using it, you can use the specific AR Core functions.
For example, it offer the AR experience based on GPS location. By using it, you can put virtual objects on the specific location.
UI Information panel
It shows the information like Latitude and Longitude at runtime.
Debug Text
It shows the information from API for debug.
Privacy Prompt
This is the prompt to get the user agreement.
Code
In sample scene, there is Geospatial controller, which manage the API and UI. I believe it is basement, therefore if you understand it, you can use it for your own game.
Awake
In Awake function, this app lock the user screen to portrait.
Screen.autorotateToLandscapeLeft = false;
Screen.autorotateToLandscapeRight = false;
Screen.autorotateToPortraitUpsideDown = false;
Screen.orientation = ScreenOrientation.Portrait;
OnEnable
This function starts location service like below code.
_startLocationService = StartLocationService();
StartCoroutine(_startLocationService);
Location Service
When it starts, you can get user location.
Input.location.Start();
while (Input.location.status == LocationServiceStatus.Initializing)
{
yield return null;
}
_waitingForLocationService = false;
if (Input.location.status != LocationServiceStatus.Running)
{
Debug.LogWarning($"Location service ended with {Input.location.status} status.");
Input.location.Stop();
}
Start
When the app starts, the start screen will be displayed. Then when you click the start button, the location service starts.
In this code, when clicked, what user confirmed a privacy prompt will be saved while proceed next screen.
public void OnGetStartedClicked()
{
PlayerPrefs.SetInt(_hasDisplayedPrivacyPromptKey, 1);
PlayerPrefs.Save();
SwitchToARView(true);
}
Additionally, it will change UI status.
_isReturning = false;
_enablingGeospatial = false;
InfoPanel.SetActive(false);
GeometryToggle.gameObject.SetActive(false);
AnchorSettingButton.gameObject.SetActive(false);
AnchorSettingPanel.gameObject.SetActive(false);
GeospatialAnchorToggle.gameObject.SetActive(false);
TerrainAnchorToggle.gameObject.SetActive(false);
RooftopAnchorToggle.gameObject.SetActive(false);
ClearAllButton.gameObject.SetActive(false);
DebugText.gameObject.SetActive(Debug.isDebugBuild && EarthManager != null);
Finally, it will switch from the privacy prompt screen to AR view screen.
SwitchToARView(PlayerPrefs.HasKey(_hasDisplayedPrivacyPromptKey));
Update
Update debug text
update debug text
$"IsReturning: {_isReturning}\n" +
$"IsLocalizing: {_isLocalizing}\n" +
$"SessionState: {ARSession.state}\n" +
$"LocationServiceStatus: {Input.location.status}\n" +
$"FeatureSupported: {supported}\n" +
$"EarthState: {EarthManager.EarthState}\n" +
$"EarthTrackingState: {EarthManager.EarthTrackingState}\n" +
$" LAT/LNG: {pose.Latitude:F6}, {pose.Longitude:F6}\n" +
$" HorizontalAcc: {pose.HorizontalAccuracy:F6}\n" +
$" ALT: {pose.Altitude:F2}\n" +
$" VerticalAcc: {pose.VerticalAccuracy:F2}\n" +
$". EunRotation: {pose.EunRotation:F2}\n" +
$" OrientationYawAcc: {pose.OrientationYawAccuracy:F2}";
Check application status
If user leave the app, the app ends.
Example (When user click escape button, the app ends)
if (Input.GetKeyUp(KeyCode.Escape))
{
Application.Quit();
}
Check localizing status
- Check the AR session state
- Check earth tracking state
- Check earth pose
bool isSessionReady = ARSession.state == ARSessionState.SessionTracking &&
Input.location.status == LocationServiceStatus.Running;
var earthTrackingState = EarthManager.EarthTrackingState;
var pose = earthTrackingState == TrackingState.Tracking ?
EarthManager.CameraGeospatialPose : new GeospatialPose();
If the localizing is lost, error message will be displayed.
While if the localizing success, all anchor will be displayed.
foreach (var go in _anchorObjects)
{
go.SetActive(true);
}
Anchor Type
To place 3D contents in the real world, the API use Anchor, which eusure these contents appear to stay at the same position and orientation in space. it helps to maintain the illusion of virtual objects placed in the real world.
There are three types of Geospatial anchors, which can handle altitude differently.
WGS84 anchors
This type can place 3D contents at any latitude, longitude, and altitude.
Terrain anchors
This type can place 3D contents using only latitude and longitude with a height relative to the terrain at that position. The altitude is determined relative to the ground or floor by using VPS.
Rooftop anchors
This type can place content using only latitude and longitude with a height relative to a building's rooftop at that position. The altitude is determined relative to the top of a building by using Streetscape Geometry.
Streetscape Geometry
This function can provide the geometry of terrain , buildings, or other structures in a scene. The geometry can be used for occlusion, rendering, or placing AR content.
Top comments (0)