- Home /
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[i] = 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.
uv1 has been renamed to uv2, they are the same property. What you are doing should work otherwise, i cannot find that error in our source. Does the console show any line numbers? Also, can you try setting triangles last, after setting all the other properties?
Hi Jonas, thanks for the fast answer. Setting the triangles last does not change anything. The console shows nothing but the editor log says:
$$anonymous$$ono: successfully reloaded assembly Unhandled vertex structure for strided buffers! UnityEditor.EditorGUIUtility:INTERNAL_CALL_RenderGameViewCameras(Rect&, Rect&, Boolean, Boolean) UnityEditor.EditorGUIUtility:RenderGameViewCameras(Rect, Rect, Boolean, Boolean) (at C:\BuildAgent\work\6bc5f79e0a4296d6\Editor\$$anonymous$$onoGenerated\Editor\EditorGUIUtility.cs:261) UnityEditor.GameView:OnGUI() (at C:\BuildAgent\work\6bc5f79e0a4296d6\Editor\$$anonymous$$ono\GameView\GameView.cs:372) System.Reflection.$$anonymous$$ono$$anonymous$$ethod:InternalInvoke(Object, Object[], Exception&) System.Reflection.$$anonymous$$ono$$anonymous$$ethod:Invoke(Object, BindingFlags, Binder, Object[], CultureInfo) System.Reflection.$$anonymous$$ethodBase:Invoke(Object, Object[]) UnityEditor.HostView:Invoke(String, Object) (at C:\BuildAgent\work\6bc5f79e0a4296d6\Editor\$$anonymous$$ono\GUI\DockArea.cs:213) UnityEditor.HostView:Invoke(String) (at C:\BuildAgent\work\6bc5f79e0a4296d6\Editor\$$anonymous$$ono\GUI\DockArea.cs:206) UnityEditor.DockArea:OnGUI() (at C:\BuildAgent\work\6bc5f79e0a4296d6\Editor\$$anonymous$$ono\GUI\DockArea.cs:635)
[C:/BuildAgent/work/6bc5f79e0a4296d6/Runtime/Shaders/VBO.cpp line 492] (Filename: C:/BuildAgent/work/6bc5f79e0a4296d6/Editor/$$anonymous$$onoGenerated/Editor/EditorGUIUtility.cs Line: 261)
vertexSize != stride UnityEditor.EditorGUIUtility:INTERNAL_CALL_RenderGameViewCameras(Rect&, Rect&, Boolean, Boolean) UnityEditor.EditorGUIUtility:RenderGameViewCameras(Rect, Rect, Boolean, Boolean) (at C:\BuildAgent\work\6bc5f79e0a4296d6\Editor\$$anonymous$$onoGenerated\Editor\EditorGUIUtility.cs:261) UnityEditor.GameView:OnGUI() (at C:\BuildAgent\work\6bc5f79e0a4296d6\Editor\$$anonymous$$ono\GameView\GameView.cs:372) System.Reflection.$$anonymous$$ono$$anonymous$$ethod:InternalInvoke(Object, Object[], Exception&) System.Reflection.$$anonymous$$ono$$anonymous$$ethod:Invoke(Object, BindingFlags, Binder, Object[], CultureInfo) System.Reflection.$$anonymous$$ethodBase:Invoke(Object, Object[]) UnityEditor.HostView:Invoke(String, Object) (at C:\BuildAgent\work\6bc5f79e0a4296d6\Editor\$$anonymous$$ono\GUI\DockArea.cs:213) UnityEditor.HostView:Invoke(String) (at C:\BuildAgent\work\6bc5f79e0a4296d6\Editor\$$anonymous$$ono\GUI\DockArea.cs:206) UnityEditor.DockArea:OnGUI() (at C:\BuildAgent\work\6bc5f79e0a4296d6\Editor\$$anonymous$$ono\GUI\DockArea.cs:635)
[C:/BuildAgent/work/6bc5f79e0a4296d6/Runtime/GfxDevice/d3d/D3D9VBO.cpp line 145] (Filename: C:/BuildAgent/work/6bc5f79e0a4296d6/Editor/$$anonymous$$onoGenerated/Editor/EditorGUIUtility.cs Line: 261)
Hmm, which version of Unity are you using? I'm wondering, because 3.4.1 does not have that error message anywhere, and also does not have a line 492 in VBO.cpp.
I am using unity 3.3.0. With unity 3.4.0 the bug has disapeared. I will download unity 3.4.1 and check if it still works.
Thanks for the great help.
Answer by Bunny83 · Sep 20, 2011 at 11:51 AM
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.
Thank you for your help. But the the given solution does not work in Unity 3.3.0; no matter which order is used. Tangents are broken in 3.3.0. I have to see if upgrading to unity 3.4.1. is an option for us.
Your answer
Follow this Question
Related Questions
Creating a point cloud engine using shaders/meshes 0 Answers
Cancel out mesh alpha 1 Answer
how to shade a mesh created in unity 2 Answers
What are normals? 1 Answer
How to realize Unity Editor Scene Filter View Effect in my game. 0 Answers