【问题标题】:Strange mesh texture created by code in UnityUnity中代码创建的奇怪网格纹理
【发布时间】:2020-04-13 22:09:02
【问题描述】:

我正在学习如何在 Unity 中通过代码进行网格划分,但纹理与图像中的一样奇怪,有人知道这是为什么吗? 我在代码中创建了 UV 贴图,搜索了很多 Google 和 Unity 文档,发现我应该使用数组顶点来映射 UV,但我认为我做错了什么

我的代码:

public Vector3[] verts = {
    new Vector3(-0.5f, 0.5f, 0f), new Vector3(0.5f, 0.5f, 0f),
    new Vector3(0.5f, -0.5f, 0f), new Vector3(-0.5f, -0.5f, 0f),
    new Vector3(-0.2f, 0.3f, 0f), new Vector3(0.2f, 0.3f, 0f),
    new Vector3(0.2f, -0.1f, 0f), new Vector3(-0.2f, -0.1f, 0f),
    new Vector3(-0.2f, 0.3f, 0f), new Vector3(0.2f, 0.3f, 0f),
    new Vector3(0.2f, -0.1f, 0f), new Vector3(-0.2f, -0.1f, 0f),
    new Vector3(-0.2f, 0.3f, 0.1f), new Vector3(0.2f, 0.3f, 0.1f),
    new Vector3(0.2f, -0.1f, 0.1f), new Vector3(-0.2f, -0.1f, 0.1f),
    new Vector3(-0.2f, 0.3f, 0.1f), new Vector3(0.2f, 0.3f, 0.1f),
    new Vector3(0.2f, -0.1f, 0.1f), new Vector3(-0.2f, -0.1f, 0.1f),
    new Vector3(-0.5f, 0.5f, 0.1f), new Vector3(-0.5f, -0.5f, 0.1f),
    new Vector3(-0.5f, -0.5f, 0.1f), new Vector3(-0.5f, -0.5f, 0.1f),
    new Vector3(-0.5f, -0.5f, 0.1f), new Vector3(-0.5f, -0.5f, 0.1f),
    new Vector3(-0.5f, -0.5f, 0.1f), new Vector3(-0.5f, -0.5f, 0.1f),
    new Vector3(-0.5f, -0.5f, 0.1f), new Vector3(-0.5f, -0.5f, 0.1f),
    new Vector3(-0.5f, -0.5f, 0.1f), new Vector3(-0.5f, -0.5f, 0.1f),
    new Vector3(-0.5f, -0.5f, 0.1f), new Vector3(-0.5f, -0.5f, 0.1f),
    new Vector3(-0.5f, -0.5f, 0.1f), new Vector3(-0.5f, -0.5f, 0.1f),
    new Vector3(-0.5f, -0.5f, 0.1f), new Vector3(-0.5f, -0.5f, 0.1f),
    new Vector3(-0.5f, -0.5f, 0.1f), new Vector3(-0.5f, -0.5f, 0.1f),
    new Vector3(-0.5f, -0.5f, 0.1f), new Vector3(-0.5f, -0.5f, 0.1f),
    new Vector3(-0.5f, -0.5f, 0.1f), new Vector3(-0.5f, -0.5f, 0.1f),
    new Vector3(-0.5f, -0.5f, 0.1f), new Vector3(-0.5f, -0.5f, 0.1f),
    new Vector3(0.5f, 0.5f, 0.1f), new Vector3(0.5f, -0.5f, 0.1f)
 };
 public int[] tris = {
     0,1,4,1,5,4,1,2,5,2,6,5,2,3,6,3,7,6,3,0,7,0,4,7,8,9,12,9,13,12,9,10,13,10,14,13,10,11,14,11,15,14,11,8,15,8,12,15,20,0,3,20,3,21,0,20,46,0,46,1,1,46,47,1,47,2,3,2,42,2,47,45,46,20,17,20,16,17,20,19,16,20,32,15,31,14,15,31,47,14,47,46,14,46,17,14
 };

MeshFilter mF = gameObject.GetComponent<MeshFilter> ();
Mesh msh = new Mesh ();
msh.vertices = verts;
msh.triangles = tris;
Vector2[] uvs = new Vector2[verts.Length];
for (int i = 0; i < uvs.Length; i++){ //uv map
    uvs[i] = new Vector2(verts[i].x, verts[i].y);
 }
 msh.uv = uvs;
 msh.RecalculateNormals ();
 mF.mesh = msh;

