Cavi

Greetings,

as I was using a coloring shader in the past to.., well, to color my sprites in every color I desire (not only tint), I wanted to try out the Skeleton Lit shader. I was pretty happy that it worked fine, but now I would like to combine these two. The problem is, when it comes to shaders I am like John Snow. That's why I tried a lot of things. When I reached a point, where I realised that I could delete almost everything from the Skeleton Lit shader and it sill worked, I gave up. I don't know why it is still working, and I have no idea, where I can put all the coloring parameters in.

That is the rest of the Skeleton Lit shader and it still works fine.
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "black" {}
[MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
_Cutoff ("Shadow alpha cutoff", Range(0,1)) = 0.1
}

// 2 texture stage GPUs
SubShader {
Tags
{
"Queue"="Transparent"
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
"CanUseSpriteAtlas"="True"
}

LOD 100

Cull Off
ZWrite Off
Blend One OneMinusSrcAlpha

Pass {
Tags { "LightMode"="Vertex" }
ColorMaterial AmbientAndDiffuse
Lighting On
SetTexture [_MainTex] {
Combine texture * primary DOUBLE, texture * primary
}
}
}
For anyone who is interested or can help me, this is a shader to colorize sprites:

Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
_Color ("Tint", Color) = (1,1,1,1)
_FlashColor ("Flash Color", Color) = (1,1,1,1)
_FlashAmount ("Flash Amount",Range(0.0,1.0)) = 0.0
}


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

Cull Off
Lighting Off
ZWrite Off
Fog { Mode Off }
Blend One OneMinusSrcAlpha

Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile DUMMY PIXELSNAP_ON
#include "UnityCG.cginc"

struct appdata_t
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
};

struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
half2 texcoord : TEXCOORD0;
};

fixed4 _Color;
fixed4 _FlashColor;
float _FlashAmount;

v2f vert(appdata_t IN)
{
v2f OUT;
OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
OUT.texcoord = IN.texcoord;
OUT.color = IN.color * _Color;
#ifdef PIXELSNAP_ON
OUT.vertex = UnityPixelSnap (OUT.vertex);
#endif

return OUT;
}

sampler2D _MainTex;

fixed4 frag(v2f IN) : COLOR
{
fixed4 c = tex2D(_MainTex, IN.texcoord) * IN.color;
c.rgb = lerp(c.rgb,_FlashColor.rgb,_FlashAmount);
c.rgb *= c.a;

return c;
}
ENDCG
}
}
I would be very grateful for any help :)


Edit: To answer my first question: The part I deleted in the skeleton lit, casts shadows in 3D space, which I'm not using. Working on the second, now.

Edit2: I managed to merge the shader, by taking the generated code. The rest is tweaking.

Edit3: Maybe my last two edits weren't clear enough. I solved my problem. :)
Cavi
  • Сообщения: 8

Pharan

Just throwing this out there. I don't know if it'll be helpful for you: Sprite Shaders for Unity
Аватара пользователя
Pharan
  • Сообщения: 5366

SoulGame

Wow, that's pretty much exactly the question I came to ask. I'm trying to merge Spine/Skeleton with DefaultColorFlash, but it's quite a mess. I can't get rid of all errors. (Although I'm not sure why I'm trying to do that since I have no idea what Spine/Skeleton is meant for, I'm just reluctant to remove it since it's probably needed by spine-runtime somehow)

Anyway, if you could share your progress with the merging it would be nice, you might be John Snow, but I'm Hodor here.
Аватара пользователя
SoulGame
  • Сообщения: 109

Cavi

Pharan писал(а):Just throwing this out there. I don't know if it'll be helpful for you: http://esotericsoftware.com/forum/Sprite-Shaders-for-Unity-5838
I saw this thread. It didn't help me any bit. Thanks anyway ;)
SoulGame писал(а):Wow, that's pretty much exactly the question I came to ask. I'm trying to merge Spine/Skeleton with DefaultColorFlash, but it's quite a mess. I can't get rid of all errors. (Although I'm not sure why I'm trying to do that since I have no idea what Spine/Skeleton is meant for, I'm just reluctant to remove it since it's probably needed by spine-runtime somehow)Anyway, if you could share your progress with the merging it would be nice, you might be John Snow, but I'm Hodor here.
Yeah sure. It's enough that one persons throws away a little life time :D

