public void Save(System.IO.Stream stream, PoseBoneMatrix last) { byte[] btag = new byte[2]; btag[0] = (byte)tag; btag[1] = last == null ? (byte)(PoseBoneMatrix.changetag.All) : (byte)tag; stream.Write(btag, 0, 2); if (last == null || (tag & PoseBoneMatrix.changetag.Rotate) > 0) { byte[] buf = BitHelper.getBytes(r); stream.Write(buf, 0, 16); } if (last == null || (tag & PoseBoneMatrix.changetag.Trans) > 0) { byte[] buf = BitHelper.getBytes(t); stream.Write(buf, 0, 12); } if (last == null || (tag & PoseBoneMatrix.changetag.Scale) > 0) { byte[] buf = BitHelper.getBytes(s); stream.Write(buf, 0, 12); } }
public static void WriteMesh(Mesh mesh, System.IO.Stream s) { byte[] nambuf = BitHelper.getBytes(mesh.name); s.Write(nambuf, 0, nambuf.Length); byte[] buf = BitHelper.getBytes(mesh.bounds); s.Write(buf, 0, 24); UInt32 vc = (UInt32)mesh.vertexCount; buf = BitConverter.GetBytes(vc); s.Write(buf, 0, 4); if (mesh.vertices != null && mesh.vertices.Length != 0) { s.WriteByte(1);//1 vb pos tag for (int i = 0; i < vc; i++) { s.Write(BitHelper.getBytes(mesh.vertices[i]), 0, 12); //if (i == 0) //{ // Debug.Log("pos0:" + mesh.vertices[i]); //} } } if (mesh.colors32 != null && mesh.colors32.Length != 0) { s.WriteByte(2);//2 vb color tag; for (int i = 0; i < vc; i++) { s.Write(BitHelper.getBytes(mesh.colors32[i]), 0, 4); //Debug.Log("color0:" + mesh.colors32[i]); //if (i == 0) //{ // Debug.Log("pos0:" + mesh.vertices[i]); //} } } if (mesh.normals != null && mesh.normals.Length != 0) { s.WriteByte(3);//3 vb normal tag; for (int i = 0; i < vc; i++) { s.Write(BitHelper.getBytes(mesh.normals[i]), 0, 12); //if (i == 0) //{ // Debug.Log("normal0:" + mesh.normals[i]); //} } } if (mesh.uv != null && mesh.uv.Length != 0) { s.WriteByte(4);//4 vb uv tag; for (int i = 0; i < vc; i++) { s.Write(BitHelper.getBytes(mesh.uv[i]), 0, 8); //if (i == 0) //{ // Debug.Log("uv0:" + mesh.uv[i]); //} } } if (mesh.uv2 != null && mesh.uv2.Length != 0) { s.WriteByte(5);//5 vb uv2 tag; for (int i = 0; i < vc; i++) { s.Write(BitHelper.getBytes(mesh.uv2[i]), 0, 8); } } if (mesh.uv3 != null && mesh.uv3.Length != 0) { s.WriteByte(6);//6 vb uv3 tag; for (int i = 0; i < vc; i++) { s.Write(BitHelper.getBytes(mesh.uv3[i]), 0, 8); } } if (mesh.tangents != null && mesh.tangents.Length != 0) { s.WriteByte(7);//7 tangents tag; for (int i = 0; i < vc; i++) { s.Write(BitHelper.getBytes(mesh.tangents[i]), 0, 16); } } if (mesh.uv4 != null && mesh.uv4.Length != 0) { s.WriteByte(8);//8 vb uv4 tag; for (int i = 0; i < vc; i++) { s.Write(BitHelper.getBytes(mesh.uv4[i]), 0, 8); } } if (mesh.bindposes != null && mesh.bindposes.Length != 0) { s.WriteByte(16); //16 bindposes s.WriteByte((byte)mesh.bindposes.Length); //length diff for (int i = 0; i < mesh.bindposes.Length; i++) { Vector3 pos; Vector3 scale; Quaternion quat; BitHelper.MatrixDeCompose(mesh.bindposes[i], out pos, out scale, out quat); s.Write(BitHelper.getBytes(pos), 0, 12); s.Write(BitHelper.getBytes(scale), 0, 12); s.Write(BitHelper.getBytes(quat), 0, 16); //Debug.Log(mesh.bindposes[i] + "\n pos:" + pos + "\n scale:" + scale + "\n quat:" + quat + "\n euler:" + quat.ToEuler()); } //mesh.bindposes = mesh.bindposes; //mesh.UploadMeshData(false); } if (mesh.boneWeights != null && mesh.boneWeights.Length != 0) { s.WriteByte(17);//17 bindposes for (int i = 0; i < vc; i++) { s.Write(BitConverter.GetBytes(mesh.boneWeights[i].boneIndex0), 0, 4); s.Write(BitConverter.GetBytes(mesh.boneWeights[i].boneIndex1), 0, 4); s.Write(BitConverter.GetBytes(mesh.boneWeights[i].boneIndex2), 0, 4); s.Write(BitConverter.GetBytes(mesh.boneWeights[i].boneIndex3), 0, 4); s.Write(BitConverter.GetBytes(mesh.boneWeights[i].weight0), 0, 4); s.Write(BitConverter.GetBytes(mesh.boneWeights[i].weight1), 0, 4); s.Write(BitConverter.GetBytes(mesh.boneWeights[i].weight2), 0, 4); s.Write(BitConverter.GetBytes(mesh.boneWeights[i].weight3), 0, 4); } } s.WriteByte(255);//vb end int sub = mesh.subMeshCount; s.WriteByte((byte)sub); { Debug.Log("sub:" + sub); } for (int i = 0; i < sub; i++) { int tv = (int)mesh.GetTopology(i); //绘制方式 s.Write(BitConverter.GetBytes(tv), 0, 4); var indices = mesh.GetIndices(i); //索引 UInt32 length = (UInt32)indices.Length; Debug.Log("indlength:" + sub); s.Write(BitConverter.GetBytes(length), 0, 4); for (int j = 0; j < length; j++) { s.Write(BitConverter.GetBytes(indices[j]), 0, 4); } } }
public static string SaveMesh(Mesh mesh, string path) { if (savedMeshes.ContainsKey(mesh.name)) { if (savedMeshes[mesh.name].GetInstanceID() != mesh.GetInstanceID()) { Debug.LogError("有重名mesh:" + mesh.name); } return(mesh.name); } if (System.IO.Directory.Exists(path) == false) { System.IO.Directory.CreateDirectory(path); } using (System.IO.Stream s = System.IO.File.Open(System.IO.Path.Combine(path, mesh.name + ".mesh.bytes"), System.IO.FileMode.Create)) { byte[] buf = BitHelper.getBytes(mesh.bounds); s.Write(buf, 0, 24); UInt32 vc = (UInt32)mesh.vertexCount; buf = BitConverter.GetBytes(vc); s.Write(buf, 0, 4); if (mesh.vertices != null && mesh.vertices.Length != 0) { s.WriteByte(1);//1 vb pos tag for (int i = 0; i < vc; i++) { s.Write(BitHelper.getBytes(mesh.vertices[i]), 0, 12); if (i == 0) { Debug.Log("pos0:" + mesh.vertices[i]); } } } if (mesh.colors32 != null && mesh.colors32.Length != 0) { s.WriteByte(2);//2 vb color tag; for (int i = 0; i < vc; i++) { s.Write(BitHelper.getBytes(mesh.colors32[i]), 0, 4); Debug.Log("color0:" + mesh.colors32[i]); if (i == 0) { Debug.Log("pos0:" + mesh.vertices[i]); } } } if (mesh.normals != null && mesh.normals.Length != 0) { s.WriteByte(3);//3 vb normal tag; for (int i = 0; i < vc; i++) { s.Write(BitHelper.getBytes(mesh.normals[i]), 0, 12); if (i == 0) { Debug.Log("normal0:" + mesh.normals[i]); } } } if (mesh.uv != null && mesh.uv.Length != 0) { s.WriteByte(4);//4 vb uv tag; for (int i = 0; i < vc; i++) { s.Write(BitHelper.getBytes(mesh.uv[i]), 0, 8); if (i == 0) { Debug.Log("uv0:" + mesh.uv[i]); } } } if (mesh.uv1 != null && mesh.uv1.Length != 0) { s.WriteByte(5);//5 vb uv1 tag; for (int i = 0; i < vc; i++) { s.Write(BitHelper.getBytes(mesh.uv1[i]), 0, 8); } } if (mesh.uv2 != null && mesh.uv2.Length != 0) { s.WriteByte(6);//6 vb uv2 tag; for (int i = 0; i < vc; i++) { s.Write(BitHelper.getBytes(mesh.uv[i]), 0, 8); } } s.WriteByte(255);//vb end int sub = mesh.subMeshCount; s.WriteByte((byte)sub); { Debug.Log("sub:" + sub); } for (int i = 0; i < sub; i++) { int tv = (int)mesh.GetTopology(i); //绘制方式 s.Write(BitConverter.GetBytes(tv), 0, 4); var indices = mesh.GetIndices(i); //索引 UInt16 length = (UInt16)indices.Length; s.Write(BitConverter.GetBytes(length), 0, 2); for (int j = 0; j < length; j++) { s.Write(BitConverter.GetBytes(indices[j]), 0, 4); } } } savedMeshes[mesh.name] = mesh; return(mesh.name); }