最近几天我一直在编写我在一款名为the Catacombs of Solaris的游戏中看到的效果 . 为了测试我创建此演示的效果:

  • 此图像叠加在屏幕上:(按比例缩放以匹配分辨率)Rainbow image overlay .

  • 按空格键时,游戏中网格顶点的屏幕坐标将转换为UV并应用于具有相同纹理的网格 .

当相机以垂直角度面向四边形时,效果如下所示:Perpendicular camera example .

要以不同的方式解释,如果四边形的左上角坐标是横向通过屏幕的30%,而垂直通过屏幕是40%,我希望为顶点(0.3,0.4)制作纹理匹配的UV完美叠加(如图所示) .

现在,该效果仅在相机可以从其他角度观看时才有效,因此这是在更新纹理后稍微旋转(绕Y轴)的相机照片:Not-perpendicular camera example .

正如您所看到的那样,纹理几乎排成一行,但有点倾斜,特别是在网格的三角形边框周围,这里显示为白色:Triangles of the mesh .

这是我用来更新纹理的代码

void UpdateTexture() {
    // test is the rainbow Texture2D
    // r is the Renderer
    r.material.mainTexture = test;

    // m is the Quad mesh
    Vector3[] vert = m.vertices;
    Vector2[] uv = m.uv;

    // We will go through each vertex of the mesh
    for(int i = 0; i < uv.Length; i++) {
        // Find where the world position of that vertex is
        Vector3 worldPosition = transform.TransformPoint(vert[i]);
        // Convert that into a screen position based on our camera
        Vector2 screenPosition = CameraToTexture.cam.WorldToScreenPoint(worldPosition);
        // Put that within the range 0...1 as a UV
        Vector2 uvPosition = new Vector2(screenPosition.x / CameraToTexture.cam.pixelWidth, screenPosition.y / CameraToTexture.cam.pixelHeight);
        // Save that UV into our temp array
        uv[i] = uvPosition;
    }

    // Apply to the UV array of the mesh
    m.uv = uv;
}

我的问题:

  • 为什么纹理在三角形边界附近扭曲?

  • 当相机不是直角时,为什么效果会有所不同?

  • 我该如何解决这个问题? /我可以研究什么来更好地了解正在发生的事情?

如果需要更多信息,我可能会提供它 .