Propose to remove baking transforms during STL Export (support instanced meshes)


This topic is to discuss support for instanced meshes during STL export. I have it working here and want to contribute.

I know this breaks backward compatibility but baking transforms makes it difficult to work with instanced meshes. STL format may not have a way to indicate vertex transforms so its required we bake them, but doing so causes instanced meshes to change transforms. Classic catch 22.

I propose we work on a copy of vertex data like here. And also finally bake transforms after STL export has finished to preserve compatibility (which still negatively affects instanced meshes).

Would like to know your thoughts.


Welcome aboard!

I think this could be a nice addition to the STL serializer!

To keep backward compatibility, you could add a new option like supportInstancedMeshes and use the new code path only if it is true.

You only need to do this if doNotBakeTransform=false. We could also just not bother with this and document that setting supportInstancedMeshes=true automatically turns on doNotBakeTransform, as there’s no reason to bake transformations when there are instances.

I was fairly sure this is already supported, but had to double check :slight_smile:

The STLExport class already have this option as the last optional parameter for CreateSTL().

let mesh = scene.meshes[0];
let download = true;
let fileName = “myStlFileName”;
let binary = false;
let isLittleEndian = true;
let doNotBakeTransform = false;
BABYLON.STLExport.CreateSTL(mesh, download, fileName, binary, isLittleEndian, doNotBakeTransform)

so all you have to do is check if the mesh has instances or is an instance

let doNotBakeTransform = mesh.hasInstances || mesh.isAnInstance;

or something like that

That won’t work, because what doNotBakeTransform=true does is that the current transformation is not baked into the vertices, and because we are taking the raw vertices data (mesh.getVerticesData()) to serialize a mesh, it means no vertex transformation is applied in this mode. So, position/rotation/scaling are not taken into account and all meshes are exported with a (0,0,0) position, no rotation and (1,1,1) scaling.

Thank you for your replies @Evgeni_Popov and @aWeirdo
I think I can work a PR keeping backwards compatibility with your suggestions :slight_smile:

I have the PR here. Added a param: supportInstancedMeshes which when true overrides doNotBakeTransform as true.

Ideally, instanced mesh export should have been implicit, but I respect that we need to have backwards compatibility.