Search Unity

[Released]Pixel Perfect Camera

Discussion in 'Assets and Asset Store' started by Douvantzis, Jul 15, 2016.

  1. Douvantzis

    Douvantzis

    Joined:
    Mar 21, 2016
    Posts:
    73
    Pixel Perfect Camera
    Asset Store Page

    [HTML5 demo]

    pixelperfectcamera.png
    It's a camera script which gives more control over the camera size and enables pixel perfect rendering for pixel art sprites.

    Features

    Pixel perfect: adjusts the camera size so that each sprite pixel is rendered to an integer number of screen pixels. This is very important when you render pixel art using "point sampling".

    Camera width: specify the camera width instead of height (the default Unity camera allows you to set only the camera height).

    Size constraints: you can optionally set the maximum allowed camera width or height. This is useful if for example the user has an ultra wide monitor and you don't want your camera's width to be wider than some amount. The script will choose a camera size that respects all constraints.

    Retro snap:enabling pixel snapping makes sprites snap to the artwork's pixel grid producing a snappy retro motion.

    How it works

    In contrast to other camera solutions, this script uses a very simple way (which will most probably not have any side-effects in your project) to achieve pixel perfection. The only thing it does is that it adjusts the camera's orthographic size.

    The script makes sure that each sprite pixel gets rendered in an integer amount of screen pixels. You can read more about the rational in my blog post.

    If you enable retroSnap, each sprite will snap in the asset's pixel grid. Thus, when a sprite is translated, it will move multiple screen pixels at once in order to snap to the artwork's pixel grid.

    Note that if you change the scale of your sprite or you rotate it, you won't get the blocky result you may expect. So, sprite scaling and rotating should probably not be used.


    How to use

    Create an orthographic camera and use the script on it. You can watch this video for more information.

    If you want to use the pixel snap functionality, you'll have to use the pixelSnap script on every sprite.



    Project set-up

    If you want to enable the pixel-perfect mode, make sure that your project has the following set-up:
    • Use an orthographic camera and throw the Pixel Perfect Camera script on it
    • Leave the sprite's scale to 1.
    • The textures of your sprites should use: point filtering, disable mip-mapping, compression to "none" (Truecolor format in older Unity versions)
    • All your textures should have the same Pixels Per Unit
    • Make sure that the player settings of the platform(s) you are targeting don't reduce the texture size
    • In your project's quality settings: set "Full res" in Texture quality and disable Anti-Aliasing for the platform you are targeting.
    • For best results use the sprite editor to set a pivot point of [0.5, 0.5] to your sprites (the default one).
    • Place your game objects in positions with coordinates that are either integer or multiples of [1 / Pixels Per Unit]. For example, if Pixels Per Unit is 100, have the initial position of your game objects be at multiples of 0.01 such as 2.04, 4.32 but not 2.043 nor 0.001 etc.
    UPDATE: In order to continue the support and development of Pixel Perfect Camera further, we decided to put a price tag on the addon.
     
    Last edited: Sep 27, 2017
    IvanAuda, rosdi, DevMember and 2 others like this.
  2. KhenaB

    KhenaB

    Joined:
    Aug 21, 2014
    Posts:
    270
    Hey,

    Just tried the plugin and it seems to work well, however i need the camera size to be static or else we can see outside the boundaries in some of my rooms, what are my solutions?

    I've checked some other pixel art games and they all seem to be pixel perfect and to keep the same camera sizes, what's the secret?

    Here's an example of the issue, at some resolutions we can't even see the doors in this room where the camera doesn't move.
     
    Last edited: Jan 18, 2017
  3. Douvantzis

    Douvantzis

    Joined:
    Mar 21, 2016
    Posts:
    73
    Hello,

    Pixel perfect rendering and "static" camera size for different screen resolutions can't work together unfortunately. It's only logical that a specific screen resolution allows for specific pixel perfect camera sizes.

    The other pixel art games that you checked are most probably not pixel perfect. Note that if the ratio of screen pixels per asset pixel is quite large (very blocky pixel art), you can disable pixel-perfect mode and the user may not ever notice it.

    Also, if the game is a pc game, you could add black bars on the sides and render the same camera size in a pixel perfect way. PixelPerfectCamera does not offer this capability but it can be tweaked. Alternatively, some games may render to a render texture in a pixel perfect way and scale that to the screen. It will not be 100% pixel perfect but it's ok.

    If you want to stick to PixelPerfectCamera, as it is right now, you can play with max size to restrict the possible camera size you may end up with.
     
  4. KhenaB

    KhenaB

    Joined:
    Aug 21, 2014
    Posts:
    270
    Thanks for the detailed answer, i have to find a solution as right now there is a lot of artifacts, i've seen the render texture solution being mentioned a lot so i'll try to look into that.
     
  5. KhenaB

    KhenaB

    Joined:
    Aug 21, 2014
    Posts:
    270
    I tested the game "Hyper Light Drifter" on two of my monitors, i read somewhere online that they render the game in 480x270 and then stretch that to any resolutions, i'm guessing the render target in Unity would be a similar approach.

    Here's a zoomed in crop of the fullscreen game on both of my monitors, we can definitely see that the game is pixel perfect on my 1080p monitor, but do you know what they're doing to the other resolution that makes it look like it has some filtering, is it just bilinear filtering? The game looks great even when stretched to non native resolutions, there is no artifacts and the pixels don't look weird, the game is less sharp however.

    1920x1080


    1680x1050
     
  6. KhenaB

    KhenaB

    Joined:
    Aug 21, 2014
    Posts:
    270
    Also another question, after trying to make my game pixel perfect i've come to the realization that my art is either scaled too small or too big, that probably means that i have to redo all of it with pixel perfect scaling in mind?
     
  7. Douvantzis

    Douvantzis

    Joined:
    Mar 21, 2016
    Posts:
    73
    The first screenshot resolution is 1920 = 480 x 4 . So, it renders pixel perfect.

    Regarding the second screenshot (1680x1050), it doesn't look like normal bilinear filtering.

    My guess is that they use some shader to render the 480x270 render texture to 1680x1050. You can read the theory behind a shader like this here:
    https://csantosbh.wordpress.com/2014/01/25/manual-texture-filtering-for-pixelated-games-in-webgl/

    I have used the following asset that has a shader that renders using point - sampling at the center and bilinear when close to the edges that produces a similar result with your screenshot:
    https://www.assetstore.unity3d.com/en/#!/content/65509

    You could use this shader to render the low-res render texture to full screen.

    Ideally, your pixel art should be used with scaling set to 1. Thus, each asset should be created with the same "scale" in mind and the assets should look good next to each other.
     
    theANMATOR2b likes this.
  8. KhenaB

    KhenaB

    Joined:
    Aug 21, 2014
    Posts:
    270
    Thanks for the links, i've heard of the retro AA shader, i guess i'll go the render target route and try to reproduce what Hyper Light Drifter is doing.

    About scaling, all my assets use the same PPU scale, what i'm referring to is that at my current resolution and PPU my game is only pixel perfect with a camera scale of 2.7 or 5.4, at 2.7 it's too zoomed in and my character appears too large, at 5.4 the character is way too small, unless there is anything i can do about that i guess i'm going to have to redraw my art a bit smaller and go with 2.7.
     
  9. Douvantzis

    Douvantzis

    Joined:
    Mar 21, 2016
    Posts:
    73
    Note that the higher your screen resolution, the more camera scales will be available for the pixel-perfect camera to choose from. As you may have already noticed, the camera scales chosen depend on the resolution of the game view, when you are running it in the editor.

    Since you are going the render texture way, you can actually choose the camera scale you want, calculate the pixel-perfect render-texture resolution and then render it on screen using a shader. You don't need to change your assets.

    Also, I am proposing to use the retro AA shader for rendering the render texture on screen. Not for rendering each sprite, as it's its normal use case.
     
  10. KhenaB

    KhenaB

    Joined:
    Aug 21, 2014
    Posts:
    270
    This is interesting, well i've already decided that i want my game to be pixel perfect at 1080p, my base resolution would be 480x270 but since i'm also using particles and other non point filtered elements, i'm going with a render texture the size of 960x540.

    My PPU right now is at 50, i realize now that this number isn't ideal but i use physics so i can't use a PPU of 1, wouldn't that force me to use a camera size of (540 / PPU / 2) 5.4 or 2.7 ?

    It doesn't seem to be pixel perfect at any other camera sizes.

    Thank you so much for taking the time to answer my questions, i know that this isn't related to your plugin anymore, but there is a lot of confusion regarding pixel perfect rendering and it's something that is so important for every 2D pixel art developers, i wish Unity would handle this themselves.
     
  11. Douvantzis

    Douvantzis

    Joined:
    Mar 21, 2016
    Posts:
    73
    Yes, 2.7 would be the correct size. Note that since each asset pixel will render to 2x2 render texture pixels, this will allow your sprites to move to half an "asset pixel". This is also the case with PixelPerfectCamera, unless you use retro snap that makes the sprites snap to asset pixels. I personally find the motion smoother this way, but you may find that it's not pixel-art correct.

    There's already a feature request for pixel perfect 2D mode, which you can upvote:
    https://feedback.unity3d.com/suggestions/pixelperfect-2d-mode
     
  12. KhenaB

    KhenaB

    Joined:
    Aug 21, 2014
    Posts:
    270
    Yeah i already added my 10 votes to that feature request, hopefully they'll listen.

    Also another observation, while pixel perfect, my sprites aren't all synchronized when in motion, i'm guessing this is because some of my sprites are placed at floating point accuracy in the world without being snapped to the pixel grid, without being able to set my PPU to 1 this has proven to be challenging, other than having a "snap to the pixel grid script" on each of my sprites, do you have any suggestions?



    Thanks for all the help Douvantzis, i'll definitely recommend your plugin to those who don't need a static camera size.
     
    Last edited: Jan 21, 2017
  13. sngdan

    sngdan

    Joined:
    Feb 7, 2014
    Posts:
    987
    I have played around quite a bit with pixel perfect and it is not that straight forward. The principle itself is of course simple, there are just many ways / settings to disturb it.

    My recommendation is only to bother if you design for a specific target platform (and the other aspect ratios / resolutions are more of a nice to have).

    Otherwise try an early prototype with the retro aa shader (I have not used it but understand he principle behind it) and see if the results are acceptable. Based on the screenshot you shared (looks great) your game could be quite fine even if not pixel perfect (seems more forgiving then let's say more static , line based art with harder color edges)
     
  14. KhenaB

    KhenaB

    Joined:
    Aug 21, 2014
    Posts:
    270
    Thanks, i'll definitely try some non pixel perfect alternatives like that Retro AA shader and see if it helps. I jsut wish they had a demo version of the plugin.
     
  15. Douvantzis

    Douvantzis

    Joined:
    Mar 21, 2016
    Posts:
    73
    KhenaB if you are rendering in a pixel perfect resolution and have the initial position of the sprites snapped to the grid (this is easy to achieve from the editor), then this problem should not appear. To clarify further, if the rock and the plant behind are translated with the same offset in every frame (assuming that you don't use any parallax effect that results in a different offsets for each other), you don't have to "snap" or round their position in runtime using a script in every sprite. The problem should not appear.

    Not that this problem will also appear even if you render in 480x270. It will also appear if you use the Retro AA shader. However, as I explained, the solution is really easy.

    Since you use 50 pixels/unit, then snapping to the grid in the editor is using a position that is multiple of 1/50=0,02. If you have something like 0,021 then there's a problem.
     
    Last edited: Jan 21, 2017
  16. sngdan

    sngdan

    Joined:
    Feb 7, 2014
    Posts:
    987
    Just to clarify, when I mentioned 'pixel perfect' in my previous post, I did not refer to the asset by @Douvantzis but pixel perfect rendering with unity in general.

    And with settings / ways to disturb I mean, I.e. Quality settings defaults, moving vs static camera, moving game objects, import settings, sprite sizes & pivot points (it can make a difference if they are pot, even dimension or uneven dimensions), if you use physics or not (how much of control you have on the world position and/or at what stage you need to adjust it) etc...

    Why did you choose 1:50? Much easier to think in 1:1 or 1:100.

    My personal opinion as stated before is that it is worth going for real pixel perfect if you have a main resolution / platform to release for. If it's a mix of resolutions / aspect ratios, try the shader. It's a sound principle and you can read up on it, it's no magic but better than the standard one - not a solution for the 'purist' ;)
     
  17. KhenaB

    KhenaB

    Joined:
    Aug 21, 2014
    Posts:
    270
    Yes, i know that if the position of my sprites is a multiple of 0.02 they'll all look great, but that's not the case when using the translate tool in unity with a PPU of 50, i don't want to have to enter the position manually to make sure it's a multiple of 0.02, that would make level design a pain, having a script that would snap my sprites to 0.02 increments (in editor) would work, but would also be a pain to add to every sprites. having a PPU of 1 would allow me to use the translate tool without worrying about it, but it would mess up the physics, so i'm looking for a better solution.

    I went with a PPU of 50 because at first i thought it was only important for physics, i placed a 2 units (2 meter) cube (Typical door height), then placed my character next to it, a PPU of 100 made my character too small next to the "door" so i just halved it.
     
    Last edited: Jan 21, 2017
  18. Douvantzis

    Douvantzis

    Joined:
    Mar 21, 2016
    Posts:
    73
    @KhenaB I thing there must be a way to write an editor script that goes through all scene's game objects and adjusts the position according to the pixels per units used. This would solve your problem, without interfering with the level design. Perhaps someone out there has already done this on the asset store!

    Also, regarding pixels per units and physics, I think that you can adjust some constants (gravity etc) and have the same effect for different pixels per units settings. In other words, you could set1 pixel per unit and after adjusting those constants, your physics could produce the same results.
     
    KhenaB likes this.
  19. KhenaB

    KhenaB

    Joined:
    Aug 21, 2014
    Posts:
    270
    Thanks Douvantzis, i'll give the second suggestion a try, also i'll see if i can find some information around about how to make such an editor script.
     
  20. flipwon

    flipwon

    Joined:
    Dec 29, 2016
    Posts:
    93
    Before I get into whats going on I just want to say this asset pretty much saved my game and I thank you for the hard work you put into it. It's really well done <3

    Now over the past couple of days I've been getting the warning message:

    Render mode: ScreenSpaceOverlay is not supported by CanvasScreenSpaceScaler
    UnityEngine.Debug:Log(Object)
    CanvasScreenSpaceScaler:Initialize(Boolean) (at Assets/Addons/PixelPerfectCamera/Scripts/CanvasScreenSpaceScaler.cs:28)
    CanvasScreenSpaceScaler:OnValidate() (at Assets/Addons/PixelPerfectCamera/Scripts/CanvasScreenSpaceScaler.cs:54)

    Thing is I'm not using screen space overlay on anything in my game, anywhere on any canvas, let alone in the one that it's yelling at me for. It is in fact Screen Space - Camera. Strange. Everything is still working as intended, nothing is broken, that's just popping up every time I run my game. I'm going to comment out that debug just thought I'd give you a heads up though :)

    Thanks again for the awesome asset.


    edit: Also in your docs it says to use truecolor format. Is this RGBA 32 bit now?
     
    Last edited: Jan 25, 2017
  21. Douvantzis

    Douvantzis

    Joined:
    Mar 21, 2016
    Posts:
    73
    Hey flipwon. Thank you!

    Have you saved a prefab of the Canvas that uses Screen Space - Camera? In the prefab, the camera is not set and Unity returns ScreenSpaceOverlay as render mode. I can reproduce the issue using prefabs. I'll try to detect this case and silence the message.

    Yes it's RGBA 32, which results when setting compression to "none". I need to update the readme for the latest Unity!
     
  22. flipwon

    flipwon

    Joined:
    Dec 29, 2016
    Posts:
    93
    Ah yes, it is a prefab. Makes sense. Either way thanks a bunch for the asset dude, it's amazing.
     
  23. Douvantzis

    Douvantzis

    Joined:
    Mar 21, 2016
    Posts:
    73
    Just uploaded a fix.

    Glad to help :)
     
  24. BlisterFingers

    BlisterFingers

    Joined:
    Sep 2, 2015
    Posts:
    11
    Hey, lovely script, thanks for supplying it for free but I am having a small problem when I've come to use it in my latest project.

    My desired result is a pixel perfect view of my artwork at a 2x pixel size. On starting my game, the script clearly isn't working but if I fiddle with the settings enough it will eventually work. I can't figure out why and it seems very inconsistent so I made a video. (watch at 1080p)

    In the video, I show at the beginning that the script isn't working and then proceed to mess with the settings.

    At around the 2 minute mark I get it to work at 1x pixel size but then doubling the height just messes it up again.
    At 3:40 I even put the settings back to what they were at when it worked and it's not working again.
    Then, at the end at 4:04, I change the setting to what it was at the very start and BAM, it works... I am completely bewildered.

    Any help would be appreciated, thanks.
     
    Last edited: Feb 22, 2017
  25. Douvantzis

    Douvantzis

    Joined:
    Mar 21, 2016
    Posts:
    73
    Hey,

    Sorry, but I didn't go through all the video, but it seems to be working. My guess is that you don't understand what the script is supposed to do. Please watch the tutorial video from the asset, check out the readme and the documentation in the code to better understand its usage.

    For example, you should set the asset pixels per unit to the actual value used when importing your assets. I don't see the point in tweaking that.

    Secondly, the script isn't' supposed to make your pixel art render at 2x. If this is your intent, you can tweak the script and make it work that way. What the script does is setting the actual width of the camera as close as possible to the target width that you have set, while being pixel perfect. This means that the result may be 2X, 3X or whatever. This depends on the screen resolution. Setting a higher screen resolution will result in higher multiplier.

    The goal of the script is to keep the camera size (in units) near the target size. Your goal is to render everything in a specific scale (which results in different camera sizes for different screen resolutions).
     
    Last edited: Feb 22, 2017
  26. BlisterFingers

    BlisterFingers

    Joined:
    Sep 2, 2015
    Posts:
    11
    Thanks for your reply.

    I may, indeed, not fully understand your script. Ignore what I said about 2x, I'm aware that that's not it's intended purpose but that's not rellevant for my current problem. However, if I set the properties so that it reports 1x should I not have nice, clean, pixel perfection?

    Cause this is what I'm looking at right now.



    I should add that the point of the video was to demonstrate that if I just fiddle with the settings enough, it will eventually work, as you can see here. All I did was fiddle with the height settings and even through they're back to exactly the same values, it's now working.

    (I'm also aware that changing the PPU should be the correct value, in the video I was just confused and trying to figure out what settings made it work by fiddling with them all but since I've learned that I only need mess with the height value.)

    Another edit: I actually think moving my camera around might be helping the problem go away. I have no idea how but if I disable my movement script, I can't get it to start working no matter how much I fiddle. I've probably been moving my camera around without thinking about it.
     
    Last edited: Feb 22, 2017
  27. Douvantzis

    Douvantzis

    Joined:
    Mar 21, 2016
    Posts:
    73
    Yes you should be watching something pixel perfect, but you obsioudly aren't.

    Is every 3-column green thing a single sprite or separate sprites or even vertices?

    I noticed that PixelPerfectCamera reports a screen resolution of 1333x999, which are odd numbers in both dimensions. This results in the GPU trying to sample exactly between the sprite's texels. This can be a bit unpredictable.

    I bet that if you change your window so that the screen resolution is even numbers, the problem will go away.

    PS. also make sure that you have disabled AA. See all the other recommending settings in the script's readme.
     
  28. Douvantzis

    Douvantzis

    Joined:
    Mar 21, 2016
    Posts:
    73
    Seeing again the video makes me think that even though you are rendering in a pixel perfect way, the artifacts that you see can appear because of subpixel placement of your objects. This is why moving the camera (or the objects) makes the artifacts appear/disappear.

    When you are using sprites, this problem only shows up at the border of the sprite and is usually not visible. In your case the problem seems very intense. Is every green column a separate vertex?

    A fix would be to round the position of your objects. You can try placing the PixelSnap script on your objects and enable the RetroSnap option in the camera. Does this solve the issue?
     
    Last edited: Feb 22, 2017
  29. BlisterFingers

    BlisterFingers

    Joined:
    Sep 2, 2015
    Posts:
    11
    I've narrowed down the problem and how I manage to make it look right and made another, much shorter video which hopefully demonstrates better.

    Unfortunately my mic sound isn't good enough so I'll explain what happens.

    • At the start, you can see the pixels are messed up
    • I change the PPC height setting down to 10
    • Then I move the camera on X & Y until the artifacts are gone and it looks nice
    • I then set the PPC height back to it's original 40 and it still looks nice and remains looking nice even when I pan the camera around
    • If I set the camera back to 0,0 the problem returns.

    I'm not actually moving my objects, they're all perfectly aligned to unity's unit grid. with 1x1 unity units being mapped to 10x10 pixels in my texture which has mipmaps off and point filtering. And I have AA turned off. ( Check out the very end of the video for a quick look in edit mode set to shaded wireframe which should show how my custom mesh is set up)

    I suspect that you're right when you mention the screen's resolution. Let's say the screen is 701 pixels wide. The camera is at 0,0 so the bottom left corner is at -350.5. As far as I'm aware, GPUs start their drawing from the bottom left so this means that the sampling is offset by 0.5. When i've moved the camera, I'm setting that bottom left corner to a value which means that the pixels are not offset and it looks good.
    When I put it back to 0, I'm back to being off by 0.5 again. So, in theory, I should be able to fix my problem by just positioning my camera based on it's bottom left corner instead of it's centre and if the screen width or height is odd, then the camera just won't be perfectly centered but as it's only by a tiny amount, it won't matter.

    I believe the graphics stay looking nice after my adjustment when I'm panning the camera around because I'm using the mouse's position delta between frames divided by the PPU so I'm moving in 0.2 or other "good" increments.

    I was at the point of rolling my own solution but I can't imagine what I'd do differently than what your script does, I think I'd still have this problem.

    I'd love to hear any thoughts or suggestions you'd have but otherwise, thanks for your time and your input, it was extremely helpful and I think and I can manage to fudge a solution now.
     
  30. Douvantzis

    Douvantzis

    Joined:
    Mar 21, 2016
    Posts:
    73
    As I told you in the previous post, your problem is ton related to the script itself but to the relative position of the game objects to the camera. Can you upload a simple Unity project that demonstrates the issue?

    Regarding your explanation about the way the GPUs rasterization process works, you make a lot of assumptions and mention information that is not quite right. For example, DX9 samples the fragments from the top left edge instead of the center of the fragment.There are really a lot that can go wrong and I don't think it's worth discussing this in more depth.

    Why don't you adjust the scene's window size (or set a fixed size like 1920x1080) so that you end up with normal even size resolution like you will when your game will be running in a window or fullscreen? Does this solve the problem? I would refrain from panning the camera if the resolution is odd, because this case will never actually occur.

    Also, I didn't quite understand what your game objects are. It looks like each tile is a sprite. Are you using something else? You are not using canvas, right? Are you using the default sprite shader, without checking options like "snap" etc? A demo project would really help!

    Edit: Try to use PixelSnap Script on your object and enable RetroSnap in PPC. Did this help? if you take a look at the code of PixelSnap.sc it takes into account the screen resolution, the sprite anchor point, the relative position of the object to the camera etc.
     
    Last edited: Feb 22, 2017
  31. BlisterFingers

    BlisterFingers

    Joined:
    Sep 2, 2015
    Posts:
    11
    My apologies, I don't believe I've explained well. I know that's not very accurate. I expected everything to be aligned as the camera and object were both at 0,0. I was just trying to point out that the camera being at 0,0 doesn't matter if the window size is odd as the sampling begins at some corner or another.

    Anyway, I have managed to engineer my own solution just by moving and positioning my camera differently.

    Thanks for your help.
     
  32. Douvantzis

    Douvantzis

    Joined:
    Mar 21, 2016
    Posts:
    73
    It's not that the sampling begins somewhere. That's not the way rasterizing or sampling works, but I udnerstand what you're trying to say. The point the texture is sampled is related to:
    the relative position of the sprite to the camera,
    the vertex shader (if it adjusts the vertex coordinates),
    the anchor point of the sprite (this affects the texture offsets),
    the way the Graphics API selects fragments to render on screen (AA can interfere here),
    the fragment shader of the sprite (this is eventually where the final texture coordinates are calculated),
    Here's a link that will help you understand better what's going on:

    http://drilian.com/2008/11/25/understanding-half-pixel-and-half-texel-offsets/

    As I told you earlier, if you switch to DX9, having an odd resolution will work better than having an even resolution.

    When using something else than DX9 with AA turned off, 1:1 texel to fragment (pixel perfect enabled and ratio shows as : X1) then you end up with sampling from exactly the center of the coordinates (0,0) which is exactely between 2 texels.

    Anyway, I'm glad you have figured it out. I will prompt you again to see the source code of PixelSnap that takes care of a lot of those factors (apart from checking if DX9 is used).
     
  33. aaversa

    aaversa

    Joined:
    Sep 5, 2016
    Posts:
    21
    Does the pixel perfect camera support smooth, artifact-free CAMERA movement? For example my game is a dungeon crawler using 32px sprites. While game objects move around on the map using lerps, smoothdamp etc., the camera itself also moves to follow the player. Will this plugin make the camera movement artifact-free as well?
     
  34. Douvantzis

    Douvantzis

    Joined:
    Mar 21, 2016
    Posts:
    73
    PPC's primary purpose it to render a sprite pixel to an integer amount of screen pixels.

    It also comes bundled with PixelSnap script which you can add in every game object that you want its position adjusted relative to the camera. If you do that and enable "retrosnap" in PPC's settings, it will do what you want. You can check out the HTML5 demo and toggle this option to see it in action.
     
  35. aaversa

    aaversa

    Joined:
    Sep 5, 2016
    Posts:
    21
    OK. I'm having a lot of trouble since my game takes place on a large map, and while actors move, the camera also moves quite a bit. When the camera moves I get tons of artifacts and shimmer :/ It seems like PPC doesn't help with this, since the jitter is coming from the camera moving, not the sprites.
     
  36. Douvantzis

    Douvantzis

    Joined:
    Mar 21, 2016
    Posts:
    73
    Shimmering in 90% percent caused by not being pixel perfect. Do check the online demo of ppc and you ll understand.

    Also, the pixelSnap script included does exactly what you want. It makes the pixels of the sprites snap to the pixels of the screen to avoid subpixel artifacts like the ones discussed in the previous posts.

    Eirher way setting up ppc takes less than 1 minute so you check it out. It's free after all ;-)
     
  37. Cilerba

    Cilerba

    Joined:
    Dec 12, 2016
    Posts:
    4
    So far this asset has worked wonders for my game! So thank you for this amazing set of scripts.

    Unfortunately I've run into a bit of an issue however. From what I've seen, when played in fullscreen the pixel snapping should still occur but when I play a build of my game in full screen, the objects (all with the Pixel Snap script) do not align properly. Here is an image of the settings I have on my camera script: http://i.imgur.com/USj0tix.png (Posting a link because it's a large image)

    The game, in the editor, runs perfectly fine with pixel snapping even at the scale being 10x so I'm not exactly sure if I'm missing a step somewhere.
     
  38. Douvantzis

    Douvantzis

    Joined:
    Mar 21, 2016
    Posts:
    73
    Hello Cilerba. Perhaps there's a bug somewhere. Is pixel - perfect mode working or it's just the snapping that has a problem?

    When you play a build of your game in a window, does everything work? What are your build settings (OS, graphics API etc)? Please try to build one of the demo scene included in the asset. Do they work correctly?
     
  39. Cilerba

    Cilerba

    Joined:
    Dec 12, 2016
    Posts:
    4
    The snapping is the problem.

    The snapping doesn't work in windowed mode either yet when I build the demo scene (both of them), they both work perfectly (with the snapping). I've tried it on Windows 10 and on macOS (MacBook Pro). It doesn't seem like I'm missing any additional steps here when comparing it to the demo scenes so I'm not quite sure what exactly is going wrong.
     
  40. Douvantzis

    Douvantzis

    Joined:
    Mar 21, 2016
    Posts:
    73
    Is pixel perfect working in your project in windowed or fullscreen mode?

    In the same projects, the demo scenes work while your scene doesn't? This means that the problem is not related to the project's settings.

    Try changing the script execution order and placing PixelSnap.cs so that it runs after all the scripts.

    If this didn't help, you can try uploading a simplified version of the project so that I can try finding the problem and fixing it.
     
  41. Cilerba

    Cilerba

    Joined:
    Dec 12, 2016
    Posts:
    4
    Changing the script execution didn't work. At first I thought this may have something to do with the background + ground objects being meshes but when I looked closely the player, which is a sprite, moves 1 pixel at a time at full screen rather than several pixels at one to snap to the pixel grid.
     
  42. Douvantzis

    Douvantzis

    Joined:
    Mar 21, 2016
    Posts:
    73
    I can't help you further if you don't upload a project. Also, you didn't answer if pixel perfect is working.

    Judging form the screenshot you sent earlier, you screen resolution results to a x1 texture pixel to screen fragment ratio. So, it's only logical that everything moves 1 screen pixel at a time and not more. As a result, everything should snap correctly even without using the PixelSnap script. Using a higher screen resolution (in the editor) will show if pixel snapping is actually working or not.

    Make sure that you have set the pixels per unti to the actual value used by your sprites.
     
  43. Douvantzis

    Douvantzis

    Joined:
    Mar 21, 2016
    Posts:
    73
    @Cilerba @aaversa @BlisterFingers There was a bug in PixelSnap.cs script that didn't allow RetroSnap to work on game objects that were not sprites. Thus, they would move per screen pixel instead of snapping and the asset's pixels. Sprites did work correctly.

    The fix will be available in Pixel Perfect Camera v 1.41 when it gets reviewed and published.
     
  44. slkjdfv

    slkjdfv

    Joined:
    Oct 23, 2010
    Posts:
    424
    How do I fix this? It only occurs when I have your script enabled. Yes AA is off. My texture is set to clamp, uncompressed no mip-maps.

    Screen Tear.png
     
  45. Douvantzis

    Douvantzis

    Joined:
    Mar 21, 2016
    Posts:
    73
    The problem is not because of the script, but the fact that you render in a pixel-perfect way makes this issue visible.

    Are you using an atlas for each tile or a separate texture? If you are using an atlas, makes sure that mip-mapping is disabled and that the software you used to create the atlas has extruded the edges by 1 or 2 pixels. I think that Unity Sprite packer can do this. Personally, I use TexturePacker.
     
  46. Douvantzis

    Douvantzis

    Joined:
    Mar 21, 2016
    Posts:
    73
    Also, how are the distances between the tiles calculated? Are you sure they are correct? If the distance between 2 adjacent tiles is a bit more that what it should, this artifact can occur. You can try making the tiles snap to the screen pixels by using the PixelSnap.cs script on each tile and enabling "retro snap" on PPC.
     
  47. slkjdfv

    slkjdfv

    Joined:
    Oct 23, 2010
    Posts:
    424
    Yes it's an atlas, I created the tile set with pyxel edit and use the Super Tile Map editor. I'l try re-exporting it with spacing between tiles, thanks.
     
  48. slkjdfv

    slkjdfv

    Joined:
    Oct 23, 2010
    Posts:
    424
    That worked. I do get vertical black lines very rarely now but not to the extent I was getting before thank you :)
     
  49. Douvantzis

    Douvantzis

    Joined:
    Mar 21, 2016
    Posts:
    73
    Instead of spacing make sure that the individual sprites (in the atlas) have their edges extruded. This will make it work perfectly. Extrude is when the border pixels of all atlas sprites are extended and use the same color value as the initial border pixels.

    The reason that this problem occurs is that sometimes, a screen pixel will try to sample the sprite texture from a point exactly between 2 texels. So, the GPU will sometimes choose the nearest texel to the border texel of your sprite. If you use extrude, this texel will have the same value as the border one. So, the artifact will not be visible.
     
    slkjdfv likes this.
  50. festenhas

    festenhas

    Joined:
    Mar 9, 2017
    Posts:
    1
    Douvantzis, thanks for the free asset!

    But I'm having a problem, I can't seem to get my resolution to be 480x270.
    I started a project and did the following settings
    Main Camera: Scale 1x1x1, Orthographic Size 135,
    PixelPerfectCamera Script: Height 135, Assets Pixels Per Unit: 1, Pixel Perfect: Checked.

    With these settings my viewport gets resized to 262.25 x 147x50
    What am I doing wrong and how can I get a nice 480x270 resolution?
     
unityunity