• Unity
  • Sprite Shaders for Unity

Related Discussions
...

ToddRivers, maybe you could use GitHub or even a Gist? Then we'd have a history of your changes, which would help us keep the runtime copy up to date.

Although, worst case scenario: I just need to make a blank Unity project, version control that somewhere, and just check the resulting changes every time I update to ToddRivers' latest package.

Yeah let me stick them in a GitHub depot quickly, that makes the best sense and it's something I should've done ages ago (I've just been checking them in my games own git depot).


14 Nov 2016, 14:04


Ok here you go: https://github.com/traggett/UnitySpriteShaders/tree/master/SpriteShaders
I'll put a link to it in the first post too.

I also realised the pixel lit shader wasn't calculating non important vertex lights in its base pass! I never use them so never noticed but thats now fixed 🙂

ToddRivers a écrit

I also realised the pixel lit shader wasn't calculating non important vertex lights in its base pass! I never use them so never noticed but thats now fixed 🙂

I'm going to use them a lot with bump mapping instead! 😃

A thing that could be missing would be a normal map intensity multiplier, to better fine tune the bump, or to create effects like transitioning to 3d-like to fully flat texture.

I'd do that myself, but would end up with having to make that change on every update, would it be possible to have this feature in the next shader versions?

I think a multiplier (or a color where applicable) could be handy to fine tune the influence amount of other textures in the shader.

Of course, to give some artistic freedom, there wouldn't be any need to clam or limit the value, so a simple float(also including negative values) would be more than welcome 🙂

@[supprimé]
Can I ask how you create your normal maps for your spine animated art? I'm looking at using Sprite Illuminator and am looking for any tips / guidance in regards to creating normal maps for a spine animated character. Are you just painting the normal of the packed texture that Spine exports?

rxmarcus a écrit

@[supprimé]
Can I ask how you create your normal maps for your spine animated art? I'm looking at using Sprite Illuminator and am looking for any tips / guidance in regards to creating normal maps for a spine animated character. Are you just painting the normal of the packed texture that Spine exports?

I was using the same tool (trial edition) but I think I'll be using this http://www.2deegameart.com/p/sprite-dlight.html

Hey is anyone using the Unity 5.5 beta?
I'm trying it at the mo and for some reason with these shaders the Fixed View Space Normal now needs to be flipped in Z (so (0,0,1) instead of (0,0,-1).

No idea why this would be the case :S


17 Nov 2016, 16:49


Ok looking at the upgrade guide it seems Unity has flipped the Z Buffer direction (so 1 is at the camera and 0 is furtherest away).
https://docs.unity3d.com/550/Documentation/Manual/UpgradeGuide55.html
and this is causing it.

Is #if defined(UNITY_REVERSED_Z) a Cg directive?

Ooh thanks, I hadnt heard of that define but using it did the job!
Default directx mode must've changed in 5.5 so now UNITY_REVERSED_Z is true.

Will check in a fix for that in github!

I'm trying to make the Spine version defaults Spine-friendly.
I'm mostly done.
But how do I make it so that "use mesh normals" is false by default?
I saw the conditionals and I got super confused. :p

@[supprimé]

ToddRivers a écrit

Hmm thats weird. The spike is happening when its first setting that shader for use I think. It could be caused by the Unity running out of shader keywords (the defines used to turn shader features on and off, these shaders now have a fair few of them to allow you to switch all the feature on/off).
At the mo I think theres a 64 keyword limit but its going up to 256 in Unity 5.5 so that might fix it!

I've uploaded new shaders that improve the fake spherical harmonics in vertex lighting mode. Should be a lot faster and look a bit better too 🙂

Thanks man you're awesome! Been busy recently, so haven't had time to play in Unity, but will let you know how it goes when I do. :beer:

@Pharan Sorry for the delay. I've just checked in an update to the shaders that cleans up a few editor things and it also adds a new function called 'SetDefaultSpriteKeywords'.
All you need to do is uncomment out the line that says //SetKeyword(material, "_FIXED_NORMALS", true) 🙂

Hi @ToddRivers,

I'm using the shaders of about a week ago and I'm facing a few issues when the sprite gets exposed to Spotlights with Intensity of 4:

Normal:

Lit by a spotlight with an intensity of 4 (using HDR doesn't change anything)

This is the material I'm using:
Note that even changing the blending mode to premultiplied alpha I get the same results.
It's like the sprite is getting... transparent, you can see through it like it was made of veils.

Note how selecting Pixel Lit the inspector still shows Vertex Lit.
Also, sometimes when switching between shaders the shader crashes and shows a pink/error material. Selecting again the shader from the list solves the issue.

Hey! Sorry that was my bad, I forgot to change the code when I changed the shaders names so the shaders weren't switching. I actually fixed it just before you posted, if you grab latest the problem will disappear 🙂

I've also been messing around with post effects in my project and noticed if you generate a DepthNormals texture for a camera then the normals would be random for any sprites that use a FixedNormal.

I made a new RenderType for sprites with fixed normals and then made a custom shader for creating a DepthNormals texture which renders Sprites properly.
Its prob not of interest for most peops but if you want to use SSAO or a similaur effect that needs the scenes normals then it might be usefull, its on the github anyways 🙂

ToddRivers a écrit

Hey! Sorry that was my bad, I forgot to change the code when I changed the shaders names so the shaders weren't switching. I actually fixed it just before you posted, if you grab latest the problem will disappear 🙂

I've also been messing around with post effects in my project and noticed if you generate a DepthNormals texture for a camera then the normals would be random for any sprites that use a FixedNormal.

I made a new RenderType for sprites with fixed normals and then made a custom shader for creating a DepthNormals texture which renders Sprites properly.
Its prob not of interest for most peops but if you want to use SSAO or a similaur effect that needs the scenes normals then it might be usefull, its on the github anyways 🙂

Nice, the new shaders don't crash anymore!

Do you have any clue about the PixelLit shader becoming transparent as per picture?

Yeah so Pixel/Forward lighting is done over multiple passes (1 pass for each light) meaning bits of the sprite lit by additional lights will draw over the top of each other and give that effect as there is no depth test being performed.
To use pixel lighting you need to turn on depth writing, however as you cant write a translucent depth value you need to use a hard clipped alpha rather than having a nice smooth faded alpha.
Vertex lighting is done in one pass meaning you wont get that translucent overdraw and can have fully faded alpha but isn't as accurate (although if your sprite has enough verts you won't notice).

It depends on the project / sprite what mode works best. I'd recommend vertex lighting most of the time though but have a play with the two modes and turning write to depth on/off as well as adjusting the alpha depth cutoff value.

ToddRivers a écrit

Yeah so Pixel/Forward lighting is done over multiple passes (1 pass for each light) meaning bits of the sprite lit by additional lights will draw over the top of each other and give that effect as there is no depth test being performed.
To use pixel lighting you need to turn on depth writing, however as you cant write a translucent depth value you need to use a hard clipped alpha rather than having a nice smooth faded alpha.
Vertex lighting is done in one pass meaning you wont get that translucent overdraw and can have fully faded alpha but isn't as accurate (although if your sprite has enough verts you won't notice).

