Exploring the Whole World in VR with Bing 3D Maps and MRTK

Published Mar 31 2021 10:40 AM 5,145 Views
Microsoft

lazerwalker_5-1617120105454.png

 

When Microsoft Flight Simulator came out a year ago, I was completely entranced by it. I’m not particularly into hyper-accurate flight simulation games, but the concept of a game where you can go anywhere in the real world and see an accurate 3D representation of that real-world space, blew my mind. What a concept! That also got me thinking: could I make something like that myself? As a solo creator I don’t have the benefit of a team of many engineers working with map data for years. 

 

How Flight Simulator’s World Works 

Microsoft Flight Simulator uses three distinct types of world maps: 

 

  1. Azure 3D Maps. Flight Simulator uses high-quality 3D photogrammetry data when it exists in Microsoft’s library. It’s pretty easy to identify these areas: there are relatively high-quality textures, but often buildings and landmarks like trees look a bit worn down as the 3D models aren’t perfect. 
  1. AI-Augmented 2D Satellite Maps. For areas where no good 3D map data is available, Flight Simulator takes 2D satellite map footage instead. Deep learning techniques are applied to infer what sort of 3D buildings and landmarks might exist, and then a library of hand-authored 3D models are programmatically placed in the world. If you’re visiting a relatively non-descript location in Flight Sim where buildings look pristine and high-quality, you’re probably in an AI-augmented zone. 
  1. Hand-authored content. A lot of Flight Simulator areas are modeled by hand to resemble their real-world locations. Notable examples include the hand-modeled airports in the Deluxe and Premium Deluxe editions of the game. 

The second and third types of map data require an exorbitant amount of work, either designing procedural generation algorithms or doing 3D modeling and level design by hand. But the first one is doable!

 

Specifically, the Bing Maps SDK gives us exactly what we want: a drop-in Unity component that, given a location (via latitude/longitude coordinate pair) will render a 3D scene containing that real-world location, powered by the same 3D maps data that Flight Simulator uses. That’s so cool! I'm going to spend the remainder of this blog post walking you through setting up a pretty simple VR “flight sim” example that will let you move around a 3D representation of any location in the world! 

 

I’m going to assume you have basic familiarity with Unity to configure a few GameObject components and write a small amount of custom code. If this is more than you’re comfortable with,  please check out other tutorials to familiarize yourself with Unity’s UI. 

 

Get a Bing Maps API Key 

To use this API, you need an API key. Sign in or sign up at the Bing Maps Portal. Once you’re logged in, create a new API key by going to My account -> My Keys -> Create a new key. Name it whatever you want, choose a “Basic” key, and for now select “Dev/Test” as the application type. 

 

lazerwalker_0-1617120105445.png

 

After creating a key, keep this page handy, as you’ll need the key in a few minutes. 

 

Download the Bing Maps SDK and open the sample project 

For now, we’re going to use the sample starter project included with the Bing Maps SDK, although there are instructions if you would prefer to integrate the SDK manually with an existing or new project using the Unity Package Manager. 

 

Grab the 0.10.0 release of the SDK from this page and unzip it. In Unity Hub, click “Add”, then locate this folder and select the “SampleProject” folder. If you don’t have a Unity 2019.4 release, Unity Hub may prompt you to download and install it. 

 

Creating our scene 

Start by adding a new scene by selecting File -> New Scene. 

 

Add a new empty GameObject to your scene by right-clicking anywhere within the Hierarchy window and selecting “Create Empty”. I called mine “MapManager” (since it’s going to manage our 3D map component). 

 

lazerwalker_1-1617120105446.png

 

In the right-side inspector pane for this new GameObject, click the “Add Component” button and search for the “MapRenderer” component. This is a Unity component provided by the Bing Maps SDK for Unity that will automatically render our 3D map in our scene! 

 

In the GameObject inspector pane, enter your API key from the Bing Maps Portal earlier into the Developer Key field in the Map Session pane. Add in the lat/long coordinates for the location where you want players to start their adventure into the “Location” fields at the top of Map Renderer.  

 

lazerwalker_2-1617120105430.png

 

I set mine to a latitude of 37.7958421 and a longitude of -122.3959688, which is the Ferry Building in downtown San Francisco. I found this by looking up the Ferry Building in Bing Maps and right-clicking the Ferry Building. If there’s a location you'd rather explore in 3D, you can get its lat/long coordinates the same way and enter them instead. 

 

Now you will see a map of the world in your Unity scene! If you adjust the “Zoom Level” slider below latitude and longitude, you can see it zoom in on San Francisco (or your chosen location). I’ve found that setting the zoom to 19 is a good fit for our use case. 

 

lazerwalker_3-1617120105448.png

 

lazerwalker_4-1617120105451.png

 

If you were to open this in VR now, it would look more like a diorama than a lush living world. Let’s make it bigger to match the scale of a person in VR! In the Transform section at the top of the inspector, set it’s the scale to (20, 20, 20). Down in the Map Layout section of the Map Renderer tab, set the Local Map Dimensions to (3, 3). This should make the map much larger and more to-scale with a human-sized VR viewer. 

 

