Unity:反序列化网格会创建不完整的网格物体?

时间:2018-05-18 15:45:44

标签: c# unity3d mesh

我正在尝试对网格进行反序列化,实际上结果是混乱的三角形,丢失了大量数据。这个过程很简单,我得到字节数组,读取位置,法线,颜色,并设置索引。 c ++程序中完全相同的字节数组可以完美呈现,但是当它达到统一时,它就不会。我已经验证了所有缓冲区的前500个元素的值到死区匹配,所以我不认为它是解串器,而是Unity中缺少的一些细节。

缓冲区大小为:

+-------+----------------------+----------------------+-----------------------+-------------+
| MapID | LENGTH(VertexBuffer) | LENGTH(ColorsBuffer) | LENGTH(NormalsBuffer) | VertexCount |
+-------+----------------------+----------------------+-----------------------+-------------+
|     2 |              3252744 |               813186 |               3252744 |      271062 |
+-------+----------------------+----------------------+-----------------------+-------------+

网格反序列化器代码是:

      byte[] results = www.downloadHandler.data;
        if( results.Length > 4 )
        {
            Debug.Log("MeshData length = " + results.Length );

            //First 4 bytes are a packed 32 bit uint of vertex count
            BinaryReader meshBuffer = new BinaryReader(new MemoryStream(results));
            int numVertices = meshBuffer.ReadInt32();
            Debug.Log("Number of Vertices: " + numVertices );

            if( numVertices > 0 )
            {
                //Create new MeshObject
                GameObject newMesh = new GameObject("Mesh");
                newMesh.AddComponent<MeshFilter>();
                newMesh.AddComponent<MeshRenderer>();

                //Create mesh
                Mesh downloadedMesh = new Mesh();
                Vector3 [] positions = new Vector3[numVertices];
                Color [] colors = new Color[numVertices];
                Vector3 [] normals = new Vector3[numVertices];
                int [] indices = new int[numVertices];

                //Vertex Positions
                for( int i = 0; i<numVertices; i++ )
                {
                    positions[i].x = meshBuffer.ReadSingle();
                    positions[i].y = meshBuffer.ReadSingle();
                    positions[i].z = meshBuffer.ReadSingle();
                    indices[i] = i;
                }

                //Vertex Colors
                for( int i = 0; i<numVertices; i++ )
                {
                    colors[i].r =  (float)(meshBuffer.ReadByte()/255f);
                    colors[i].g =  (float)(meshBuffer.ReadByte()/255f);
                    colors[i].b =  (float)(meshBuffer.ReadByte()/255f);
                    colors[i].a = 1;
                }

                //Vertex Normals
                for( int i = 0; i<numVertices; i++ )
                {
                    normals[i].x = meshBuffer.ReadSingle();
                    normals[i].y = meshBuffer.ReadSingle();
                    normals[i].z = meshBuffer.ReadSingle();
                }

                downloadedMesh.vertices = positions;
                downloadedMesh.normals = normals;
                downloadedMesh.colors = colors;
                downloadedMesh.triangles = indices;

                newMesh.GetComponent<MeshRenderer>().material = VertexColoringMaterial;
                newMesh.GetComponent<MeshFilter>().mesh = downloadedMesh;

                //Attach to scene graph
                newMesh.transform.parent = GameObject.Find("3DRoot").transform;
            }
        }

输出:

  

网格数据长度= 7318678顶点数:271062

     

PositionBufferSize = 271062 * 3 [x,y,z] * sizeof(float)== 3252744

     

ColorBufferSize = 271062 * 3 [r,g,b] * sizeof(ubyte)== 813186

     

NormalBufferSize = 271062 * 3 [x,y,z] * sizeof(float)== 3252744

     

总和:7318674 + 4个字节用于顶点计数    = 7318678

我的C ++渲染: enter image description here

Unity的渲染: enter image description here

作为一个相关的注释,在OpenGL中,我可以传递颜色,法线和顶点,而无需通过顶点属性进行索引。在Unity中是否有一个等价物,以便我不需要通过创建一个不需要的int [numVertices]索引数组来炸毁ram?

0 个答案:

没有答案