Search code examples
unity-game-enginetexturestexture2d

Struggling to apply offset image texture to all sides of a cube primitive in Unity


I have a texture in Unity containing four different stitched together images (see below).

enter image description here

My goal is to take the green "BOTTOM-LEFT" portion of the image and apply it as a texture to all the faces of a cube primitive in Unity using mesh UVs. The problem is I don't have a full grasp of how the vertices are arranged so I am struggling to make the texture match up properly.

If you look at the code below, the variables bottomLeft, bottomRight, topLeft and topRight correspond to the the green "BOTTOM-LEFT" portion of my image texture. You can test this easily by just dragging the provided image onto a cube in Unity and adding this script to Start().

So far I have managed to figure out the 'front', 'top' and 'back' sides of the cube...but the 'bottom' and 'left' sides are rotated 90 degrees for some reason...and 'right' is completely wrong. I can't figure out which UVs correspond to what so I am just guessing at trying to rearrange the UVs[xx].

void Start()
{    
    Mesh mesh = GetComponent<MeshFilter>().mesh;         
    Vector2[] UVs = new Vector2[mesh.vertices.Length];

    Vector2 bottomLeft = new Vector2(0.0f, 0.0f);
    Vector2 bottomRight = new Vector2(0.5f, 0.0f);
    Vector2 topLeft = new Vector2(0.0f, 0.5f);
    Vector2 topRight = new Vector2(0.5f, 0.5f);

    /* Working */

    // front
    UVs[0] = bottomLeft;        
    UVs[1] = bottomRight;       
    UVs[2] = topLeft;           
    UVs[3] = topRight;          

    // top
    UVs[8] = bottomLeft;        
    UVs[9] = bottomRight;       
    UVs[4] = topLeft;           
    UVs[5] = topRight;          

    // back
    UVs[10] = bottomLeft;        
    UVs[11] = bottomRight;      
    UVs[6] = topLeft;           
    UVs[7] = topRight;          


    /* Kinda Working */

    // bottom
    UVs[13] = bottomLeft;        
    UVs[12] = bottomRight;       
    UVs[14] = topLeft;           
    UVs[15] = topRight;          

    // left
    UVs[18] = bottomLeft;        
    UVs[16] = bottomRight;      
    UVs[17] = topLeft;           
    UVs[19] = topRight;          

    /* Not Working */

    // right
    UVs[21] = bottomLeft;        
    UVs[20] = bottomRight;       
    UVs[22] = topLeft;           
    UVs[23] = topRight;          

    mesh.uv = UVs;
}

Any guidance is welcome as I don't know where to begin trying to match where each point is on each triangle that makes up the cube.


Solution

  • Got it.

        // front side of cube
        UVs[0] = bottomLeft;        
        UVs[1] = bottomRight;       
        UVs[2] = topLeft;           
        UVs[3] = topRight;          
    
        // top side of cube
        UVs[4] = topLeft;           
        UVs[5] = topRight;          
        UVs[8] = bottomLeft;       // note here it's UVs 8 and 9 for 'top'
        UVs[9] = bottomRight;         
    
        // back side of cube
        UVs[6] = bottomRight;      // and UVs 6 and 7 are actually part of 'back'
        UVs[7] = bottomLeft;         
        UVs[10] = topRight;          
        UVs[11] = topLeft;              
    
        // bottom side of cube
        UVs[12] = bottomLeft;        
        UVs[13] = topLeft;           
        UVs[14] = topRight;          
        UVs[15] = bottomRight;       
    
        // left side of cube
        UVs[16] = bottomLeft;        
        UVs[17] = topLeft;           
        UVs[18] = topRight;          
        UVs[19] = bottomRight;       
        
        // right side of cube
        UVs[20] = bottomLeft;        
        UVs[21] = topLeft;           
        UVs[22] = topRight;          
        UVs[23] = bottomRight;