Jemeh

Hey! I've recently been working on a spine project that runs on a custom unlit vertex shader that draws outline behind the spine animation.
Now I've decided to include normal mapping to make the spine animations react to light! After some research, I figured that it is too difficult a task for me to get a vertex shader to work with Unity's GI system. As such, I resorted to convert my vertex shader into a surface shader.

Right now, I can't figure out how to make my surface shader work with tint black!
Below is my current code:
CGPROGRAM
#pragma surface surf Lambert alpha
#pragma target 3.0

sampler2D _MainTex;
sampler2D _BumpTex;

struct Input {
float2 uv_MainTex : TEXCOORD;
float2 uv_black1 : TEXCOORD1;
float2 uv_black2 : TEXCOORD2;
float4 vertexColor : COLOR;
};

half _Glossiness;
half _Metallic;
fixed4 _Color;
half _Cutoff;
float4 _Black;
fixed4 _ShadowDetectColor;

void surf (Input IN, inout SurfaceOutput o) {
fixed4 albedo = tex2D (_MainTex, IN.uv_MainTex);
clip (albedo.a - _Cutoff);

//o.Albedo = albedo.rgb;
o.Normal = UnpackNormal(tex2D(_BumpTex, IN.uv_MainTex));

if (IN.vertexColor.r == _ShadowDetectColor.r )
if (IN.vertexColor.g == _ShadowDetectColor.g )
if (IN.vertexColor.b == _ShadowDetectColor.b )
discard;

o.Albedo = (albedo * IN.vertexColor.rgb) +float4(((1-albedo.rgb) * (_Black.rgb + float3(IN.uv_black1.r, IN.uv_black1.g, IN.uv_black2.r)) * albedo.a*IN.vertexColor.a), 0);
o.Alpha = IN.vertexColor.a * albedo.a;
}
ENDCG
In the editor, If I change the fixed4 variable "_Black" to the color of my choosing, it actually works. I'm just not sure why the animation frames set in spine is not changing the "_Black" value by itself :think: :think:
Jemeh
  • Сообщения: 4

Harald

Spine will update the vertex colors, not the _Black color parameter. Not sure if I understood your question right though.
Аватара пользователя
Harald

Harri
  • Сообщения: 1105

Jemeh

Spine will update the vertex colors, not the _Black color parameter.
Hmm, I see! Thanks for letting me know!
I took this line:
return (texColor * i.vertexColor) + float4(((1-texColor.rgb) * (_Black.rgb + float3(i.uv1.r, i.uv1.g, i.uv2.r)) * texColor.a[i]_Color.a[/i]i.vertexColor.a), 0);
from spine's own tint black "Spine-Skeleton-TintBlack.shader" which worked fine before I converted my vertex shader into a surface shader. Changing the _Black color parameter during runtime manually in the editor still gave me the tint black effect, so I thought that was it. Sorry, I'm also a real beginner when it comes to shader coding.
Not sure if I understood your question right though.
Previously, my vertex shader was working with tint black. After I converted the shader into a surface shader, it stopped working. :'( My guess is that this line of code helps handle spine tint black:
o.Albedo = (albedo * IN.vertexColor.rgb) +float4(((1-albedo.rgb) * (_Black.rgb + float3(IN.uv_black1.r, IN.uv_black1.g, IN.uv_black2.r)) * albedo.a*IN.vertexColor.a), 0);

And somewhere in there, the tint black value is not being modified based on the exported spine bytes file.

After a little more research over the weekend, I am now suspicious of the "IN.uv_black1.r, IN.uv_black1.g, IN.uv_black2.r" I am using to replace the original's "i.uv1.r, i.uv1.g, i.uv2.r". Any ideas?
Jemeh
  • Сообщения: 4

Harald

Your Input struct seems to be problematic, at least according to the Unity docs:
https://docs.unity3d.com/Manual/SL-SurfaceShaders.html, section "Surface Shader input structure".
Regarding uv coords it states:
Texture coordinates must be nameduvfollowed by texture name (or start it withuv2to use second texture coordinate set).
So you could try
struct Input {
float2 uv_MainTex;
float2 uv1;
float2 uv2;
float4 vertexColor : COLOR;
};
What is strange is that according to the docs uv2 is the second tex coord set, which should be the third, but maybe they just made a mistake there. If not, you could try uv2 and uv3 instead.
Аватара пользователя
Harald

Harri
  • Сообщения: 1105

Jemeh

