Jelajahi Sumber

[unity] OnPostProcessVertices callback now provides access to uv2 and uv3 buffers. Closes #2230.

Harald Csaszar 2 tahun lalu
induk
melakukan
a8e6552b81

+ 2 - 0
CHANGELOG.md

@@ -29,6 +29,8 @@
 * **Officially supported Unity versions are 2017.1-2022.1**.
 * **Officially supported Unity versions are 2017.1-2022.1**.
 
 
 * **Additions**
 * **Additions**
+  * `OnPostProcessVertices` callback parameter `MeshGeneratorBuffers` now provides access to `uv2Buffer` and `uv3Buffer` properties of `MeshGenerator`, automatically allocating buffers upon access if `tintBlack` is disabled. This allows for passing own vertex data to a shader on second and third uv channels.
+
 * **Breaking changes**
 * **Breaking changes**
   
   
 * **Changes of default values**
 * **Changes of default values**

+ 46 - 35
spine-unity/Assets/Spine/Runtime/spine-unity/Mesh Generation/MeshGenerator.cs

@@ -48,12 +48,20 @@ namespace Spine.Unity {
 		/// <summary> Vertex positions. To be used for UnityEngine.Mesh.vertices.</summary>
 		/// <summary> Vertex positions. To be used for UnityEngine.Mesh.vertices.</summary>
 		public Vector3[] vertexBuffer;
 		public Vector3[] vertexBuffer;
 
 
-		/// <summary> Vertex UVs. To be used for UnityEngine.Mesh.uvs.</summary>
+		/// <summary> Vertex texture coordinates (UVs). To be used for UnityEngine.Mesh.uv.</summary>
 		public Vector2[] uvBuffer;
 		public Vector2[] uvBuffer;
 
 
 		/// <summary> Vertex colors. To be used for UnityEngine.Mesh.colors32.</summary>
 		/// <summary> Vertex colors. To be used for UnityEngine.Mesh.colors32.</summary>
 		public Color32[] colorBuffer;
 		public Color32[] colorBuffer;
 
 
+		/// <summary> Optional vertex texture coordinates (UVs), second channel. To be used for UnityEngine.Mesh.uv2.
+		/// Using this accessor automatically allocates and resizes the buffer accordingly.</summary>
+		public Vector2[] uv2Buffer { get { return meshGenerator.UV2; } }
+
+		/// <summary> Optional vertex texture coordinates (UVs), third channel. To be used for UnityEngine.Mesh.uv3.
+		/// Using this accessor automatically allocates and resizes the buffer accordingly.</summary>
+		public Vector2[] uv3Buffer { get { return meshGenerator.UV3; } }
+
 		/// <summary> The Spine rendering component's MeshGenerator. </summary>
 		/// <summary> The Spine rendering component's MeshGenerator. </summary>
 		public MeshGenerator meshGenerator;
 		public MeshGenerator meshGenerator;
 	}
 	}
