Browse Source

[unity] Fixed SkeletonRenderTexture and SkeletonGraphicRenderTexture issues when mesh is empty. Closes #2529.

Harald Csaszar 1 năm trước cách đây
mục cha
commit
98a9deb5df

+ 25 - 11
spine-unity/Assets/Spine Examples/Scripts/Sample Components/SkeletonRenderTexture/SkeletonGraphicRenderTexture.cs

@@ -67,6 +67,19 @@ namespace Spine.Unity.Examples {
 		protected SkeletonSubmeshGraphic quadMaskableGraphic;
 		protected readonly Vector3[] worldCorners = new Vector3[4];
 
+		public void ResetMeshRendererMaterials () {
+			meshRendererMaterialForTexture.Clear();
+			AtlasAssetBase[] atlasAssets = skeletonGraphic.SkeletonDataAsset.atlasAssets;
+			for (int i = 0; i < atlasAssets.Length; ++i) {
+				foreach (Material material in atlasAssets[i].Materials) {
+					if (material.mainTexture != null) {
+						meshRendererMaterialForTexture.Add(
+							new TextureMaterialPair(material.mainTexture, material));
+					}
+				}
+			}
+		}
+
 		protected override void Awake () {
 			base.Awake();
 			skeletonGraphic = this.GetComponent<SkeletonGraphic>();
@@ -97,16 +110,7 @@ namespace Spine.Unity.Examples {
 
 		void Reset () {
 			skeletonGraphic = this.GetComponent<SkeletonGraphic>();
-			AtlasAssetBase[] atlasAssets = skeletonGraphic.SkeletonDataAsset.atlasAssets;
-			for (int i = 0; i < atlasAssets.Length; ++i) {
-				foreach (Material material in atlasAssets[i].Materials) {
-					if (material.mainTexture != null) {
-						meshRendererMaterialForTexture.Add(
-							new TextureMaterialPair(material.mainTexture, material));
-					}
-				}
-			}
-
+			ResetMeshRendererMaterials();
 #if UNITY_EDITOR
 			string[] assets = UnityEditor.AssetDatabase.FindAssets("t:material RenderQuadGraphicMaterial");
 			if (assets.Length > 0) {
@@ -122,6 +126,7 @@ namespace Spine.Unity.Examples {
 			skeletonGraphic.AssignMeshOverrideMultipleRenderers += RenderMultipleMeshesToRenderTexture;
 			skeletonGraphic.disableMeshAssignmentOnOverride = true;
 			skeletonGraphic.OnMeshAndMaterialsUpdated += RenderOntoQuad;
+			skeletonGraphic.OnAnimationRebuild += OnRebuild;
 			List<CanvasRenderer> canvasRenderers = skeletonGraphic.canvasRenderers;
 			for (int i = 0; i < canvasRenderers.Count; ++i)
 				canvasRenderers[i].cull = true;
@@ -136,6 +141,7 @@ namespace Spine.Unity.Examples {
 			skeletonGraphic.AssignMeshOverrideMultipleRenderers -= RenderMultipleMeshesToRenderTexture;
 			skeletonGraphic.disableMeshAssignmentOnOverride = false;
 			skeletonGraphic.OnMeshAndMaterialsUpdated -= RenderOntoQuad;
+			skeletonGraphic.OnAnimationRebuild -= OnRebuild;
 			List<CanvasRenderer> canvasRenderers = skeletonGraphic.canvasRenderers;
 			for (int i = 0; i < canvasRenderers.Count; ++i)
 				canvasRenderers[i].cull = false;
@@ -156,6 +162,10 @@ namespace Spine.Unity.Examples {
 			AssignAtQuad();
 		}
 
+		void OnRebuild (ISkeletonAnimation skeletonGraphic) {
+			ResetMeshRendererMaterials();
+		}
+
 		protected void PrepareForMesh () {
 			// We need to get the min/max of all four corners, rotation of the skeleton
 			// in combination with perspective projection otherwise might lead to incorrect
@@ -207,6 +217,7 @@ namespace Spine.Unity.Examples {
 		}
 
 		protected void RenderSingleMeshToRenderTexture (Mesh mesh, Material graphicMaterial, Texture texture) {
+			if (mesh.subMeshCount == 0) return;
 			Material meshRendererMaterial = MeshRendererMaterialForTexture(texture);
 			foreach (int shaderPass in shaderPasses)
 				commandBuffer.DrawMesh(mesh, transform.localToWorldMatrix, meshRendererMaterial, 0, shaderPass);
@@ -217,9 +228,12 @@ namespace Spine.Unity.Examples {
 			Mesh[] meshes, Material[] graphicMaterials, Texture[] textures) {
 
 			for (int i = 0; i < meshCount; ++i) {
+				Mesh mesh = meshes[i];
+				if (mesh.subMeshCount == 0) continue;
+
 				Material meshRendererMaterial = MeshRendererMaterialForTexture(textures[i]);
 				foreach (int shaderPass in shaderPasses)
-					commandBuffer.DrawMesh(meshes[i], transform.localToWorldMatrix, meshRendererMaterial, 0, shaderPass);
+					commandBuffer.DrawMesh(mesh, transform.localToWorldMatrix, meshRendererMaterial, 0, shaderPass);
 			}
 			Graphics.ExecuteCommandBuffer(commandBuffer);
 		}

+ 9 - 0
spine-unity/Assets/Spine Examples/Scripts/Sample Components/SkeletonRenderTexture/SkeletonRenderTexture.cs

@@ -128,6 +128,11 @@ namespace Spine.Unity.Examples {
 		}
 
 		void RenderOntoQuad (SkeletonRenderer skeletonRenderer) {
+			Vector3 size = meshFilter.sharedMesh.bounds.size;
+			if (size.x == 0f || size.y == 0f) {
+				AssignNullMeshAtQuad();
+				return;
+			}
 			PrepareForMesh();
 			RenderToRenderTexture();
 			AssignAtQuad();
@@ -198,6 +203,10 @@ namespace Spine.Unity.Examples {
 			quadMeshRenderer.sharedMaterial.mainTexture = this.renderTexture;
 			quadMeshRenderer.sharedMaterial.color = color;
 		}
+
+		protected void AssignNullMeshAtQuad () {
+			quadMeshFilter.mesh = null;
+		}
 #endif
 	}
 }

+ 1 - 1
spine-unity/Assets/Spine Examples/package.json

@@ -2,7 +2,7 @@
   "name": "com.esotericsoftware.spine.spine-unity-examples",
   "displayName": "spine-unity Runtime Examples",
   "description": "This plugin provides example scenes and scripts for the spine-unity runtime.",
-  "version": "4.2.30",
+  "version": "4.2.31",
   "unity": "2018.3",
   "author": {
     "name": "Esoteric Software",

+ 1 - 1
spine-unity/Assets/Spine/package.json

@@ -2,7 +2,7 @@
 	"name": "com.esotericsoftware.spine.spine-unity",
 	"displayName": "spine-unity Runtime",
 	"description": "This plugin provides the spine-unity runtime core.",
-	"version": "4.2.63",
+	"version": "4.2.64",
 	"unity": "2018.3",
 	"author": {
 		"name": "Esoteric Software",