Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question How would I calculate Uvs for Marching Cubes?

Discussion in 'Scripting' started by Fluffysnails46, Nov 22, 2022.

  1. Fluffysnails46

    Fluffysnails46

    Joined:
    Jan 15, 2021
    Posts:
    52
    So I have been trying to texture a procedurally generated mesh made with marching cubes with textures. So far I have looked at a few options, and out of all of them, I would like to use uvs. The problem is, I have never done anything with uvs, and my code uses mesh interpolation to make the mesh smoother.

    Also if you need to look at my code, here is this random mess that makes everything work:
    PlanetGenerationCode/MarchingCubes.cs at main · Fluffysnails/PlanetGenerationCode (github.com)
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    Santa and orionsyndrome like this.
  3. Fluffysnails46

    Fluffysnails46

    Joined:
    Jan 15, 2021
    Posts:
    52
    It worked, although is there any way I could add multiple textures using a texture atlas? I would like to have different textures on certain parts of the procedurally generated mesh.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    For true atlassing you would need to integrate knowledge about which texture is where within the atlas, and what triangles should be mapped to it, and then you would have to do clever things for wrapping. It would be a lot of fiddly work to get right.

    And easier way would just be to use submeshes: submeshes each map to separate textures. That approach will "just work" with the above script without any further modifications.
     
  5. Furgl

    Furgl

    Joined:
    Jan 6, 2022
    Posts:
    13
    I used the snippet of your code below for calculating UVs for my meshes generated from MC:
    Code (CSharp):
    1. for (int i = 0; i < triangles.Length - 2; i += 3) {
    2.     Vector3 norm = Vector3.Cross(
    3.         vertices[triangles[i + 1]] - vertices[triangles[i + 0]],
    4.         vertices[triangles[i + 1]] - vertices[triangles[i + 2]]).normalized;
    5.  
    6.     float dotX = Mathf.Abs(Vector3.Dot(norm, Vector3.right));
    7.     float dotY = Mathf.Abs(Vector3.Dot(norm, Vector3.up));
    8.     float dotZ = Mathf.Abs(Vector3.Dot(norm, Vector3.forward));
    9.  
    10.     if (dotX > dotY && dotX > dotZ) {
    11.         for (int j = 0; j < 3; j++) {
    12.             uvs[triangles[i + j]] = new Vector2(vertices[triangles[i + j]].z, vertices[triangles[i + j]].y);
    13.         }
    14.     }
    15.     else {
    16.         if (dotY > dotX && dotY > dotZ) {
    17.             for (int j = 0; j < 3; j++) {
    18.                 uvs[triangles[i + j]] = new Vector2(vertices[triangles[i + j]].x, vertices[triangles[i + j]].z);
    19.             }
    20.         }
    21.         else {
    22.             for (int j = 0; j < 3; j++) {
    23.                 uvs[triangles[i + j]] = new Vector2(vertices[triangles[i + j]].x, vertices[triangles[i + j]].y);
    24.             }
    25.         }
    26.     }
    27. }
    For most angles, the UVs work great, but there are some angles that the UVs are stretched or distorted:

    Am I missing something or is there anything that can be changed to improve the UVs for these angles? I'm afraid I don't understand enough currently to fix it myself.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    I think what you are seeing can normally be fixed by selecting ZERO for the smoothing angle on model import, and telling it to Calculate.

    Since these are coming from your MC output, what you're going to have to do is duplicate vertices such that no two triangles use the same vert, or at least no two triangles that fall into separate X/Y/Z planar projecting buckets.

    The reason is that these edges where you see the packed-in lines are actually "tied" to the other edges that are mapping vastly different, and only one can win. Since UV and position are shared in a given vert, the verts must be duped and connected to opposing faces.

    EDIT: Come to think of it you could probably wiggle it directly into the SetUVToWorld code... but that kinda breaks its purity a bit. Maybe with a checkbox? OR! Make it smart... it knows the X/Y/Z planar buckets...

    "We're using BUCKETS!!!!!" - ARC
     
    Last edited: Apr 11, 2023
    Furgl likes this.
  7. Furgl

    Furgl

    Joined:
    Jan 6, 2022
    Posts:
    13
    Having duplicate vertices for triangles in the same X/Y/Z planar projecting plane worked perfectly, thank you!

    Duplicate vertices pose an issue for mesh simplification, but that's a problem for another day!
     
    Kurt-Dekker likes this.