Kaynağa Gözat

[unity] Fixed `Spine/Sprite` core shaders now correctly drawing Additive Slots. Closes #1853.

Harald Csaszar 4 yıl önce
ebeveyn
işleme
47e357aa88

+ 5 - 10
spine-unity/Assets/Spine/Runtime/spine-unity/Shaders/CGIncludes/Spine-Skeleton-Lit-Common.cginc

@@ -109,16 +109,11 @@ sampler2D _MainTex;
 fixed4 frag (VertexOutput i) : SV_Target {
 	fixed4 tex = tex2D(_MainTex, i.uv0);
 	ALPHA_CLIP(tex, i.color);
-
-	fixed4 col;
-	#if defined(_STRAIGHT_ALPHA_INPUT)
-	col.rgb = tex * i.color * tex.a;
-	#else
-	col.rgb = tex * i.color;
-	#endif
-
-	col *= 2;
-	col.a = tex.a * i.color.a;
+#if defined(_STRAIGHT_ALPHA_INPUT)
+	tex.rgb *= tex.a;
+#endif
+	fixed4 col = tex * i.color;
+	col.rgb *= 2;
 	return col;
 }
 

+ 14 - 0
spine-unity/Assets/Spine/Runtime/spine-unity/Shaders/Sprite/CGIncludes/ShaderShared.cginc

@@ -240,6 +240,20 @@ uniform fixed _Cutoff;
 
 #endif
 
+////////////////////////////////////////
+// Additive Slot blend mode
+// return unlit textureColor, alpha clip textureColor.a only
+//
+#if defined(_ALPHAPREMULTIPLY_ON)
+	#define RETURN_UNLIT_IF_ADDITIVE_SLOT(textureColor, vertexColor) \
+	if (vertexColor.a == 0 && (vertexColor.r || vertexColor.g || vertexColor.b)) {\
+		ALPHA_CLIP(texureColor, fixed4(1, 1, 1, 1))\
+			return texureColor * vertexColor;\
+	}
+#else
+	#define RETURN_UNLIT_IF_ADDITIVE_SLOT(textureColor, vertexColor)
+#endif
+
 ////////////////////////////////////////
 // Color functions
 //

+ 44 - 44
spine-unity/Assets/Spine/Runtime/spine-unity/Shaders/Sprite/CGIncludes/SpritePixelLighting.cginc

@@ -1,6 +1,6 @@
 #ifndef SPRITE_PIXEL_LIGHTING_INCLUDED
 #define SPRITE_PIXEL_LIGHTING_INCLUDED
-	
+
 #include "ShaderShared.cginc"
 #include "SpriteLighting.cginc"
 #include "SpriteSpecular.cginc"
@@ -24,24 +24,24 @@
 	#define _LIGHT_COORD_INDEX_0 4
 	#define _LIGHT_COORD_INDEX_1 5
 	#define _FOG_COORD_INDEX 6
-#endif // _NORMALMAP	
+#endif // _NORMALMAP
 
 struct VertexOutput
 {
-	float4 pos : SV_POSITION;				
+	float4 pos : SV_POSITION;
 	fixed4 color : COLOR;
 	float2 texcoord : TEXCOORD0;
 	float4 posWorld : TEXCOORD1;
 	half3 normalWorld : TEXCOORD2;
 #if defined(_NORMALMAP)
-	half3 tangentWorld : TEXCOORD3;  
+	half3 tangentWorld : TEXCOORD3;
 	half3 binormalWorld : TEXCOORD4;
 #endif // _NORMALMAP
 	fixed3 vertexLighting : _VERTEX_LIGHTING_INDEX;
 	LIGHTING_COORDS(_LIGHT_COORD_INDEX_0, _LIGHT_COORD_INDEX_1)
 #if defined(_FOG)
 	UNITY_FOG_COORDS(_FOG_COORD_INDEX)
-#endif // _FOG	
+#endif // _FOG
 
 	UNITY_VERTEX_OUTPUT_STEREO
 };