@@ -118,6 +126,13 @@ namespace Spine.Unity {
 		[NonSerialized] Vector2[] tempTanBuffer;
 		[NonSerialized] Vector2[] tempTanBuffer;
 		[NonSerialized] ExposedList<Vector2> uv2;
 		[NonSerialized] ExposedList<Vector2> uv2;
 		[NonSerialized] ExposedList<Vector2> uv3;
 		[NonSerialized] ExposedList<Vector2> uv3;
+
+		/// <summary> Optional vertex texture coordinates (UVs), second channel. To be used for UnityEngine.Mesh.uv2.
+		/// Using this accessor automatically allocates and resizes the buffer accordingly.</summary>
+		public Vector2[] UV2 { get { PrepareOptionalUVBuffer(ref uv2, vertexBuffer.Count); return uv2.Items; } }
+		/// <summary> Optional vertex texture coordinates (UVs), third channel. To be used for UnityEngine.Mesh.uv3.
+		/// Using this accessor automatically allocates and resizes the buffer accordingly.</summary>
+		public Vector2[] UV3 { get { PrepareOptionalUVBuffer(ref uv3, vertexBuffer.Count); return uv3.Items; } }
 		#endregion
 		#endregion
 
 
 		public int VertexCount { get { return vertexBuffer.Count; } }
 		public int VertexCount { get { return vertexBuffer.Count; } }
@@ -766,17 +781,8 @@ namespace Spine.Unity {
 					int vi = vertexIndex;
 					int vi = vertexIndex;
 					b2.y = 1f;
 					b2.y = 1f;
 
 
-					{
-						if (uv2 == null) {
-							uv2 = new ExposedList<Vector2>();
-							uv3 = new ExposedList<Vector2>();
-						}
-						if (totalVertexCount > uv2.Items.Length) { // Manual ExposedList.Resize()
-							Array.Resize(ref uv2.Items, totalVertexCount);
-							Array.Resize(ref uv3.Items, totalVertexCount);
-						}
-						uv2.Count = uv3.Count = totalVertexCount;
-					}
+					PrepareOptionalUVBuffer(ref uv2, totalVertexCount);
+					PrepareOptionalUVBuffer(ref uv3, totalVertexCount);
 
 
 					Vector2[] uv2i = uv2.Items;
 					Vector2[] uv2i = uv2.Items;
 					Vector2[] uv3i = uv3.Items;
 					Vector2[] uv3i = uv3.Items;
@@ -1039,17 +1045,9 @@ namespace Spine.Unity {
 
 
 			int ovc = vertexBuffer.Count;
 			int ovc = vertexBuffer.Count;
 			int newVertexCount = ovc + vertexCount;
 			int newVertexCount = ovc + vertexCount;
-			{
-				if (uv2 == null) {
-					uv2 = new ExposedList<Vector2>();
-					uv3 = new ExposedList<Vector2>();
-				}
-				if (newVertexCount > uv2.Items.Length) { // Manual ExposedList.Resize()
-					Array.Resize(ref uv2.Items, newVertexCount);
-					Array.Resize(ref uv3.Items, newVertexCount);
-				}
-				uv2.Count = uv3.Count = newVertexCount;
-			}
+
+			PrepareOptionalUVBuffer(ref uv2, newVertexCount);
+			PrepareOptionalUVBuffer(ref uv3, newVertexCount);
 
 
 			Vector2[] uv2i = uv2.Items;
 			Vector2[] uv2i = uv2.Items;
 			Vector2[] uv3i = uv3.Items;
 			Vector2[] uv3i = uv3.Items;
@@ -1058,6 +1056,25 @@ namespace Spine.Unity {
 				uv3i[ovc + i] = bo;
 				uv3i[ovc + i] = bo;
 			}
 			}
 		}
 		}
+
+		void PrepareOptionalUVBuffer (ref ExposedList<Vector2> uvBuffer, int vertexCount) {
+			if (uvBuffer == null) {
+				uvBuffer = new ExposedList<Vector2>();
+			}
+			if (vertexCount > uvBuffer.Items.Length) { // Manual ExposedList.Resize()
+				Array.Resize(ref uvBuffer.Items, vertexCount);
+			}
+			uvBuffer.Count = vertexCount;
+		}
+
+		void ResizeOptionalUVBuffer (ref ExposedList<Vector2> uvBuffer, int vertexCount) {
+			if (uvBuffer != null) {
+				if (vertexCount != uvBuffer.Items.Length) {
+					Array.Resize(ref uvBuffer.Items, vertexCount);
+					uvBuffer.Count = vertexCount;
+				}
+			}
+		}
 		#endregion
 		#endregion
 
 
 		#region Step 3 : Transfer vertex and triangle data to UnityEngine.Mesh
 		#region Step 3 : Transfer vertex and triangle data to UnityEngine.Mesh
@@ -1100,18 +1117,12 @@ namespace Spine.Unity {
 					mesh.normals = this.normals;
 					mesh.normals = this.normals;
 				}
 				}
 
 
-				if (settings.tintBlack) {
-					if (uv2 != null) {
-						// Sometimes, the vertex buffer becomes smaller. We need to trim the size of the tint black buffers to match.
-						if (vbiLength != uv2.Items.Length) {
-							Array.Resize(ref uv2.Items, vbiLength);
-							Array.Resize(ref uv3.Items, vbiLength);
-							uv2.Count = uv3.Count = vbiLength;
-						}
-						mesh.uv2 = this.uv2.Items;
-						mesh.uv3 = this.uv3.Items;
-					}
-				}
+				// Sometimes, the vertex buffer becomes smaller. We need to trim the size of
+				// the uv2 and uv3 buffers (used for tint black) to match.
+				ResizeOptionalUVBuffer(ref uv2, vbiLength);
+				ResizeOptionalUVBuffer(ref uv3, vbiLength);
+				mesh.uv2 = this.uv2 == null ? null : this.uv2.Items;
+				mesh.uv3 = this.uv3 == null ? null : this.uv3.Items;
 			}
 			}
 		}
 		}