Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用网格UV在世界纹理中匹配覆盖图像_C#_Unity3d_Textures_Rendering - Fatal编程技术网

C# 使用网格UV在世界纹理中匹配覆盖图像

C# 使用网格UV在世界纹理中匹配覆盖图像,c#,unity3d,textures,rendering,C#,Unity3d,Textures,Rendering,在过去的几天里,我一直在为我在一个名为。要测试我创建此演示的效果,请执行以下操作: 此图像叠加在屏幕上:(缩放以匹配分辨率) 按下空格键,游戏中网格顶点的屏幕坐标将转换为UV,并应用于具有相同纹理的网格 当摄影机以垂直角度面向四边形时,效果如下: 以另一种方式解释,如果四边形的左上角坐标水平穿过屏幕30%,垂直穿过屏幕40%,我希望使顶点的UV(0.3,0.4)纹理与覆盖完美匹配(如图所示) 现在,仅当摄影机也可以从其他角度观看时,效果才起作用,因此,以下是更新纹理后摄影机轻微旋转(围绕Y轴)的

在过去的几天里,我一直在为我在一个名为。要测试我创建此演示的效果,请执行以下操作:

  • 此图像叠加在屏幕上:(缩放以匹配分辨率)
  • 按下空格键,游戏中网格顶点的屏幕坐标将转换为UV,并应用于具有相同纹理的网格
  • 当摄影机以垂直角度面向四边形时,效果如下:

    以另一种方式解释,如果四边形的左上角坐标水平穿过屏幕30%,垂直穿过屏幕40%,我希望使顶点的UV(0.3,0.4)纹理与覆盖完美匹配(如图所示)

    现在,仅当摄影机也可以从其他角度观看时,效果才起作用,因此,以下是更新纹理后摄影机轻微旋转(围绕Y轴)的图片:

    正如您所看到的,纹理几乎成一条直线,但有点倾斜,尤其是在网格的三角形边界周围,此处显示为白色:

    下面是我用来更新纹理的代码

    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;
    } 
    
    void UpdateTexture(){
    //测试是彩虹纹理2d
    //r是渲染器
    r、 material.main纹理=测试;
    //m是四边形网格
    向量3[]顶点=m个顶点;
    向量2[]uv=m.uv;
    //我们将遍历网格的每个顶点
    对于(int i=0;i
    我的问题是:

  • 为什么纹理围绕三角形边界扭曲
  • 为什么相机角度不正确时效果会有所不同
  • 我如何解决这个问题?/我可以研究什么来更好地了解正在发生的事情

  • 如果需要更多的信息,我很可能会提供。

    这是由于视角扭曲造成的,但我对主题了解不够,无法提供答案。我已经读了一些书。好像我在这里陷得有点深了。