Getting "vertexSize != stride" when using mesh tangents

Hi!

I am trying to use the tangents fields of a procedural generated mesh, but Unity allways shows those exceptions:

  • Unhandled vertex structure for strided buffers!
  • vertexSize != stride

I am using this code (simplified version) to generate the mesh on awake of a monobehaviour:

private Mesh BuildMesh()
{
    const float PxSize = 0.0009765625f;
    const int VertexSize = 4;
    const int TriangleSize = 6;

    Rect uv0Data = new Rect(PxSize * 1.0f, PxSize * 1.0f, PxSize * 3.0f, PxSize * 3.0f) ;
    Vector2 uv1Data = new Vector2(2.0f, 4.0f) ;
    
    Vector3[] verts = new Vector3[VertexSize];
    Vector3[] normals = new Vector3[VertexSize];
    Vector2[] uv = new Vector2[VertexSize];
    Vector2[] uv1 = new Vector2[VertexSize];
    Vector4[] tangents = new Vector4[VertexSize];
    int[] tri = new int[TriangleSize];

    verts[0] = new Vector3(0, 0, 0);
    verts[1] = new Vector3(1, 0, 0);
    verts[2] = new Vector3(0, 0, 1);
    verts[3] = new Vector3(1, 0, 1);

    for (int i = 0; i < 4; i++)
    {
        normals *= Vector3.up;*

}
uv[0] = new Vector2(uv0Data.x, uv0Data.y);
uv[1] = new Vector2(uv0Data.x + uv0Data.width, uv0Data.y);
uv[2] = new Vector2(uv0Data.x, uv0Data.y + uv0Data.height);
uv[3] = new Vector2(uv0Data.x + uv0Data.width, uv0Data.y + uv0Data.height);
uv1[0] = uv1[1] = uv1[2] = uv1[3] = uv1Data;
tri[0] = 0;
tri[1] = 2;
tri[2] = 3;
tri[3] = 0;
tri[4] = 3;
tri[5] = 1;
tangents[0] = tangents[1] = tangents[2] = tangents[3] = new Vector4(uv0Data.x, uv0Data.y, uv0Data.width, uv0Data.height);
Mesh m = new Mesh
{ vertices = verts, triangles = tri, uv = uv, uv1 = uv1, normals = normals, tangents = tangents };
return m;
}
The uv0, uv1/2 and normals attributes are already used. I require the tangent field for custom data which I have to deliver to the shader. I already tried:
- Using UV1 and UV2 (impossible because Unity writes both channels to TEXCOORD1)
- Using color field for the user data (impractible because color gets clamped and reduced to 8bit)
Is there a way to get rid of the exceptions or pass a 4D float vector as vertex attribute?
Thanks in advance.

Well, that sounds like an internal error. My guess is that happens because you assign the triangles too early. Unity have to decide what Vertex-format it should use for this mesh, but since they split the data into seperate array you can’t say what data you’re going to use. By assigning the triangles array the actual VBOs are generated. uv and normaly is no problem since nearly every format uses them so i guess Unity always includes those. Tangents are rarely used.

The order in which you assign the array is up to you but you should keep those things in mind:

  • If you want to change the vertex count you have to assign the vertices-array first.
  • Next step you should complete the Vertex data information (uvs,normals,colors,tangents,…)
  • According to the docs, assign tangents after normals.
  • The last step should be to assign the index-buffer (triangles array) since it will actually create the mesh.
  • If you want to completely change the vertex format you have to call Mesh.Clear() and start all over.