This PG https://www.babylonjs-playground.com/#VYM1E#36 shows a pre-built cylinder joining two spheres as they move.
The important parts in this PG are
- initially force cylinder to lie along x axis, baking transformation and so scaling the x property of the mesh and ensuring axis1 is the first parameter of the RotationFromAxes
axis1 = (sphere1.position).subtract(sphere2.position);
axis3 = BABYLON.Vector3.Cross(camera.position, axis1);
axis2 = BABYLON.Vector3.Cross(axis3, axis1);
mid = ((sphere1.position).add(sphere2.position)).scale(0.5);
mesh.scaling.x = axis1.length(); // reset scale to match distance between spheres
mesh.position = mid; // reposition mesh so that its center is the mid point between spheres
mesh.rotation = BABYLON.Vector3.RotationFromAxis(axis1, axis2, axis3); //match rotation to line joining spheres
- The formation of axis3 uses the camera position as an arbitrary vector to calculate an axis perpendicular to axis1
This PG https://www.babylonjs-playground.com/#VYM1E#37 starts with the created cyclinder in its created orientation, vertical. So provided axis1 is assigned to the y axis (rather than the x axis) there is no need to bake the mesh. The orientation of your imported mesh and whether it is already aligned to one of the axes x, y, or z depends on the need for baking and which axis (x, y, or z) that axis1 is assigned to. Sometimes you may need to swap axis2 and axis3 around for the cylinder to be in the proper direction.