From 4741279d7d88ba9f9714b159da90ff26514ebd6a Mon Sep 17 00:00:00 2001 From: Harald Csaszar Date: Wed, 24 Jul 2019 15:09:57 +0200 Subject: [PATCH] [unity] `Spine/Sprite/Pixel Lit` shader: Fixed bug where parts were shining through - changed ZWrite to always On, otherwise ForwardAdd pass will incorrectly show lit hidden parts. Added recommendation warning box in SkeletonRenderer inspector for Z Spacing on enabled ZWrite shaders. Closes #1335. --- .../Editor/Shaders/SpineSpriteShaderGUI.cs | 27 ++++++++++-------- .../Shaders/Sprite/SpritesPixelLit.shader | 8 +++--- .../spine-unity/Utility/MaterialChecks.cs | 28 ++++++++++++++++++- 3 files changed, 47 insertions(+), 16 deletions(-) diff --git a/spine-unity/Assets/Spine/Editor/spine-unity/Editor/Shaders/SpineSpriteShaderGUI.cs b/spine-unity/Assets/Spine/Editor/spine-unity/Editor/Shaders/SpineSpriteShaderGUI.cs index e68ebf4c9..30de38653 100644 --- a/spine-unity/Assets/Spine/Editor/spine-unity/Editor/Shaders/SpineSpriteShaderGUI.cs +++ b/spine-unity/Assets/Spine/Editor/spine-unity/Editor/Shaders/SpineSpriteShaderGUI.cs @@ -200,7 +200,7 @@ public class SpineSpriteShaderGUI : ShaderGUI { _pixelSnap = FindProperty("PixelSnap", props); - _writeToDepth = FindProperty("_ZWrite", props); + _writeToDepth = FindProperty("_ZWrite", props, false); _depthAlphaCutoff = FindProperty("_Cutoff", props); _shadowAlphaCutoff = FindProperty("_ShadowAlphaCutoff", props); _renderQueue = FindProperty("_RenderQueue", props); @@ -435,18 +435,23 @@ public class SpineSpriteShaderGUI : ShaderGUI { EditorGUI.BeginChangeCheck(); - bool mixedValue = _writeToDepth.hasMixedValue; - EditorGUI.showMixedValue = mixedValue; - bool writeTodepth = EditorGUILayout.Toggle(_depthText, _writeToDepth.floatValue != 0.0f); + bool showDepthAlphaCutoff = true; + // e.g. Pixel Lit shader always has ZWrite enabled + if (_writeToDepth != null) { + bool mixedValue = _writeToDepth.hasMixedValue; + EditorGUI.showMixedValue = mixedValue; + bool writeTodepth = EditorGUILayout.Toggle(_depthText, _writeToDepth.floatValue != 0.0f); - if (EditorGUI.EndChangeCheck()) { - SetInt("_ZWrite", writeTodepth ? 1 : 0); - _depthAlphaCutoff.floatValue = writeTodepth ? 0.5f : 0.0f; - mixedValue = false; - dataChanged = true; + if (EditorGUI.EndChangeCheck()) { + SetInt("_ZWrite", writeTodepth ? 1 : 0); + _depthAlphaCutoff.floatValue = writeTodepth ? 0.5f : 0.0f; + mixedValue = false; + dataChanged = true; + } + + showDepthAlphaCutoff = writeTodepth && !mixedValue && GetMaterialBlendMode((Material)_materialEditor.target) != eBlendMode.Opaque; } - - if (writeTodepth && !mixedValue && GetMaterialBlendMode((Material)_materialEditor.target) != eBlendMode.Opaque) { + if (showDepthAlphaCutoff) { EditorGUI.BeginChangeCheck(); _materialEditor.RangeProperty(_depthAlphaCutoff, _depthAlphaCutoffText.text); dataChanged |= EditorGUI.EndChangeCheck(); diff --git a/spine-unity/Assets/Spine/Runtime/spine-unity/Shaders/Sprite/SpritesPixelLit.shader b/spine-unity/Assets/Spine/Runtime/spine-unity/Shaders/Sprite/SpritesPixelLit.shader index 17117d679..423b987d6 100644 --- a/spine-unity/Assets/Spine/Runtime/spine-unity/Shaders/Sprite/SpritesPixelLit.shader +++ b/spine-unity/Assets/Spine/Runtime/spine-unity/Shaders/Sprite/SpritesPixelLit.shader @@ -4,9 +4,9 @@ Shader "Spine/Sprite/Pixel Lit" { _MainTex ("Main Texture", 2D) = "white" {} _Color ("Color", Color) = (1,1,1,1) - + _BumpScale("Scale", Float) = 1.0 - _BumpMap ("Normal Map", 2D) = "bump" {} + _BumpMap ("Normal Map", 2D) = "bump" {} [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0 [PerRendererData] _AlphaTex ("External Alpha", 2D) = "white" {} @@ -24,7 +24,6 @@ Shader "Spine/Sprite/Pixel Lit" _DiffuseRamp ("Diffuse Ramp Texture", 2D) = "gray" {} _FixedNormal ("Fixed Normal", Vector) = (0,0,1,1) - _ZWrite ("Depth Write", Float) = 1.0 _Cutoff ("Depth alpha cutoff", Range(0,1)) = 0.5 _ShadowAlphaCutoff ("Shadow alpha cutoff", Range(0,1)) = 0.1 _CustomRenderQueue ("Custom Render Queue", Float) = 0.0 @@ -64,7 +63,8 @@ Shader "Spine/Sprite/Pixel Lit" Name "FORWARD" Tags { "LightMode" = "ForwardBase" } Blend [_SrcBlend] [_DstBlend] - ZWrite [_ZWrite] + // Note: ZWrite needs to be enabled for following ForwardAdd pass, otherwise parts will look as if shining through by getting lit. + ZWrite On ZTest LEqual Cull [_Cull] diff --git a/spine-unity/Assets/Spine/Runtime/spine-unity/Utility/MaterialChecks.cs b/spine-unity/Assets/Spine/Runtime/spine-unity/Utility/MaterialChecks.cs index 280009115..f72dda2df 100644 --- a/spine-unity/Assets/Spine/Runtime/spine-unity/Utility/MaterialChecks.cs +++ b/spine-unity/Assets/Spine/Runtime/spine-unity/Utility/MaterialChecks.cs @@ -43,12 +43,24 @@ namespace Spine.Unity { "Warning: Premultiply-alpha atlas textures not supported in Linear color space!\n\nPlease\n" + "a) re-export atlas as straight alpha texture with 'premultiply alpha' unchecked or\n" + "b) switch to Gamma color space via\nProject Settings - Player - Other Settings - Color Space.\n"; + public static readonly string kZSpacingRequiredMessage = + "Warning: Z Spacing required on selected shader! Otherwise you will receive incorrect results.\n\nPlease\n" + + "1) make sure at least minimal 'Z Spacing' is set at the SkeletonRenderer/SkeletonAnimation component under 'Advanced' and\n" + + "2) ensure that the skeleton has overlapping parts on different Z depth. You can adjust this in Spine via draw order.\n"; + public static readonly string kZSpacingRecommendedMessage = + "Warning: Z Spacing recommended on selected shader configuration!\n\nPlease\n" + + "1) make sure at least minimal 'Z Spacing' is set at the SkeletonRenderer/SkeletonAnimation component under 'Advanced' and\n" + + "2) ensure that the skeleton has overlapping parts on different Z depth. You can adjust this in Spine via draw order.\n"; public static bool IsMaterialSetupProblematic (SkeletonRenderer renderer, ref string errorMessage) { var materials = renderer.GetComponent().sharedMaterials; bool isProblematic = false; foreach (var mat in materials) { - isProblematic |= MaterialChecks.IsMaterialSetupProblematic(mat, ref errorMessage); + if (mat == null) continue; + isProblematic |= IsMaterialSetupProblematic(mat, ref errorMessage); + if (renderer.zSpacing == 0) { + isProblematic |= IsZSpacingRequired(mat, ref errorMessage); + } } return isProblematic; } @@ -57,6 +69,20 @@ namespace Spine.Unity { return !IsColorSpaceSupported(material, ref errorMessage); } + public static bool IsZSpacingRequired(Material material, ref string errorMessage) { + bool hasForwardAddPass = material.FindPass("FORWARD_DELTA") >= 0; + if (hasForwardAddPass) { + errorMessage += kZSpacingRequiredMessage; + return true; + } + bool zWrite = material.HasProperty("_ZWrite") && material.GetFloat("_ZWrite") > 0.0f; + if (zWrite) { + errorMessage += kZSpacingRecommendedMessage; + return true; + } + return false; + } + public static bool IsColorSpaceSupported (Material material, ref string errorMessage) { if (QualitySettings.activeColorSpace == ColorSpace.Linear) { if (IsPMAMaterial(material)) {