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).
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
The STLExport class already have this option as the last optional parameter for CreateSTL().
let mesh = scene.meshes;
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;
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.