What is Mesh.makeGeometryUnique()?

Hey folks,

Just wondering, can anyone help me to understand what does this function do exactly?

For example if we apply it to a cloned or instanced mesh. What’s happening in the backend?
Does it duplicate only vertex? or doing something else too? Is it expensive to use? or anything we should be aware of when we use this function.

Just trying to understand it so I can have better ideas when to use it.

Many thanks!
Tawibox

It is doing a full copy of all the vertex buffers so that the mesh for which you called makeGeometryUnique has unique geometries, not shared with any other meshes.

You should normally not call this method, except if for some reasons you first cloned a mesh (which share geometries) and you need at some point to have unique geometries for the mesh.

@Evgeni_Popov

Thank you for the response!

So can we say, for an imported .obj mesh (or other format), turning on makeGeomertryUnique() is equivalent to importing another brand new mesh (even same content) all over again?

Or there are still some differences?

Many thanks!
Tawibox

I’m not sure I understand. mesh.makeGeomertryUnique() will create unique geometry for the mesh it is called on. But if the geometry is already unique (meaning, not shared with other meshes), there’s no purpose to call makeGeomertryUnique.

The only context I ever see makeGeomertryUnique called by user code was when a mesh was created with mesh2 = mesh.clone() and at some point mesh2 needed unique geometry.

@Evgeni_Popov

Sorry for the confusion! And thank you for the further explanation too! Let me put my follow up question this way:

I have 2 processes now:

Process #1:

  • I have an .obj filw. I am using OBJFileLoader() load it in a babylon scene as meshA .
  • I use meshB1 = meshA.clone() get meshB1()
  • I use meshB1.makeGeomertryUnique() to make it unique

Process #2:

  • [same as #1] I have an .obj file. I am using OBJFileLoader() load it in a babylon scene as meshA .
  • I call OBJFileLoader() again and load the same .obj file in to the scene as meshB2

From my understanding, meshB1 and meshB2 should be same right?

So my question was, in terms of system load, ram usage etc…are these 2 processes and results are same?

Hope this question makes sense. :laughing:
Many thanks,
Tawibox

Option 1 is less cpu usage than 2, as 2 will require the browser to check http cache. As for uniqueness… im not sure, and it could even be different behavior between file types since the loaders are implemented differently. If you can freely choose between 1 and 2, do 1 for sure.

Tangential opinion here. Personally, i try to avoid babylons hidden http abstractions where possible because they are a bit dated and because they only emulate xhr on the native engine, so i think updating the browser impl is prob off the table. Its not xhr vs fetch, that doesnt really matter - its that browsers make user of threads in service workers, so its possible to do prefetching and transforms off the main thread but you cant do it directly within the loaders. There is, however, a worker pool you can use within the loaders.

2 Likes

In process #1, meshA and meshB1 will be two separate meshes with no shared geometry, and in process #2, meshA and meshB2 will also be two separate meshes with no shared geometry.

So, the result is the same in both cases, but #1 is faster because it does not require to read and parse the .obj file a second time: cloning + calling makeGeometryUnique is faster.

3 Likes

Jinx, you owe me a coke.

@jeremy-coleman @Evgeni_Popov Thank you for the help guys!!