Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mapbox VR scale is wrong #1378

Open
asmccormick opened this issue May 28, 2019 · 7 comments · May be fixed by #1384
Open

Mapbox VR scale is wrong #1378

asmccormick opened this issue May 28, 2019 · 7 comments · May be fixed by #1384

Comments

@asmccormick
Copy link

asmccormick commented May 28, 2019

I'm trying to use Mapbox to create an accurate environment for VR. For example, if a real skyscraper in NYC is 100m tall in real life, it should be 100m tall in VR. I've tried adjusting the zoom scale and changing World Scale to Custom, but no combination of these will make the buildings appear at their actual height.

In hopes of finding a Mapbox-to-Realworld ratio, I gathered some data points.
building heights and ratios

Here's my methodology:

  • Zoom = 16.
  • Scaling Options = Custom, 100 Unity Tile Size. (Using World Scale created the unusable ratios in column G.)
  • Elevation layer type = Flat Terrain. (Using terrain with elevation made the buildings harder to measure, and it also changed their heights slightly.)
  • In the Unity Editor, focus on the building game object (F key), spawn a cube at its center, then increase the cube's y-scale until it was the same height as the building. This height is recorded in column H.
  • Divide building height in meters by it's height in Unity units. This is the ratio in column I.

From these 7 data points, I calculated an average ratio of 6.12. However, I'm not terribly confident in the accuracy of my method, nor do I have any idea what this number signifies.

Does anyone know what 6.12 refers to?
Does anyone know of a precise zoom level where Mapbox creates buildings to their actual scale?

  • Unity version: 2018.3.0f2
  • Mapbox SDK version: 2.0.0
  • The platform you're building to: PC
@brnkhy
Copy link
Contributor

brnkhy commented Jun 3, 2019

Hey @asmccormick!
Great catch and thanks for all detailed data.
height precision isn't easy and to be honest not something used/required regularly so I guess we looked over this.
After a quick inspection, I think I found something.

if you check MapScalingAtWorldScaleStrategy class you'll find this method.
original

public void SetUpScaling(AbstractMap map)
{
	var scaleFactor = Mathf.Pow(2, (map.AbsoluteZoom - map.InitialZoom));
	map.SetWorldRelativeScale(scaleFactor * Mathf.Cos(Mathf.Deg2Rad * (float)map.CenterLatitudeLongitude.x));
}

That calculation starting with mathf.Cos is actually a fix for the latitude in fixed size tiles since forcing tiles down to a certain size is a little more complex in y axis because of the mercator tile sizes (they shrink as latitude goes higher/lower).
But that is unnecessary in world scale because in that mode we're not scaling tiles at all! So I just commented that out and tested with Columbia Tower which came very close to 286m using the exact method you used.

changed

public void SetUpScaling(AbstractMap map)
{
	var scaleFactor = Mathf.Pow(2, (map.AbsoluteZoom - map.InitialZoom));
	map.SetWorldRelativeScale(scaleFactor); 
}

So if you can change that line as shown above and test in WorldScale mode again, I think you will get much better results. I'm not creating a branch and PR as I would like to test a little bit more myself as well, but theoretically it makes sense and we probably just looked over the fact that it's not necessary in world scale mode while writing that class.

Hope that helps!

@brnkhy brnkhy linked a pull request Jun 3, 2019 that will close this issue
@brnkhy
Copy link
Contributor

brnkhy commented Jun 3, 2019

I couldn't resists so I created a PR; #1384
we can test and talk there as well anyway. Thanks a lot again @asmccormick

@abhishektrip
Copy link
Contributor

@asmccormick Thanks for the detailed analysis and awesome ticket 🎉

@asmccormick
Copy link
Author

asmccormick commented Oct 16, 2019

Simplest fix is to set AbstractMap to World Scale and zoom to 16x. Then set y-scale of map gameObject like this

void FixMapScale ()
    {
        float verticalScale = 1 / Mathf.Cos(Mathf.Deg2Rad * (float)AbstractMapScript.CenterLatitudeLongitude.x);
        MapTransform.localScale = new Vector3(1f, verticalScale, 1f);
    }

Note that changing the scale of a Unity object may create some strange behaviors from physics, colldiers, raycasting, etc etc.

@supergra
Copy link

Confirmed, this fixes elevation for my application as well, across a range of latitudes.

Note that if you are also calling AbstractMap.QueryElevationInUnityUnitsAt(), then you need to multiply the result by the same verticalScale factor computed above, even if you scale the map transform, as done above.

However, if you scale the map transform and call AbstractMap.GeoToWorldPosition() with the queryHeight parameter, then don't scale the result, it already includes it, since it uses the Unity tile transform, and so picks up the map scale.

@supergra
Copy link

Really would love to see #1384 fixed and pushed!

@Markovicho
Copy link

Markovicho commented Aug 18, 2020

I can confirm that changing the mentioned line in MapScalingAtWorldScaleStrategy.SetUpScaling will fix this issue like you can see in the pictures below:

Test with 200m Cube (respecting WorldRelativeScale) vs. 200m real world tower

Before
image

After
image

Look's legit now :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants