• Unity
  • Sprite Shaders for Unity

@ToddRivers . In case you wanted to polish up your tool some more. I ran into this bug involving ambient occlusion. I tried messing around with the settings, but nothing seemed to do the trick. You can check out the screenshots here. Hope it helps. 🙂

@Pharan. Thanks for the explanation. I'll give it a try. :party:

 Loading Image

Related Discussions
...
ToddRivers wrote

Plus nice, was gonna ask you what you used to make gifs too! They show off lighting effects so much better than static images.

Well I think they way I did is not the best one haha.

I recorded the screen and imported to After Effects, cut the part I wanted to make a perfect loop, imported to photoshop and made a gif from there, lots of work haha.

@ToddRivers Hey, your shader is working pretty well for me. Below is an animation I did with the VertexLit shader which was later edited in Photoshop to add emission from the sword and a few minor tweaks (more on that below).

I have a few requests:

-An Emissive map. From what I can tell, it should be fairly simple to implement.

-Support for Opacity/Fade animation. As @RtFishers mentioned above, I have a similar problem with my animations. As my character's head turns to face the camera there is a sprite for his nose that is supposed to fade away as a new sprite (the "nose shadow") fades in. But I have observed that anytime I try to use opacity in my animations the shader seems to think it needs to DARKEN the sprite instead of just make it fade away (I had to edit the dark nose out in Photoshop). Now I'm guessing this could be a complicated issue, but I hope you can find a solution around it (otherwise I have limited options for the "3D head turn").

-Self-Shadowed Sprites. Now this could just be labelled wishful thinking, but if anyone has used Finn Morgan's SpriteLamp shader they will be familiar with the option for you to include a "Depth Map" into your material which the shader can then read as height and use some sort of increment function to make the sprite cast shadows on itself. But this sounds like a harder feature to add and I don't mind if it's not implemented anytime soon.

-Also, is it possible to enable the VertexLit shader to be able to receive shadows? I can't really use PixelLit as most of my characters have pieces that overlap everywhere.

Hi, I'm currently testing the shaders in the project my team's working on and so far I like them a lot.

I would also like to join LoneWarrior in request to add an emission channel to the shader.

16 days later

Hey guys! Sorry for going missing, I somehow didn't see the responses.

Firstly I've fixed some pesky typo's in the shaders where I wrote .rbg instead or .rgb grr
Anyways!

@AlaNintendo so first up you should never use mesh normals with rim lighting on spine meshes. Actually you shouldn't really use mesh normals at all on spine animations for lighting in general. The normals will just point outwards from the mesh and make it look like a flat bit of paper (which in essenese is what a sprite is!).
By using a shader normal the shaders will treat it so the normal is constant in camera space meaning the normal is always facing into the camera even when the sprite is not. This might sound weird but it ends up making the lighting look a lot more 3d and realistic for sprites.

For rim lighting however you need a range of normals for it to work (it needs to find the 'rims' of places where the normals change dramatically).
For this on Spine Animations you need to use a normal map. I use this which works pretty nicely: https://www.codeandweb.com/spriteilluminator

For your second image I've found a bug with the way I apply fog, I need to lerp the fog color to clear depending on the pixels alpha, I've now fixed this and it'll be in the latest shaders 🙂

What exactly is going on with ambient occlusion in the last pic? Is this a Screen Space Ambient Occlusion effect? Or something else?

@LoneWarrior I love that art style! Emission would be hella cool and something I'd love to have in my game so is prob next on the list 🙂
I'm curious as to whats going on with the alpha fade, will look into that too.

Self shadowed sprite would be very cool but very hard to implement I think. Decent normal maps can help give that illusion but actual shadows a prob going to be too hard for now.

Shadows for vertext lighting is something I looked into very briefly and think its possible just harder than pixel lit shadows - you can use pixel lighting with hard alphas though, the animation in you gif should work pretty well with that. It's more accurate than vertex lighting and supports full shadows and as long as your sprites got solid edges work fine (to get it to work turn on write to depth and play around with the Depth Alpha Cutoff).

I use pixel lighting in most place although I've got one character thats furry and pixel lighting doesnt work with this as furr needs full alpha blending to look right so for that character I use vertex lighting. Will look into shadows for that too!


16 Sep 2016, 12:31


Ok emission was nice and easy, it's in the latest shaders on the front page 🙂


16 Sep 2016, 16:45


@RtFishers I had a look at the alpha fade bug and think I've fixed it for vertex lighting as best I can , however you'll always get the fade to black problem when using pixel lighting.
Because pixel shading is done over multiple passes it has to use depth testing to stop drawing lit areas over the top of each other.
Because it writes to depth it blocks lighting happening on areas beneath the top most sprite. As this top sprite fades out you start seeing the unlit sprite behind it, unfortunately theres no way around this.
Unity standard shader does not allow for alpha fading in "Cutout" mode for this reason.
Anyhow if you're fading sprite in and out OR using any areas with partial alpha then I recommend using vertex lighting 🙂

@LoneWarrior I had a look into recieving shadows in vertext lighting and alas I don't think it's possible without some crazy hacks like I did to get normal maps working in vertex mode but I don't think theres enough texture coord channels to support both at the same time!
Unity's own docs say vertex lighting does not support shadows here (although it also says it doesnt support normal maps and managed to get that one to work 😉 )
https://docs.unity3d.com/Manual/RenderTech-VertexLit.html

