Unity:根据平面的缩放重复贴图

huangapple go评论102阅读模式
英文:

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

这是原始脚本在立方体上的效果:

1x1x1 比例
Unity:根据平面的缩放重复贴图

3x1x1 左右拉伸
Unity:根据平面的缩放重复贴图

英文:

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:

1x1x1 scale
Unity:根据平面的缩放重复贴图

3x1x1~ish scale
Unity:根据平面的缩放重复贴图

答案1

得分: 0

正确的做法是使用四边形(Quad)。一个平面被分成了200个三角形,而四边形只有2个。 这就是为什么我在设置UV时遇到问题的原因。

要使其正常工作,您只需更改SetupUvMapCheckForDefaultSize

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&lt;MeshFilter&gt;());
    gameObject.AddComponent&lt;MeshFilter&gt;();
    GetComponent&lt;MeshFilter&gt;().sharedMesh = quad.GetComponent&lt;MeshFilter&gt;().sharedMesh;

    DestroyImmediate(quad);

    return true;
}

Although I did stop using the CheckForDefaultSize function altogether in my case.

huangapple
  • 本文由 发表于 2023年5月28日 07:36:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76349431.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定