Hello Guest

Author Topic: Scaling in world space  (Read 9011 times)

andrewcarvalho

  • Newbie
  • *
  • Posts: 3
    • View Profile
Scaling in world space
« on: April 22, 2013, 10:03:46 pm »
Hi guys!

I have a question about a specific use case and was hoping to get an answer that will make my life easier.

I come from a more traditional/code-only background and I'm used to setting up the scale of my sprites manually using the world coordinate system.  I'm currently working on a prototype in Unity (for the first time, been at it about a month) and now that game mechanics are in and working with crappy programmer art, I wanted to get all the pixel perfect assets in.

My issue is that to draw pixel perfect, I need to change all my sprite object scales back to 1 and leave the sprite scale as 1 in the sprite sheet.

If I want to draw in world coordinates (aka not change my object scales) I need to calculate the right scale for each sprite in the sprite sheet which feels backwards to how tk2d was meant to be used.

The reason why I have always worked in world coordinates is that I could just swap assets behind the scenes to target a new resolution.  No code changes and the sprites are pixel perfect on a different screen/platform, yay!  For example, if 1 world unit is 10 pixels and my character is 5 units high, I drop in a 50px high sprite.  If I want to target a platform where 1 world unit is now 20 pixels, I simply swap out the sprite for a 100px version and change nothing else to get pixel perfect rendering.

So ideally, I'd like to (without having to set scales for each sprite individually) have the sprites be scaled by the world coordinate system I have set up so that I can use the world scale to size them in code and have them look pixel perfect.

Is there an easy way to achieve this?

 (I'm also posting this on the unity forum so if you see it twice just answer in one spot!)

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Scaling in world space
« Reply #1 on: April 22, 2013, 10:36:58 pm »
You can work out the sprite sizes whatever way you like fairly easily. If you want to scale your sprite so its 2x the size, double the ortho size in the sprite collection. There is a "global scale" parameter but that isn't exposed in the currently released version. Alternatively if you use tk2dCamera, you can do this, but it'll require a little bit of code in there.

About swapping scales and having everything "just work", 2D Toolkit does the platform specific sprite collections exactly like that. When swapped, the sprites remain the same physical size (regardless of how this is configured - see above), just have 2x more detail, or 0.5x the detail. No need to reposition anything anywhere.

andrewcarvalho

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Scaling in world space
« Reply #2 on: April 23, 2013, 03:40:36 am »
Thanks for the reply!

If a platform specific sprite sheet will automatically handles the resizing that will save me a lot of work.  I'm used to creating a unit sized quad then scaling and texturing it for sprites.  I'm assuming on the back end it's creating the quad vertices based on the size of the image and the target resolution which is why all sprites are pixel perfect at a scale of 1?

I have a couple follow up questions, though.  We are targeting multiple platforms with multiple resolutions (and aspect ratios but that's a different beast all together) and I was trying to reuse the same camera and world space for all those resolutions since certain things in the game are not sprites but invisible information for pathing and are tied to the initial scale we applied to our world.

I'd previously been using an ortho camera with a size to match the world space and plotted the vertices on a path relative to that size.  Switching to the tk2dCamera and resetting the sprite collections, all the sprite sizes have changed so now the path no longer lines up with the image.  This is the reason why I wanted to scale the sprites myself and only swap the textures without resizing them for other resolutions.  What I've done instead is set the target height for the sprite collections to the pixel dimension of our background and an ortho set to our world scale.  I then use this with a basic Unity camera with the same ortho size.  I gave up on scaling to world size since a sprite that isn't square is still scaled by 1 and even changing the ortho, I'd still have to modify each sprite (or head down a code path I'd rather not) to make things scale with the world.  So what I did is for any object with a sprite, create a child with the logic (which uses the scale values for some calculations) and a parent with the sprite scaled to 1.  It seems to all be playing nicely together but just wanted to confirm that this is a good approach to use with the 2d toolkit.  I'd rather try to use the framework 'how it's supposed to' as opposed to bending it to my will as in my experience it's not worth the effort to not play by a frameworks standards (that doesn't just go for tk2d but for unity in general which was a huge shift away from my usual C++ low level coding).

And while I'm here, I was looking at the platform specific sprite sheet settings and I see the 1x 2x and 4x scales.  Setting them sends it out to automatically search for files and then returns an error.  I'm going to assume I'll need to use the iOS standard of throwing 2x and 4x at the end of the file name?

Also, the platform flag will only work for resolutions that are multiples of each other (like iOS devices), correct?  If I want to support various Android dimensions or Desktop windows, is it easiest to load up a different camera/collection set for each platform and aspect ratio?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Scaling in world space
« Reply #3 on: April 23, 2013, 09:42:27 am »
Your assumption is correct. The sprites are created at the correct scale to display pixel perfect at the requested resolution & ortho camera.

You're correct about the naming convention, but tk2d supports some other options too if you're not too fond of the iOS naming convention. The search order is documented here:
http://unikronsoftware.com/2dtoolkit/doc/advanced/platform_specific_sprite_collections.html
You have full source, so you can of course tweak this to suit your workflow.

The platform stuff only supports multiples of 2 out of the box mainly for support reasons. Sometimes people don't understand the implications of having a non-divisible texture size, and the alignment will go off with the sprites. Internally though, the scale is just a float, so you can add as many multiples as you'd like, as long as you keep them all nicely divisible the system will just work fine. tk2dSystem.assetPlatforms is the array you want. I had it user defineable at some poitn in the past, but you can simply add what you like in there, the scale parameter is simply the scale relative to 1x.

I suggest sticking with a normal ortho camera and waiting for tk2d 2.0 - there will be tk2dCamera style automatic scaling and aspect ratio compensation in that version, which should really help with what you're trying to do.

andrewcarvalho

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Scaling in world space
« Reply #4 on: April 23, 2013, 03:00:52 pm »
That sounds great!  Looking forward to the next version. 

Thanks for the help.  The 2d Toolkit really is the best go to 2D solution for Unity.