The idea is to optimize some meshes data at runtime using the Advanced Mesh API. But I’m having issues after I try it. The Mesh ends up distorted, and I’m getting an error:
SkinnedMeshRenderer: Mesh has been changed to one which is not compatible with the expected mesh data size and vertex stride. Aborting rendering
This is what I’m doing:
- Clone the original mesh
- Get its VertexAttributes by using GetVertexAttributes()
- Loop thru each existing attribute
- If it is Position, Normal, Tangent, or BlendWeight, I set its format to Float16 and the dimension to 4.
- If it is TexCoord0, I set its format to Float16 and dimension to 2
- If it is BlendIndices, I change its format to UInt16
- Set mesh data using mesh.SetVertexBufferParams()
- Set the mesh to the Skinned Mesh Renderer.
If I remove the code that modifies Position, Normal and Tangent Attributes, I don’t get the distortion, and the other Attributes get optimized. So, I think that I’m doing something wrong with those…
The mesh looks like it was successfully optimized.
But in the scene, I have that error I wrote at the beginning, and the mesh is distorted:
And, here is the code:
private Mesh Optimize(Mesh original)
{
var mesh = Instantiate(original);
var attributes = mesh.GetVertexAttributes();
for (int i = 0; i < attributes.Length; i++) {
var attribute = attributes[i];
if (attribute.attribute == VertexAttribute.Position
|| attribute.attribute == VertexAttribute.Normal
|| attribute.attribute == VertexAttribute.Tangent
|| attribute.attribute == VertexAttribute.BlendWeight
)
{
attribute.format = VertexAttributeFormat.Float16;
attribute.dimension = 4;
}
if (attribute.attribute == VertexAttribute.TexCoord0) {
attribute.format = VertexAttributeFormat.Float16;
attribute.dimension = 2;
}
if (attribute.attribute == VertexAttribute.BlendIndices) {
attribute.format = VertexAttributeFormat.UInt16;
}
//Save modified attribute
attributes[i] = attribute;
}
mesh.SetVertexBufferParams(mesh.vertexCount, attributes);
return mesh;
}
Btw, I also tried using the mesh.SetVertexBufferData(), but it usually doesn’t do anything else, and sometimes I get more errors with it.
Thanks for reading all these!