How to adjust the zoom through a render script (SOLVED)

Just a quick question, I would like to be able to adjust the zoom of my camera object, but I am inexperienced with tweaking the render script. Can someone give me a step-by-step solution to address my problem? Thanks!

1 Like

The default render script does not use the projection data when a camera is made active. Which means the z position of the camera won’t matter. You’ll need to modify your render script, and some information on the camera. Check this example. Look at the information of the camera and the render script compared to vanilla settings.

PerspectiveCamera.zip (13.0 KB)

Or do you want to keep an orthographic projection?

3 Likes

Yeah, sorry for the confusion. I am looking for an orthographic zoom. Also, how do I open the file you linked in the editor as a Defold project like the ones I have hosted?

Scratch that last question, I worked around it by brute-force copypasta on a new project. Is there a more elegant way of opening a file like a project?

Not in Editor 1 but probably in Editor 2 when it’s available. What you did was the proper way.

If you want an orthographic zoom then you will need to just resize the view projection rectangle. Smaller rectangle = zoomed in, larger rectangle = zoomed out. Of course the relative size seen is based on the relative size of your view port.

By default, view port is set to the size of your game’s window size while running.

render.set_viewport(0, 0, render.get_window_width(), render.get_window_height())

And the default projection for drawing sprites is based not on the actual screen size but the width / height of your project’s width / height. When apps are first opened on desktop this will probably be 1:1 with the window size.

render.set_projection(vmath.matrix4_orthographic(0, render.get_width(), 0, render.get_height(), -1, 1))

But you’ll notice if you maximize a default project it will stretch unevenly.

If you wish to zoom in could you would modify this to

render.set_projection(vmath.matrix4_orthographic(0, render.get_width() / 2, 0, render.get_height() /2, -1, 1))

Zoom out

render.set_projection(vmath.matrix4_orthographic(0, render.get_width() * 2, 0, render.get_height() * 2, -1, 1))

The 0s are for the x,y origin. If you wish to center your zoom on a character then you will need to offset these values. To pan your camera around you’ll need to offset them based on the position of whatever you are wanting to look at.

You can send information to your render script from a Game Object script for the position of what to look at, and then handle that data in the render script on its update. So you can apply a zoom factor of whatever you want along with general offsetting to move and zoom your camera. I don’t have an example of this handy right now, maybe someone else does? Is that enough info for you to figure it out?

7 Likes

Awesome! That cleared a lot up, thanks for taking the time to give me a hand! :grinning:

1 Like

Once you get it working how you want, check out this example for a more advanced setup that handles keeping aspect ratio fixed / screen centered while project window is resized, and handles translation of screen click positions to game positions.

You could implement in your project now too and modify it to your needs.

4 Likes

You are one step ahead of me! I’ll check it out, thanks. :slight_smile:

1 Like