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

问题描述

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

enter image description here

我的目标是将图像的绿色“BottOM-LEFT”部分作为纹理应用到 Unity 中使用网格 UV 的立方体基元的所有面。问题是我没有完全掌握顶点的排列方式,所以我正在努力使纹理正确匹配。

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

到目前为止,我已经设法弄清楚立方体的“正面”、“顶部”和“背面”……但是“底部”和“左侧”由于某种原因旋转了 90 度……而“对”是完全错误的。我无法弄清楚哪些 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;