But be prepared, as I mentioned in my edit, I took the generated code. The lighting-shader code is pretty messed up now (but at the moment I don't really care too much about a 'clean shader code')
To clear things up at least a little here. Every adjustment I took is marked with the comment "//color shader"
You will find those at line: 102, 124, 150, 160
Shader "YourProjectHere/YourFavouriteShaderName"
{
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
_Color ("Tint", Color) = (1,1,1,1)
_FlashColor ("Flash Color", Color) = (1,1,1,1)
_FlashAmount ("Flash Amount",Range(0.0,1.0)) = 0.0
[MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
_Cutoff ("Shadow alpha cutoff", Range(0,1)) = 0.1
}


// 2 texture stage GPUs
SubShader {
LOD 100
Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }

Cull Off
ZWrite Off
Blend One OneMinusSrcAlpha

Pass {
Tags { "LIGHTMODE"="Vertex" "QUEUE"="Transparent" "IGNOREPROJECTOR"="true" "RenderType"="Transparent" }
ZWrite Off
Cull Off
Blend One OneMinusSrcAlpha

CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityShaderVariables.cginc"
#pragma multi_compile_fog
#include "UnityCG.cginc"
#define USING_FOG (defined(FOG_LINEAR) || defined(FOG_EXP) || defined(FOG_EXP2))

// ES2.0/WebGL/3DS can not do loops with non-constant-expression iteration counts :(
#if defined(SHADER_API_GLES)
#define LIGHT_LOOP_LIMIT 8
#elif defined(SHADER_API_N3DS)
#define LIGHT_LOOP_LIMIT 4
#else
#define LIGHT_LOOP_LIMIT unity_VertexLightParams.x
#endif
#define ENABLE_SPECULAR (!defined(SHADER_API_N3DS))

// Compile specialized variants for when positional (point/spot) and spot lights are present
#pragma multi_compile __ POINT SPOT

// Compute illumination from one light, given attenuation
half3 computeLighting (int idx, half3 dirToLight, half3 eyeNormal, half3 viewDir, half4 diffuseColor, half shininess, half atten, inout half3 specColor) {
half NdotL = max(dot(eyeNormal, dirToLight), 0.0);
// diffuse
half3 color = NdotL * diffuseColor.rgb * unity_LightColor[idx].rgb;
return color * atten;
}

// Compute attenuation & illumination from one light
half3 computeOneLight(int idx, float3 eyePosition, half3 eyeNormal, half3 viewDir, half4 diffuseColor, half shininess, inout half3 specColor) {
float3 dirToLight = unity_LightPosition[idx].xyz;
half att = 1.0;
#if defined(POINT) || defined(SPOT)
dirToLight -= eyePosition * unity_LightPosition[idx].w;
// distance attenuation
float distSqr = dot(dirToLight, dirToLight);
att /= (1.0 + unity_LightAtten[idx].z * distSqr);
if (unity_LightPosition[idx].w != 0 && distSqr > unity_LightAtten[idx].w) att = 0.0; // set to 0 if outside of range
dirToLight *= rsqrt(distSqr);
#if defined(SPOT)
// spot angle attenuation
half rho = max(dot(dirToLight, unity_SpotDirection[idx].xyz), 0.0);
half spotAtt = (rho - unity_LightAtten[idx].x) * unity_LightAtten[idx].y;
att *= saturate(spotAtt);
#endif
#endif
att *= 0.5; // passed in light colors are 2x brighter than what used to be in FFP
return min (computeLighting (idx, dirToLight, eyeNormal, viewDir, diffuseColor, shininess, att, specColor), 1.0);
}

// uniforms
int4 unity_VertexLightParams; // x: light count, y: zero, z: one (y/z needed by d3d9 vs loop instruction)
float4 _MainTex_ST;

// vertex shader input data
struct appdata {
float3 pos : POSITION;
float3 normal : NORMAL;
half4 color : COLOR;
float3 uv0 : TEXCOORD0;
};

// vertex-to-fragment interpolators
struct v2f {
fixed4 color : COLOR0;
float2 uv0 : TEXCOORD0;
#if USING_FOG
fixed fog : TEXCOORD1;
#endif
float4 pos : SV_POSITION;
};

// color shader
fixed4 _Color;
fixed4 _FlashColor;
float _FlashAmount;

// vertex shader
v2f vert (appdata IN) {
v2f o;
half4 color = IN.color;
float3 eyePos = mul (UNITY_MATRIX_MV, float4(IN.pos,1)).xyz;
half3 eyeNormal = normalize (mul ((float3x3)UNITY_MATRIX_IT_MV, IN.normal).xyz);
half3 viewDir = 0.0;
// lighting
half3 lcolor = half4(0,0,0,1).rgb + color.rgb * glstate_lightmodel_ambient.rgb;
half3 specColor = 0.0;
half shininess = 0 * 128.0;
for (int il = 0; il < LIGHT_LOOP_LIMIT; ++il) {
lcolor += computeOneLight(il, eyePos, eyeNormal, viewDir, color, shininess, specColor);
}
color.rgb = lcolor.rgb;
color.a = color.a;

// color shader
o.color = IN.color * _Color * saturate(color);

// compute texture coordinates
o.uv0 = IN.uv0.xy * _MainTex_ST.xy + _MainTex_ST.zw;
// fog
#if USING_FOG
float fogCoord = length(eyePos.xyz); // radial fog distance
UNITY_CALC_FOG_FACTOR(fogCoord);
o.fog = saturate(unityFogFactor);
#endif
// transform position
o.pos = mul(UNITY_MATRIX_MVP, float4(IN.pos,1));
return o;
}

// textures
sampler2D _MainTex;

// fragment shader
fixed4 frag (v2f IN) : SV_Target {
fixed4 col;
fixed4 tex, tmp0, tmp1, tmp2;
// SetTexture #0
tex = tex2D (_MainTex, IN.uv0.xy);

// color shader
col.rgb = tex * IN.color;

// color shader
col *= 1.8;
col.a = tex.a * IN.color.a;
// fog
#if USING_FOG
col.rgb = lerp (unity_FogColor.rgb, col.rgb, IN.fog);
#endif

// color shader
col.rgb *= lerp(col.rgb, _FlashColor.rgb, _FlashAmount);
col.rgb *= col.a
return col;
}

// texenvs
//! TexEnv0: 02010103 01050107 [_MainTex]
ENDCG
}

Pass
{
Name "Caster"
Tags { "LightMode"="ShadowCaster" }
Offset 1, 1

Fog { Mode Off }
ZWrite On
ZTest LEqual
Cull Off
Lighting Off

CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_shadowcaster
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
struct v2f {
V2F_SHADOW_CASTER;
float2 uv : TEXCOORD1;
};

uniform float4 _MainTex_ST;

v2f vert (appdata_base v) {
v2f o;
TRANSFER_SHADOW_CASTER(o)
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
return o;
}

uniform sampler2D _MainTex;
uniform fixed _Cutoff;

float4 frag (v2f i) : COLOR {
fixed4 texcol = tex2D(_MainTex, i.uv);
clip(texcol.a - _Cutoff);
SHADOW_CASTER_FRAGMENT(i)
}
ENDCG
}
}
// 1 texture stage GPUs
SubShader {
Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
LOD 100

Cull Off
ZWrite Off
Blend One OneMinusSrcAlpha

Pass {
Tags { "LightMode"="Vertex" }
ColorMaterial AmbientAndDiffuse
Lighting On
SetTexture [_MainTex] {
Combine texture * primary DOUBLE, texture * primary
}
}
}
}
You should be able to copy-paste the whole thing. (and adjust the name). I hope I can save you some headaches. Hodor.

Edit: I just realized, that you asked for the 'normal' Skeleton shader. I could replace it with the DefaultColorShader. That doesn't work for you?
Cavi
  • Сообщения: 8

Pharan

Nice work, Cavi. It's so annoying just to get extra functionality on top of a vertex lit shader.
What's TexEnv0?
Аватара пользователя
Pharan
  • Сообщения: 5366

Cavi

Pharan писал(а):Nice work, Cavi. It's so annoying just to get extra functionality on top of a vertex lit shader.
What's TexEnv0?
Thanks. I'm glad if it helps.

TexEnv0 is a magical comment which appeared after generating the shader code.

I'm sorry but since google won't help me out, I have no clue.
Cavi
  • Сообщения: 8


Вернуться в Unity