Kaynağa Gözat

Merge branch '3.8' of https://github.com/EsotericSoftware/spine-runtimes into 3.8

badlogic 6 yıl önce
ebeveyn
işleme
0e4125686c

+ 5 - 0
CHANGELOG.md

@@ -177,6 +177,11 @@
     * `WaitForSpineAnimationComplete` now proves an additional `bool includeEndEvent` parameter, defaults to `false` (previous behaviour).
     * Added a new `WaitForSpineAnimationEnd` yield instruction.
     * Added a new generic `WaitForSpineAnimation` yield instruction which can be configured to wait for any combination of animation track events. It is now used as base class for `WaitForSpineAnimationComplete` and `WaitForSpineAnimationEnd`.
+  * Additional **Fix Draw Order** parameter at SkeletonRenderer, defaults to `disabled` (previous behaviour).
+    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 leave this parameter disabled when everything is drawn correctly to save the additional performance cost.
 
 * **Changes of default values**
   * `SkeletonMecanim`'s `Layer Mix Mode` now defaults to `MixMode.SpineStyle` instead of `MixMode.MixAlways`.

+ 17 - 0
spine-csharp/src/ExposedList.cs

@@ -141,6 +141,23 @@ namespace Spine {
 			}
 		}
 
+		// Additional overload provided because ExposedList<T> only implements IEnumerable<T>,
+		// leading to sub-optimal behavior: It grows multiple times as it assumes not
+		// to know the final size ahead of insertion.
+		public void AddRange (ExposedList<T> list) {
+			CheckCollection(list);
+
+			int collectionCount = list.Count;
+			if (collectionCount == 0)
+				return;
+
+			GrowIfNeeded(collectionCount);
+			list.CopyTo(Items, Count);
+			Count += collectionCount;
+
+			version++;
+		}
+
 		public void AddRange (IEnumerable<T> collection) {
 			CheckCollection(collection);
 

+ 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 = false;
+		#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
 	}
 }

+ 22 - 2
spine-unity/Assets/Spine/Runtime/spine-unity/Utility/AtlasUtilities.cs

@@ -269,7 +269,8 @@ namespace Spine.Unity.AttachmentTools {
 			newTexture.name = newAssetName;
 			// Copy settings
 			if (texturesToPack.Count > 0) {
-				newTexture.anisoLevel = texturesToPack[0].anisoLevel;
+				var sourceTexture = texturesToPack[0];
+				newTexture.CopyTextureAttributesFrom(sourceTexture);
 			}
 			var rects = newTexture.PackTextures(texturesToPack.ToArray(), padding, maxAtlasSize);
 
@@ -323,6 +324,9 @@ namespace Spine.Unity.AttachmentTools {
 			var skinAttachments = o.Attachments;
 			var newSkin = new Skin(newName);
 
+			newSkin.bones.AddRange(o.bones);
+			newSkin.constraints.AddRange(o.constraints);
+
 			// Use these to detect and use shared regions.
 			var existingRegions = new Dictionary<AtlasRegion, int>();
 			var regionIndexes = new List<int>();
@@ -362,7 +366,11 @@ namespace Spine.Unity.AttachmentTools {
 			// Fill a new texture with the collected attachment textures.
 			var newTexture = new Texture2D(maxAtlasSize, maxAtlasSize, textureFormat, mipmaps);
 			newTexture.mipMapBias = AtlasUtilities.DefaultMipmapBias;
-			newTexture.anisoLevel = texturesToPack[0].anisoLevel;
+
+			if (texturesToPack.Count > 0) {
+				var sourceTexture = texturesToPack[0];
+				newTexture.CopyTextureAttributesFrom(sourceTexture);
+			}
 			newTexture.name = newName;
 			var rects = newTexture.PackTextures(texturesToPack.ToArray(), padding, maxAtlasSize);
 
@@ -428,6 +436,7 @@ namespace Spine.Unity.AttachmentTools {
 				int width = (int)r.width;
 				int height = (int)r.height;
 				output = new Texture2D(width, height, textureFormat, mipmaps) { name = ar.name };
+				output.CopyTextureAttributesFrom(sourceTexture);
 				AtlasUtilities.CopyTexture(sourceTexture, r, output);
 				CachedRegionTextures.Add(ar, output);
 				CachedRegionTexturesList.Add(output);
@@ -440,12 +449,14 @@ namespace Spine.Unity.AttachmentTools {
 			var spriteTexture = s.texture;
 			var r = s.textureRect;
 			var newTexture = new Texture2D((int)r.width, (int)r.height, textureFormat, mipmaps);
+			newTexture.CopyTextureAttributesFrom(spriteTexture);
 			AtlasUtilities.CopyTexture(spriteTexture, r, newTexture);
 			return newTexture;
 		}
 
 		static Texture2D GetClone (this Texture2D t, TextureFormat textureFormat = SpineTextureFormat, bool mipmaps = UseMipMaps) {
 			var newTexture = new Texture2D((int)t.width, (int)t.height, textureFormat, mipmaps);
+			newTexture.CopyTextureAttributesFrom(t);
 			AtlasUtilities.CopyTexture(t, new Rect(0, 0, t.width, t.height), newTexture);
 			return newTexture;
 		}
@@ -566,6 +577,15 @@ namespace Spine.Unity.AttachmentTools {
 			return material.mainTexture as Texture2D;
 		}
 
+		static void CopyTextureAttributesFrom(this Texture2D destination, Texture2D source) {
+			destination.filterMode = source.filterMode;
+			destination.anisoLevel = source.anisoLevel;
+			destination.alphaIsTransparency = source.alphaIsTransparency;
+			destination.wrapModeU = source.wrapModeU;
+			destination.wrapModeV = source.wrapModeV;
+			destination.wrapModeW = source.wrapModeW;
+		}
+
 		static void ApplyPMA (this Texture2D texture, bool applyImmediately = true) {
 			var pixels = texture.GetPixels();
 			for (int i = 0, n = pixels.Length; i < n; i++) {