Skip to main content
Tweeted twitter.com/StackGameDev/status/853973351940984832
added 114 characters in body
Source Link

enter image description here

I have the 3D-coordinates of every vertex of the mesh but that's about it. How would I automatically generate a UV map for this mesh, since this list of vertices will change every time I generate a rock? How do I get all these faces correctly mapped in a (1,1) square?

enter image description hereEDIT 5:

I had to move the code a bit but it finally works now! Like you guys predicted, the seems are not optimal, but I'm happy how it is now. Thanks!

enter image description here

enter image description here

I have the 3D-coordinates of every vertex of the mesh but that's about it. How would I automatically generate a UV map for this mesh, since this list of vertices will change every time I generate a rock? How do I get all these faces correctly mapped in a (1,1) square?

enter image description here

I have the 3D-coordinates of every vertex of the mesh but that's about it. How would I automatically generate a UV map for this mesh, since this list of vertices will change every time I generate a rock? How do I get all these faces correctly mapped in a (1,1) square?

EDIT 5:

I had to move the code a bit but it finally works now! Like you guys predicted, the seems are not optimal, but I'm happy how it is now. Thanks!

enter image description here

added 573 characters in body
Source Link

EDIT 4:

Mesh mesh = new Mesh();
mesh.name = "RockMesh";
List<int> Triangles = new List<int>();

var vertices = Vertices.Select(x => new Vertex(x)).ToList();

var result = MIConvexHull.ConvexHull.Create(vertices);
mesh.vertices = result.Points.Select(x => x.ToVec()).ToArray();
var xxx = result.Points.ToList();

foreach(var face in result.Faces)
{
    Triangles.Add(xxx.IndexOf(face.Vertices[0]));
    Triangles.Add(xxx.IndexOf(face.Vertices[1]));
    Triangles.Add(xxx.IndexOf(face.Vertices[2]));
}

mesh.triangles = Triangles.ToArray();

enter image description here

enter image description here

EDIT 4:

Mesh mesh = new Mesh();
mesh.name = "RockMesh";
List<int> Triangles = new List<int>();

var vertices = Vertices.Select(x => new Vertex(x)).ToList();

var result = MIConvexHull.ConvexHull.Create(vertices);
mesh.vertices = result.Points.Select(x => x.ToVec()).ToArray();
var xxx = result.Points.ToList();

foreach(var face in result.Faces)
{
    Triangles.Add(xxx.IndexOf(face.Vertices[0]));
    Triangles.Add(xxx.IndexOf(face.Vertices[1]));
    Triangles.Add(xxx.IndexOf(face.Vertices[2]));
}

mesh.triangles = Triangles.ToArray();

enter image description here

added 44 characters in body
Source Link

Which gives me the following UV map: enter image description here[![enter image description here][2]][2]

  float scaleFactor = 0.5f;

int[] tris = mesh.triangles;

Vector3[] verts = mesh.vertices;
Vector2[] uvs = new Vector2[verts.Length];

// Iterate over each face (here assuming triangles)
for(int index = 0; index < mesh.trianglestris.Length; index += 3)
{
    // Get the three vertices bounding this triangle.
    Vector3 v1 = verts[mesh.triangles[index]];verts[tris[index]];
    Vector3 v2 = verts[mesh.triangles[indexverts[tris[index + 1]];
    Vector3 v3 = verts[mesh.triangles[indexverts[tris[index + 2]];

    // Compute a vector perpendicular to the face.
    Vector3 normal = Vector3.Cross(v3 - v1, v2 - v1);

        Debug.Log("surface " + index/3 + " : " + (normal*0.5f).magnitude);

    // Form a rotation that points the z+ axis in this perpendicular direction.
    // Multiplying by the inverse will flatten the triangle into an xy plane.
    Quaternion rotation = Quaternion.Inverse(Quaternion.LookRotation(normal));

    // Assign the uvs, applying a scale factor to control the texture tiling.
    uvs[mesh.triangles[index]]uvs[tris[index]]     = (Vector2)(rotation * v1) * scaleFactor;
    uvs[mesh.triangles[indexuvs[tris[index + 1]] = (Vector2)(rotation * v2) * scaleFactor;
    uvs[mesh.triangles[indexuvs[tris[index + 2]] = (Vector2)(rotation * v3) * scaleFactor;
}

The red triangle on the left and the dark blue one on the right should be the same shape/size:

enter image description here

Which gives me the following UV map: enter image description here

  float scaleFactor = 0.5f;

Vector3[] verts = mesh.vertices;
Vector2[] uvs = new Vector2[verts.Length];

// Iterate over each face (here assuming triangles)
for(int index = 0; index < mesh.triangles.Length; index += 3)
{
    // Get the three vertices bounding this triangle.
    Vector3 v1 = verts[mesh.triangles[index]];
    Vector3 v2 = verts[mesh.triangles[index + 1]];
    Vector3 v3 = verts[mesh.triangles[index + 2]];

    // Compute a vector perpendicular to the face.
    Vector3 normal = Vector3.Cross(v3 - v1, v2 - v1);

    // Form a rotation that points the z+ axis in this perpendicular direction.
    // Multiplying by the inverse will flatten the triangle into an xy plane.
    Quaternion rotation = Quaternion.Inverse(Quaternion.LookRotation(normal));

    // Assign the uvs, applying a scale factor to control the texture tiling.
    uvs[mesh.triangles[index]]     = (Vector2)(rotation * v1) * scaleFactor;
    uvs[mesh.triangles[index + 1]] = (Vector2)(rotation * v2) * scaleFactor;
    uvs[mesh.triangles[index + 2]] = (Vector2)(rotation * v3) * scaleFactor;
}

Which gives me the following UV map: [![enter image description here][2]][2]

  float scaleFactor = 0.5f;

int[] tris = mesh.triangles;

Vector3[] verts = mesh.vertices;
Vector2[] uvs = new Vector2[verts.Length];

// Iterate over each face (here assuming triangles)
for(int index = 0; index < tris.Length; index += 3)
{
    // Get the three vertices bounding this triangle.
    Vector3 v1 = verts[tris[index]];
    Vector3 v2 = verts[tris[index + 1]];
    Vector3 v3 = verts[tris[index + 2]];

    // Compute a vector perpendicular to the face.
    Vector3 normal = Vector3.Cross(v3 - v1, v2 - v1);

        Debug.Log("surface " + index/3 + " : " + (normal*0.5f).magnitude);

    // Form a rotation that points the z+ axis in this perpendicular direction.
    // Multiplying by the inverse will flatten the triangle into an xy plane.
    Quaternion rotation = Quaternion.Inverse(Quaternion.LookRotation(normal));

    // Assign the uvs, applying a scale factor to control the texture tiling.
    uvs[tris[index]]     = (Vector2)(rotation * v1) * scaleFactor;
    uvs[tris[index + 1]] = (Vector2)(rotation * v2) * scaleFactor;
    uvs[tris[index + 2]] = (Vector2)(rotation * v3) * scaleFactor;
}

The red triangle on the left and the dark blue one on the right should be the same shape/size:

enter image description here

added 1231 characters in body
Source Link
Loading
added 393 characters in body
Source Link
Loading
added 95 characters in body
Source Link
Loading
added 209 characters in body
Source Link
Loading
Source Link
Loading