Git Product home page Git Product logo

mapbox-ar-unity's Introduction

THIS REPOSITORY IS DEPREACTED!

AR support been merged into the main repository https://github.com/mapbox/mapbox-unity-sdk/

Please open issues or PRs there!


unity-repo-banner_preview

Mapbox Unity SDK + ARKit + ARCore For World Scale AR Experiences

A place to create/learn with Unity, ARKit/ARCore and Mapbox!

Note: This library is experimental and will change. It is published to gather feedback from the community. Please use with caution and open issues for any problems you see or missing features that should be added.

We'd love to have you contribute to the project!

Also, see our related iOS library.

What is it?

Check out this presentation for reference.

ARKit specific checks

One limitation of ARKit is that it does not have any knowledge of where in the world a session was started (or ended), nor does it know the True North alignment with the real world. ARKit is great for location positional tracking, but suffers over distance, or when tracking is poor. My testing has shown that ARKit's position begins to drift after just a few meters and becomes very noticeable after 50 meters (average accumulated drift ~10 meters with GPS accuracy 5 meters).

This project aims to inform ARKit of global positioning using location data from the phone, which will enable Unity developers to "anchor" augmented reality experiences to real world coordinates. Given the sheer volume of data that Mapbox provides, the possibilities are endless.

For additional inspiration and reference, please see this library for iOS. Concepts and potential solutions should be similar, though implementation will vary greatly.

Please note: while it is possible to use Mapbox to display 3D maps on your coffee table, this project is aimed at building "world scale" maps and augmented reality experiences.

Examples

In this Repository

Requirements

Known Issues

  • Location permissions on Android must be enabled manually after installing application! See here for related information.
  • AR Tracking state is not yet exposed in the ARInterface
  • Focus square has rendering issues on iOS and Android

Usage

  1. Configure your Mapbox API token.
  2. Open the AutomaticWorldSynchronization scene OR drag WorldAlignmentKit prefab into your scene.
  3. Tune parameters (read more below--I recommend testing with defaults, first):
    1. DeviceLocationProvider
    2. SimpleAutomaticSynchronizationContextBehaviour
    3. AverageHeadingAlignmentStrategy
  4. Build.
  5. When the scene loads, ensure you find at least one ARKit anchor (blue AR plane).
  6. Begin walking. Try walking in a straight line to assist in calculating an accurate heading. How far you need to walk before getting a good Alignment depends on your settings.

How it Works

If you're not familiar with the Mapbox Unity SDK, it may help to check out the built-in examples. You should be familiar with building a map and using LocationProvider. For brevity, I will assume you know how these components work.

All relevant AR World Alignment components live in Mapbox/Unity/AR.

At the core, we use a SimpleAutomaticSynchronizationContext to align our map (world) to where the AR camera thinks it is in the world. This context uses the AR position deltas (a vector) and GPS position deltas (mapped to Unity coordinates) to calculate an angle. This angle represents the ARKit offset from True North. Note: I could have used LocationService compass heading, but I've found it's often far more inaccurate than these manual calculations.

ISynchronizationContext

OnAlignmentAvailable

This event is sent when the context has successfully calculated an alignment for the world. This alignment can be used to manipulate a root transform so that it appears to be aligned with the AR camera.

SimpleAutomaticSynchronizationContext

Due to ARKit positional drift over distance, we need to constantly refresh our offset (and potentially our heading). To do so, we should consider the range at which ARKit begins to visually drift, as well as the accuracy of the reported location (GPS). You can think of this relationship as a venn diagram.

venn

The center of the circles represent reported ARKit and GPS positions, respectively. The radius of the circles represent "accuracy." The intersection represents a potentially more accurate position than either alone can provide. The bias value represents where inside that "intersection" we want to be.

As previously mentioned, we use the delta between position updates to calculate a heading offset. Generally (depending on GPS accuracy), I've found this to be far more reliable and accurate than compass data.

angle

The end result of a successful synchronization is an Alignment, which offers a rotation and position offset. These values are meant to be used to modify the transform of a world root object. We have to do this because ARKit's camera should not be modified directly.

UseAutomaticSynchronizationBias

Attempt to compute the bias (see below) based on GPS accuracy and ArTrustRange.

SynchronizationBias

How much to compensate for drift using location data (1 = full compensation). This is only used if you are not using UseAutomaticSynchronizationBias.

MinimumDeltaDistance

