Positioning mesh in the center of the canvas

I have a scene with a custom mesh, but using the scene.createDefaultCamera(); creates a camera that cuts the mesh off halfway, but I want the camera to show the whole mesh and have the mesh ideally be in the center of the canvas. Is there a good way to position the camera to show the whole thing without touching the mesh’s position property?

Without an example showing an exact case, we can only guess to what’s going on, try;

camera.setTarget( meshVariable.position );

1 Like

I can’t reproduce it using the playground using append for some reason, but I was able to reproduce the issue using AssetManager as can be seen here: https://www.babylonjs-playground.com/#TT2BK1#11. How do I get the duck to be in the center of the scene by adjusting the camera?

Code used is explained in the comments :slight_smile:

https://www.babylonjs-playground.com/#TT2BK1#12

1 Like

@aWeirdo, that code works for the duck, but it doesn’t seem to work for some other meshes, is there a way to make it generic so that it applies to any mesh?

@freetoplay
Tough to do unless each file has a simular structure.

The duck had a root (empty parent) at task.loadedMeshes[0], so i had to use task.loadedMeshes[1] to get the duck mesh itself, perhaps 0 will work for your other file(s).

You could try something like this;
https://www.babylonjs-playground.com/#TT2BK1#13

Hi.
This should work for any mesh I hope
https://www.babylonjs-playground.com/#TT2BK1#14

1 Like