One final piece to the puzzle is moving the camera position. Since we’ve grown the map to be as tall as 20 units tall, but the camera is still down at the original starting point, trying to play this in VR would spawn you in the middle of the ground! Select the Main Camera object from the scene inspector, and set the camera position to (0, 12, 0). This should get you a good vantage point of the scene. 

 

Give it a try! 

Click the “Play” button in the top middle of the Unity editor now and see a fully-rendered 3D map of the location you’ve entered! If you have a VR headset connected to your computer, it should automatically render to the VR headset as well, including using head tracking to control the camera and let you look around. 

 

lazerwalker_5-1617120105454.png

 

Moving Around 

It’s so cool that with such little work we’re already able to drop ourselves into a 3D rendering of the actual world! But we can take this further: let’s add some movement! As mentioned, I’m not particularly into detailed simulations, so for now let’s just add some super simple movement code. Hold down a button on your VR controller to move at a reasonable speed in whatever direction you are currently looking. 

 

With our custom GameObject open in the inspector, click “Add Component”, then “New script”, and call it CameraMovement. Double-click the C# script name in the inspector to open it in your text editor of choice, then paste in the following code. Don’t worry, I’ll walk you through it in a moment! 

 

using UnityEngine; 
using Microsoft.Geospatial; 
using Microsoft.Maps.Unity; 

public class CameraMovement : MonoBehaviour 
{ 
    void Update() 
    { 
        if (Input.GetButton("Fire1")) { 
            var heading = Camera.main.transform.eulerAngles.y * Mathf.Deg2Rad; 
            var speed = 0.00015f; 

            var map = GetComponent<MapRenderer>(); 
            map.Center = new LatLon( 
                map.Center.LatitudeInDegrees + speed * Mathf.Cos(heading), 
                map.Center.LongitudeInDegrees + speed * Mathf.Sin(heading) 
            );
        } 
    } 
} 

 

Let’s walk through this! 

 