The minimum distance that BOTH gps and ar delta vectors must differ before new nodes can be added. This is to prevent micromovements in AR from being registered if GPS is bouncing around.

ArTrustRange

This represents the radius for which you trust ARKit's positional tracking, relative to the last alignment. Think of it as accuracy, but for AR position.

AddSynchronizationNodes(Location gpsNode, Vector3 arNode)

You can think of a synchronization node as a comparison of ARKit and location data. You are essentially capturing the position of both "anchors" at the same time. We use this information to compute our Alignment.

SimpleAutomaticSynchronizationContextBehaviour

This class is mostly a monobehaviour wrapper around the context itself, which allows you to specify settings in the inspector. However, it also has knowledge of when ARAnchors are added, so as to offset the Alignment height based on a detected anchor height.

This class is also responisble for listening to location updates from the LocationProvider and adding synchronization nodes (gps + ar positions) to the context. Important: GPS positions must be converted to Unity coordinate space before adding to the context!

Lastly, this object needs an AbstractAlignmentStrategy which is used to determine how an Alignment should be processed. For example, you can snap, lerp, or filter and then lerp a transform (such as the WorldRoot). I've had the best success and most stable results using the AverageHeadingAlignmentStrategy.

ManualSynchronizationContextBehaviour

This example context relies on a TransformLocationProvider that is a child of a camera responsible for drawing a top-down map. You can use touch input to drag (one finger) and rotate (two fingers) the camera to manually position and orient yourself relative to the map (your target location is represented with the red arrow in the example scene). On the release of a touch, the alignment will be created.

Note: This implementation does not attempt to compensate for ARKit-related drift over time!

AverageHeadingAlignmentStrategy

This AlignmentStrategy is responsible for averaging the previous alignment headings to determine a better heading match, over time. Additionally, it will not use Alignments with reported rotations outside of some threshold to reposition the world root transform. This is important because sometimes a GPS update is wrong and gives us a bad heading. If we were to offset our map with this heading, our AR object would appear to be misaligned with the real world.

Note: this implementation is actually a bit of a hack. Ideally, filtering of this type should be done directly in an ISynchronizationContext. I've used this approach in the interest of time and to keep the example context as simple as possible.

MaxSamples

How many heading samples we should average. This is a moving average, which means we will prune earlier heading values when we reach this maxmimum.

IgnoreAngleThreshold

We will not use any alignments that report a heading outside of our average plus this threshold to position or rotate our map root. This helps create a more stable environment.

LerpSpeed

When we get a new alignment (that should not be dismissed), this value represents how quickly we will interpolate from our previous world root alignment to our new world root alignment. Smaller values mean the transition will appear more subtle.

DeviceLocationProvider

You will need to experiment with various DesiredAccuracyInMeters and UpdateDistanceInMeters settings. I recommend keeping your update distance on the higher side to prevent unnecssary alignment computation. The tradeoff, of course, is that you may begin to drift. Which value you use depdends entirely on your application.

Limitations

While I have done extensive testing "on the ground," I've been in limited, specific locations, with ideal GPS accuracy. I make no guarantees that what is currently provided in this library will solve your problems or work in your area (please help me make it better).

There are various TODO and FIXME tasks scattered around in the Mapbox.Unity.Ar namespace. Please take a look at these to get a better idea of where I think there are some shortcomings. In general, my implementation so far is quite naive. Hopefully the community can help improve this with new context implementations or more sophisticated algorithms/filters.

Solving for UX is not an easy matter. Manual calibration works great, but is not user-friendly (or immune to human error). Automatic calibration works, but still has shortcomings, such as requiring the user to walk x meters before acquiring a workable alignment.

