Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Mesh UV Error

Discussion in 'Scripting' started by DeveloperZeeDE, Jun 3, 2020.

  1. DeveloperZeeDE

    DeveloperZeeDE

    Joined:
    Oct 28, 2018
    Posts:
    6
    not working.PNG
    Hello guys, whats the problem?



    Create Voxel Method:
    Code (CSharp):
    1.     private void CreateCube ()
    2.     {
    3.         // pos = some custom position for testing
    4.         Vector3[] vertices =
    5.         {
    6.             pos + new Vector3 (0, 0, 0),
    7.             pos + new Vector3 (1, 0, 0),
    8.             pos + new Vector3 (1, 1, 0),
    9.             pos + new Vector3 (0, 1, 0),
    10.             pos + new Vector3 (0, 1, 1),
    11.             pos + new Vector3 (1, 1, 1),
    12.             pos + new Vector3 (1, 0, 1),
    13.             pos + new Vector3 (0, 0, 1),
    14.         };
    15.  
    16.         int[] triangles =
    17.         {
    18.             0, 2, 1, //face front
    19.             0, 3, 2,
    20.             2, 3, 4, //face top
    21.             2, 4, 5,
    22.             1, 2, 5, //face right
    23.             1, 5, 6,
    24.             0, 7, 4, //face left
    25.             0, 4, 3,
    26.             5, 4, 7, //face back
    27.             5, 7, 6,
    28.             0, 6, 7, //face bottom
    29.             0, 1, 6
    30.         };
    31.  
    32.         mesh.Clear ();
    33.         mesh.vertices = vertices;
    34.         mesh.triangles = triangles;
    35.         mesh.uv = UvCalculator.CalculateUVs(vertices, 1.0f);
    36.         mesh.RecalculateNormals();
    37.         mesh.Optimize();
    38.  
    39.         print("Vertex Count: " + mesh.vertexCount);
    40.         print("Triangles: " + mesh.triangles.Length);
    41.         print("UVS: " + mesh.uv.Length);
    42.     }
    UvCalculator Code:
    Code (CSharp):
    1.  public class UvCalculator
    2. {
    3.      private enum Facing { Up, Forward, Right };
    4.  
    5.      public static Vector2[] CalculateUVs(Vector3[] v/*vertices*/, float scale)
    6.      {
    7.          var uvs = new Vector2[v.Length];
    8.      
    9.          for (int i = 0 ; i < uvs.Length; i += 3)
    10.          {
    11.                  int i0 = i;
    12.                  int i1 = i+1;
    13.                  int i2 = i+2;
    14.                  //Special handling if vertex count isn't a multiple of 3
    15.                  if(i == uvs.Length - 1) {
    16.                      i1 = 0;
    17.                      i2 = 1;
    18.                  }
    19.                  if(i == uvs.Length - 2) {
    20.                      i2 = 0;
    21.                  }
    22.              
    23.          
    24.              Vector3 v0 = v[i0];
    25.              Vector3 v1 = v[i1];
    26.              Vector3 v2 = v[i2];
    27.          
    28.              Vector3 side1 = v1 - v0;
    29.              Vector3 side2 = v2 - v0;
    30.              var direction = Vector3.Cross(side1, side2);
    31.              var facing = FacingDirection(direction);
    32.              switch (facing)
    33.              {
    34.              case Facing.Forward:
    35.                  uvs[i0] = ScaledUV(v0.x, v0.y, scale);
    36.                  uvs[i1] = ScaledUV(v1.x, v1.y, scale);
    37.                  uvs[i2] = ScaledUV(v2.x, v2.y, scale);
    38.                  break;
    39.              case Facing.Up:
    40.                  uvs[i0] = ScaledUV(v0.x, v0.z, scale);
    41.                  uvs[i1] = ScaledUV(v1.x, v1.z, scale);
    42.                  uvs[i2] = ScaledUV(v2.x, v2.z, scale);
    43.                  break;
    44.              case Facing.Right:
    45.                  uvs[i0] = ScaledUV(v0.y, v0.z, scale);
    46.                  uvs[i1] = ScaledUV(v1.y, v1.z, scale);
    47.                  uvs[i2] = ScaledUV(v2.y, v2.z, scale);
    48.                  break;
    49.              }
    50.          }
    51.          return uvs;
    52.      }
    53.  
    54.      private static bool FacesThisWay(Vector3 v, Vector3 dir, Facing p, ref float maxDot, ref Facing ret)
    55.      {
    56.          float t = Vector3.Dot(v, dir);
    57.          if (t > maxDot)
    58.          {
    59.              ret = p;
    60.              maxDot = t;
    61.              return true;
    62.          }
    63.          return false;
    64.      }
    65.  
    66.      private static Facing FacingDirection(Vector3 v)
    67.      {
    68.          var ret = Facing.Up;
    69.          float maxDot = Mathf.NegativeInfinity;
    70.      
    71.          if (!FacesThisWay(v, Vector3.right, Facing.Right, ref maxDot, ref ret))
    72.              FacesThisWay(v, Vector3.left, Facing.Right, ref maxDot, ref ret);
    73.      
    74.          if (!FacesThisWay(v, Vector3.forward, Facing.Forward, ref maxDot, ref ret))
    75.              FacesThisWay(v, Vector3.back, Facing.Forward, ref maxDot, ref ret);
    76.      
    77.          if (!FacesThisWay(v, Vector3.up, Facing.Up, ref maxDot, ref ret))
    78.              FacesThisWay(v, Vector3.down, Facing.Up, ref maxDot, ref ret);
    79.      
    80.          return ret;
    81.      }
    82.  
    83.      private static Vector2 ScaledUV(float uv1, float uv2, float scale)
    84.      {
    85.          return new Vector2(uv1 / scale, uv2 / scale);
    86.      }
    87. }
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    What material have you assigned, and where do you recalculate the tangents?

    Also... optimize AFTER recalculating normals / tangents?
     
  3. DeveloperZeeDE

    DeveloperZeeDE

    Joined:
    Oct 28, 2018
    Posts:
    6
    im using the default material, im already using Optimize(); after recalculating normals / tangents..
     

    Attached Files:

    • look.PNG
      look.PNG
      File size:
      12.2 KB
      Views:
      318
  4. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    On closer inspection I'm not sure what your problem is. What do you expect? If you expect to see a red tint, make sure the red light has a radius that is large enough, and remove the directional light.

    Also, I recommend you move Optimize() to be executed before you recalc norms/tans, but that may be only cosmetic.

    For a first blush, you may also reduce complexity by commenting out the uv calculations.
     
  5. DeveloperZeeDE

    DeveloperZeeDE

    Joined:
    Oct 28, 2018
    Posts:
    6
    thats my problem, the lighting is different..
    i think the uv calculation is wrong.. i dont know :/
     

    Attached Files:

  6. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Why do you think the UV mapping is wrong? That's only for the textures, and since you use the default material, you don't need it.
     
  7. DeveloperZeeDE

    DeveloperZeeDE

    Joined:
    Oct 28, 2018
    Posts:
    6
    okay but why is the lighting so different, how i can fix that? whats wrong?
     
  8. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Also worth a look: what lighting do you use in your scenery? Baked will not work, so make sure you have enabled real-time.
     
  9. DeveloperZeeDE

    DeveloperZeeDE

    Joined:
    Oct 28, 2018
    Posts:
    6
    in my opinion my "Lighting Settings" are to 100% fine..
    but im pretty sure the uv calculation is wrong (look at the picture)
     

    Attached Files:

  10. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Can you turn off Mixed Lighting and see how the scene looks after that?

    Wrt to the uv: IIRC you do not need the uv meshes for lighting, so remove all uv processing and generation until the lighting is figured out. Once that's resolved, lets focus on texture mapping. But let's try to resolve one bug after the other.