Loading Image
Hello!
I've created some shaders to allow for bump mapped sprite rending in Unity that people might find useful.
I've made them into a single 'uber-shader' for sprites with a custom inspector in unity so you can change blend modes / lighting modes etc easily via the editor without dealing with swapping around a billion different shaders (technically it's 3 shader labs shaders behind the scenes).
It supports a whole load of lighting & blending modes:
Bump Maps This works both with Pixel and Vertex lighting modes.
Vertex Lighting Up to 4 vertex lights per sprite are supported - including bump map approximation (the data for the 4 vertex lights is passed to the fragment shader which is then used to render with the bump map).
Pixel Lighting Pixel lighting's a lot more accurate than vertex lighting but as well as being more expensive, with sprites it normally means you get loads of nasty overdraw of bits that should be hidden but are just rendered additvely on top of the sprite due to there being no depth tests. Because of this with Pixel lighting its recommended writing to depth and clip pixels below an alpha threshold meaning you wont get the overdraw BUT you will get hard alpha edges due to the clipping.
If your mesh has enough verts, Vertex shadings going to look pretty much the same but depending on your games art style pixel lighting may be a better option. Worth trying out both and seeing what works!
Premultiply Alpha toggle You can also choose whether a sprite uses premultiplied alpa or vanilla alpha blending using the blend modes drop down in unity .
Solid, Additive and Multipy blend modes
Diffuse Ramping which can be used to generate cartoony lighting effects
Color Adjustment Adjust the Hue, Saturation and Brightness of sprites and fade to a sold color for effects like taking damage.
Emission an emission channel can be optionally used when lighting the sprite.
Rim Lighting which works in both pixel and vertex lighting modes. If you use these shaders on a sprite you'll need a normal map texture in order to get the range of normals needed in rim lighting.
Render Queue is exposed meaning you can force a particular draw order between sprites.
Shadows All the shaders support casting shadows (with hard alpha clipping). Only Pixel lighting allows for receiving shadows(but vertex lighting and unlit modes still support casting shadows).
To use the shaders just import the attached unity package and then set your skeleton animations material's shader to one of the 3 sprite shaders (eg Game\Sprites Unlit) 🙂
Notes!!
Normals
For lighting sprites it's recommended to use 'Fixed Normals' - ie a constant defined normal rather than the mesh's normals. (Sprite verts always face the same way after all).
This Fixed Normal is defined in camera space and for Unity this should be negative Z axis (ie the inverse of Camera forward).
To do this in the material inspector make sure the 'Use Mesh Normals' box is unticked and the normal is (0, 0, 1).
You should also untick the 'Add Normals' box in the advanced section of the Skeleton Animation component as the mesh normals aren't required.
If in your game you rotate your sprites 180 degrees around Y to make sprites face the opposite way, you should also tick 'Allow Back Rendering' in order for lighting to work correctly when Sprites are facing away from the camera.
If you use negative scale or 'FlipX' instead then leave it unticked and it will save a little bit of processing time 🙂
Bump Mapping
To get bump maps working the model needs to have correctly generated tangents. You'll need the latest Spine Runtimes, then just click the 'Solve Tangents' box on your Skeleton Animation component.
Depth Writing
To get depth writing (ie hard alphas) working without Z fighting you will also need to set the animations Z spacing to something less than zero (the amount will depend on your cameras near and far planes). This is also set in the same place as the 'Solve Tangents' box above.
The shaders are now up on GitHub. To download them follow this link and click 'Clone or download' and then download as a zip. Copy the SpriteShaders folder into anywhere inside your Unity Assets folder.
https://github.com/traggett/UnitySpriteShaders