There's a giant Log button. Use this log to help diagnose issues. If you're seeing lots of red lines (or the alignment just doesn't seem to be working), then something is wrong. Search the C# solution to see what may be the cause of those. If you want, log your own data there, too! You can also use the map toggle to show your paths (AR = red, GPS = blue). If you are aligned properly, the two paths should nearly be on top of one another.

Other issues to note:

  • ARKit tracking state is not really used to infuence this alignment process. If you lose tracking, fail to find anchors, background the application, etc., you will need to start a new session and calibrate again.

What about Mapbox?

With access to Mapbox geospatial data, you can easily augment the AR experience to great effect. Here are some ideas:

  • Use the road layer to construct navigation meshes for your zombie apocalypse simulation
  • Use the building layer to occlude AR elements or anchor AR billboards on building facades
  • Use Mapbox Directions API to perform realtime navigation and overlay the routes in AR
  • Geofence your AR experiences based on landuse or custom data (or procedurally place gameobjects based on type)
  • Show points of interest (POI) above or on real places
  • Use global elevation data to more accurately plant AR objects on the ground (especially useful for distant objects—imagine geotagging something on a cliff or in a valley)
  • Use various label layers to show street and water names
  • Use Geocoding to find out what's nearby and show that information
  • Use building layer to raycast against for gameplay purposes (ARKit cannot detect vertical planes, but a building could subsitute for this)
  • Capture data/input from users at runtime and upload with Mapbox Datasets (use these datasets to generate a tileset to show on a map or to use for logic at runtime)
  • Using the above: create world persistence that everyone experiences simultaneously (multiplayer with local and global context)
  • Build indoor navigation networks and use buildings footprints for geofencing purposes (when a building is entered, disable GPS tracking and switch to ARKit tracking—we also likely known which entrance was used)

Looking to the Future

What can we do to improve automatic alignment? Here are several ideas:

  • Use compass data to augment the angles we calculate. Confirm or more heavily weight a computed angle if it is similar to the compass. Note: this likely requires the device to be facing the direction it is moving in.
  • Use weighted moving averages or linear regression of location and AR position to find more optimal alignments. Location updates with poor accuracy will have very little influence on the overall alignment. AR position updates with poor tracking state or lots of cumulative drift will have very little weight. Time will affect both, such that more recent updates are weighted more heavily.
  • Using the above, weighted heading values will also help improve position offset. This is because we use the heading to "rotate" our GPS location.
  • Store last known "good" alignment on application background and use that as a restore point until you successfully find a new alignment.
  • We could try to ignore GPS entirely up to a certain cumulative AR distance, barring some complication (tracking state changed). This may lead to longer stretches of properly "anchored" AR elements, relative to the AR camera.
  • Use smaller DeviceLocationProvider UpdateDistanceInMeters to more quickly find an initial alignment. Increase this value one calibration is achieved.
  • Ignore alignments immediately after ARKit is determined to have taken drastic turns. Local tracking is far better suited for measuring sharp turns than GPS.
  • Completely filter out poor GPS results. Drift could become problematic, but this may be ideal compared to a very inaccurate GPS update.
  • Is ARKit drift fairly consistent (across devices, assuming enough resources available to properly track)? If so, we can project our position along the delta vector to compensate for that drift. GPS would be used rarely, in this instance (and that's a good thing for a "stable" world).

mapbox-ar-unity's People

Contributors

abhishektrip avatar crashthatch avatar jellena avatar lndsay avatar tevfikufuk avatar wilhelmberg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mapbox-ar-unity's Issues

Add 3D object to specific LatLong in Mapbox Unity3D

So I am new to Mapbox and I have been trying to make an AR location-based game.
My Mapbox type is LOCATION-BASED GAME and I want to add a 3D object into the map.

I also want to enter into AR environment (turn the camera on, finding the AR object using camera), when I am near the object

There aren't much documentary nor videos for reference.
Please HELP

AddSynchronizationNodes() adds new nodes even if MinimumDeltaDistance is not reached

AddSynchronizationNodes() always adds to _gpsNodes / _arNodes, even if the distance travelled during this location-update is < MinimumDeltaDistance. Since the calculation in ComputeAlignment() uses the difference between the last 2 added _gpsNodes, it could calculate the alignment based on very small movements (that MinimumDeltaDistance is designed to avoid).

  • Imagine situation where MinimumDeltaDistance is set to 10m.
  • The devices moves 9.9m east, so the GPS and LocationService both adjust accordingly, and _arDelta & _gpsDelta are incremented by 9.9
  • 9.9 < 10.0, so AddSynchronizationNodes exits early and does not call ComputeAlignment().
  • Now the device moves 0.2m west (back the way it came), but the GPS is slightly inaccurate and registers a further small movement east 0.2m
  • _arDelta and _gpsDelta are increased to 10.1m (> MinimumDeltaDistance), so ComputeAlignment() is called, but _currentArVector = (-0.2,0,0) and _currentAbsoluteGpsVector = (0.2,0,0), so rotation=180 (incorrect)

I would suggest that ComputeAlignment() should be considering the sum of all movements since the last ComputeAlignment call (ie. 9.9+0.2 vs 9.9-0.2) to compute the rotation. I think this can be achieved by removing _arDelta completely, and making _currentArVector a "running total" of the new location vectors until _currentArVector.magnitude > MinimumDeltaDistance, at which point ComputeAlignment() would be called (and similar for _currentAbsoluteGpsVector).

Do you agree, or have I misunderstood somewhere?

Aligning AR to 3D Terrain

Hi,

When using a terrain factory to produce the map, the terrain is much higher than the AR Root & Camera, so isn't visible, due to the 3D terrain being fixed at it's relative height in the real world, at the AR Camera being at 0,0,0.

unity_2018-01-29_11-34-15

I can't move the terrain down to 0,0,0, as it moves itself back up (I think the location provider is pulling it back up again), and I haven't found any way to move the AR components up to the same height.

Any ideas?

Update library to use AR Interface

AR Interface was announced at Unite Austin. It is intended to be a cross-platform mobile AR solution, provided directly by Unity. In other words, this library would be fully compatible with ARCore (and many android-based headset providers). Also, we would no longer be based on a beta ARKit plugin.

Unity Cloud Build Issue

I work using Windows and am developing for iOS using Unity Cloud Build.
None of the builds have completed successfully.
I cloned this repo and replaced the UnityARKitPlugin folder with the latest from the bitbucket repo.

This is the log from the latest build:

https://pastebin.com/VVEuQtmg

I've left out a number of warnings, but can include them if necessary.

Can anyone figure this out? Is it ok to replace the UnityARKitPlugin folder with code from the bitbucket repo?

AverageHeadingAlignmentStrategy doesn't calculate circular average over 360°

Consider 2 alignments: 10° and 350°. The "midpoint" of these alignments should be 0°, but the current calculation will calculate it as (10+350)/2 = 180°, which isn't really what we want.

Even if the rotations provided were in (-180, 180], the calculation would fail for -10° and 10° ((-10+360) + 10)/2 = (350+10)/2 = 180 as above.

I think this requires Mean of circular quantities to calculate the "average" heading close to the observations. But I'm confused as to how it seems to work at the moment?

Weird changes in camera euler angles at runtime (observed on Android)

I think Focus square causes lots of ugly height values and sometimes weird tilt orientation of maproot relative to x or z axis. Is it possible to remove focus square and give an constant value to _lastHeight ? Does Focus square exist only for getting _latHeight value? If not, it is better not to use it and we can manually align maproot height or give a constant value since sometimes it rotates maproot around x or z axis.

Inaccurate positions when you are far accurate when you are near

When i put objects in map using SpawnOnMap script they appear not exactly in the location i gave but the more you get near them the more accurate are their positions, why happens this? i notice as i move to the virtual object it corrects its position smoothly, it doesnt jump from one position to other but as you move

How to place an object using a predefined lat and long?

I dont understand well how works mapbox and your project but seems it just spawns some objects set in the map visualizer but i want to modify that data to add certain lat/loc that i want and spawn an item there and also know the height of the terrain in that lat/long in unity coordinates

World scale ar experience demo

May I know whether there is a demo scene refer to the reference video?
(World scale ar with pathfinding, direction arrow display etc.)

POI Vector Map does not show POI

In the POI Vector map demo, the information canvas says a vector tile map showcasing poi_label markers. But the ClipMapVectorFactory has building as the key instead of poi_label.
Looking at the studio tileset details provided, there is are two different keys: building and poi_label.

How can I see the poi_labels instead of the buildings?

AutomaticSynchronizationContext example not working

Hi,
I just started playing around with your project and had a look at the example scenes, modified them, etc.
It looks to me that the LocationProvider property of SimpleAutomaticSynchronizationContextBehaviour is actually never set. Instead the getter just pulls it out of the factory singleton. However, the behaviour never actually subscribes the wrapped SynchronizationContext to the OnLocationUpdated event like the setter does. Hence the location is never updated.

I solved this by adding
_locationProvider.OnLocationUpdated += LocationProvider_OnLocationUpdated;
to the property's setter.

Is this by design, i.e. did I miss something, or is the example actually broken?

Object Syncing

I'm not really sure where to ask this so I will ask here. So far everything in this package works great. They only issue I am having is when viewing the AR from street level and have a map in the corner to see my position is that it is putting me in the correct location on the map but in the real world street view it takes more then a block for the objects to slide into position.

Is there anyway to improve accuracy over a shorter distance? I understand the GPS takes a few meters to calculate position but it seems like it takes a lot longer then when it syncs on the map view.

ARC Semantic Issues

I'm getting several ARC semantic issues including:

"No visible @interface for 'ARCamera' declares the selector 'projectionMatrixWithViewportSize:orientation:zNear:zFar:'

"No visible @interface for 'ARCamera' declares the selector 'projectionMatrixWithViewportSize:orientation:zNear:zFar:'"

and

"No visible @interface for 'ARFrame' declares the selector 'displayTransformWithViewportSize:orientation:'

Any idea why?

Zoomable Map in AR

I've been trying to import the Zoomable Map example into the ARTabletopScene, but it does not seem to allow drag like it does in the demo.

Thing that I've attached to the map gameobject:
image

Can I please get help to figure out why it doesn't work?

Create a basic UX to encourage proper calibration

For example: one developer created some cubes (arranged in a line) in front of the camera that the user should walk through. These were placed just a short distance apart. The location provider minimum distance update can be lowered to help provide several updates in just a short distance walked.

  • Encourage user to walk in straight line
  • Give them visual targets
  • Text to encourage movement and provide context for why
  • Lower location update threshold temporarily (may not work in low accuracy environments)
  • Automatic sync implementation may also need it's min distance lowered.

xcode (9A235) build errors

Hi!
Thanks for the awesome project, but I can't get it to build correctly on xcode..
It gives me a bunch of errors..
Do you have a solution for this?

Thanks!
Here are some of the errors:

Users/Jan/AR_GPS/Builds/PrevizAR_v00/Libraries/UnityARKitPlugin/Plugins/iOS/UnityARKit/NativeInterface/ARSessionNative.mm:689:5: error: unknown type name 'ARWorldTrackingSessionConfiguration'
ARWorldTrackingSessionConfiguration* config = [ARWorldTrackingSessionConfiguration new];
^
/Users/Jan/AR_GPS/Builds/PrevizAR_v00/Libraries/UnityARKitPlugin/Plugins/iOS/UnityARKit/NativeInterface/ARSessionNative.mm:689:52: error: use of undeclared identifier 'ARWorldTrackingSessionConfiguration'
ARWorldTrackingSessionConfiguration* config = [ARWorldTrackingSessionConfiguration new];

/Users/Jan/AR_GPS/Builds/PrevizAR_v00/Libraries/UnityARKitPlugin/Plugins/iOS/UnityARKit/NativeInterface/ARSessionNative.mm:702:5: error: unknown type name 'ARWorldTrackingSessionConfiguration'
ARWorldTrackingSessionConfiguration* config = [ARWorldTrackingSessionConfiguration new];
^
/Users/Jan/AR_GPS/Builds/PrevizAR_v00/Libraries/UnityARKitPlugin/Plugins/iOS/UnityARKit/NativeInterface/ARSessionNative.mm:702:52: error: use of undeclared identifier 'ARWorldTrackingSessionConfiguration'
ARWorldTrackingSessionConfiguration* config = [ARWorldTrackingSessionConfiguration new];
^
/Users/Jan/AR_GPS/Builds/PrevizAR_v00/Libraries/UnityARKitPlugin/Plugins/iOS/UnityARKit/NativeInterface/ARSessionNative.mm:712:5: error: unknown type name 'ARSessionConfiguration'
ARSessionConfiguration* config = [ARSessionConfiguration new];
^
/Users/Jan/AR_GPS/Builds/PrevizAR_v00/Libraries/UnityARKitPlugin/Plugins/iOS/UnityARKit/NativeInterface/ARSessionNative.mm:712:39: error: use of undeclared identifier 'ARSessionConfiguration'
ARSessionConfiguration* config = [ARSessionConfiguration new];

Implement GPS filtering

Based on physical improbabilities. This will assist in more stable traces, which will lead to better heading results.

Building prefabs on maps from Mapbox for Unity3d

Everybody, Hi. I have a problem. Could you help me to solve it. I want to create a third-party objects-prefabs on map from Mapbox to used Unity3d. After adding prefabs and then scaling and moving the map my prefabs are deleted. If you return to the original position on the map again and the scale of these objects-prefabs is already gone. How can I solve this problem so that third-party prefabs in Mapbox for Unity3d remain on the map when scaling and moving the map itself? Thank.

Investigate using a lookup table + constants to assist with drift.

Capture drift values in varying environments (lighting scenarios, walking speed, surface type or feature count, etc.). Measure AR camera movement delta over a fixed distance several times to ensure drift is, in fact, relatively static.

In doing so, we may be able to compensate for at least some amount of drift.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.