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. Dismiss Notice

Question How to make square ring closed.

Discussion in 'General Graphics' started by Lein, Sep 1, 2023.

  1. Lein

    Lein

    Joined:
    May 4, 2014
    Posts:
    14
    I created a mesh of square ring, which has 16 vertices, then there were 8 square,
    each square has two triangles.

    Please check the picture attached.

    I got right mesh, it's a ring, but wrong image texture.
    The image texture repeated a whole image at the end (top of the square ring).

    Code (CSharp):
    1.  
    2.     static float w = 0.75f;
    3.     static float h = 1f;
    4.     static float t = 0.3f;
    5.     static float arc = 0.1f;
    6.     GameObject main;
    7.     GameObject front;
    8.     GameObject bottom;
    9.     GameObject side;
    10.  
    11.     public static Mesh createMesh(GameObject obj) {
    12.         Mesh mesh = null;
    13.         MeshFilter meshFilter = obj.AddComponent<MeshFilter>();
    14.  
    15.         mesh = new Mesh();
    16.         meshFilter.mesh = mesh;
    17.         MeshRenderer renderer = obj.AddComponent<MeshRenderer>();
    18.  
    19.         Material mat = new Material(Shader.Find("Legacy Shaders/Transparent/Diffuse"));
    20.         mat.color = Color.white;
    21.         renderer.material = mat;
    22.  
    23.         return mesh;
    24.     }
    25.     private void Start() {
    26.          Mesh meshSide = createMesh(side);
    27.         NativeList<JobHandle> jobHandleList = new NativeList<JobHandle>(Allocator.Temp);
    28.  
    29.         int id = JobSideMeshStruct.getId();
    30.         MeshData data = new MeshData();
    31.         meshDataMap.Add(id, data);
    32.  
    33.         JobSideMeshStruct jobCreateSide = new JobSideMeshStruct(id);
    34.         jobHandleList.Add(jobCreateSide.Schedule());
    35.  
    36.         JobHandle.CompleteAll(jobHandleList);
    37.         jobHandleList.Dispose();
    38.  
    39.         meshSide.vertices = data.vertices;
    40.         meshSide.uv = data.uv;
    41.         meshSide.triangles = data.triangles;
    42.  
    43.         StartCoroutine(loadSidePicture());
    44.     }
    45.  
    46.     private IEnumerator loadSidePicture() {
    47.         side.GetComponent<MeshRenderer>().material.mainTexture = Resources.Load("type/side") as Texture;
    48.         yield return null;
    49.     }
    50.  
    51.     static Dictionary<int, MeshData> meshDataMap = new Dictionary<int, MeshData>();
    52.  
    53.     //[BurstCompile]
    54.     public struct JobSideMeshStruct : IJob
    55.     {
    56.         const int vecterCount = 16;
    57.         static private int ID = 0;
    58.         static private object l = new object();
    59.         public static int getId() {
    60.             int i = 0;
    61.             lock(l) {
    62.                 ID++;
    63.                 i = ID;
    64.             }
    65.             return i;
    66.         }
    67.  
    68.         private int id;
    69.  
    70.         public JobSideMeshStruct(int i) {
    71.             id = i;
    72.         }
    73.  
    74.         public void Execute()
    75.         {
    76.             /*byte[] arr = new byte[inStr.Length];
    77.             inStr.CopyTo(arr);
    78.             string s = new string(Encoding.ASCII.GetChars(arr));*/
    79.  
    80.             MeshData data = meshDataMap[id];
    81.  
    82.             Vector3[] vertices = new Vector3[vecterCount+2];
    83.             Vector2[] uv = new Vector2[vecterCount+2];
    84.  
    85.             int i = 0;
    86.             vertices[0]  = new Vector3(arc, h, 0);
    87.             vertices[2]  = new Vector3(0, h - arc, 0);
    88.             vertices[4]  = new Vector3(0, arc, 0);
    89.             vertices[6]  = new Vector3(arc, 0, 0);
    90.             vertices[8]  = new Vector3(w - arc, 0, 0);
    91.             vertices[10] = new Vector3(w, arc, 0);
    92.             vertices[12] = new Vector3(w, h - arc, 0);
    93.             vertices[14] = new Vector3(w - arc, h, 0);
    94.  
    95.             for (i = 1; i < vecterCount; i += 2) {
    96.                 vertices[i] = vertices[i - 1];
    97.                 vertices[i].z = t;
    98.             }
    99.  
    100.             float bevelSize = (float)Math.Sqrt(2 * arc * arc);
    101.             float _w = w - 2 * arc;
    102.             float _h = h - 2 * arc;
    103.  
    104.             float y = 0;
    105.             float len = (float) ((_w + _h) * 2 + bevelSize * 4);
    106.  
    107.             Debug.Log("side total len="+len+ ",bevelSize="+ bevelSize+ ",_w="+ _w+ ", _h="+ _h);
    108.             String log = "";
    109.             int step = 0;
    110.             float last = 0;
    111.             float cur = 0;
    112.             for (i = 0; i < 16; i++) {
    113.            
    114.                 uv[i++] = new Vector2(1, y/len);
    115.                 uv[i] = new Vector2(0, y/len);
    116.                 cur = (y * 200);
    117.                 log += "step=" + step + ", y=" + (y / len)+", img y="+ cur + ",v="+(cur-last)+"\n";
    118.  
    119.                 last = cur;
    120.  
    121.                 if (step % 2 == 0)
    122.                     y += bevelSize;
    123.                 else
    124.                 {
    125.                     if (step % 4 == 1)
    126.                         y += _h;
    127.                     else
    128.                         y += _w;
    129.                 }
    130.  
    131.                 step++;
    132.             }
    133.             Debug.Log(log);
    134.  
    135.             /* this got no use
    136.             vertices[16] = vertices[0];
    137.             vertices[17] = vertices[1];
    138.  
    139.             uv[16] = uv[0];
    140.             uv[17] = uv[1];
    141.             */
    142.             data.vertices = vertices;
    143.             data.uv = uv;
    144.  
    145.             int[] triangles = new int[48];
    146.             i = 0;
    147.             int idx = 0;
    148.             for (i=0; i< vecterCount; i+=2) {
    149.                 triangles[idx++] = i;
    150.                 triangles[idx++] = i + 2;
    151.                 triangles[idx++] = i + 1;
    152.  
    153.                 triangles[idx++] = i + 1;
    154.                 triangles[idx++] = i + 2;
    155.                 triangles[idx++] = i + 3;
    156.  
    157.                 //Debug.Log("idx="+idx+", i="+(i+3));
    158.             }
    159.  
    160.             for (i=0; i< triangles.Length-2; i++) {
    161.                 if (triangles[i] >= vecterCount)
    162.                 {
    163.                     Debug.LogError("triangles[" + i + "]=" + triangles[i]);
    164.                     triangles[i] = triangles[i] % vecterCount;
    165.                 }
    166.             }
    167.  
    168.             data.triangles = triangles;
    169.         }
    170.     }
    How to make the texture closed right?
    May thanks for any help!
     

    Attached Files:

    • q1.png
      q1.png
      File size:
      75 KB
      Views:
      20
    Last edited: Sep 1, 2023
  2. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    547
    That's because you are going from u=0.9 to u=0 instead of u=0.9 to u=1.
    You have to duplicate the vertices which close the loop and assign u=0 and u=1 to them respectively.

    upload_2023-9-1_8-52-8.png
     
    Last edited: Sep 1, 2023
  3. Lein

    Lein

    Joined:
    May 4, 2014
    Posts:
    14

    Many thanks!

    Code (CSharp):
    1.             vertices[16] = vertices[0];
    2.             vertices[17] = vertices[1];
    3.             uv[16] = new Vector2(0, 1);
    4.             uv[17] = new Vector2(1, 1);
    5.            
    6.            /*for (i=0; i< triangles.Length-2; i++) {//wrong code
    7.                 if (triangles[i] >= vecterCount)
    8.                 {
    9.                     Debug.LogError("triangles[" + i + "]=" + triangles[i]);
    10.                     triangles[i] = triangles[i] % vecterCount;
    11.                 }
    12.             }*/
    13.  
    14.  
    I added overlap vertices and uv.
     

    Attached Files:

    • ok.png
      ok.png
      File size:
      48.3 KB
      Views:
      15
    c0d3_m0nk3y likes this.