- Home /
How to georeference model in Unity3d?
Hello everyone, I wonder if there is a possibility to assign GPS coordinates to a 3D model (.fpx), because, in part of my project, I have a shp file in which I have GIS data and to import file into Unity3d I had to go through the blender to convert the shapefile to .fpx model, but by doing this, I lose information about GPS coordinates. Would you have any ideas for me ?, any suggestion will be welcome.
Answer by rich2020 · Sep 06, 2016 at 10:43 AM
Hi, I don't know what it is that you are trying to do, so what I'm about to say may not be relevant and for that, I apologise.
Anyway, I am looking at mapping objects with lat and lon coordinates to the surface of a sphere. I took the coordinates from various places around the world from this site (http://www.latlong.net/) so that I could place the objects and see if their positions matched with the texture of the sphere (earth texture).
Note: Unity mesh is not suitable for this... you must get a mesh from blender with the UVs or download one from the web. I used this one: http://tf3dm.com/3d-model/planet-earth-99065.html.
Import the mesh into Unity, and download a texture: http://visibleearth.nasa.gov/view_cat.php?categoryID=1484 I used this one: http://eoimages.gsfc.nasa.gov/images/imagerecords/73000/73909/world.topo.bathy.200412.3x5400x2700.png
To map from Polar (lat lon) coordinates to Cartesian (xyz), you can use this function:
private Vector3 PolarToCartesian(LatLon polar, float radius)
{
var latitude = DegreeToRadian(polar.Latitude);
var longitude = DegreeToRadian(polar.Longitude);
latitude = 1.570795765134f - latitude; // subtract 90 degrees (in radians)
return new Vector3
{
x = (radius)*Mathf.Sin(latitude)*Mathf.Cos(longitude),
y = (radius)*Mathf.Cos(latitude),
z = (radius)*Mathf.Sin(latitude)*Mathf.Sin(longitude)
};
}
Radius is the radius of the sphere onto which I map the objects.
LatLon is just...
public class LatLon
{
public float Latitude { get; set; }
public float Longitude { get; set; }
}
and you need to convert your degrees to Radians:
private float DegreeToRadian(float input)
{
return Mathf.PI * input / 180;
}
So, for example, you have a sphere with radius 10 and you want to place a game object at based on the coordinates of London: 51.500083, -0.126182.
You can create a new game object like such:
GameObject item = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
Vector3 itemPosition = (PolarToCartesian(new LatLon {Latitude = lat,Longitude= lon},RadiusOfParentSphere));
item.transform.Translate(itemPosition );
item.gameObject.GetComponent<Renderer>().material.color = color;
Everything here is based off the fantastic work done by mgear: http://unitycoder.com/blog/2016/03/01/latitude-longitude-position-on-3d-sphere-v2
Note: you may need to rotate your sphere a bit to get the mapped object to align with the texture.
Hope this helps!
Your answer
Follow this Question
Related Questions
How to georeference model in Unity3d? 1 Answer
Do I have to create the texture atlas in Unity or Blender? 0 Answers
How to load android app when the button clicked in unity scene? 4 Answers
Imported Blender Animations not Working 2 Answers
Unity Ads can not show more than 1 time after install game 0 Answers