GUI Button to screen coordinates


With babylon, I know I can find a button with this sort of code

b = scene.getNodeByName("planeForADT").material.emissiveTexture.getDescendants().find(d => === "buttonName")

And I see a method called getLocalCoordinates(vector2), but that doesn’t looks like it will return the correct data (it gets control space from global space, and ive tried it, it looks to be scaled control space).
How can i get the global coordinates (and ultimately screen coordinates) from a button’s local coordinates?




Primarily, when you are adding controls, you store the reference to the control in a variable, so yo can do this:

But let’s assume you don’t have the reference stored so you can do this:

advancedTexture.getControlByName(controlName) is the function you are after and the properties are called centerX and centerY on the control itself.

Usage: move the camera and click the button. It will be updated with the screen coords of the button.


1 Like

Anyway, i’m giving you a medal for this overengineered piece of code :slight_smile: :1st_place_medal: It’s hilarious dude!

The babylon playground seems to be completely accurate, but in our app, we’re using CreatePlane, and AdvancedDynamicTexture.CreateForMesh, and the centerX/centerY coordinates don’t seem to correspond to the actual screen position at all.

Actual coords roughly: 886 x 553
centerX/Y 352,539

Also center X/Y never update if the camera moves.

Would you please provide a PG next time you asking a question? It’ll help to avoid misunderstanding of your problem.

So if your GUI is on a plane the control coordinates are relative to that plane so you just need to add screen cordinates of that plane to the coordinates of the control. You won’t need that funky piece of code at all.

getClientRect function source:

extent source:

Ah thanks!

Yes I’ll supply a more complete example in the future. I thought this was mainly a question of “where in the API is this”. I see that you definitely gotta do it the long way (coords/world widths x world transform x camera x projection).