- Home /
What's wrong with this simple mesh manipulation?
When I move some vertices around randomly, I end up with this:
void Start() {
Mesh mesh = GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;
Vector3[] normals = mesh.normals;
for(int i = 0; i < vertices.Length; i++) {
float rand = Random.Range(-0.1f, 0.1f);
vertices[i].x += rand;
vertices[i].y += rand;
vertices[i].z += rand;
}
mesh.vertices = vertices;
mesh.RecalculateNormals();
}
Am I missing something?
Answer by Owen-Reynolds · Aug 29, 2011 at 03:09 AM
If you print vertices.Length
, you'll see it's 24 -- 4 for each face. The code is working perfectly. Each of the three verts at a corner is getting its own random shift.
Turns out graphics cards want to smooth-shade everything. When you make a hard-edge (a crease, in a modelling program) you're really splitting the verts on that edge. Seems a waste of verts, but every other way takes longer to draw. Technically, it's possible to set a model to no smooth-shading at all, but you have to specially redo the normals based on the order of the face array, which is too big a pain.
There's no easy pattern to find "same" verts: Debug.Log shows (0.5,0.5,0.5) occurs at indexes 2, 8 and 21, for example. I guess each time you change one, you could loop again to also change ones with the same values by that amount; with another array of bools to marked them as changed.
graphics cards wanting to smooth-shade everything, is that true on mobile Android/iOS as well? (p.s. we have the same user avatar icon!)