x


What's wrong with this simple mesh manipulation?

When I move some vertices around randomly, I end up with this:

alt text



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?

more ▼

asked Aug 29 '11 at 02:33 AM

Dan the Man gravatar image

Dan the Man
142 5 9 12

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Aug 29 '11 at 03:09 AM

Owen Reynolds gravatar image

Owen Reynolds
11.6k 1 7 45

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!)

Nov 02 '11 at 08:12 AM ina
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1366
x139
x122
x34

asked: Aug 29 '11 at 02:33 AM

Seen: 813 times

Last Updated: Nov 02 '11 at 08:12 AM