sarahnorthway

I'm trying to incorporate Spine animated sprites into an existing surface shader in Unity. Everything works except the lighting, which has a ghostly overlapping effect on the individual sprites of my character (see attached screenshot). Here is (a simplified version of) my shader:
Shader "Northway/SpritesDiffuseWave" {
Properties {
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
}

SubShader {
Tags {
"Queue"="Transparent"
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
"CanUseSpriteAtlas"="True"
}

Cull Off
Lighting Off
ZWrite Off
Blend One OneMinusSrcAlpha

CGPROGRAM
#pragma surface surf SimpleLambert vertex:vert nolightmap nodynlightmap keepalpha noinstancing
#include "UnitySprites.cginc"

struct Input {
float2 uv_MainTex;
fixed4 color;
};

half4 LightingSimpleLambert(SurfaceOutput s, half3 lightDir, half atten) {
half4 c;
c.rgb = s.Albedo * _LightColor0.rgb * atten;
c.a = s.Alpha;
return c;
}

void vert (inout appdata_full v, out Input o) {
UNITY_INITIALIZE_OUTPUT(Input, o);
o.color = float4(1, 1, 1, 1);

// ... unrelated custom shader code
}

void surf (Input IN, inout SurfaceOutput o) {
fixed4 color = tex2D(_MainTex, IN.uv_MainTex);
o.Albedo = color.rgb;
o.Alpha = color.a;
}

ENDCG
}
}
Looking at the compiled shader, I see Unity performs two passes: "LightMode" = "ForwardBase" and "LightMode" = "ForwardAdd". By contrast, Spine's "Skeleton Lit" frag shader combines them in one "LIGHTMODE"="Vertex" pass.

I don't understand what's going wrong - why Spine characters look ghostly when my Unity sprites using the same shader look fine. What is different about them? How do I fix it?

I would rather adapt my existing surface shader to work than modify Spine's frag shader.

---

This may be related to a known bug http://esotericsoftware.com/forum/Lit-shader-problem-with-layers-11810?p=52916&hilit=shader#p52916 in which the pixel lit shader(s) behave in the same ghostly overlapping way. Whatever the fix is, it may also help me and my custom shader.

BTW when I try the Spine/Sprite/Pixel Lit shader on my sprites I can't seem to generate any lighting at all, or fog. I'm using version 3.7 of the Unity sdk.

---

This may be related to a known bug http://esotericsoftware.com/forum/Lit-shader-problem-with-layers-11810?p=52916&hilit=shader#p52916 in which the pixel lit shader(s) behave in the same ghostly overlapping way. Whatever the fix is, it may also help me and my custom shader.

BTW when I try the Spine/Sprite/Pixel Lit shader on my sprites I can't seem to generate any lighting at all, or fog. I'm using version 3.7 of the Unity sdk.
Attachments
spine_unity_lighting.png
sarahnorthway
  • Posts: 9

Harald

Sorry to hear that you are facing problems.

I am currently looking into the other issue you mentioned above.

You could have a quick test if changing blending from additive:
Blend One OneMinusSrcAlpha
to normal blending (if you receive black outlines tue to atlas exported with Premultiply alpha, you can disable this and export as straight alpha with bleed instead):
Blend SrcAlpha OneMinusSrcAlpha
I will keep you updated if can provide any insights into the issue.
User avatar
Harald

Harri
  • Posts: 3975

sarahnorthway

I tried switching to straight alpha and using Blend SrcAlpha OneMinusSrcAlpha, but am still seeing that ghostly effect.

Using Blend SrcAlpha also produces a thin bright outline around lit Spine sprites and more noticeable large outline around Unity sprites using the same shader (screenshot attached). Adding o.Albedo *= color.a to my surf function fixed this but added black outlines to both (both using straight alpha). I'll stick to additive for now and keep an eye on this issue. Let me know if there's anything I can do to help troubleshoot this.
Attachments
spine_ghost_SrcAlpha.png
sarahnorthway
  • Posts: 9

Harald

I have now created a writeup of the problem and possible solutions on the issue ticket:
[unity] Bug in pixel-lit shader - parts shining through · #1335

Long story short: ZWrite needs to be enabled and minimal Z Spacing set at the SkeletonAnimation or SkeletonRenderer component.
I will update the shader and provide some mechanisms to warn/adjust improper Z Spacing.
User avatar
Harald

Harri
  • Posts: 3975


Return to Unity