gpt4 book ai didi

c# - 为什么所有立方体的纹理 UV 贴图都发生了变化?

转载 作者:行者123 更新时间:2023-11-30 01:27:42 28 4
gpt4 key购买 nike

我正在尝试在 Unity 中更改和调整放置在 bilt-in 立方体面上的纹理。没有任何问题,我设法用这段代码在每张脸上放置不同的图像:

void Start()
{
Mesh mesh = GetComponent<MeshFilter>().mesh;
Vector2[] UVs = new Vector2[mesh.vertices.Length];
// Front
UVs[0] = new Vector2(0.0f, 0.0f);
UVs[1] = new Vector2(0.333f, 0.0f);
UVs[2] = new Vector2(0.0f, 0.333f);
UVs[3] = new Vector2(0.333f, 0.333f);
// Top
UVs[4] = new Vector2(0.334f, 0.333f);
UVs[5] = new Vector2(0.666f, 0.333f);
UVs[8] = new Vector2(0.334f, 0.0f);
UVs[9] = new Vector2(0.666f, 0.0f);
// Back
UVs[6] = new Vector2(1.0f, 0.0f);
UVs[7] = new Vector2(0.667f, 0.0f);
UVs[10] = new Vector2(1.0f, 0.333f);
UVs[11] = new Vector2(0.667f, 0.333f);
// Bottom
UVs[12] = new Vector2(0.0f, 0.334f);
UVs[13] = new Vector2(0.0f, 0.666f);
UVs[14] = new Vector2(0.333f, 0.666f);
UVs[15] = new Vector2(0.333f, 0.334f);
// Left
UVs[16] = new Vector2(0.334f, 0.334f);
UVs[17] = new Vector2(0.334f, 0.666f);
UVs[18] = new Vector2(0.666f, 0.666f);
UVs[19] = new Vector2(0.666f, 0.334f);
// Right
UVs[20] = new Vector2(0.667f, 0.334f);
UVs[21] = new Vector2(0.667f, 0.666f);
UVs[22] = new Vector2(1.0f, 0.666f);
UVs[23] = new Vector2(1.0f, 0.334f);
mesh.uv = UVs;
GetComponent<MeshFilter>().mesh = mesh
}

这可行,但它仅使用附加到立方体/着色器的纹理(纹理图集??)。我想知道是否可以使用两个不同的纹理集,例如,我有 4 个面具有纹理 1 的部分,2 个面具有纹理 2 的部分。或者也许可以使用 6 个不同的代码通过代码生成纹理图集纹理?可以使用 PackTexture() 方法吗?

最佳答案

要使用更多纹理,您需要一个单独的子网格。使用单独的 subMesh Unity 允许附加其他 Material 和其他纹理。

看看

http://docs.unity3d.com/ScriptReference/Mesh.SetTriangles.html

例如,使用此代码创建脚本并将其附加到带有立方体的游戏对象。

void Awake () 
{
Mesh currentMesh = this.GetComponent<MeshFilter>().mesh;

int[] submesh0 = new int[]{0,2,3,0,3,1,8,4,5,8,5,9,10,6,7,10,7,11,12,13,14,12,14,15};
int[] submesh1 = new int[]{16,17,18,16,18,19, 20,21,22,20,22,23};

currentMesh.subMeshCount=2;

currentMesh.SetTriangles(submesh0,0);
currentMesh.SetTriangles(submesh1,1);
}

唤醒后,将两种 Material 附加到立方体上,它用于 4 个面一个 Material ,其他 2 个面使用另一个。

请注意,这不是最好的方法。使用两种 Material 会破坏统一优化,需要两个单独的绘图调用(成本更高)。

关于c# - 为什么所有立方体的纹理 UV 贴图都发生了变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36215806/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com