使用网格UV匹配世界中的纹理以覆盖图像

时间:2018-06-01 11:03:40

标签: c# unity3d textures rendering

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

  1. 此图片叠加在屏幕上:(缩放以匹配分辨率)Rainbow image overlay
  2. 按空格键时,游戏中网格顶点的屏幕坐标将转换为UV并应用于具有相同纹理的网格。
  3. 当相机以垂直角度面向四边形时,效果如下所示:Perpendicular camera example

    以不同的方式解释,如果四边形的左上角坐标是水平方向通过屏幕的30%,而垂直方向通过屏幕是40%,我希望将顶点的紫外线(0.3,0.4)设为纹理将完美匹配叠加(如图所示)。

    现在,只有当相机可以从其他角度观看时效果才有效,所以这是在更新纹理后稍微旋转(绕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;
    } 
    

    我的问题:

    1. 为什么纹理在三角形边界周围扭曲?
    2. 当相机不是直角时,为什么效果会有所不同?
    3. 我该如何解决这个问题? /我可以通过什么来更好地了解正在发生的事情?
    4. 如果需要更多信息,我可能会提供。

0 个答案:

没有答案