It depends on the project / sprite what mode works best. I'd recommend vertex lighting most of the time though but have a play with the two modes and turning write to depth on/off as well as adjusting the alpha depth cutoff value.

Thanks for the explanation!

I kind of managed to make it work, even if I had to ramp up the lights to obtain the same overbright effect.
Z write on, premultiplied apha, and Z spacing < 0.

The lighting still cumulates when different instances of the characters on the same Sorting Layer and Layer Order overlap.

Should I fix this by assigning an incremental value to the layer index, by offsetting sprites randomly on the Z axis, or is there any other shader-based solution available?

(2 final notes: I can't seem to be able to illuminate vertex lit sprites, and I can't choose "Solid" blend from the shader's dropdown menu anymore)

Ok I've fixed the Solid dropdown bug. Not sure whats going on with vertex lighting for you, what's your setup? Are you using mesh normals or fixed normals, is the sprite flipped or has it got negative scale?
Vertex lights working fine for me.

Yeah you'll want to shift them along the z axis if your using write to depth and pixel lighting otherwise you'll get Z fighting.

ToddRivers a écrit

Ok I've fixed the Solid dropdown bug. Not sure whats going on with vertex lighting for you, what's your setup? Are you using mesh normals or fixed normals, is the sprite flipped or has it got negative scale?
Vertex lights working fine for me.

Yeah you'll want to shift them along the z axis if your using write to depth and pixel lighting otherwise you'll get Z fighting.

Solved, I thought some basic normals were calculated. I checked "calculate normals" and used mesh normals and all is running fine 🙂

Thanks for fixing the issues so quickly!

Yet, it would have been nice to have both alpha blended sprites and proper lighting together on mobile - I had hand painted characters and having sharp crisp cutout edges somehow ruined the "painted" effect. I'm not sure if using deferred rendering is going to be good on mobile...

6 jours plus tard

Hi!, I'm new using this shader and I have this problem and I don't know how to fix it.

Image supprimée en raison de l'absence de support de HTTPS. | Afficher quand même

Whenever I choose one of your shaders the Spine skeleton is rendered this way.
This are the settings for the atlas texture:

Image supprimée en raison de l'absence de support de HTTPS. | Afficher quand même

Also I'm exporting from Spine as pre-multiplied alpha. Straight alpha doesn't work too.
Help?

Thanks!