Anyhow casting shadows still works fine in vertext mode, if you really want a character to recieve shadows I recommend authoring it without any faded edges and then using pixel lighting with write to depth enabled 🙂

@ToddRivers thanks a lot for implementing the emission channel! It works quite nice as you can see:

character in full light
 Loading Image

character in almost no light
 Loading Image

4 days later

@ToddRivers Thanks so much! Was trying to find other ways to contact you.... please don't disappear like that again 🙂. It's great that we also get an emission channel now, too. This shader is getting cooler all the time...

@Gabriev Woah that emission does look cool! It is pretty awesome for doing nice sci-fi lights or glowing runes etc!

I fixed a couple more bugs in the shaders - overlay color now works properly. It's super usefull for flashing sprites to a solid color when hit or for spell effects etc etc (as opposed to tint which is just multiplied with the texture color).

@RtFishers Haha I'll try not to!

@ToddRivers We modified the shader by adding Emission Power to it. (it's the last version, before your fixes). Feel free to include it in your package. It can be used to achieve brighter/more intensive light from the glow map and combined with some postprocessing like simple bloom it can produce neat effects:

 Loading Image

9 days later

@ToddRivers.

Thanks alot for the advice on the rim lighting and mesh normals. I'll give those a try once I fix up my spine animations.

What exactly is going on with ambient occlusion in the last pic? Is this a Screen Space Ambient Occlusion effect? Or something else?

Yea thats the default image effects from Unity's "Screen Space Ambient Occulsion" script. For some reason when I'm using the Blend Modes in your script, the AO from the background seems to render in front? or some kind of additive effect is happening? Sorry I wish I was more technical to explain. I was hoping the screenshots would help explain.

Let me know if I can do some other tests?

Thanks again for the support on this amazing tool! Much appreciated
It's very fun to use 🙂 🙂

5 days later

@AlaNintendo Ok I found the problem with Ambient Occulsion, it was caused by how Unity writes a special Depth+Normals texture, for that it needs the RenderType tag to be correct and I was setting it to "AlphaTest" when Unity needed "TransparentCutout" (even though you need to use AlphaTest for the Queue tag grr stupid Unity!).

Anyways it should work now but you might have to change something in the material property for it to update your material with the correct tag (just uncheck and then check the write to depth flag or something).

@Gabriev Nice! I've put your emission power in the latest shaders 🙂

Hi, I'm just trying to use the awesome shader package and ran into an issue where nothing would work.

I had an error showing up in the inspector panel for all shaders in Game>Shaders>Sprites saying "undeclared identifier 'unity_ObjectToWorld' d3d11 ShaderShared.cginc:15".

I'm stuck on Unity 5.3.4p6 for target platform reasons. I basically changed the line in ShaderShared.cginc from:

/* line:15 */  return mul(unity_ObjectToWorld, vertex);

to

return mul(_Object2World, vertex);

And everything now works.

Just thought it was worth mentioning for anyone else stuck on this!

18 days later

Hi,

I'm just looking at trying out these shaders again. I imported the package from the first post, but Im not sure what I should be expecting.

The character renders as all pink, and I believe I should be seeing a custom GUI for the shader settings?
This is what it looks like for me
 Loading Image

Is there anything I need to do after importing the package? Reading through the thread it seems like the required changes have been integrated into the latest runtime, which I just recently updated to.

Also, do these work with baked lighting and light probes? The biggest issue I am trying to solve is that when the character is facing left he is lighted differently than when facing right.

Thanks

Your import seems to be broken, even the inspector/editor code didn't load.
Delete all the files from the previous version and reimport the latest unitypackage.

yeah, not sure why they dont work for me. I'm on Unity 5.3.6

I had this issue. See my post a few posts up. Check your console to see if you see 1 error that matches the error mentioned in my previous post.

MattouBatou wrote

I had this issue. See my post a few posts up. Check your console to see if you see 1 error that matches the error mentioned in my previous post.

Ah, thanks that was it. I was not getting anything logged to the console, I must have missed your post. Thanks for the help

Glad it was helpful. I spent a good while scratching my head wondering why the inspector panel looked different to the screenshots in the rest of the thread. If it wasn't for the console logs I was getting, I would have been clueless too. It's a little odd that you didn't get any logs. Ah well, happy shading!

As a sidenote: I recommend Sprite Illuminator for creating your normal maps. I've been hand "carving" them using the angle brush tool and it's been a dream. I attempted setting up a workflow in Photoshop but was a pain previewing the normal map with lighting on a mesh. Sprite Illumintor has a few handy views you can flick between as you paint.

7 days later

Thanks for all your work @ToddRivers!

I'd definitely be paying for your work, please put it on the Asset Store! Is there a way to get in touch with you directly, maybe to get some active and responsive support, possibly supported by donations?
I thought Spine runtimes for Unity included some basic lit shaders but all the work you're doing is definitely a must have for every Unity Spine user!

Some general questions:

Which Unity Runtimes do your shaders work on? Have you already tested it on Unity 5.4.2f2?

Do the shader support ambient lighting, spot, omni and directional lights?