BitHelper.WriteMesh C# (CSharp) Method

WriteMesh() public static method

public static WriteMesh ( Mesh, mesh, System s ) : void
mesh Mesh,
s System
return void
    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 boneweights
            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 Mesh ReadMesh(byte[] data, int _seek)

Usage Example

コード例 #1
0
ファイル: nodeParser.cs プロジェクト: liro0206/EgretUnity
        public string SaveMesh(Mesh mesh)
        {
            int    id   = mesh.GetInstanceID();
            string name = null;

            if (savecache.TryGetValue(id, out name))
            {
                return(name);
            }
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            {
                BitHelper.WriteMesh(mesh, ms);
                byte[] bs   = ms.ToArray();
                string sha1 = ResLibTool.ComputeHashString(bs);

                name       = sha1 + ".mesh.bin";
                bufs[name] = bs;
            }
            savecache[id] = name;
            return(name);
        }