In general, how do I store Babylon's Mesh (a three-dimensional figure) in a DB (Postgresql) and use it in Unity?

Hello, everyone. Babylon community

I have a question.

The work I want to do is as follows.

  1. I use Babylon to make the three-dimensional figure I want.
  2. Store this data in the DB
  3. Call from Unity and render to geographical coordinate system (4326) 0,0.

There are some fundamental questions to address this.

fundamental questions

  1. Is the coordinate system used by Babylon ultimately the same as the 3857 coordinate system (Mercator)?
  2. What is the format for storing Babylon-generated data in a DB (Postgresql) in general?
    ex) GeoJSON, Json (Vertexes, indications, noramls are extracted and stored separately),
  3. Should PostGIS Geometry type of DB (Postgresql) be used universally?
  4. Rendering to geographical coordinate systems (WGS84, 4326) would require coordinate transformation, how do you usually do this?

Working with Babylon for the first time, the question might be a bit basic.
Please understand, and it would be very much appreciated if you could help me.

Hello :slight_smile:

There are two very different questions here so I’ll separate them :


1. 3D modeling → Storing to PostgreSQL DB

I don’t exactly get the usage of BabylonJS you do here :thinking: . BabylonJS’ purpose is realtime rendering some meshes in a web context. It’s not (like, not at all :stuck_out_tongue: ) a 3D modeling tool. Why don’t you use Blender or such tool to generate your geometry, if you aim to render in Unity at the end ?

To answer your question, maybe the simpler would be to used the binary format data available on PostgreSQL, and store your meshes as .GLB files. Depending on the size, you might look for another solution better suited for big files…


2. Converting Lat/Long coord to X,Y,Z

Since Unity is cartesian X,Y,Z, what you need is retrieving those coordinates from the Latitude and Longitude (and eventually altitude)… The official computation would take in consideration curvature and exentricity (the earth is not a sphere…) but considering it’s for a Unity project, I think you could just get rid of them and simply use basic Cos and Sin compute…

If you need to actually convert coord from Lat/Long to X,Y,Z (For example you are rendering stuffs on an actual globe, and you need X,Y,Z coords on a sphere)
It would be :

public class CoordinateConverter : MonoBehaviour
{
    public static Vector3 LatLongTo3D(float latitude, float longitude, float altitude = 0)
    {
        const float R = 6371000f; // Earth's radius in meters
        float latRad = Mathf.Deg2Rad * latitude;
        float lonRad = Mathf.Deg2Rad * longitude;
        // XY is Equator plane
        float x = (R + altitude) * Mathf.Cos(latRad) * Mathf.Sin(lonRad);
        float y = (R + altitude) * Mathf.Cos(latRad) * Mathf.Cos(lonRad);
        // Z is from South Pole to North Pole
        float z = (R + altitude) * Mathf.Sin(latRad);
        return new Vector3(x, y, z);
    }
}

If you need to get flat coords (for example you want to get positions on a flat map, like a GPS or Google Map)
Then it’s much simpler, Y is proportionnal to Latitude, and X is proportionnal to Longitude (with cos(lat) as a ratio)

public class CoordinateConverter : MonoBehaviour
{
    public static Vector3 LatLongTo3D(float latitude, float longitude, float altitude = 0)
    {
        const float R = 6371000f; // Earth's radius in meters
        float latRad = Mathf.Deg2Rad * latitude;
        float lonRad = Mathf.Deg2Rad * longitude;
        float x = R * Mathf.Cos(latRad) * lonRad;
        float y = R * latRad;
        float z = altitude;
        return new Vector3(x, y, z);
    }
}

1 Like

Thank you for your answer.

First, the representative of the company wants to do 3D modeling on the web.

Second, I also considered saving it in GLB format, but they want to save it in data as 3857 coordinate system data.

I got a mission, and I have to solve it.

I’m going crazy.

Still, the 3857 data seems to be applied by changing the world coordinate system (? I don’t know if this is the right expression) and y and z axes used by Babylon.

I’m not sure even though I’m doing it.

If you have any advice, please feel free to tell me!

Thank you.

Ok. I don’t know exactly what is your project, and as a huge fan of BabylonJS, I don’t want to unecessary avoid you from using BabylonJS, but just to be clear, if the goal is just to manually model some 3D shapes, other tools such as clara.io are best suited for this.

Then that said, if you plan to go procedural, use some Javascript code to do some automatic modeling or such stuff, then you are good to go with BabylonJS :smiling_face:

I think there is indeed some confusion here about coord systems. I’ll try to explain my point of view :

  • Common 3D tools are using cartesian X,Y,Z system (Blender, Houdini, Maya, BabylonJS, Unity, Unreal, etc…)
  • In some context such as world map, GPS, etc, you need to use other systems, using Latitude, Longitude, or Mercator (projection of these on 2D plane)

But at the end on the 3D side you will always stick to cartesian.

For example :


Let’s say you are developping this GPS in 3D where the car is an actual 3D mesh.

  • Your input to place the car would be Lat / Long GPS coords
  • Your car mesh is anyway stored in cartesian X,Y,Z
  • You would convert these Lat/Long to cartesian X,Y,Z at the end

There is few chance that you actually need to store a .GLB file in a non cartesian coord system…

1 Like