Skip to main content

On Sale: GamesAssetsToolsTabletopComics
Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines
0
Members

Asset scaling to match camera size

A topic by MathieuLoutre created Sep 01, 2016 Views: 971 Replies: 3
Viewing posts 1 to 4

Hi all,

I've been struggling with this problem for a little while. I'd like for one of my sprites to cover the whole screen, whatever the size or ratio. Currently the game doesn't have a ratio configured to leave it responsive and it seems to work pretty well for my purpose.

To scale the sprite to the camera size, I found a first hurdle: I couldn't get the pixel ratio of a world unit (the camera has an orthographic scale of 20, which as far as I understand means that its height is worth 20 whatever the actual screen height). I added a couple of methods to get the width/height of the screen/camera by using something like `this.actor.gameInstance.threeRenderer.domElement.clientWidth * this.viewport.width` inside the Camera.

Now I'm able to do camera width/sprite width (taken from the grid size) which gives me a scaling factor which should stretch/shrink the sprite to match the size of the camera. However, it seems like there's a variable in there I'm not accounting since it's always too big or too small!


The last values in the console are screen width in px, sprite width in px, scaling factor to make it fit in the window, screen width in world unit, screen height in world unit.

What am I missing/doing wrong?

Alright so after a lot of blind digging, I found that changing the pixel per unit of the sprite affected how the scale responded. I finally worked out that if the camera pixel per unit (which is not given by the framework currently!) is different from the sprite's you need to multiply the scaling factor by `spritePixelPerUnit / cameraPixelPerUnit` and it'll fit!

I think the camera pixel per unit ratio is something that should be added to the engine, otherwise such calculations are near impossible without some dirty hacks.

Moderator

Hi!

There is no "cameraPixelPerUnit" but for an orthographic camera, the orthographic scale is the vertical number of world units that will fit on screen. The horizontal number of units can then be calculated by dividing by height and then multiplying by width (aka. multiplying by the screen ratio).

Hope that helps!

Thanks! I had worked that out. Because I'm trying to scale a sprite, the only values I found for it are pixel based and not world unit based. So to convert the sprite pixel size to world unit size, I needed to know how much a world unit is worth in pixel which is not given anywhere. The solution is relatively simple, it's `height of the screen in px / orthographic scale`. That allowed me to scale the sprite and then work out the pxPerUnit difference between the sprite and the world to correct the ratio. Hope that made sense.

All of this to say, it's great to have the dimensions of the screen in world unit using the orthographic scale and the screen ratio, but knowing the current pxPerUnit for the camera is an information that the framework doesn't deliver and is necessary to build responsive games.