/// <summary> /// Serialize a custom object its mesh, so we can save it /// </summary> /// <param name="customMeshIndex">The custom object index in our serialized scene</param> /// <param name="sceneId">The unique ID of our scene</param> /// <param name="meshToken">The unique token we received from the server for our custom mesh object</param> /// <returns></returns> public SerializableMesh SerializeCustomObject(int customMeshIndex, string sceneId, string meshToken) { var targetMesh = customMeshObjects[customMeshIndex].GetComponent <MeshFilter>().mesh; var newSerializableMesh = new SerializableMesh { sceneId = sceneId, meshToken = meshToken, version = Application.version, meshBitType = (targetMesh.indexFormat == IndexFormat.UInt32) ? 1 : 0, verts = MeshSerializer.FlattenVector3Array(targetMesh.vertices), //uvs = MeshSerializer.FlattenVector2Array(targetMesh.uv), //No texture support yet. So we dont need these yet. normals = MeshSerializer.FlattenVector3Array(targetMesh.normals), subMeshes = SerializeSubMeshes(targetMesh) }; return(newSerializableMesh); }
/// <summary> /// Parse a mesh object we downloaded /// </summary> /// <param name="serializableMesh">The data object we use to construct our mesh</param> /// <returns></returns> private Mesh ParseSerializableMesh(SerializableMesh serializableMesh) { Mesh parsedMesh = new Mesh(); parsedMesh.indexFormat = (serializableMesh.meshBitType == 0) ? IndexFormat.UInt16 : IndexFormat.UInt32; var subMeshCount = serializableMesh.subMeshes.Length; parsedMesh.subMeshCount = subMeshCount; parsedMesh.SetVertices(MeshSerializer.SeperateVector3Array(serializableMesh.verts)); parsedMesh.SetUVs(0, MeshSerializer.SeperateVector2Array(serializableMesh.uvs)); parsedMesh.SetNormals(MeshSerializer.SeperateVector3Array(serializableMesh.normals)); for (int i = 0; i < subMeshCount; i++) { var subMesh = serializableMesh.subMeshes[i]; parsedMesh.SetTriangles(subMesh.triangles, i); } return(parsedMesh); }