Hey! Thanks for the suggestion! I did try that already previously and got the following errors:
Shader error in 'Outline2D': redefinition of '_ST' at line 202 (on glcore)
Shader error in 'Outline2D': not enough actual parameters for macro 'TRANSFORM_TEX' at line 213 (on glcore)
Strange thing is, my shader ends at line 161. Here's my full code:
Shader "Outline2D" {
Properties {
_Color ("Main Tint", Color) = (1,1,1,1)
_OutlineColor ("Outline Color", Color) = (1,1,1,1)

_ShadowDetectColor ("Shadow Detect Color", Color) = (0,0,0,1)
_ShadowDrawColor ("Shadow Draw Color", Color) = (0.31,0.13,0.25,0.5)

_MainTex ("Albedo (RGB)", 2D) = "white" {}
_BumpTex ("Normal Map (RGB)", 2D) = "bump" {}

_Cutoff ("Main Alpha Cutoff", Range(0,1)) = 0.5
_OutlineCutoff ("Outline Alpha Cutoff", Range(0,1)) = 0.25
_LineOffset ("Outline Depth Offset", Range(0,-10000)) = -1000

_CharOffset ("Character Sort Layer", Range(-10000,10000)) = 2
_ShadowOffset ("Shadow Sort Layer", Range(-10000,10000)) = 2


_Color ("Tint Color", Color) = (1,1,1,1)
_Black ("Black Point", Color) = (0,0,0,0)
}
SubShader {
//Outline Pass
Tags {"Queue"="Transparent" "RenderType"="Transparent" "LightMode"="ForwardBase"}


Offset 1, [_LineOffset]
Cull Off
ZWrite On

CGPROGRAM
#pragma surface surf Lambert alpha:fade
#pragma target 3.0

sampler2D _MainTex;
sampler2D _BumpTex;

struct Input {
float2 uv_MainTex;
float2 uv_BumpTex;
fixed4 color :COLOR;
};

fixed4 _OutlineColor;
half _OutlineCutoff;
fixed4 _ShadowDetectColor;

void surf (Input IN, inout SurfaceOutput o) {
fixed4 albedo = tex2D (_MainTex, IN.uv_MainTex);
clip (albedo.a - _OutlineCutoff);

if (albedo.r == _ShadowDetectColor.r )
if (albedo.g == _ShadowDetectColor.g )
if (albedo.b == _ShadowDetectColor.b )
discard;

o.Albedo = _OutlineColor.rgb;
o.Alpha = IN.color.a;
}
ENDCG
//Standard Pass
Tags {"Queue"="Transparent" "RenderType"="Transparent"}
LOD 200

Cull Off
Offset -3, [_CharrOffset]
ZWrite On
Blend SrcAlpha OneMinusSrcAlpha

CGPROGRAM
#pragma surface surf Standard alpha:fade
#pragma target 3.0

sampler2D _MainTex;
sampler2D _BumpTex;

struct Input {
float2 uv_MainTex;
float2 uv1;
float2 uv2;
fixed4 vertexColor :COLOR;
};

half _Cutoff;
fixed4 _ShadowDetectColor;
float4 _Black;


void surf (Input IN, inout SurfaceOutputStandard o) {

fixed4 albedo = tex2D (_MainTex, IN.uv_MainTex);
clip (albedo.a - _Cutoff);

if (albedo.b == _ShadowDetectColor.b )
discard;

o.Albedo = (albedo * IN.vertexColor.rgb) +float4(((1-albedo.rgb) * (_Black.rgb + float3(IN.uv1.r, IN.uv1.g, IN.uv2.r)) * albedo.a*IN.vertexColor.a), 0);
o.Alpha = IN.vertexColor.a * albedo.a;

}
ENDCG




//Shadow Pass
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}

Offset -1, [_ShadowOffset]
Cull Off
ZWrite On
Blend SrcAlpha OneMinusSrcAlpha

CGPROGRAM
#pragma surface surf Lambert alpha
#pragma target 3.0

sampler2D _MainTex;

struct Input {
float2 uv_MainTex;
float4 vertexColor : COLOR;
};

fixed4 _ShadowDetectColor;
fixed4 _ShadowDrawColor;
half _Cutoff;

void surf (Input IN, inout SurfaceOutput o) {
fixed4 albedo = tex2D (_MainTex, IN.uv_MainTex);
clip (albedo.a - 0.3);

if (albedo.r != _ShadowDetectColor.r )
if (albedo.g != _ShadowDetectColor.g )
if (albedo.b != _ShadowDetectColor.b )
discard;
albedo.r = _ShadowDrawColor.r;
albedo.g = _ShadowDrawColor.g;
albedo.b = _ShadowDrawColor.b;

o.Albedo = albedo.rgb;
o.Alpha = albedo.a;
}
ENDCG
}
FallBack "Diffuse"
}
Look for the //Standard Pass
Jemeh
  • Сообщения: 4

Harald

Strange thing is, my shader ends at line 161.
You would need to find the relevant include that is referenced there, which will get even worse as the surface shaders are just a custom Unity part that will be assembled to complete vertex and fragment shaders later, they are not an official hlsl or glsl stage. This makes it worse.

What always helps is to download the built-in shaders from their download page, expand the Downloads (Win) dropdown and select the shaders option:
https://unity3d.com/get-unity/download/archive

You could also have a try if it works to manually fill in the relevent vertex data parts, as described
here, section "Normal Extrusion with Vertex Modifier" and next section below.

---

Apart from that, you could ask on the official Unity forums, they should be more aware of this kind of error messages or how to get your additional uv sets into the surface shader.
Аватара пользователя
Harald

Harri
  • Сообщения: 1105

Jemeh

I see! I'll go check it out! Thank you for your time and helpful reply! Will post my findings here!
Jemeh
  • Сообщения: 4


Вернуться в Runtimes