Ortho camera - Pixel to world coordinate & world coordinate to pixel

Hi everyone.

I know there is a lot of resource with a lot of answers about this question, but there is something that i still don’t understand.

I am creating a 2D tile system. It’s a grid (x,y) and the resolution is defined by the level. Like google map for example or any tile system. Level is here to choose the resolution.
So, I have a scene, an orthographic camera, and one geometry (it’s a quad mesh) that i move to (0,0) to display the first tile, then to (512,0) for the second tile and so on. (My tiles are 512*512 pixels)

Everything works great ! And i can convert pixel to world coordinate and world coordinate easily (by normalizing between -1 and 1 and then unproject the vector to the camera)

But now, if i want to initialize my world (in world coordinate) with zoom = 2.

I do :
camera.zoom = 2
camera.position.set(worldX, worldY)
and i update the camera matrix.

The problem is this only works well if camera.zoom = 1. So, i need to compute the worldX, worldY which will work if my camera is at zoom = 2

And, for some reason, i also need to scale my whole scene, and apply transformations like shear.
If i do. that, when i compute pixel to worldCoordinate, it doesnt works at all :frowning:

Is there something i am missing ?
I hope my message is understandable. If not, i will make some illustration about my problem :slight_smile:

Thank you for your help.

Keep in mind there are multiple matrices not only one. If you change the zoom property of the camera, you have to recompute the projection matrix via updateProjectionMatrix(). If you transform the camera in 3D space and you directly use Vector3.project() or Vector3.unproject(), you have to call updateMatrixWorld().

1 Like

Yeah,

But i mean, if my (0,0) is a top left of my screen with an ortho camera, and i draw a square 512px*512px at (0,0) so i can see my square at the top left of the screen.

Now, if make camera.zoom = 0.5 and camera.updateProjectionMatrix(), everything works fine. So now my square is not at the top left of my screen anymore, of course because i unzoom.

So finally, my question is, how can i compute the (tx, ty) to put the good position to the camera to make the square at the top left of the screen again but with the zoom still at 0.5

I’m sure that i missed something really easy to understand, but i don’t understand how to compute that.

1 Like