title: Unity: Deserializing a mesh creates incomplete mesh?
body: <p>I am trying to deserialize a mesh, and essentially the result is a jumbled mess of triangles with a lot of data missing. The process is straight forward, I get the byte array, and read in the positions, normals, colors, and set the indices. The exact same byte array in a c++ program renders perfectly but when it gets to unity, it doesn't. I have verified the values of the first 500 elements of the all the buffers to a dead match so I don't think it is the deserializer but rather some detail from Unity that I am missing.</p>
  <p>The buffer sizes are:</p>
  <pre><code>+-------+----------------------+----------------------+-----------------------+-------------+
  | MapID | LENGTH(VertexBuffer) | LENGTH(ColorsBuffer) | LENGTH(NormalsBuffer) | VertexCount |
  +-------+----------------------+----------------------+-----------------------+-------------+
  |     2 |              3252744 |               813186 |               3252744 |      271062 |
  +-------+----------------------+----------------------+-----------------------+-------------+
  </code></pre>
  <p>The mesh deserializer code is:</p>
  <pre><code>      byte[] results = www.downloadHandler.data;
          if( results.Length &gt; 4 )
          {
              Debug.Log(&quot;MeshData length = &quot; + 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(&quot;Number of Vertices: &quot; + numVertices );
  
              if( numVertices &gt; 0 )
              {
                  //Create new MeshObject
                  GameObject newMesh = new GameObject(&quot;Mesh&quot;);
                  newMesh.AddComponent&lt;MeshFilter&gt;();
                  newMesh.AddComponent&lt;MeshRenderer&gt;();
  
                  //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&lt;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&lt;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&lt;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&lt;MeshRenderer&gt;().material = VertexColoringMaterial;
                  newMesh.GetComponent&lt;MeshFilter&gt;().mesh = downloadedMesh;
  
                  //Attach to scene graph
                  newMesh.transform.parent = GameObject.Find(&quot;3DRoot&quot;).transform;
              }
          }
  </code></pre>
  <p>Output:</p>
  <blockquote>
  <p>MeshData length = 7318678 Number of Vertices: 271062</p>
  <p>PositionBufferSize = 271062 * 3 [x,y,z] * sizeof(float) == 3252744</p>
  <p>ColorBufferSize = 271062 * 3 [r,g,b] * sizeof(ubyte) == 813186</p>
  <p>NormalBufferSize = 271062 * 3 [x,y,z] * sizeof(float) == 3252744</p>
  <p>Sum : 7318674 + 4 bytes for the vertex Count
  = 7318678</p>
  </blockquote>
  <p>My C++ Render:
  <a href="https://i.stack.imgur.com/xRR4P.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xRR4P.jpg" alt="enter image description here" /></a></p>
  <p>Unity's Render:
  <a href="https://i.stack.imgur.com/iHDEt.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iHDEt.jpg" alt="enter image description here" /></a></p>
  <p>As a related side note, in OpenGL I can pass the colors, normals, and vertices without the need for indexing through vertex attributes. Is there an equivalent in Unity so that I don't unnecessarily need to blow up ram by creating an unneeded int[numVertices] indexing array ?</p>