UnityPixelSnap in Surface shader (2D sprites)

Hi there.

Figured I’d dust off my Unity forum account and see if I can get some help on a problem I’m having.

I’m making a 2D game in Unity, but I want to use normal-bumped sprites for lighting effects. I want to keep things as simple as possible, so I am using the very basic Legacy/Transparent/Bumped Diffuse as the basis.

My goal is to have pixel-perfect art. I’m porting an older game, and the assets are made for a specific resolution, so I am forced to work with low-res assets. I want to avoid as much of the “the crawlies” as I can, ie: When a sprite moves at subpixel resolution, the pixels change shape in a very distracting manner.

However, the problem I am having is that I want to enable sub-pixel snapping in the shader, just like the Sprite/Default shader does. In that shader, they use UnityPixelSnap() to round up the post-processed vertices so they align with screen-space coordinates. That looks great and cuts down a lot of the “the crawlies” when I move objects via Rigidbodies, without me having to resort to scripts and extra GameObjects to fix the issue.

But here is what I cannot figure out: Surface shaders do not give us access (as far as I can tell) to the post-transformed vertices. You can define a vertex function with the #pragma vertex tag, but it seems to work on model-space vertices.

Is there any way to access post-transformed vertices in a Surface shader? I’m fairly weak on my Shader Lab and GLSL shaders. I know I can skip using a Surface shader and implement my own fragment/vertex shader myself, but that means integrating Unity’s lighting code and that’s making my eyes go cross.

If anyone has any ideas, I’d love to hear them! Thanks for your time.

Just insert this code in your shader:

#pragma multi_compile _ PIXELSNAP_ON

an then you can use it like that in your “vert” function:

2915918--215123--upload_2017-1-11_0-51-47.png

1 Like