Search Unity

Setting UV, Cube Issue

Discussion in 'Scripting' started by renman3000, Dec 18, 2014.

  1. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,699
    Hi there,
    I am trying to set the UVs of a cube, using an atlas texture, but am not getting the results I would expect.


    Here is the image I want to place on each side of the cube...



    This is what I am trying to set as the image that occupies each side of the cube. (4 different sides)



    This image is on it's atlas (bottom left)





    Here is my code.
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class TestUVs : MonoBehaviour {
    6.  
    7.     public MeshFilter theMesh;
    8.     public Vector2[] theUVs;
    9.  
    10.  
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.         //////////////////////////////////////////////
    15.         Vector2[] uvs = new Vector2[theMesh.mesh.uv.Length];
    16.         int l = 0;
    17.         for (int i = 0; i < uvs.Length; i++){
    18.             uvs[i] = theUVs[l];
    19.             l++;
    20.             if(l == 4)
    21.             l = 0;
    22.         }
    23.         theMesh.mesh.uv = uvs;
    24.     }
    25. }
    26.  
    27.  




    What puzzles me mostly is why am I getting the white line (oultine of the cube) running the diagonal?
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    I get the impression that you are expecting the uv array to be in some specific order (to match it to your publicly defined array). It isnt.
    The uv array and the vertex position array run in parallel, you either need to change the order of the vertex array to match the uv array you are creating (and also the tris array), or create the uv array in the same order as the existing vert array.
     
  3. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,699
    Well, "theUVs", a public Vector2[] I set to a size of 4. This is because my thinking tells me there are 4 per side. So Instead of making 24, in repeating 4's, I set the first 4 and repeat the others through.

    ?
     
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Well yeah, there is 4 per side, but there's no guarantee that they are in order
     
  5. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,699
    The thing is, each set of UVs, 4 per side, has the same values set.

    So, uvs[0]= 0,0, uvs[1] = 1,0.5 and so on up to uvs[3]. These four values I then apply in the same order to the remaining sets of 4's. So I am not only puzzled by the diagonal layout I am getting, but also that I am getting different appearances on different sides.


    This should really be a default script in Unity.
     
  6. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    But the order of the VERTS is not the same as the order that you are trying to set the UVS
     
  7. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Here for example is an array of 6 verts

    Trying to set 'sensibly ordered' uvs like 0>1>2>3>4>5 actually makes no sense
     
  8. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,699
    Ok
    So is your image the correct layout?

    For instance, how do I know which verticie represents which corner?
     
  9. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    No, I have no idea what order they are in.
    How have you created the cubes? Judging by the image in the OP, these aren't cube primitves (side missing), which means that YOU have set the verts in some order.

    3d modelling programs often use some type of uv mapping gizmo, which applies some transformation to the vertex positions to create uv positions, you could try the same
     
  10. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,699
    Cool.Thanks.
    this really should be a default script available somewhere. Seems so important and common.
     
  11. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Except not really.
    You handle asset creation in applications that have tools for the job
     
  12. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,699
    Yah. Im just venting. I am sure it is easy once you grasp it.
    Thanks