if (Input.GetButton("Fire1")) { 

 

Every time the Update function is called (for a VR project, usually 72 or 90 times per second), we’re going to check if the player has pressed the “Fire1” button on their VR controller. On most VR headsets, the Mixed Reality Toolkit maps this to a prominent “main” button on the face of the controller (and not a trigger). If the player is pressing the button on this frame, we will want to move the player!

 

var heading = Camera.main.transform.eulerAngles.y * Mathf.Deg2Rad; 

 

This fetches the current y-axis value of the main camera, which is being controlled by the VR headset. The y axis is the equivalent of turning your head left or right. This means we won’t let players move up or down, but that’s fine for our simple use case. The Mathf.Deg2Rad multiplication is just to get that value as a radian instead of degrees since that’s what our trigonometry functions will take a few lines down. 

 

var speed = 0.00015f; 

 

How many world units players will move per frame they’re moving. I hand-tuned this value based on testing. Feel free to make yourself faster or slower! 

 

var map = GetComponent<MapRenderer>(); 

 

Grabs a reference to the MapRenderer map object off of our GameObject. 

 

map.Center = new LatLon(
    map.Center.LatitudeInDegrees + speed * Mathf.Cos(heading), 
    map.Center.LongitudeInDegrees + speed * Mathf.Sin(heading) 
);

 

This may look complicated if you’re rusty on basic trigonometry, but it’s not that bad!  

 

The first thing to understand is a classic videogame movement trick: we’re not moving the player, we’re moving the entire world around the player like they’re on a treadmill. We’re neither moving the camera nor the position of the map view in the scene; we’re simply shifting what latitude and longitude coordinates are at the center of the map, which will result in the map moving (slightly, since we’re dealing with very small changes) and a perception that the player is moving. 

 

Given our goal of moving 0.00015 units per frame, multiplying that speed by the sin and cosine of the angle ensures that, while we shift both the latitude and the longitude, we change them proportionately so that the total distance moved is exactly 0.0015. 

If you click “Play” and put on your VR headset, it all should work now! Pressing the “fire1” button on your controllers (it may take some experimentation to find it) should move you in the direction you’re looking. 

 

lazerwalker_6-1617120105458.png

 

I don’t know about you, but I’m pretty blown away at how impressive this looks after such relatively little work! You could imagine expanding this to include more complex controls, real flight physics, goals and objectives, or whatever else you want, but for now this is a heck of a demo for such a short period of time and such little code. 

 

Building more weird things with real-world data 

Separate from how much I love this specific experience of being able to soar around my favorite cities of the world, this also points towards one of my favorite ways to build games and mixed reality experiences: on top of already-existing real-world data sources. 

 

If you’re working by yourself, building a 3D scale model of whatever city you live in is likely prohibitively expensive. The thought of building a game where you can fly around an entire city-scale map, let alone a world-scale one, is extremely time-consuming and requires a whole bunch of disparate skillsets, even if you’re taking advantage of resources such as existing Unity Asset Store assets. In this case, finding something like the Bing Maps SDK that provides you with a 3D scene makes it categorically possible for you to make projects and experiences that wouldn’t be possible otherwise. 

1 Comment
Visitor

I wonder if it's possible to submit photos for their 3D library?

tada !

Libraries for Microsoft Flight Simulator

%3CLINGO-SUB%20id%3D%22lingo-sub-2245284%22%20slang%3D%22en-US%22%3EExploring%20the%20Whole%20World%20in%26nbsp%3BVR%26nbsp%3Bwith%20Bing%26nbsp%3B3D%26nbsp%3BMaps%20and%20MRTK%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2245284%22%20slang%3D%22en-US%22%3E%3CP%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhen%20%3CA%20href%3D%22https%3A%2F%2Fflightsimulator.com%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%20target%3D%22_blank%22%3EMicrosoft%20Flight%26nbsp%3BSimulator%3C%2FA%3E%26nbsp%3Bcame%20out%20a%20year%20ago%2C%20I%20was%20completely%20entranced%20by%20it.%26nbsp%3BI%E2%80%99m%26nbsp%3Bnot%20particularly%20into%20hyper-accurate%20flight%20simulation%20games%2C%20but%20the%20concept%20of%20a%20game%20where%20you%20can%20go%26nbsp%3Banywhere%26nbsp%3Bin%20the%20real%26nbsp%3Bworld%20and%26nbsp%3Bsee%26nbsp%3Ban%20accurate%26nbsp%3B3D%20representation%20of%20that%20real-world%20space%2C%26nbsp%3Bblew%26nbsp%3Bmy%20mind.%20What%20a%20concept!%26nbsp%3BThat%26nbsp%3Balso%20got%20me%20thinking%3A%20could%20I%20make%20something%20like%20that%20myself%3F%26nbsp%3BAs%20a%20solo%20creator%20I%26nbsp%3Bdon%E2%80%99t%26nbsp%3Bhave%20the%20benefit%20of%20a%20team%20of%20many%26nbsp%3Bengineers%20working%20with%20map%20data%20for%26nbsp%3Byears.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHow%20Flight%20Simulator%E2%80%99s%20World%20Works%26nbsp%3B%3C%2FP%3E%3CP%3EMicrosoft%26nbsp%3BFlight%20Simulator%26nbsp%3Buses%26nbsp%3Bthree%26nbsp%3Bdistinct%26nbsp%3Btypes%26nbsp%3Bof%20world%20maps%3A%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3EAzure%203D%20Maps.%20Flight%20Simulator%20uses%20high-quality%203D%26nbsp%3Bphotogrammetry%26nbsp%3Bdata%20when%20it%20exists%20in%20Microsoft%E2%80%99s%20library.%26nbsp%3BIt%E2%80%99s%26nbsp%3Bpretty%20easy%26nbsp%3Bto%26nbsp%3Bidentify%26nbsp%3Bthese%20areas%3A%20there%20are%26nbsp%3Brelatively%20high-quality%26nbsp%3Btextures%2C%20but%20often%26nbsp%3Bbuildings%26nbsp%3Band%26nbsp%3Blandmarks%20like%20trees%20look%20a%20bit%20worn%20down%20as%20the%203D%20models%26nbsp%3Baren%E2%80%99t%26nbsp%3Bperfect.%26nbsp%3B%20AI-Augmented%26nbsp%3B2D%26nbsp%3BSatellite%20Maps.%26nbsp%3BFor%20areas%20where%20no%20good%203D%20map%20data%20is%20available%2C%20Flight%20Simulator%20takes%202D%20satellite%20map%20footage%26nbsp%3Binstead.%20Deep%20learning%20techniques%26nbsp%3Bare%20applied%26nbsp%3Bto%26nbsp%3Binfer%26nbsp%3Bwhat%20sort%20of%203D%20buildings%20and%20landmarks%20might%20exist%2C%20and%20then%20a%20library%20of%20hand-authored%203D%20models%26nbsp%3Bare%26nbsp%3Bprogrammatically%20placed%20in%20the%20world.%20If%26nbsp%3Byou%E2%80%99re%26nbsp%3Bvisiting%20a%26nbsp%3Brelatively%20non-descript%20location%20in%20Flight%20Sim%20where%20buildings%26nbsp%3Blook%20pristine%20and%20high-quality%2C%26nbsp%3Byou%E2%80%99re%26nbsp%3Bprobably%20in%26nbsp%3Ban%20AI-augmented%26nbsp%3Bzone.%26nbsp%3B%20Hand-authored%20content.%26nbsp%3BA%20lot%20of%26nbsp%3BFlight%26nbsp%3BSimulator%26nbsp%3Bareas%26nbsp%3Bare%20modeled%26nbsp%3Bby%20hand%20to%20resemble%20their%20real-world%20locations.%20Notable%26nbsp%3Bexamples%26nbsp%3Binclude%26nbsp%3Bthe%26nbsp%3Bhand-modeled%26nbsp%3Bairports%26nbsp%3Bin%20the%26nbsp%3BDeluxe%20and%20Premium%20Deluxe%20editions%20of%20the%20game.%26nbsp%3B%3CP%3EThe%20second%20and%20third%20types%20of%20map%20data%20require%20an%20exorbitant%20amount%20of%20work%2C%20either%20designing%20procedural%20generation%20algorithms%20or%20doing%203D%20modeling%20and%20level%20design%20by%20hand.%20But%20the%20first%26nbsp%3Bone%20is%26nbsp%3Bdoable!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESpecifically%2C%20the%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fmicrosoft%2FMapsSDK-Unity%22%20rel%3D%22noopener%20noreferrer%22%20target%3D%22_blank%22%3EBing%20Maps%26nbsp%3BSDK%3C%2FA%3E%20gives%20us%20exactly%20what%20we%20want%3A%20a%20drop-in%20Unity%26nbsp%3Bcomponent%26nbsp%3Bthat%2C%20given%20a%20location%20(via%26nbsp%3Blatitude%2Flongitude%20coordinate%20pair)%20will%26nbsp%3Brender%26nbsp%3Ba%203D%20scene%26nbsp%3Bcontaining%26nbsp%3Bthat%20real-world%20location%2C%20powered%20by%20the%20same%203D%20maps%20data%20that%20Flight%26nbsp%3BSimulator%26nbsp%3Buses.%26nbsp%3BThat%E2%80%99s%26nbsp%3Bso%20cool!%26nbsp%3BI'm%26nbsp%3Bgoing%20to%20spend%20the%26nbsp%3Bremainder%26nbsp%3Bof%20this%20blog%20post%20walking%20you%20through%20setting%20up%20a%26nbsp%3Bpretty%20simple%26nbsp%3BVR%20%E2%80%9Cflight%20sim%E2%80%9D%20example%20that%20will%20let%20you%20move%20around%20a%203D%20representation%20of%20any%20location%20in%20the%20world!%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%E2%80%99m%26nbsp%3Bgoing%20to%20assume%20you%20have%20basic%20familiarity%20with%20Unity%26nbsp%3Bto%20configure%26nbsp%3Ba%20few%26nbsp%3BGameObject%26nbsp%3Bcomponents%20and%26nbsp%3Bwrite%26nbsp%3Ba%20small%26nbsp%3Bamount%20of%20custom%20code.%26nbsp%3BIf%26nbsp%3Bthis%26nbsp%3Bis%20more%26nbsp%3Bthan%26nbsp%3Byou%E2%80%99re%26nbsp%3Bcomfortable%26nbsp%3Bwith%2C%26nbsp%3B%26nbsp%3Bplease%26nbsp%3Bcheck%20out%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fvisualstudio%2Fmac%2Funity-walkthrough%3FWT.mc_id%3Dspatial-19824-emwalker%22%20rel%3D%22noopener%20noreferrer%22%20target%3D%22_blank%22%3Eother%20tutorials%3C%2FA%3E%26nbsp%3Bto%20familiarize%20yourself%20with%20Unity%E2%80%99s%26nbsp%3BUI.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EGet%20a%20Bing%20Maps%26nbsp%3BAPI%26nbsp%3BKey%26nbsp%3B%3C%2FP%3E%3CP%3ETo%20use%20this%20API%2C%20you%20need%20an%20API%20key.%26nbsp%3BSign%20in%20or%20sign%20up%20at%20the%20%3CA%20href%3D%22https%3A%2F%2Fwww.bingmapsportal.com%2F%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%20target%3D%22_blank%22%3EBing%20Maps%26nbsp%3BPortal%3C%2FA%3E.%20Once%26nbsp%3Byou%E2%80%99re%26nbsp%3Blogged%20in%2C%20create%20a%20new%20API%20key%20by%20going%20to%20My%20account%20-%26gt%3B%20My%20Keys%20-%26gt%3B%26nbsp%3BCreate%20a%20new%20key.%20Name%20it%20whatever%20you%20want%2C%20choose%20a%20%E2%80%9CBasic%E2%80%9D%20key%2C%20and%20for%20now%20select%20%E2%80%9CDev%2FTest%E2%80%9D%20as%20the%20application%26nbsp%3Btype.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAfter%20creating%20a%20key%2C%20keep%20this%20page%26nbsp%3Bhandy%2C%20as%26nbsp%3Byou%E2%80%99ll%26nbsp%3Bneed%26nbsp%3Bthe%26nbsp%3Bkey%20in%20a%20few%20minutes.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDownload%20the%20Bing%20Maps%26nbsp%3BSDK%26nbsp%3Band%20open%20the%20sample%26nbsp%3Bproject%26nbsp%3B%3C%2FP%3E%3CP%3EFor%20now%2C%26nbsp%3Bwe%E2%80%99re%26nbsp%3Bgoing%20to%20use%20the%20sample%20starter%20project%20included%20with%20the%20Bing%20Maps%20SDK%2C%20although%20there%20are%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fmicrosoft%2FMapsSDK-Unity%2Fwiki%2FGetting-Started%22%20rel%3D%22noopener%20noreferrer%22%20target%3D%22_blank%22%3Einstructions%26nbsp%3B%3C%2FA%3Eif%26nbsp%3Byou%20would%20prefer%20to%20integrate%20the%20SDK%20manually%20with%20an%20existing%20or%20new%20project%20using%20the%20Unity%20Package%20Manager.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EGrab%20the%200.10.0%20release%20of%20the%20SDK%20from%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fmicrosoft%2FMapsSDK-Unity%2Freleases%2Ftag%2F0.10.0%22%20rel%3D%22noopener%20noreferrer%22%20target%3D%22_blank%22%3Ethis%26nbsp%3Bpage%3C%2FA%3E%26nbsp%3Band%20unzip%20it.%20In%20Unity%20Hub%2C%20click%20%E2%80%9CAdd%E2%80%9D%2C%26nbsp%3Bthen%20locate%20this%20folder%20and%20select%20the%20%E2%80%9CSampleProject%E2%80%9D%20folder.%20If%20you%26nbsp%3Bdon%E2%80%99t%26nbsp%3Bhave%20a%20Unity%202019.4%20release%2C%20Unity%20Hub%20may%20prompt%20you%20to%26nbsp%3Bdownload%20and%20install%20it.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECreating%20our%20scene%26nbsp%3B%3C%2FP%3E%3CP%3EStart%26nbsp%3Bby%20adding%20a%26nbsp%3Bnew%20scene%20by%20selecting%20File%20-%26gt%3B%20New%20Scene.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAdd%20a%20new%20empty%26nbsp%3BGameObject%26nbsp%3Bto%26nbsp%3Byour%26nbsp%3Bscene%26nbsp%3Bby%20right-clicking%20anywhere%20within%20the%20Hierarchy%20window%20and%20selecting%20%E2%80%9CCreate%20Empty%E2%80%9D.%26nbsp%3BI%20called%20mine%20%E2%80%9CMapManager%E2%80%9D%20(since%26nbsp%3Bit%E2%80%99s%26nbsp%3Bgoing%20to%20manage%20our%203D%20map%26nbsp%3Bcomponent).%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIn%20the%20right-side%20inspector%20pane%20for%20this%20new%26nbsp%3BGameObject%2C%20click%20the%20%E2%80%9CAdd%20Component%E2%80%9D%20button%20and%20search%20for%20the%20%E2%80%9CMapRenderer%E2%80%9D%26nbsp%3Bcomponent.%20This%20is%20a%20Unity%26nbsp%3Bcomponent%26nbsp%3Bprovided%20by%20the%20Bing%20Maps%20SDK%20for%20Unity%20that%20will%20automatically%26nbsp%3Brender%26nbsp%3Bour%203D%20map%20in%20our%20scene!%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIn%20the%26nbsp%3BGameObject%26nbsp%3Binspector%20pane%2C%20enter%20your%20API%20key%20from%20the%20Bing%20Maps%20Portal%20earlier%26nbsp%3Binto%20the%20Developer%20Key%20field%20in%20the%20Map%20Session%20pane.%26nbsp%3BAdd%20in%20the%26nbsp%3Blat%2Flong%20coordinates%20for%26nbsp%3Bthe%26nbsp%3Blocation%26nbsp%3Bwhere%26nbsp%3Byou%20want%20players%20to%20start%20their%20adventure%26nbsp%3Binto%20the%20%E2%80%9CLocation%E2%80%9D%20fields%20at%20the%20top%20of%20Map%20Renderer.%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%26nbsp%3Bset%20mine%26nbsp%3Bto%26nbsp%3Ba%20latitude%20of%2037.7958421%26nbsp%3Band%20a%20longitude%20of%20-122.3959688%2C%26nbsp%3Bwhich%20is%20the%20Ferry%20Building%20in%20downtown%20San%20Francisco.%20I%20found%20this%20by%20%3CA%20href%3D%22https%3A%2F%2Fwww.bing.com%2Fmaps%3Fosid%3Dba186378-178a-4318-9f3a-42d82ac0a49c%26amp%3Bcp%3D37.796013~-122.398731%26amp%3Blvl%3D16%26amp%3Bimgid%3D68536573-2565-4140-9be1-b0297ced0aa6%26amp%3Bv%3D2%26amp%3BsV%3D2%26amp%3Bform%3DS00027%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%20target%3D%22_blank%22%3Elooking%20up%20the%20Ferry%20Building%20in%20Bing%26nbsp%3BMaps%3C%2FA%3E%26nbsp%3Band%26nbsp%3Bright-clicking%26nbsp%3Bthe%20Ferry%20Building.%20If%26nbsp%3Bthere%E2%80%99s%26nbsp%3Ba%20location%26nbsp%3Byou'd%26nbsp%3Brather%20explore%20in%203D%2C%20you%20can%20get%26nbsp%3Bits%26nbsp%3Blat%2Flong%20coordinates%26nbsp%3Bthe%20same%20way%20and%20enter%20them%20instead.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENow%20you%20will%26nbsp%3Bsee%20a%20map%20of%20the%20world%20in%20your%20Unity%20scene!%20If%20you%20adjust%20the%20%E2%80%9CZoom%20Level%E2%80%9D%20slider%20below%20latitude%20and%20longitude%2C%20you%20can%20see%20it%20zoom%20in%20on%20San%20Francisco%26nbsp%3B(or%20your%26nbsp%3Bchosen%26nbsp%3Blocation).%26nbsp%3BI%E2%80%99ve%26nbsp%3Bfound%20that%20setting%20the%20zoom%20to%2019%20is%20a%20good%20fit%20for%20our%26nbsp%3Buse%20case.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20you%20were%20to%20open%20this%20in%20VR%20now%2C%20it%20would%20look%20more%20like%20a%20diorama%20than%20a%20lush%20living%20world.%26nbsp%3BLet%E2%80%99s%26nbsp%3Bmake%20it%20bigger%20to%20match%20the%20scale%20of%20a%20person%20in%20VR!%20In%26nbsp%3Bthe%20Transform%20section%20at%20the%20top%20of%20the%20inspector%2C%20set%26nbsp%3Bit%E2%80%99s%26nbsp%3Bthe%26nbsp%3Bscale%20to%20(20%2C%2020%2C%2020).%20Down%20in%20the%20Map%20Layout%20section%20of%20the%20Map%20Renderer%20tab%2C%20set%20the%20Local%20Map%20Dimensions%20to%20(3%2C%203).%20This%20should%20make%20the%20map%26nbsp%3Bmuch%20larger%20and%26nbsp%3Bmore%20to-scale%20with%20a%20human-sized%20VR%20viewer.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EOne%20final%20piece%20to%20the%20puzzle%20is%20moving%20the%20camera%20position.%20Since%26nbsp%3Bwe%E2%80%99ve%26nbsp%3Bgrown%20the%20map%20to%20be%20as%20tall%20as%2020%20units%20tall%2C%20but%20the%20camera%20is%20still%20down%20at%20the%20original%20starting%20point%2C%20trying%20to%20play%20this%20in%20VR%20would%20spawn%20you%20in%20the%20middle%20of%20the%20ground!%20Select%20the%20Main%20Camera%20object%20from%20the%20scene%20inspector%2C%20and%20set%20the%20camera%20position%20to%20(0%2C%2012%2C%200).%20This%20should%20get%20you%20a%20good%20vantage%20point%20of%20the%20scene.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EGive%20it%20a%20try!%26nbsp%3B%3C%2FP%3E%3CP%3EClick%20the%20%E2%80%9CPlay%E2%80%9D%20button%20in%20the%20top%20middle%20of%20the%20Unity%20editor%20now%26nbsp%3Band%26nbsp%3Bsee%20a%26nbsp%3Bfully-rendered%26nbsp%3B3D%20map%20of%20the%20location%26nbsp%3Byou%E2%80%99ve%26nbsp%3Bentered!%26nbsp%3BIf%20you%20have%20a%20VR%20headset%20connected%20to%20your%20computer%2C%20it%20should%20automatically%26nbsp%3Brender%26nbsp%3Bto%26nbsp%3Bthe%20VR%20headset%20as%20well%2C%20including%20using%20head%20tracking%20to%20control%20the%20camera%20and%20let%20you%20look%20around.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EMoving%20Around%26nbsp%3B%3C%2FP%3E%3CP%3EIt%E2%80%99s%26nbsp%3Bso%20cool%20that%20with%20such%20little%20work%26nbsp%3Bwe%E2%80%99re%26nbsp%3Balready%20able%20to%20drop%20ourselves%20into%20a%203D%26nbsp%3Brendering%26nbsp%3Bof%20the%20actual%20world!%26nbsp%3BBut%20we%20can%20take%20this%26nbsp%3Bfurther%3A%26nbsp%3Blet%E2%80%99s%26nbsp%3Badd%26nbsp%3Bsome%20movement!%26nbsp%3BAs%20mentioned%2C%26nbsp%3BI%E2%80%99m%26nbsp%3Bnot%20particularly%20into%20detailed%20simulations%2C%20so%20for%26nbsp%3Bnow%26nbsp%3Blet%E2%80%99s%26nbsp%3Bjust%20add%20some%20super%20simple%20movement%20code.%26nbsp%3BHold%20down%20a%20button%20on%20your%20VR%26nbsp%3Bcontroller%26nbsp%3Bto%26nbsp%3Bmove%20at%20a%20reasonable%20speed%20in%20whatever%20direction%20you%20are%20currently%26nbsp%3Blooking.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWith%20our%20custom%26nbsp%3BGameObject%26nbsp%3Bopen%20in%20the%20inspector%2C%20click%20%E2%80%9CAdd%20Component%E2%80%9D%2C%26nbsp%3Bthen%20%E2%80%9CNew%20script%E2%80%9D%2C%26nbsp%3Band%20call%20it%26nbsp%3BCameraMovement.%20Double-click%20the%20C%23%20script%20name%20in%20the%20inspector%20to%20open%20it%20in%20your%20text%20editor%20of%20choice%2C%20then%20paste%20in%20the%20following%20code.%26nbsp%3BDon%E2%80%99t%26nbsp%3Bworry%2C%26nbsp%3BI%E2%80%99ll%26nbsp%3Bwalk%20you%20through%20it%20in%20a%20moment!%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3Eusing%20UnityEngine%3B%20using%20Microsoft.Geospatial%3B%20using%20Microsoft.Maps.Unity%3B%20public%20class%20CameraMovement%20%3A%20MonoBehaviour%20%7B%20void%20Update()%20%7B%20if%20(Input.GetButton(%22Fire1%22))%20%7B%20var%20heading%20%3D%20Camera.main.transform.eulerAngles.y%20*%20Mathf.Deg2Rad%3B%20var%20speed%20%3D%200.00015f%3B%20var%20map%20%3D%20GetComponent%3CMAPRENDERER%3E()%3B%20map.Center%20%3D%20new%20LatLon(%20map.Center.LatitudeInDegrees%20%2B%20speed%20*%20Mathf.Cos(heading)%2C%20map.Center.LongitudeInDegrees%20%2B%20speed%20*%20Mathf.Sin(heading)%20)%3B%20%7D%20%7D%20%7D%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ELet%E2%80%99s%26nbsp%3Bwalk%20through%20this!%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3Eif%20(Input.GetButton(%22Fire1%22))%20%7B%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EEvery%20time%20the%20Update%20function%20is%20called%20(for%20a%20VR%20project%2C%20usually%2072%20or%2090%20times%20per%20second)%2C%26nbsp%3Bwe%E2%80%99re%26nbsp%3Bgoing%20to%20check%20if%20the%20player%20has%20pressed%20the%20%E2%80%9CFire1%E2%80%9D%20button%20on%20their%20VR%20controller.%20On%20most%20VR%20headsets%2C%20the%20%3CA%20href%3D%22https%3A%2F%2Faka.ms%2FLearn-MRTK-Unity%22%20rel%3D%22noopener%20noreferrer%22%20target%3D%22_blank%22%3EMixed%20Reality%26nbsp%3BToolkit%3C%2FA%3E%3CA%20href%3D%22https%3A%2F%2Faka.ms%2FLearn-MRTK-Unity%22%20rel%3D%22noopener%20noreferrer%22%20target%3D%22_blank%22%3E%26nbsp%3B%3C%2FA%3Emaps%20this%20to%20a%20prominent%20%E2%80%9Cmain%E2%80%9D%20button%20on%20the%20face%20of%20the%20controller%20(and%20not%20a%20trigger).%26nbsp%3BIf%20the%20player%20is%20pressing%20the%20button%26nbsp%3Bon%20this%26nbsp%3Bframe%2C%20we%26nbsp%3Bwill%26nbsp%3Bwant%20to%20move%20the%20player!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3Evar%20heading%20%3D%20Camera.main.transform.eulerAngles.y%20*%20Mathf.Deg2Rad%3B%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20fetches%20the%20current%20y-axis%20value%20of%20the%20main%20camera%2C%20which%20is%20being%20controlled%20by%20the%20VR%20headset.%26nbsp%3BThe%20y%20axis%20is%20the%20equivalent%20of%20turning%20your%20head%20left%20or%20right.%20This%20means%20we%26nbsp%3Bwon%E2%80%99t%26nbsp%3Blet%20players%20move%20up%20or%20down%2C%20but%26nbsp%3Bthat%E2%80%99s%26nbsp%3Bfine%20for%20our%20simple%20use%20case.%20The%20Mathf.Deg2Rad%20multiplication%20is%20just%20to%20get%20that%20value%20as%20a%20radian%20instead%20of%26nbsp%3Bdegrees%20since%26nbsp%3Bthat%E2%80%99s%26nbsp%3Bwhat%20our%20trigonometry%20functions%20will%20take%20a%20few%20lines%20down.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3Evar%20speed%20%3D%200.00015f%3B%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHow%20many%20world%26nbsp%3Bunits%26nbsp%3Bplayers%20will%20move%20per%20frame%26nbsp%3Bthey%E2%80%99re%26nbsp%3Bmoving.%26nbsp%3BI%20hand-tuned%20this%20value%26nbsp%3Bbased%20on%20testing.%26nbsp%3BFeel%26nbsp%3Bfree%20to%20make%20yourself%20faster%20or%20slower!%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3Evar%20map%20%3D%20GetComponent%3CMAPRENDERER%3E()%3B%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EGrabs%20a%20reference%20to%26nbsp%3Bthe%26nbsp%3BMapRenderer%26nbsp%3Bmap%20object%26nbsp%3Boff%20of%26nbsp%3Bour%26nbsp%3BGameObject.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3Emap.Center%20%3D%20new%20LatLon(%20map.Center.LatitudeInDegrees%20%2B%20speed%20*%20Mathf.Cos(heading)%2C%20map.Center.LongitudeInDegrees%20%2B%20speed%20*%20Mathf.Sin(heading)%20)%3B%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20may%20look%20complicated%20if%26nbsp%3Byou%E2%80%99re%26nbsp%3Brusty%20on%20basic%20trigonometry%2C%20but%26nbsp%3Bit%E2%80%99s%26nbsp%3Bnot%20that%20bad!%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20first%20thing%20to%20understand%20is%20a%20classic%20videogame%20movement%20trick%3A%26nbsp%3Bwe%E2%80%99re%26nbsp%3Bnot%20moving%20the%26nbsp%3Bplayer%2C%26nbsp%3Bwe%E2%80%99re%26nbsp%3Bmoving%20the%20entire%20world%20around%20the%20player%20like%26nbsp%3Bthey%E2%80%99re%26nbsp%3Bon%20a%20treadmill.%26nbsp%3BWe%E2%80%99re%26nbsp%3Bneither%20moving%20the%20camera%20nor%20the%20position%20of%20the%20map%20view%20in%20the%20scene%3B%26nbsp%3Bwe%E2%80%99re%26nbsp%3Bsimply%20shifting%20what%20latitude%20and%20longitude%20coordinates%20are%20at%20the%20center%20of%20the%20map%2C%20which%20will%20result%20in%20the%20map%20moving%20(slightly%2C%20since%26nbsp%3Bwe%E2%80%99re%26nbsp%3Bdealing%20with%26nbsp%3Bvery%20small%26nbsp%3Bchanges)%20and%20a%26nbsp%3Bperception%26nbsp%3Bthat%20the%20player%20is%20moving.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EGiven%20our%20goal%20of%20moving%200.00015%20units%20per%20frame%2C%20multiplying%20that%20speed%20by%20the%20sin%20and%20cosine%20of%20the%20angle%26nbsp%3Bensures%20that%2C%20while%20we%20shift%20both%20the%20latitude%20and%20the%20longitude%2C%20we%20change%20them%20proportionately%20so%20that%20the%20total%20distance%20moved%20is%20exactly%200.0015.%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20you%20click%20%E2%80%9CPlay%E2%80%9D%20and%20put%20on%20your%20VR%20headset%2C%20it%20all%20should%20work%20now!%20Pressing%20the%20%E2%80%9Cfire1%E2%80%9D%20button%20on%20your%20controllers%20(it%20may%20take%20some%20experimentation%20to%20find%20it)%20should%20move%20you%20in%20the%20direction%26nbsp%3Byou%E2%80%99re%26nbsp%3Blooking.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%26nbsp%3Bdon%E2%80%99t%26nbsp%3Bknow%20about%20you%2C%20but%26nbsp%3BI%E2%80%99m%26nbsp%3Bpretty%20blown%20away%26nbsp%3Bat%20how%20impressive%20this%20looks%20after%20such%26nbsp%3Brelatively%20little%26nbsp%3Bwork!%20You%20could%20imagine%20expanding%20this%20to%20include%20more%20complex%20controls%2C%20real%20flight%20physics%2C%26nbsp%3Bgoals%26nbsp%3Band%26nbsp%3Bobjectives%2C%20or%20whatever%20else%20you%20want%2C%20but%26nbsp%3Bfor%20now%20this%20is%20a%20heck%20of%20a%20demo%20for%20such%26nbsp%3Ba%20short%20period%26nbsp%3Bof%20time%20and%20such%20little%20code.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBuilding%20more%20weird%20things%20with%20real-world%20data%26nbsp%3B%3C%2FP%3E%3CP%3ESeparate%20from%20how%20much%20I%20love%20this%20specific%20experience%20of%20being%20able%20to%20soar%20around%20my%20favorite%20cities%20of%20the%20world%2C%20this%20also%20points%20towards%20one%20of%20my%20favorite%20ways%20to%20build%20games%20and%20mixed%20reality%20experiences%3A%20on%20top%20of%20already-existing%20real-world%20data%20sources.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%26nbsp%3Byou%E2%80%99re%26nbsp%3Bworking%20by%20yourself%2C%20building%20a%203D%20scale%20model%20of%20whatever%20city%20you%20live%20in%20is%26nbsp%3Blikely%26nbsp%3Bprohibitively%26nbsp%3Bexpensive.%20The%20thought%20of%20building%20a%20game%20where%20you%20can%20fly%20around%20an%20entire%20city-scale%20map%2C%20let%20alone%20a%20world-scale%20one%2C%20is%20extremely%20time-consuming%20and%20requires%26nbsp%3Ba%20whole%20bunch%20of%20disparate%20skillsets%2C%20even%20if%26nbsp%3Byou%E2%80%99re%26nbsp%3Btaking%20advantage%20of%20resources%20such%20as%20existing%20Unity%20Asset%20Store%20assets.%20In%20this%20case%2C%20finding%20something%20like%20the%20Bing%20Maps%20SDK%20that%26nbsp%3Bprovides%26nbsp%3Byou%26nbsp%3Bwith%20a%26nbsp%3B3D%26nbsp%3Bscene%26nbsp%3Bmakes%20it%20categorically%20possible%20for%20you%20to%20make%20projects%20and%20experiences%20that%26nbsp%3Bwouldn%E2%80%99t%26nbsp%3Bbe%20possible%20otherwise.%26nbsp%3B%3C%2FP%3E%3C%2FMAPRENDERER%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-2245284%22%20slang%3D%22en-US%22%3E%3CP%3E%3C%2FP%3E%3CP%3EWhen%20%3CA%20href%3D%22https%3A%2F%2Fflightsimulator.com%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%20target%3D%22_blank%22%3EMicrosoft%20Flight%26nbsp%3BSimulator%3C%2FA%3E%26nbsp%3Bcame%20out%20a%20year%20ago%20and%20let%20players%20fly%20all%20over%20a%20realistic%203D%20representation%20of%20the%20whole%20world%2C%20I%20had%20one%20question%3A%20could%20I%20build%20that%20myself%20in%20Unity%3F%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3C%2FMAPRENDERER%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2719297%22%20slang%3D%22en-US%22%3ERe%3A%20Exploring%20the%20Whole%20World%20in%26nbsp%3BVR%26nbsp%3Bwith%20Bing%26nbsp%3B3D%26nbsp%3BMaps%20and%20MRTK%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2719297%22%20slang%3D%22en-US%22%3E%3CP%3EI%20wonder%20if%20it's%20possible%20to%20submit%20photos%20for%20their%203D%20library%3F%3C%2FP%3E%3CP%3Etada%20!%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fflightsim.to%2Fc%2Fscenery%2Flibraries%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3ELibraries%20for%20Microsoft%20Flight%20Simulator%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Co-Authors
Version history
Last update:
‎Mar 31 2021 10:38 AM
Updated by: