Explorar o código

[unity] Fixed incorrect draw order issue when using LWRP and 3+ submeshes with alternating materials. Introduces a new "Fix draw order" parameter at SkeletonRenderer inspector. Closes #1486.

Harald Csaszar %!s(int64=6) %!d(string=hai) anos
pai
achega
e4ae0ceb1c

+ 11 - 2
spine-unity/Assets/Spine/Editor/spine-unity/Editor/Components/SkeletonRendererInspector.cs

@@ -33,6 +33,10 @@
 #define NO_PREFAB_MESH
 #endif
 
+#if UNITY_2018_1_OR_NEWER
+#define PER_MATERIAL_PROPERTY_BLOCKS
+#endif
+
 #if UNITY_2017_1_OR_NEWER
 #define BUILT_IN_SPRITE_MASK_COMPONENT
 #endif
@@ -55,7 +59,7 @@ namespace Spine.Unity.Editor {
 
 		protected SerializedProperty skeletonDataAsset, initialSkinName;
 		protected SerializedProperty initialFlipX, initialFlipY;
-		protected SerializedProperty singleSubmesh, separatorSlotNames, clearStateOnDisable, immutableTriangles;
+		protected SerializedProperty singleSubmesh, separatorSlotNames, clearStateOnDisable, immutableTriangles, fixDrawOrder;
 		protected SerializedProperty normals, tangents, zSpacing, pmaVertexColors, tintBlack; // MeshGenerator settings
 		protected SerializedProperty maskInteraction;
 		protected SerializedProperty maskMaterialsNone, maskMaterialsInside, maskMaterialsOutside;
@@ -70,7 +74,7 @@ namespace Spine.Unity.Editor {
 		protected bool deleteOutsideMaskMaterialsQueued = false;
 
 		protected GUIContent SkeletonDataAssetLabel, SkeletonUtilityButtonContent;
-		protected GUIContent PMAVertexColorsLabel, ClearStateOnDisableLabel, ZSpacingLabel, ImmubleTrianglesLabel, TintBlackLabel, SingleSubmeshLabel;
+		protected GUIContent PMAVertexColorsLabel, ClearStateOnDisableLabel, ZSpacingLabel, ImmubleTrianglesLabel, TintBlackLabel, SingleSubmeshLabel, FixDrawOrderLabel;
 		protected GUIContent NormalsLabel, TangentsLabel, MaskInteractionLabel;
 		protected GUIContent MaskMaterialsHeadingLabel, MaskMaterialsNoneLabel, MaskMaterialsInsideLabel, MaskMaterialsOutsideLabel;
 		protected GUIContent SetMaterialButtonLabel, ClearMaterialButtonLabel, DeleteMaterialButtonLabel;
@@ -116,6 +120,7 @@ namespace Spine.Unity.Editor {
 			TangentsLabel = new GUIContent("Solve Tangents", "Calculates the tangents per frame. Use this if you are using lit shaders (usually with normal maps) that require vertex tangents.");
 			TintBlackLabel = new GUIContent("Tint Black (!)", "Adds black tint vertex data to the mesh as UV2 and UV3. Black tinting requires that the shader interpret UV2 and UV3 as black tint colors for this effect to work. You may also use the default [Spine/Skeleton Tint Black] shader.\n\nIf you only need to tint the whole skeleton and not individual parts, the [Spine/Skeleton Tint] shader is recommended for better efficiency and changing/animating the _Black material property via MaterialPropertyBlock.");
 			SingleSubmeshLabel = new GUIContent("Use Single Submesh", "Simplifies submesh generation by assuming you are only using one Material and need only one submesh. This is will disable multiple materials, render separation, and custom slot materials.");
+			FixDrawOrderLabel = new GUIContent("Fix Draw Order", "Applies only when 3+ submeshes are used (2+ materials with alternating order, e.g. \"A B A\"). If true, MaterialPropertyBlocks are assigned at each material to prevent aggressive batching of submeshes by e.g. the LWRP renderer, leading to incorrect draw order (e.g. \"A1 B A2\" changed to \"A1A2 B\"). You can disable this parameter when everything is drawn correctly to save the additional performance cost.");
 			MaskInteractionLabel = new GUIContent("Mask Interaction", "SkeletonRenderer's interaction with a Sprite Mask.");
 			MaskMaterialsHeadingLabel = new GUIContent("Mask Interaction Materials", "Materials used for different interaction with sprite masks.");
 			MaskMaterialsNoneLabel = new GUIContent("Normal Materials", "Normal materials used when Mask Interaction is set to None.");
@@ -137,6 +142,7 @@ namespace Spine.Unity.Editor {
 			clearStateOnDisable = so.FindProperty("clearStateOnDisable");
 			tintBlack = so.FindProperty("tintBlack");
 			singleSubmesh = so.FindProperty("singleSubmesh");
+			fixDrawOrder = so.FindProperty("fixDrawOrder");
 			maskInteraction = so.FindProperty("maskInteraction");
 			maskMaterialsNone = so.FindProperty("maskMaterials.materialsMaskDisabled");
 			maskMaterialsInside = so.FindProperty("maskMaterials.materialsInsideMask");
@@ -334,6 +340,9 @@ namespace Spine.Unity.Editor {
 						using (new SpineInspectorUtility.LabelWidthScope()) {
 							// Optimization options
 							if (singleSubmesh != null) EditorGUILayout.PropertyField(singleSubmesh, SingleSubmeshLabel);
+							#if PER_MATERIAL_PROPERTY_BLOCKS
+							if (fixDrawOrder != null) EditorGUILayout.PropertyField(fixDrawOrder, FixDrawOrderLabel);
+							#endif
 							if (immutableTriangles != null) EditorGUILayout.PropertyField(immutableTriangles, ImmubleTrianglesLabel);
 							EditorGUILayout.PropertyField(clearStateOnDisable, ClearStateOnDisableLabel);
 							EditorGUILayout.Space();

+ 47 - 0
spine-unity/Assets/Spine/Runtime/spine-unity/Components/SkeletonRenderer.cs

@@ -31,6 +31,10 @@
 #define NEW_PREFAB_SYSTEM
 #endif
 
+#if UNITY_2018_1_OR_NEWER
+#define PER_MATERIAL_PROPERTY_BLOCKS
+#endif
+
 #if UNITY_2017_1_OR_NEWER
 #define BUILT_IN_SPRITE_MASK_COMPONENT
 #endif
@@ -90,6 +94,15 @@ namespace Spine.Unity {
 		/// <remarks>This disables SkeletonRenderSeparator functionality.</remarks>
 		public bool singleSubmesh = false;
 
+		#if PER_MATERIAL_PROPERTY_BLOCKS
+		/// <summary> Applies only when 3+ submeshes are used (2+ materials with alternating order, e.g. "A B A").
+		/// If true, MaterialPropertyBlocks are assigned at each material to prevent aggressive batching of submeshes
+		/// by e.g. the LWRP renderer, leading to incorrect draw order (e.g. "A1 B A2" changed to "A1A2 B").
+		/// You can disable this parameter when everything is drawn correctly to save the additional performance cost.
+		/// </summary>
+		public bool fixDrawOrder = true;
+		#endif
+
 		/// <summary>If true, the mesh generator adds normals to the output mesh. For better performance and reduced memory requirements, use a shader that assumes the desired normal.</summary>
 		[UnityEngine.Serialization.FormerlySerializedAs("calculateNormals")] public bool addNormals = false;
 
@@ -422,6 +435,12 @@ namespace Spine.Unity {
 				AssignSpriteMaskMaterials();
 			}
 			#endif
+
+			#if PER_MATERIAL_PROPERTY_BLOCKS
+			if (fixDrawOrder && meshRenderer.sharedMaterials.Length > 2) {
+				SetDrawOrderMaterialPropertyBlocks();
+			}
+			#endif
 		}
 
 		public void FindAndApplySeparatorSlots (string startsWith, bool clearExistingSeparators = true, bool updateStringArray = false) {
@@ -580,5 +599,33 @@ namespace Spine.Unity {
 		#endif // UNITY_EDITOR
 
 		#endif //#if BUILT_IN_SPRITE_MASK_COMPONENT
+
+		#if PER_MATERIAL_PROPERTY_BLOCKS
+		private MaterialPropertyBlock reusedPropertyBlock;
+		public static readonly int SUBMESH_DUMMY_PARAM_ID = Shader.PropertyToID("_Submesh");
+
+		/// <summary>
+		/// This method was introduced as a workaround for too aggressive submesh draw call batching,
+		/// leading to incorrect draw order when 3+ materials are used at submeshes in alternating order.
+		/// Otherwise, e.g. when using Lightweight Render Pipeline, deliberately separated draw calls
+		/// "A1 B A2" are reordered to "A1A2 B", regardless of batching-related project settings.
+		/// </summary>
+		private void SetDrawOrderMaterialPropertyBlocks() {
+			if (reusedPropertyBlock == null) reusedPropertyBlock = new MaterialPropertyBlock();
+			
+			bool hasPerRendererBlock = meshRenderer.HasPropertyBlock();
+			if (hasPerRendererBlock) {
+				meshRenderer.GetPropertyBlock(reusedPropertyBlock);
+			}
+
+			for (int i = 0; i < meshRenderer.sharedMaterials.Length; ++i) {
+				if (!hasPerRendererBlock) meshRenderer.GetPropertyBlock(reusedPropertyBlock, i);
+				// Note: this parameter shall not exist at any shader, then Unity will create separate
+				// material instances (not in terms of memory cost or leakage).
+				reusedPropertyBlock.SetFloat(SUBMESH_DUMMY_PARAM_ID, i);
+				meshRenderer.SetPropertyBlock(reusedPropertyBlock, i);
+			}
+		}
+		#endif
 	}
 }