Hello Guest

Author Topic: Moving objects and moving cameras with pixel perfection  (Read 5006 times)

dbanfield

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 30
    • View Profile
Moving objects and moving cameras with pixel perfection
« on: March 16, 2014, 10:46:49 am »
Hi,
Recently I submitted a problem that came down to solving the issue of pixel perfection from a moving camera.
http://2dtoolkit.com/forum/index.php/topic,3607.msg17372.html#msg17372

I thought starting a new thread would make sense to keep this as a distinct subject matter.

I wanted to clarify something in regards to pixel perfection. The toolkit has the ability to make a sprite pixel perfect. You set up your camera, be it orthographic or perspective, hit the 1:1 ratio button and hey presto, you have a pixel perfect sprite.

In relation to the problem in the other thread, it seems to me that as soon as you move an object, or the camera, pixel perfection basically goes out the window when sprites are next to each other. UNLESS you specifically move your game objects or your camera by 1 pixel.

In the documentation I do recall it saying about setting up the camera so that 1 unit = 1 pixel, which would make this easier. Only move your game objects or your camera by 1 unit and you will maintain pixel perfection.

But if you want to move objects or your camera not by 1 unit, which is pretty much a given if you're using physics or requiring tweening or lerping of your camera, there doesn't seem to be a solution out of the box.


Is this correct? It seems to me it would be nice to have some script which would give the same ease-of-use of the 1:1 button. You could attach a script to the camera, to ensure that it is always snapped to pixel perfection for a given sprite ratio, and a second script that can be attached to game objects that make sure that they are always snapped to a pixel perfect position.



Does this makes sense, or I just talking crazy? I feel like I'm missing something in regards to this pixel perfection malarkey. I would be happy to go and write the scripts myself, but I just want to make sure I'm not missing anything and that the above is accurate.

Thanks
Dan
« Last Edit: March 16, 2014, 11:07:38 am by dbanfield »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Moving objects and moving cameras with pixel perfection
« Reply #1 on: March 16, 2014, 03:02:01 pm »
It depends on what your priorities are, and the solutions depend very much on the parameters in your project. In that other post, you wanted to use a perspective camera - it adds a lot of complexity especially if you don't understand the implications.

With the camera, simply keep it orthographic for simplicity and use your own position and don't ever read from transform.position and thats pretty much it - you can snap the position to a pixel as you assign it to transform.position.

In terms of moving objects, if you never scale your sprites you can simply snap to the closest pixel position in the shader - this adds a little cost to the vertex shader. The other option is to keep the sprite itself separate to the rigid body, but thats a lot more intrusive.

If you ever scale anything things get much much more complicated, even more so to handle it automatically in Unity.

esdot

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: Moving objects and moving cameras with pixel perfection
« Reply #2 on: August 21, 2014, 06:22:47 pm »
Would it be possible to get an example of doing this in a vertex shader? Would be really handy for us.