Use augmented reality (AR) to pin a scene to a table or desk for easy exploration.
Use case
Tabletop scenes allow you to use your device to interact with scenes as if they are 3D-printed model models sitting on your desk. You could use this to virtually explore a proposed development without needing to create a physical model.
How to use the sample
You'll see a feed from the camera when you open the sample. Tap on any flat, horizontal surface (like a desk or table) to place the scene. With the scene placed, you can move the camera around the scene to explore. You can also pan and zoom with touch to adjust the position of the scene.
How it works
- Create an
ARSceneView
and add it to the view.- Note: this sample uses content in the WGS 84 geographic tiling scheme, rather than the web mercator tiling scheme. Once a scene has been displayed, the scene view cannot display another scene with a non-matching tiling scheme. To avoid that, the sample starts by showing a blank scene with an invisible base surface. Touch events will not be raised for the scene view unless a scene is displayed.
- Wait for plane detection using
arView.PlanesDetectedChanged
before allowing the user to tap to place the scene. - Wait for the user to tap the view, then use
arView.SetInitialTransforamtion(tappedPoint)
to set the initial transformation, which allows you to place the scene. This method uses ARKit's built-in plane detection. - If the call to
SetInitialTransformation
returnstrue
, proceed to load the scene and display it. - To enable looking at the scene from below, set
scene.BaseSurface.NavigationConstraint
to0
. - Set the origin camera to the point in the scene where it should be anchored to the real-world surface you tapped. Typically that is the point at the center of the scene, with the altitude of the lowest point in the scene.
- Set
arView.TranslationFactor
such that the user can view the entire scene by moving the device around it. The translation factor defines how far the virtual camera moves when the physical camera moves.- A good formula for determining translation factor to use in a tabletop map experience is translationFactor = sceneWidth / tableTopWidth. The scene width is the width/length of the scene content you wish to display in meters. The tabletop width is the length of the area on the physical surface that you want the scene content to fill. For simplicity, the sample assumes a scene width of 800 meters and physical size of 1 meter.
Relevant API
- ARSceneView
- Surface
Offline data
This sample uses offline data, available as an item on ArcGIS Online.
About the data
This sample uses the Philadelphia Mobile Scene Package. It was chosen because it is a compact scene ideal for tabletop use. Note that tabletop mapping experiences work best with small, focused scenes. The small, focused area with basemap tiles defines a clear boundary for the scene.
Additional information
Tabletop AR is one of three main patterns for working with geographic information in augmented reality. See Display scenes in augmented reality in the guide for more information.
This sample uses the ArcGIS Runtime Toolkit. See Display scenes in augmented reality in the guide to learn about the toolkit and how to add it to your app.
Tags
augmented reality, drop, mixed reality, model, pin, place, table-top, tabletop
Sample Code
// Copyright 2020 Esri.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
// language governing permissions and limitations under the License.
using Android.App;
using Android.Graphics;
using Android.OS;
using Android.Views;
using Android.Widget;
using AndroidX.AppCompat.App;
using Esri.ArcGISRuntime.ARToolkit;
using Esri.ArcGISRuntime.Mapping;
using System;
namespace ArcGISRuntimeXamarin.Samples.DisplayScenesInTabletopAR
{
[Activity(ConfigurationChanges =
Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize)]
[ArcGISRuntime.Samples.Shared.Attributes.Sample(
name: "Display scenes in tabletop AR",
category: "Augmented reality",
description: "Use augmented reality (AR) to pin a scene to a table or desk for easy exploration.",
instructions: "You'll see a feed from the camera when you open the sample. Tap on any flat, horizontal surface (like a desk or table) to place the scene. With the scene placed, you can move the camera around the scene to explore. You can also pan and zoom with touch to adjust the position of the scene.",
tags: new[] { "augmented reality", "drop", "mixed reality", "model", "pin", "place", "table-top", "tabletop" })]
public class DisplayScenesInTabletopAR : AppCompatActivity
{
// Hold references to the UI controls.
private ARSceneView _arSceneView;
private TextView _helpLabel;
private Scene _tabletopScene;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
Title = "Display scenes in tabletop AR";
CreateLayout();
}
private void CreateLayout()
{
// Load the layout.
SetContentView(ArcGISRuntime.Resource.Layout.DisplayScenesInTabletopAR);
// Get references to the UI controls.
_arSceneView = FindViewById<ARSceneView>(ArcGISRuntime.Resource.Id.arSceneView);
_helpLabel = FindViewById<TextView>(ArcGISRuntime.Resource.Id.helpLabel);
// Request camera permission. Initialize will be called when permissions are granted.
Initialize();
}
private void Initialize()
{
// Display an empty scene to enable tap-to-place.
_arSceneView.Scene = new Scene(SceneViewTilingScheme.WebMercator);
// Render the scene invisible to start.
_arSceneView.Scene.BaseSurface.Opacity = 0;
// Wait for the user to tap.
_arSceneView.GeoViewTapped += ArSceneView_GeoViewTapped;
// Enable plane rendering.
_arSceneView.ArSceneView.PlaneRenderer.Enabled = true;
_arSceneView.ArSceneView.PlaneRenderer.Visible = true;
// Wait for planes to be detected.
_arSceneView.PlanesDetectedChanged += ARSceneView_PlaneDetectedChanged;
_helpLabel.Text = "Keep moving your phone to find a surface.";
}
private void ARSceneView_PlaneDetectedChanged(object sender, bool planeDetected)
{
if (planeDetected)
{
_arSceneView.PlanesDetectedChanged -= ARSceneView_PlaneDetectedChanged;
_helpLabel.Text = "Tap the dots to place scene.";
}
}
private void ArSceneView_GeoViewTapped(object sender, Esri.ArcGISRuntime.UI.Controls.GeoViewInputEventArgs e)
{
// Get the tapped screen point.
PointF screenPoint = e.Position;
if (_arSceneView.SetInitialTransformation(screenPoint))
{
DisplayScene();
_helpLabel.Visibility = ViewStates.Gone;
}
else
{
Toast.MakeText(this, "ARCore doesn't recognize that point as a plane.", ToastLength.Short);
}
}
private async void DisplayScene()
{
try
{
if (_tabletopScene == null)
{
// Load a scene from ArcGIS Online.
_tabletopScene = new Scene(new Uri("https://www.arcgis.com/home/item.html?id=31874da8a16d45bfbc1273422f772270"));
await _tabletopScene.LoadAsync();
// Set the clipping distance for the scene.
_arSceneView.ClippingDistance = 400;
// Enable subsurface navigation. This allows you to look at the scene from below.
_tabletopScene.BaseSurface.NavigationConstraint = NavigationConstraint.None;
}
// Display the scene.
_arSceneView.Scene = _tabletopScene;
// Create a camera at the bottom and center of the scene.
// This camera is the point at which the scene is pinned to the real-world surface.
var originCamera = new Esri.ArcGISRuntime.Mapping.Camera(52.52083, 13.40944, 8.813445091247559, 0, 90, 0);
// Set the origin camera.
_arSceneView.OriginCamera = originCamera;
// The width of the scene content is about 800 meters.
double geographicContentWidth = 800;
// The desired physical width of the scene is 1 meter.
double tableContainerWidth = 1;
// Set the translation factor based on the scene content width and desired physical size.
_arSceneView.TranslationFactor = geographicContentWidth / tableContainerWidth;
}
catch (System.Exception ex)
{
new Android.App.AlertDialog.Builder(this).SetMessage("Failed to load scene").SetTitle("Error").Show();
System.Diagnostics.Debug.WriteLine(ex);
}
}
protected override async void OnPause()
{
base.OnPause();
await _arSceneView.StopTrackingAsync();
}
protected override async void OnResume()
{
base.OnResume();
// Start AR tracking without location updates.
await _arSceneView.StartTrackingAsync();
}
}
}