Skip to content

Commit

Permalink
[unity] OnPostProcessVertices callback now provides access to uv2 and…
Browse files Browse the repository at this point in the history
… uv3 buffers. Closes #2230.
  • Loading branch information
HaraldCsaszar committed Jan 25, 2023
1 parent 5081519 commit a8e6552
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 35 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
* **Officially supported Unity versions are 2017.1-2022.1**.

* **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**

* **Changes of default values**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,20 @@ public struct MeshGeneratorBuffers {
/// <summary> Vertex positions. To be used for UnityEngine.Mesh.vertices.</summary>
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;

/// <summary> Vertex colors. To be used for UnityEngine.Mesh.colors32.</summary>
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>
public MeshGenerator meshGenerator;
}
Expand Down Expand Up @@ -118,6 +126,13 @@ public struct Settings {
[NonSerialized] Vector2[] tempTanBuffer;
[NonSerialized] ExposedList<Vector2> uv2;
[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

public int VertexCount { get { return vertexBuffer.Count; } }
Expand Down Expand Up @@ -766,17 +781,8 @@ public struct Settings {
int vi = vertexIndex;
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[] uv3i = uv3.Items;
Expand Down Expand Up @@ -1039,17 +1045,9 @@ public struct Settings {

int ovc = vertexBuffer.Count;
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[] uv3i = uv3.Items;
Expand All @@ -1058,6 +1056,25 @@ public struct Settings {
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

#region Step 3 : Transfer vertex and triangle data to UnityEngine.Mesh
Expand Down Expand Up @@ -1100,18 +1117,12 @@ public struct Settings {
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;
}
}

Expand Down

0 comments on commit a8e6552

Please sign in to comment.