From c5c71747c3a0ca4d242a23722bc272e5004cd1b5 Mon Sep 17 00:00:00 2001 From: Harald Csaszar Date: Wed, 3 Mar 2021 20:17:03 +0100 Subject: [PATCH] [unity] Fixed single pass additive slot blend mode for all URP and LWRP shaders (2D and 3D). Closes #1850. --- .../Spine-SkeletonLit-ForwardPass-LW.hlsl | 35 +++++++++++-------- .../Spine-Sprite-ForwardPass-LW.hlsl | 4 +++ .../Spine-Sprite-StandardPass-URP-2D.hlsl | 4 +++ .../2D/Spine-SkeletonLit-URP-2D.shader | 11 +++--- .../Spine-SkeletonLit-ForwardPass-URP.hlsl | 34 ++++++++++-------- .../Include/Spine-Sprite-ForwardPass-URP.hlsl | 4 +++ 6 files changed, 56 insertions(+), 36 deletions(-) diff --git a/spine-unity/Modules/com.esotericsoftware.spine.lwrp-shaders/Shaders/CGIncludes/Spine-SkeletonLit-ForwardPass-LW.hlsl b/spine-unity/Modules/com.esotericsoftware.spine.lwrp-shaders/Shaders/CGIncludes/Spine-SkeletonLit-ForwardPass-LW.hlsl index f84e8f0f4..a2caada38 100644 --- a/spine-unity/Modules/com.esotericsoftware.spine.lwrp-shaders/Shaders/CGIncludes/Spine-SkeletonLit-ForwardPass-LW.hlsl +++ b/spine-unity/Modules/com.esotericsoftware.spine.lwrp-shaders/Shaders/CGIncludes/Spine-SkeletonLit-ForwardPass-LW.hlsl @@ -59,6 +59,8 @@ VertexOutput vert(appdata v) { float3 positionWS = TransformObjectToWorld(v.pos); half3 fixedNormal = half3(0, 0, -1); half3 normalWS = normalize(mul((float3x3)unity_ObjectToWorld, fixedNormal)); + o.uv0 = v.uv0; + o.pos = TransformWorldToHClip(positionWS); #ifdef _DOUBLE_SIDED_LIGHTING // unfortunately we have to compute the sign here in the vertex shader @@ -69,7 +71,16 @@ VertexOutput vert(appdata v) { #endif half3 shadowedColor; - color.rgb = LightweightLightVertexSimplified(positionWS, normalWS, shadowedColor); + if (color.a == 0) { + o.color = color; +#if defined(SKELETONLIT_RECEIVE_SHADOWS) + o.shadowedColor = color; + o.shadowCoord = float4(0, 0, 0, 0); +#endif + return o; + } + + color.rgb *= LightweightLightVertexSimplified(positionWS, normalWS, shadowedColor); #if defined(SKELETONLIT_RECEIVE_SHADOWS) o.shadowedColor = shadowedColor; #endif @@ -78,10 +89,7 @@ VertexOutput vert(appdata v) { half3 vertexSH; OUTPUT_SH(normalWS.xyz, vertexSH); color.rgb += SAMPLE_GI(input.lightmapUV, vertexSH, normalWS); - o.color = color; - o.uv0 = v.uv0; - o.pos = TransformWorldToHClip(positionWS); #if defined(SKELETONLIT_RECEIVE_SHADOWS) VertexPositionInputs vertexInput; @@ -94,22 +102,19 @@ VertexOutput vert(appdata v) { half4 frag(VertexOutput i) : SV_Target{ half4 tex = tex2D(_MainTex, i.uv0); - half4 col; + #if defined(_STRAIGHT_ALPHA_INPUT) + tex.rgb *= tex.a; + #endif + + if (i.color.a == 0) + return tex * i.color; #if defined(SKELETONLIT_RECEIVE_SHADOWS) half shadowAttenuation = MainLightRealtimeShadow(i.shadowCoord); i.color.rgb = lerp(i.shadowedColor, i.color.rgb, shadowAttenuation); #endif - - #if defined(_STRAIGHT_ALPHA_INPUT) - col.rgb = tex.rgb * i.color.rgb * tex.a; - #else - col.rgb = tex.rgb * i.color.rgb; - #endif - - - col.a = tex.a * i.color.a; - return col; + return tex * i.color; } + #endif diff --git a/spine-unity/Modules/com.esotericsoftware.spine.lwrp-shaders/Shaders/CGIncludes/Spine-Sprite-ForwardPass-LW.hlsl b/spine-unity/Modules/com.esotericsoftware.spine.lwrp-shaders/Shaders/CGIncludes/Spine-Sprite-ForwardPass-LW.hlsl index ece0c54b3..f07d94f54 100644 --- a/spine-unity/Modules/com.esotericsoftware.spine.lwrp-shaders/Shaders/CGIncludes/Spine-Sprite-ForwardPass-LW.hlsl +++ b/spine-unity/Modules/com.esotericsoftware.spine.lwrp-shaders/Shaders/CGIncludes/Spine-Sprite-ForwardPass-LW.hlsl @@ -213,6 +213,10 @@ half4 ForwardPassFragmentSprite(VertexOutputLWRP input) : SV_Target fixed4 texureColor = calculateTexturePixel(input.texcoord.xy); ALPHA_CLIP(texureColor, input.vertexColor) +#if !defined(_MULTIPLYBLEND) && !defined(_MULTIPLYBLEND_X2) + if (input.vertexColor.a == 0) + return texureColor * input.vertexColor; +#endif // fill out InputData struct InputData inputData; diff --git a/spine-unity/Modules/com.esotericsoftware.spine.urp-shaders/Shaders/2D/Include/Spine-Sprite-StandardPass-URP-2D.hlsl b/spine-unity/Modules/com.esotericsoftware.spine.urp-shaders/Shaders/2D/Include/Spine-Sprite-StandardPass-URP-2D.hlsl index 01aba038d..d2b70c758 100644 --- a/spine-unity/Modules/com.esotericsoftware.spine.urp-shaders/Shaders/2D/Include/Spine-Sprite-StandardPass-URP-2D.hlsl +++ b/spine-unity/Modules/com.esotericsoftware.spine.urp-shaders/Shaders/2D/Include/Spine-Sprite-StandardPass-URP-2D.hlsl @@ -89,6 +89,10 @@ half4 CombinedShapeLightFragment(VertexOutputSpriteURP2D input) : SV_Target fixed4 texureColor = calculateTexturePixel(input.texcoord.xy); ALPHA_CLIP(texureColor, input.vertexColor) texureColor *= input.vertexColor; +#if !defined(_MULTIPLYBLEND) && !defined(_MULTIPLYBLEND_X2) + if (input.vertexColor.a == 0) + return texureColor; +#endif half4 mask = SAMPLE_TEXTURE2D(_MaskTex, sampler_MaskTex, input.texcoord.xy); half4 pixel = CombinedShapeLightShared(texureColor, mask, input.lightingUV); diff --git a/spine-unity/Modules/com.esotericsoftware.spine.urp-shaders/Shaders/2D/Spine-SkeletonLit-URP-2D.shader b/spine-unity/Modules/com.esotericsoftware.spine.urp-shaders/Shaders/2D/Spine-SkeletonLit-URP-2D.shader index 7cf903eeb..4a4c7a70e 100644 --- a/spine-unity/Modules/com.esotericsoftware.spine.urp-shaders/Shaders/2D/Spine-SkeletonLit-URP-2D.shader +++ b/spine-unity/Modules/com.esotericsoftware.spine.urp-shaders/Shaders/2D/Spine-SkeletonLit-URP-2D.shader @@ -98,14 +98,13 @@ half4 CombinedShapeLightFragment(Varyings i) : SV_Target { half4 tex = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv); - - half4 main; #if defined(_STRAIGHT_ALPHA_INPUT) - main.rgb = tex.rgb * i.color.rgb * tex.a; - #else - main.rgb = tex.rgb * i.color.rgb; + tex.rgb *= tex.a; #endif - main.a = tex.a * i.color.a; + + half4 main = tex * i.color; + if (i.color.a == 0) + return main; half4 mask = SAMPLE_TEXTURE2D(_MaskTex, sampler_MaskTex, i.uv); return CombinedShapeLightShared(main, mask, i.lightingUV); diff --git a/spine-unity/Modules/com.esotericsoftware.spine.urp-shaders/Shaders/Include/Spine-SkeletonLit-ForwardPass-URP.hlsl b/spine-unity/Modules/com.esotericsoftware.spine.urp-shaders/Shaders/Include/Spine-SkeletonLit-ForwardPass-URP.hlsl index d73bd55e5..453986e04 100644 --- a/spine-unity/Modules/com.esotericsoftware.spine.urp-shaders/Shaders/Include/Spine-SkeletonLit-ForwardPass-URP.hlsl +++ b/spine-unity/Modules/com.esotericsoftware.spine.urp-shaders/Shaders/Include/Spine-SkeletonLit-ForwardPass-URP.hlsl @@ -59,6 +59,8 @@ VertexOutput vert(appdata v) { float3 positionWS = TransformObjectToWorld(v.pos); half3 fixedNormal = half3(0, 0, -1); half3 normalWS = normalize(mul((float3x3)unity_ObjectToWorld, fixedNormal)); + o.uv0 = v.uv0; + o.pos = TransformWorldToHClip(positionWS); #ifdef _DOUBLE_SIDED_LIGHTING // unfortunately we have to compute the sign here in the vertex shader @@ -69,7 +71,16 @@ VertexOutput vert(appdata v) { #endif half3 shadowedColor; - color.rgb = LightweightLightVertexSimplified(positionWS, normalWS, shadowedColor); + if (color.a == 0) { + o.color = color; +#if defined(SKELETONLIT_RECEIVE_SHADOWS) + o.shadowedColor = color; + o.shadowCoord = float4(0, 0, 0, 0); +#endif + return o; + } + + color.rgb *= LightweightLightVertexSimplified(positionWS, normalWS, shadowedColor); #if defined(SKELETONLIT_RECEIVE_SHADOWS) o.shadowedColor = shadowedColor; #endif @@ -78,10 +89,7 @@ VertexOutput vert(appdata v) { half3 vertexSH; OUTPUT_SH(normalWS.xyz, vertexSH); color.rgb += SAMPLE_GI(input.lightmapUV, vertexSH, normalWS); - o.color = color; - o.uv0 = v.uv0; - o.pos = TransformWorldToHClip(positionWS); #if defined(SKELETONLIT_RECEIVE_SHADOWS) VertexPositionInputs vertexInput; @@ -94,22 +102,18 @@ VertexOutput vert(appdata v) { half4 frag(VertexOutput i) : SV_Target{ half4 tex = tex2D(_MainTex, i.uv0); - half4 col; + #if defined(_STRAIGHT_ALPHA_INPUT) + tex.rgb *= tex.a; + #endif + + if (i.color.a == 0) + return tex * i.color; #if defined(SKELETONLIT_RECEIVE_SHADOWS) half shadowAttenuation = MainLightRealtimeShadow(i.shadowCoord); i.color.rgb = lerp(i.shadowedColor, i.color.rgb, shadowAttenuation); #endif - - #if defined(_STRAIGHT_ALPHA_INPUT) - col.rgb = tex.rgb * i.color.rgb * tex.a; - #else - col.rgb = tex.rgb * i.color.rgb; - #endif - - - col.a = tex.a * i.color.a; - return col; + return tex * i.color; } #endif diff --git a/spine-unity/Modules/com.esotericsoftware.spine.urp-shaders/Shaders/Include/Spine-Sprite-ForwardPass-URP.hlsl b/spine-unity/Modules/com.esotericsoftware.spine.urp-shaders/Shaders/Include/Spine-Sprite-ForwardPass-URP.hlsl index cb578cba9..5065a73c2 100644 --- a/spine-unity/Modules/com.esotericsoftware.spine.urp-shaders/Shaders/Include/Spine-Sprite-ForwardPass-URP.hlsl +++ b/spine-unity/Modules/com.esotericsoftware.spine.urp-shaders/Shaders/Include/Spine-Sprite-ForwardPass-URP.hlsl @@ -223,6 +223,10 @@ half4 ForwardPassFragmentSprite(VertexOutputLWRP input) : SV_Target fixed4 texureColor = calculateTexturePixel(input.texcoord.xy); ALPHA_CLIP(texureColor, input.vertexColor) +#if !defined(_MULTIPLYBLEND) && !defined(_MULTIPLYBLEND_X2) + if (input.vertexColor.a == 0) + return texureColor * input.vertexColor; +#endif // fill out InputData struct InputData inputData;