【问题讨论】:

    标签: c# unity3d textures mesh uv-mapping


    【解决方案1】:

    我怀疑在您的情况下禁用了透视纹理校正,因为

    Unity 2019.3:♫ 一切都很棒♫

    【讨论】:

    • 我刚换了shader,它工作了,但是侧面变形了,你怎么不变形? imgur.com/JUTNbEd
    • 我使用了一个使用全局位置作为纹理的着色器,但它不适用于这个着色器,你能帮我解释一下为什么吗? codeshare.io/G7jdkk
    • 这个着色器可能禁用了与“透视纹理校正”相关的东西。因为标准(内置)的没有这样的问题
    • 在我的情况下,侧面也变形了,只是由于纹理不同而不太明显。那是因为你模拟了一种正面投影
    • 如果您不熟悉着色器并正在使用 Unity 2019.3,我建议您在 Shader Graph 中制作着色器
    【解决方案2】:

    由于您已经发现共享顶点导致了此问题,因此您可能需要测试此代码是否有效。可能在某个时候变得有用。它旨在在网格计算时分配 UV(不再需要特殊的着色器),但 要求在其独立法线之间具有角度的相邻三角形 > PI/4 在任何地方都没有共享顶点(换句话说:不同边之间没有共享顶点立方体)

    using UnityEngine;
    public class NewBehaviourScript : MonoBehaviour
    {
        [SerializeField] MeshFilter meshFilter = null;
        Vector3[] vertices = { vertices goes here };
        int[] triangles = { indices goes here };
        void Start ()
        {
            Mesh mesh = new Mesh();
            meshFilter.mesh = mesh;
    
            Vector2[] uvs = new Vector2[ vertices.Length ];
            int numTriangles = triangles.Length / 3;
            for( int t=0 ; t<numTriangles ; t++ )
            {
                int ia = triangles[ t*3 ];
                int ib = triangles[ t*3+1 ];
                int ic = triangles[ t*3+2 ];
    
                Vector3 va = vertices[ ia ];
                Vector3 vb = vertices[ ib ];
                Vector3 vc = vertices[ ic ];
    
                Vector3 normal = Vector3.Cross( vb-va , vc-va ).normalized;
                float dotForward = Vector3.Dot( normal , Vector3.forward );
                float dotRight = Vector3.Dot( normal , Vector3.right );
                float dotUp = Vector3.Dot( normal , Vector3.up );
    
                if( dotForward>0.7071f )
                {
                    // front projection
                    uvs[ ia ] = new Vector2{ x=-va.x , y=va.y };
                    uvs[ ib ] = new Vector2{ x=-vb.x , y=vb.y };
                    uvs[ ic ] = new Vector2{ x=-vc.x , y=vc.y };
                }
                else if( dotForward<-0.7071f  )
                {
                    // back projection
                    uvs[ ia ] = new Vector2{ x=va.x , y=va.y };
                    uvs[ ib ] = new Vector2{ x=vb.x , y=vb.y };
                    uvs[ ic ] = new Vector2{ x=vc.x , y=vc.y };
                }
                else if( dotRight>0.7071f )
                {
                    // right projection
                    uvs[ ia ] = new Vector2{ x=-va.z , y=va.y };
                    uvs[ ib ] = new Vector2{ x=-vb.z , y=vb.y };
                    uvs[ ic ] = new Vector2{ x=-vc.z , y=vc.y };
                }
                else if( dotRight<-0.7071f )
                {
                    // left projection
                    uvs[ ia ] = new Vector2{ x=va.z , y=va.y };
                    uvs[ ib ] = new Vector2{ x=vb.z , y=vb.y };
                    uvs[ ic ] = new Vector2{ x=vc.z , y=vc.y };
                }
                else if( dotUp>0.7071f )
                {
                    // top projection
                    uvs[ ia ] = new Vector2{ x=-va.x , y=va.z };
                    uvs[ ib ] = new Vector2{ x=-vb.x , y=vb.z };
                    uvs[ ic ] = new Vector2{ x=-vc.x , y=vc.z };
                }
                else
                {
                    // bottom projection
                    uvs[ ia ] = new Vector2{ x=va.x , y=va.z };
                    uvs[ ib ] = new Vector2{ x=vb.x , y=vb.z };
                    uvs[ ic ] = new Vector2{ x=vc.x , y=vc.z };
                }
            }
    
            mesh.vertices = vertices;
            mesh.triangles = triangles;
            mesh.uv = uvs;
    
            mesh.RecalculateNormals();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-02-05
      • 2018-01-07
      • 1970-01-01
      • 2014-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多