[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.

This commit is contained in:
Harald Csaszar 2019-07-24 15:09:57 +02:00
parent 2b5e521c34
commit 4741279d7d
3 changed files with 47 additions and 16 deletions

View File

@ -200,7 +200,7 @@ public class SpineSpriteShaderGUI : ShaderGUI {
_pixelSnap = FindProperty("PixelSnap", props); _pixelSnap = FindProperty("PixelSnap", props);
_writeToDepth = FindProperty("_ZWrite", props); _writeToDepth = FindProperty("_ZWrite", props, false);
_depthAlphaCutoff = FindProperty("_Cutoff", props); _depthAlphaCutoff = FindProperty("_Cutoff", props);
_shadowAlphaCutoff = FindProperty("_ShadowAlphaCutoff", props); _shadowAlphaCutoff = FindProperty("_ShadowAlphaCutoff", props);
_renderQueue = FindProperty("_RenderQueue", props); _renderQueue = FindProperty("_RenderQueue", props);
@ -435,18 +435,23 @@ public class SpineSpriteShaderGUI : ShaderGUI {
EditorGUI.BeginChangeCheck(); EditorGUI.BeginChangeCheck();
bool mixedValue = _writeToDepth.hasMixedValue; bool showDepthAlphaCutoff = true;
EditorGUI.showMixedValue = mixedValue; // e.g. Pixel Lit shader always has ZWrite enabled
bool writeTodepth = EditorGUILayout.Toggle(_depthText, _writeToDepth.floatValue != 0.0f); if (_writeToDepth != null) {
bool mixedValue = _writeToDepth.hasMixedValue;
EditorGUI.showMixedValue = mixedValue;
bool writeTodepth = EditorGUILayout.Toggle(_depthText, _writeToDepth.floatValue != 0.0f);
if (EditorGUI.EndChangeCheck()) { if (EditorGUI.EndChangeCheck()) {
SetInt("_ZWrite", writeTodepth ? 1 : 0); SetInt("_ZWrite", writeTodepth ? 1 : 0);
_depthAlphaCutoff.floatValue = writeTodepth ? 0.5f : 0.0f; _depthAlphaCutoff.floatValue = writeTodepth ? 0.5f : 0.0f;
mixedValue = false; mixedValue = false;
dataChanged = true; dataChanged = true;
}
showDepthAlphaCutoff = writeTodepth && !mixedValue && GetMaterialBlendMode((Material)_materialEditor.target) != eBlendMode.Opaque;
} }
if (showDepthAlphaCutoff) {
if (writeTodepth && !mixedValue && GetMaterialBlendMode((Material)_materialEditor.target) != eBlendMode.Opaque) {
EditorGUI.BeginChangeCheck(); EditorGUI.BeginChangeCheck();
_materialEditor.RangeProperty(_depthAlphaCutoff, _depthAlphaCutoffText.text); _materialEditor.RangeProperty(_depthAlphaCutoff, _depthAlphaCutoffText.text);
dataChanged |= EditorGUI.EndChangeCheck(); dataChanged |= EditorGUI.EndChangeCheck();

View File

@ -4,9 +4,9 @@ Shader "Spine/Sprite/Pixel Lit"
{ {
_MainTex ("Main Texture", 2D) = "white" {} _MainTex ("Main Texture", 2D) = "white" {}
_Color ("Color", Color) = (1,1,1,1) _Color ("Color", Color) = (1,1,1,1)
_BumpScale("Scale", Float) = 1.0 _BumpScale("Scale", Float) = 1.0
_BumpMap ("Normal Map", 2D) = "bump" {} _BumpMap ("Normal Map", 2D) = "bump" {}
[MaterialToggle] PixelSnap ("Pixel snap", Float) = 0 [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
[PerRendererData] _AlphaTex ("External Alpha", 2D) = "white" {} [PerRendererData] _AlphaTex ("External Alpha", 2D) = "white" {}
@ -24,7 +24,6 @@ Shader "Spine/Sprite/Pixel Lit"
_DiffuseRamp ("Diffuse Ramp Texture", 2D) = "gray" {} _DiffuseRamp ("Diffuse Ramp Texture", 2D) = "gray" {}
_FixedNormal ("Fixed Normal", Vector) = (0,0,1,1) _FixedNormal ("Fixed Normal", Vector) = (0,0,1,1)
_ZWrite ("Depth Write", Float) = 1.0
_Cutoff ("Depth alpha cutoff", Range(0,1)) = 0.5 _Cutoff ("Depth alpha cutoff", Range(0,1)) = 0.5
_ShadowAlphaCutoff ("Shadow alpha cutoff", Range(0,1)) = 0.1 _ShadowAlphaCutoff ("Shadow alpha cutoff", Range(0,1)) = 0.1
_CustomRenderQueue ("Custom Render Queue", Float) = 0.0 _CustomRenderQueue ("Custom Render Queue", Float) = 0.0
@ -64,7 +63,8 @@ Shader "Spine/Sprite/Pixel Lit"
Name "FORWARD" Name "FORWARD"
Tags { "LightMode" = "ForwardBase" } Tags { "LightMode" = "ForwardBase" }
Blend [_SrcBlend] [_DstBlend] 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 ZTest LEqual
Cull [_Cull] Cull [_Cull]

View File

@ -43,12 +43,24 @@ namespace Spine.Unity {
"Warning: Premultiply-alpha atlas textures not supported in Linear color space!\n\nPlease\n" "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" + "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"; + "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) { public static bool IsMaterialSetupProblematic (SkeletonRenderer renderer, ref string errorMessage) {
var materials = renderer.GetComponent<Renderer>().sharedMaterials; var materials = renderer.GetComponent<Renderer>().sharedMaterials;
bool isProblematic = false; bool isProblematic = false;
foreach (var mat in materials) { 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; return isProblematic;
} }
@ -57,6 +69,20 @@ namespace Spine.Unity {
return !IsColorSpaceSupported(material, ref errorMessage); 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) { public static bool IsColorSpaceSupported (Material material, ref string errorMessage) {
if (QualitySettings.activeColorSpace == ColorSpace.Linear) { if (QualitySettings.activeColorSpace == ColorSpace.Linear) {
if (IsPMAMaterial(material)) { if (IsPMAMaterial(material)) {