- Home /
Bone weight set/get is very slow??
I have a lot of meshes that need their bone weight offset in realtime (basically I wrote a skinned mesh combiner that adds geometry to their meshes, and appends bone weights accordingly).
The function runs extremely fast (under a millisecond) except for the part that accesses the mesh's bone weights. All it does is retrieve the original bone weight, offsets the value, and re-applies it. However, running that subroutine through each vertex takes over 30 milliseconds! (compared to under a millisecond to set all the verts, triangles, normals and uvs for the new mesh).
The pseudocode is as follow:
for (i = 0; i < mesh.vertices.length; i++)
{
mesh.boneWeights[i].boneIndex0 += offset;
}
Any way to speed this up?
Answer by Thomas Krog · Apr 12, 2011 at 02:30 PM
I think you need to access mesh.boneWeights as few times as possible. Try this simple performance test:
BoneWeight[] ws = new BoneWeight[mesh.boneWeights.Length];
for(int i = 0; i != ws.Length; ++i)
ws[i].boneIndex0 = i % numBones;
mesh.boneWeights = ws;
Answer by Paulius-Liekis · Apr 12, 2011 at 02:36 PM
I believe mesh.boneWeights[i] has to make a copy each time, so if you have 1000 vertices it will make 1000 copies. Try something like this and see if it works faster:
BoneWeight[] ws = mesh.boneWeights;
for (i = 0; i < mesh.vertices.length; i++)
{
ws[i].boneIndex0 += offset;
}
mesh.boneWeights = ws;
Anyway... modifying a mesh at runtime is not a good idea. Unity is not optimized for that.
Yes I have also experienced that mesh modifications are quite slow. A bit annoying that it is so much slower than oldschool glBegin. All I want is some flexible debugdrawing which merges with the ingame rendering. I think when we have such powerful hardware we could expect more flexibility than precached meshes (does not fit very well with functional program$$anonymous$$g sml etc)
You should think that Unity is not good at it at the moment. It is growing in multiple directions and issues like these will be addressed in the future. You just can't be good at everything at once :)