@@ -56,16 +56,16 @@ inline fixed3 calculateLightDiffuse(VertexOutput input, float3 normalWorld, inou
 {
 	//For directional lights _WorldSpaceLightPos0.w is set to zero
 	float3 lightWorldDirection = normalize(_WorldSpaceLightPos0.xyz - input.posWorld.xyz * _WorldSpaceLightPos0.w);
-	
+
 	float attenuation = LIGHT_ATTENUATION(input);
 	float angleDot = max(0, dot(normalWorld, lightWorldDirection));
-	
+
 #if defined(_DIFFUSE_RAMP)
 	fixed3 lightDiffuse = calculateRampedDiffuse(_LightColor0.rgb, attenuation, angleDot);
 #else
 	fixed3 lightDiffuse = _LightColor0.rgb * (attenuation * angleDot);
 #endif // _DIFFUSE_RAMP
-	
+
 	return lightDiffuse;
 }
 
@@ -96,7 +96,7 @@ fixed3 calculateAmbientLight(half3 normalWorld)
 {
 #if defined(_SPHERICAL_HARMONICS)
 	fixed3 ambient = ShadeSH9(half4(normalWorld, 1.0));
-#else 
+#else
 	fixed3 ambient = unity_AmbientSky.rgb;
 #endif
 	return ambient;
@@ -129,124 +129,124 @@ fixed4 calculateSpecularLightAdditive(SpecularCommonData s, float3 viewDir, floa
 VertexOutput vert(VertexInput v)
 {
 	VertexOutput output;
-	
+
 	UNITY_SETUP_INSTANCE_ID(input);
     UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
-	
+
 	output.pos = calculateLocalPos(v.vertex);
 	output.color = calculateVertexColor(v.color);
 	output.texcoord = calculateTextureCoord(v.texcoord);
 	output.posWorld = calculateWorldPos(v.vertex);
-	
+
 	float backFaceSign = 1;
-#if defined(FIXED_NORMALS_BACKFACE_RENDERING)	
+#if defined(FIXED_NORMALS_BACKFACE_RENDERING)
 	backFaceSign = calculateBackfacingSign(output.posWorld.xyz);
-#endif	
+#endif
 
 	output.normalWorld = calculateSpriteWorldNormal(v, backFaceSign);
 	output.vertexLighting = calculateVertexLighting(output.posWorld, output.normalWorld);
-	
+
 #if defined(_NORMALMAP)
 	output.tangentWorld = calculateWorldTangent(v.tangent);
 	output.binormalWorld = calculateSpriteWorldBinormal(v, output.normalWorld, output.tangentWorld, backFaceSign);
 #endif
 
 	TRANSFER_VERTEX_TO_FRAGMENT(output)
-	
+
 #if defined(_FOG)
 	UNITY_TRANSFER_FOG(output,output.pos);
-#endif // _FOG	
-	
+#endif // _FOG
+
 	return output;
 }
 
 ////////////////////////////////////////
 // Fragment programs
 //
-
 fixed4 fragBase(VertexOutput input) : SV_Target
 {
 	fixed4 texureColor = calculateTexturePixel(input.texcoord);
+	RETURN_UNLIT_IF_ADDITIVE_SLOT(texureColor, input.color) // shall be called before ALPHA_CLIP
 	ALPHA_CLIP(texureColor, input.color)
-	
+
 	//Get normal direction
 	fixed3 normalWorld = calculateNormalWorld(input);
 
 	//Get Ambient diffuse
 	fixed3 ambient = calculateAmbientLight(normalWorld);
 
-	
+
 #if defined(SPECULAR)
-	
+
 	//For directional lights _WorldSpaceLightPos0.w is set to zero
 	float3 lightWorldDirection = normalize(_WorldSpaceLightPos0.xyz - input.posWorld.xyz * _WorldSpaceLightPos0.w);
 	float attenuation = LIGHT_ATTENUATION(input);
-	
+
 	//Returns pixel lit by light, texture color should inlcluded alpha
 	half3 viewDir = normalize(_WorldSpaceCameraPos - input.posWorld.xyz);
 	fixed4 pixel = calculateSpecularLight(getSpecularData(input.texcoord.xy, texureColor, input.color), viewDir, normalWorld, lightWorldDirection, _LightColor0.rgb * attenuation, ambient + input.vertexLighting);
-	
+
 	APPLY_EMISSION_SPECULAR(pixel, input.texcoord)
-	
+
 #else
 
 	//Get primary pixel light diffuse
 	fixed3 diffuse = calculateLightDiffuse(input, normalWorld, texureColor);
-	
+
 	//Combine along with vertex lighting for the base lighting pass
 	fixed3 lighting = ambient + diffuse + input.vertexLighting;
-	
+
 	APPLY_EMISSION(lighting, input.texcoord)
-	
+
 	fixed4 pixel = calculateLitPixel(texureColor, input.color, lighting);
-	
+
 #endif
-	
+
 #if defined(_RIM_LIGHTING)
 	pixel.rgb = applyRimLighting(input.posWorld, normalWorld, pixel);
 #endif
-	
+
 	COLORISE(pixel)
 	APPLY_FOG(pixel, input)
-	
+
 	return pixel;
 }
 
 fixed4 fragAdd(VertexOutput input) : SV_Target
 {
 	fixed4 texureColor = calculateTexturePixel(input.texcoord);
-	
+
 #if defined(_COLOR_ADJUST)
 	texureColor = adjustColor(texureColor);
-#endif // _COLOR_ADJUST	
+#endif // _COLOR_ADJUST
 
 	ALPHA_CLIP(texureColor, input.color)
-	
+
 	//Get normal direction
 	fixed3 normalWorld = calculateNormalWorld(input);
-		
+
 #if defined(SPECULAR)
-	
+
 	//For directional lights _WorldSpaceLightPos0.w is set to zero
 	float3 lightWorldDirection = normalize(_WorldSpaceLightPos0.xyz - input.posWorld.xyz * _WorldSpaceLightPos0.w);
 	float attenuation = LIGHT_ATTENUATION(input);
-	
+
 	half3 viewDir = normalize(_WorldSpaceCameraPos - input.posWorld.xyz);
 	fixed4 pixel = calculateSpecularLightAdditive(getSpecularData(input.texcoord.xy, texureColor, input.color), viewDir, normalWorld, lightWorldDirection, _LightColor0.rgb * attenuation);
-	
+
 #else
-	
+
 	//Get light diffuse
 	fixed3 lighting = calculateLightDiffuse(input, normalWorld, texureColor);
 	fixed4 pixel = calculateAdditiveLitPixel(texureColor, input.color, lighting);
-	
+
 #endif
-	
+
 	COLORISE_ADDITIVE(pixel)
 	APPLY_FOG_ADDITIVE(pixel, input)
-	
+
 	return pixel;
 }
 
 
-#endif // SPRITE_PIXEL_LIGHTING_INCLUDED
+#endif // SPRITE_PIXEL_LIGHTING_INCLUDED

+ 10 - 14
spine-unity/Assets/Spine/Runtime/spine-unity/Shaders/Sprite/CGIncludes/SpriteUnlit.cginc

@@ -6,7 +6,7 @@
 ////////////////////////////////////////
 // Vertex structs
 //
-				
+
 struct VertexInput
 {
 	float4 vertex : POSITION;
@@ -22,7 +22,7 @@ struct VertexOutput
 	fixed4 color : COLOR;
 #if defined(_FOG)
 	UNITY_FOG_COORDS(1)
-#endif // _FOG	
+#endif // _FOG
 
 	UNITY_VERTEX_OUTPUT_STEREO
 };
@@ -34,39 +34,35 @@ struct VertexOutput
 VertexOutput vert(VertexInput input)
 {
 	VertexOutput output;
-	
+
 	UNITY_SETUP_INSTANCE_ID(input);
     UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
-	
-	output.pos = calculateLocalPos(input.vertex);	
+
+	output.pos = calculateLocalPos(input.vertex);
 	output.texcoord = calculateTextureCoord(input.texcoord);
 	output.color = calculateVertexColor(input.color);
 
 #if defined(_FOG)
 	UNITY_TRANSFER_FOG(output,output.pos);
 #endif // _FOG
-	
+
 	return output;
 }
 
 ////////////////////////////////////////
 // Fragment program
 //
-
-
-
-
 fixed4 frag(VertexOutput input) : SV_Target
 {
 	fixed4 texureColor = calculateTexturePixel(input.texcoord.xy);
+	RETURN_UNLIT_IF_ADDITIVE_SLOT(texureColor, input.color) // shall be called before ALPHA_CLIP
 	ALPHA_CLIP(texureColor, input.color)
-
 	fixed4 pixel = calculatePixel(texureColor, input.color);
-	
+
 	COLORISE(pixel)
 	APPLY_FOG(pixel, input)
-	
+
 	return pixel;
 }
 
-#endif // SPRITE_UNLIT_INCLUDED
+#endif // SPRITE_UNLIT_INCLUDED

+ 1 - 1
spine-unity/Assets/Spine/Runtime/spine-unity/Shaders/Sprite/CGIncludes/SpriteVertexLighting.cginc

@@ -399,10 +399,10 @@ VertexOutput vert(VertexInput input)
 ////////////////////////////////////////
 // Fragment program
 //
-
 fixed4 frag(VertexOutput input) : SV_Target
 {
 	fixed4 texureColor = calculateTexturePixel(input.texcoord.xy);
+	RETURN_UNLIT_IF_ADDITIVE_SLOT(texureColor, input.color) // shall be called before ALPHA_CLIP
 	ALPHA_CLIP(texureColor, input.color)
 
 #if defined(PER_PIXEL_LIGHTING)