• Editor
  • (Solved-ish) A normal map skeleton shader for unity

Nate :

Not sure about why flipX is borked. 🙁 Your SkeletonComponent is pretty out of date, spine-unity has seen a lot of love lately. Unfortunately it only got more complicated with submeshes to support multiple page atlases.

The latest version still doesn't support Unity's built-in lighting though. I know shaders are complicated and I wish I was familiar with them, but I'm quite bummed that Spine doesn't support it yet. Thank you stray_train for putting so much effort into doing it.

Related Discussions
...

What needs to be done to support Unity's built-in lighting? The latest has normals and tangents, though the tangents aren't computed the same as stray_train's code.

@Nate, when flipX is set to true the normals point in the wrong direction so the lighting stops working. What I did at first is just reverse the direction of the normals. Fixed the lighting but shadows were getting drawn on top of the mesh (see first bad stuff screenshot). What I've tried doing now is reversing the order of the triangles as well and it sort of works but theres what appears to be z-fighting and incorrect sorting. I suspect the solution is to reverse all of the mesh data including the sorting order?

As for the shader sorting issues, it comes from this:
Unity doesn't support casting shadows from semi transparent objects because it's a "non-trivial problem". What you do instead is create a shader that discards pixels that are less than a certain cutoff value (so either a pixel is transparent or it isn't) and shadows are casted from what remains. This has sorting issues in the shader I made (it looks like it's getting sorted in reverse order). Using the built in unity cutout shaders actually works BUT there's z-fighting. The solution to this I'm investigating.

Question about these shaders:
How compatible might Unity ShaderLab shaders with CGPROGRAMs in them be with mobile?

I have no doubts about the current fixed function Skeleton shader 'cause I assume Unity is able to automatically take care of compatibility.
But I'm actually not knowledgeable regarding the compatibility of Unity and Cg vs. GLSL when it comes to mobile.

@Pharan I actually don't know :/ havent tried them out on mobile. If they don't work however, I can use the mobile cutout shader as a base and remake them without too much issue I think. Will investigate.

Also @Nate my suspicions seem to be correct, by reversing the draworder

if (skeleton.FlipX) drawOrder.Reverse();

Everything draws correctly! Only problem left is that the mesh oscillates violently between lit and unlit, my assumption being that the normals keep getting reset and then reversed. I'll try track it down. EDIT: was because I kept reversing the order of the triangles every frame
Summary of what I do:

  • I skeleton.FlipX is set to true, reverse the draworder, normals and triangles and set that to the mesh

Alright, I've solved the first problem but it's super hacky.

When skeleton.FlipX is set to true you have to reverse the arrays of drawOrder, mesh.triangles and mesh.normals. I believe every normal has to be (0, 0, -1) and this is relative to the mesh.triangles.

@Nate could you please add this is in?

Can you make a pull request so I can see exactly the changes you want? I don't have the setup to test the changes.

@Nate, I'm not sure how to that?

Here's the project zipped up? <


using the latest version from the repo

I've cleaned it up and changed the spineboy scene to illustrate where I am (press space to set FlipX to true)

See here:
https://help.github.com/articles/creati ... ll-request
https://help.github.com/articles/using-pull-requests
I suggest SmartGit, not the command line client:
http://www.syntevo.com/smartgithg/

I see your changes, though I don't know how to test them. drawOrder means something in the skeleton object model and should not be modified solely for rendering. You could keep a boolean flipX on the SkeletonComponent and if that is not equal to the skeleton.flipX then do whatever logic and set it equal.

@Nate - This is all I meant. (Web build in link)

http://www.cinoptstudios.com/lighttest/

I grabbed the latest version of the runtime and brought the ambient lighting way down with only one point light next to the example animation. It just doesn't get affected by any form of lighting in Unity with the current shader available in the runtime. Is there something new in the runtime that makes this happen easily or is the process still the same as what stray_train has been doing?

@hedayk, the built in shader is a vertex lit shader, similar to the "unlit transparent" that comes with unity, so lighting won't work in any case.

15 天 后

Hey guys, this looks absolutely amazing. I'm assuming the shadow casting only works with unity pro?

Hopefully stray_train or someone else will help Nate to incorporate this as part of Spine Unity, it's pretty incredible!

Keep watching the post, awesome stuff. Any chance for a current progress demo?

@stray_train - Is there any progress on your shaders for spine? Since this is taking up a lot of your time, I just wanted to let you know that I am willing to donate some money to you for the time you spent on this when it is complete... If I am able to get the code for it of course 😉

6 天 后

This is some excellent work! :clap: Even just some basic lighting on my Spine character will really take him to the next level.

I've tried following along with stray_train's progress over throughout this thread, but I'm not sure why I'm getting the result that am.

Here's my character with the default unlit skeleton shader along side the flat lit skeleton shader:
 Loading Image

animated:
 Loading Image
scene view, stopped vs play mode:
 Loading Image

Any idea what I'm doing wrong? What specific part of the shader or SkeletonComponent do I need to adjust to stop the pieces from blending together? Thanks.

Hey guys, it's awesome to see that people are still interested 🙂

@heydayk I haven't made any more progress because it's been hectic at varsity recently, might take a while before I get back to working on this.

@nomad I'm not really sure whats up, are you generating normals? I'll get the latest skeleton code and try this again when I have time.

I quickly pulled the latest code and dumped the lit shader into it. Result:
 Loading Image

What I observe:

When using multiple atlas pages, sorting doesn't work. @Nate mentioned that this should be fixed in the next unity update.

Some sort of additive blending occurs when skeletons get lit from close, I'm not sure why this is. If I create a quad, slap a texture on it and apply the lit shader everything works as intended. No z-fighting, no weird blending, nothing.

A note, for this screenshot I set zWrite = true in the shader. What this does is solve the sorting issues but introduces z-fighting in the skeletons (not for the quads, even when they occupy the same position).

What I suspect is the issue:

I think it's the way the skeletons are generated. They're not double sided so they go invisible if you flip them, turning backface culling off solves this but stops the lighting from working because normals are facing the wrong direction and the triangle order is wrong. I've tried to reverse both with no luck 🙁 Because the skeletons generate quads with the same z-value, z-fighting (order of images keeps changing rapidly) occurs. This ONLY happens with skeletons and I have no idea why flips table I'll study @Nate's code again when I can and see if I can perform some wizardry.

Maybe when the awesome new 2D stuff (sorting layers I'm looking at you) comes in we'll find a better solution :yes:

@stray_train:
Yep, I'm generating normals. In fact no texture appears at all without doing so.
I don't have much technical knowledge to lend, but I've continued experimenting with different conditions and I've found just one potential clue. I noticed that directional lights do not cause the blending effect, but point lights & spot lights do but only when their render mode is set to "Important" (or "Auto").

2 point lights, renderMode="Auto"
 Loading Image

2 point lights, renderMode="Not Important"
 Loading Image

Maybe you already knew about this and I missed part of the setup, but hopefully it informs more progress! By the way, I have Zwrite off in these examples, and my skeleton and quads appear double-sided.

@stray_train I agree with you about the new 2D stuff being updated in Unity. It looks like a lot of what Spine gives you will be integrated into Unity now. I'm glad I supported Spine, but I think I'm going to wait for Unity's update instead.

@nomad I'm also seeing the same issue you were with sprites blending with each other at the spots where they overlap. I'm not using the shader that stray_train wrote though, I'm using the built-in Unity Transparent Bumped Diffuse Shader.

Unfortunately, setting the Light to be "Not Important" means it will then use per vertex lighting for that light which isn't a good solution for my case. If I figure out what causing this I'll report back.