How to transform mesh UVs via matrix rotation

I’m building a mesh procedurally. And I am multiplying the vertices against a matrix to rotate it.

Matrix4x4 rotation = Matrix4x4.Rotate(Quaternion.Euler(rx, ry, rz));
foreach(Vector3 v in verts) { 
    Vector3 n = rotation.MultiplyPoint3x4(v);
}

How would I do the same with its UVs though? Since they are in 2D space I think there is another step needed.

You shouldn’t need to rotate the UVs although you may need to rotate the normals which I believe can be done with MultiplyVector.

Hey zulo, I am building the mesh procedurally with burst and I was wanting the UVs to stay in the same positions no matter how I rotate it.

if (t) {
    verts.Add(new(-0.5f, s, -0.5f));
    verts.Add(new(-0.5f, s, +0.5f));
    verts.Add(new(+0.5f, s, +0.5f));
    verts.Add(new(+0.5f, s, -0.5f));
    MB.mesh.tris.Write<int>(i1 + 0); MB.mesh.tris.Write<int>(i1 + 1); MB.mesh.tris.Write<int>(i1 + 2);
    MB.mesh.tris.Write<int>(i1 + 0); MB.mesh.tris.Write<int>(i1 + 2); MB.mesh.tris.Write<int>(i1 + 3);
    MB.mesh.faces.Write<int>(i2 + 0); MB.mesh.faces.Write<int>(i2 + 1);
    MB.mesh.IDs.Write<int>(ID); MB.mesh.IDs.Write<int>(ID);
    MB.mesh.highlighter.Write<int3>(new(i2 + 0, i1, 4)); MB.mesh.highlighter.Write<int3>(new(i2 + 1, i1, 4));
    i1 += 4; i2 += 2;
    MB.mesh.uvs.Write<Vector3>(new(0,0,m));
    MB.mesh.uvs.Write<Vector3>(new(0,h,m));
    MB.mesh.uvs.Write<Vector3>(new(1,h,m));
    MB.mesh.uvs.Write<Vector3>(new(1,0,m));
}

So if you’re rotating the mesh around its Y axis then you would shift the UVs like this:

uv.x+=rotateAmount/360;

I need to take the verts of a face, convert them to world space, transform them via matrix, and then convert them back to 2D space but I’m not sure how.

I think the closest you’ll get to that is remapping the mesh’s UVs using spherical mapping. That’s something typically done in modeling applications. I think you’ll struggle to find something like this for Unity because it has very limited use because it can only really work well on simple convex meshes like spheres and cubes.

Alrighty thx, I’ll try a different approach.