努力将偏移图像纹理应用于 Unity 中立方体基元的所有面

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

我在 Unity 中有一个纹理包含四个不同的拼接在一起的图像(见下文)。

我的目标是获取图像的绿色“底部-左”部分,并使用网格 UV 将其作为纹理应用于 Unity 中立方体基元的所有面。问题是我没有完全掌握顶点的排列方式,所以我很难使纹理正确匹配。

如果您查看下面的代码,变量 bottomLeft、bottomRight、topLeft 和 topRight 对应于我图像纹理的绿色“BOTTOM-LEFT”部分。您只需将提供的图像拖到 Unity 中的立方体上并将此脚本添加到 Start() 即可轻松进行测试。

到目前为止,我已经设法找出立方体的 'front'、'top' 和 'back' 面...但是 'bottom' 和 'left' 边由于某种原因被旋转了 90 度......而 'right' 是完全错误的。我不知道哪些 UV 对应什么,所以我只是在猜测尝试重新排列 UV[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;
}

欢迎任何指导,因为我不知道从哪里开始尝试匹配构成立方体的每个三角形上的每个点的位置。

知道了。

    // 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;