英文:
Unity: Repeating a texture over a plane depending on it's scale
问题
我已经尝试使用以下脚本来操作立方体。我尝试将脚本改为适用于平面,但似乎很困难。
最明显的变化是在CheckForDefaultSize()
函数内部使用PrimitiveType.Plane
而不是PrimitiveType.Cube
。尝试这样做时,纹理在平面上完全破裂。我怀疑问题出现在设置UV时,但我不知道该如何解决。
立方体的UV:
// 前面
meshUVs[0] = new Vector2(0, 0);
meshUVs[1] = new Vector2(width, 0);
meshUVs[2] = new Vector2(0, height);
meshUVs[3] = new Vector2(width, height);
// 后面
meshUVs[4] = new Vector2(0, 0);
meshUVs[5] = new Vector2(width, 0);
meshUVs[6] = new Vector2(0, height);
meshUVs[7] = new Vector2(width, height);
// 左侧
meshUVs[8] = new Vector2(0, 0);
meshUVs[9] = new Vector2(width, 0);
meshUVs[10] = new Vector2(0, height);
meshUVs[11] = new Vector2(width, height);
// 右侧
meshUVs[12] = new Vector2(0, 0);
meshUVs[13] = new Vector2(width, 0);
meshUVs[14] = new Vector2(0, height);
meshUVs[15] = new Vector2(width, height);
这是原始脚本在立方体上的效果:
英文:
I've managed to do this with a cube by using the following script. I tried adapting the script to a plane, although it seems difficult.
The most obvious change is to use PrimitiveType.Plane
instead of PrimitiveType.Cube
inside of the CheckForDefaultSize()
function. When trying this out, the texture is just broken all over the plane. I'm suspecting the issue comes up when setting the UVs, although I have no idea of how to fix this.
The cube's UVs:
//Front
meshUVs[0] = new Vector2(0, 0);
meshUVs[1] = new Vector2(width, 0);
meshUVs[2] = new Vector2(0, height);
meshUVs[3] = new Vector2(width, height);
//Back
meshUVs[4] = new Vector2(0, 0);
meshUVs[5] = new Vector2(width, 0);
meshUVs[6] = new Vector2(0, height);
meshUVs[7] = new Vector2(width, height);
//Left
meshUVs[8] = new Vector2(0, 0);
meshUVs[9] = new Vector2(width, 0);
meshUVs[10] = new Vector2(0, height);
meshUVs[11] = new Vector2(width, height);
//Right
meshUVs[12] = new Vector2(0, 0);
meshUVs[13] = new Vector2(width, 0);
meshUVs[14] = new Vector2(0, height);
meshUVs[15] = new Vector2(width, height);
Here's how the original script looks on a cube:
答案1
得分: 0
正确的做法是使用四边形(Quad)。一个平面被分成了200个三角形,而四边形只有2个。 这就是为什么我在设置UV时遇到问题的原因。
要使其正常工作,您只需更改SetupUvMap
和CheckForDefaultSize
:
private Vector2[] SetupUvMap(Vector2[] meshUVs)
{
var width = _currentScale.x;
var height = _currentScale.y;
meshUVs[0] = new Vector2(0, 0);
meshUVs[1] = new Vector2(width, 0);
meshUVs[2] = new Vector2(0, height);
meshUVs[3] = new Vector2(width, height);
return meshUVs;
}
private bool CheckForDefaultSize()
{
if (_currentScale != Vector3.one) return false;
var quad = GameObject.CreatePrimitive(PrimitiveType.Quad);
DestroyImmediate(GetComponent<MeshFilter>());
gameObject.AddComponent<MeshFilter>();
GetComponent<MeshFilter>().sharedMesh = quad.GetComponent<MeshFilter>().sharedMesh;
DestroyImmediate(quad);
return true;
}
虽然在我的情况下,我完全停止使用CheckForDefaultSize
函数。
英文:
The correct way to do this is to use a Quad instead. A plane is divided into 200 triangles, while quads only have 2. That's why I was having issues setting the UVs.
To make it work, you just have to change SetupUvMap
and CheckForDefaultSize
:
private Vector2[] SetupUvMap(Vector2[] meshUVs)
{
var width = _currentScale.x;
var height = _currentScale.y;
meshUVs[0] = new Vector2(0, 0);
meshUVs[1] = new Vector2(width, 0);
meshUVs[2] = new Vector2(0, height);
meshUVs[3] = new Vector2(width, height);
return meshUVs;
}
private bool CheckForDefaultSize()
{
if (_currentScale != Vector3.one) return false;
var quad = GameObject.CreatePrimitive(PrimitiveType.Quad);
DestroyImmediate(GetComponent<MeshFilter>());
gameObject.AddComponent<MeshFilter>();
GetComponent<MeshFilter>().sharedMesh = quad.GetComponent<MeshFilter>().sharedMesh;
DestroyImmediate(quad);
return true;
}
Although I did stop using the CheckForDefaultSize
function altogether in my case.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论