Example #1
0
    public void SaveTex()
    {
        int w = t2d.width;
        int h = t2d.height;
        int c = 3;

        byte[] bytes = new byte[12 + w * h * c];

        //记录图片信息
        var bw = ConvertToByte.ToByte(w);
        var bh = ConvertToByte.ToByte(h);
        var bc = ConvertToByte.ToByte(c);

        for (int i = 0; i < 4; i++)
        {
            bytes[i]     = bw[i];
            bytes[i + 4] = bh[i];
            bytes[i + 8] = bc[i];
        }


        int cid = 12;

        for (int y = h - 1; y >= 0; y--)
        {
            for (int x = 0; x < w; x++)
            {
                var col = t2d.GetPixel(x, y);

                bytes[cid]     = (byte)(int)(col.r * 255.99f);
                bytes[cid + 1] = (byte)(int)(col.g * 255.99f);
                bytes[cid + 2] = (byte)(int)(col.b * 255.99f);

                cid += 3;
            }
        }
        System.IO.File.WriteAllBytes("Assets/Save.dat", bytes);
    }
    void AssetExport(Texture2D t2d)
    {
        //检查是否已保存
        if (assetInfo.ContainsKey(t2d.GetInstanceID()))
        {
            return;
        }

        Debug.Log($"Save Texture {t2d.name} as {t2d.GetInstanceID()}");

        //处理保存路径
        string filename  = t2d.GetInstanceID().ToString() + ".dat";
        string _savePath = savePath + textureAssetPath;

        if (!Directory.Exists(_savePath))
        {
            Directory.CreateDirectory(_savePath);
        }

        _savePath += filename;
        string _infoPath = textureAssetPath + filename;

        int w = t2d.width;
        int h = t2d.height;
        int c = 3;

        byte[] bytes = new byte[12 + w * h * c];

        //记录图片信息
        var bw = ConvertToByte.ToByte(w);
        var bh = ConvertToByte.ToByte(h);
        var bc = ConvertToByte.ToByte(c);

        for (int i = 0; i < 4; i++)
        {
            bytes[i]     = bw[i];
            bytes[i + 4] = bh[i];
            bytes[i + 8] = bc[i];
        }


        int cid = 12;

        for (int y = h - 1; y >= 0; y--)
        {
            for (int x = 0; x < w; x++)
            {
                var col = t2d.GetPixel(x, y);

                bytes[cid]     = (byte)(int)(col.r * 255.99f);
                bytes[cid + 1] = (byte)(int)(col.g * 255.99f);
                bytes[cid + 2] = (byte)(int)(col.b * 255.99f);

                cid += 3;
            }
        }
        File.WriteAllBytes(_savePath, bytes);

        //更新资源信息
        assetInfo.Add(t2d.GetInstanceID(), _infoPath);
    }
    void AssetExport(Mesh mesh)
    {
        //检查是否已保存
        if (assetInfo.ContainsKey(mesh.GetInstanceID()))
        {
            return;
        }

        string filename  = mesh.GetInstanceID().ToString() + ".dat";
        string _savePath = savePath + meshAssetPath;

        if (!Directory.Exists(_savePath))
        {
            Directory.CreateDirectory(_savePath);
        }

        _savePath += filename;
        string _infoPath = meshAssetPath + filename;

        //写入
        using (FileStream fs = new FileStream(_savePath, FileMode.OpenOrCreate))
        {
            Debug.Log($"Write Mesh{mesh.name} as {filename}");
            //写入顶点总数
            var vCount = ConvertToByte.ToByte(mesh.vertexCount);
            Debug.Log(vCount.Length);
            fs.Write(vCount, 0, vCount.Length);

            bool hadColor = mesh.colors.Length == mesh.vertexCount;
            bool hadUV1   = mesh.uv2.Length == mesh.vertexCount;

            //写入顶点数据
            for (int i = 0; i < mesh.vertexCount; i++)
            {
                var ve  = ConvertToByte.ToByte(mesh.vertices[i]);
                var co  = hadColor ? ConvertToByte.ToByte(mesh.colors[i]) : ConvertToByte.ToByte(Color.white);
                var nr  = ConvertToByte.ToByte(mesh.normals[i]);
                var ta  = ConvertToByte.ToByte(mesh.tangents[i]);
                var uv0 = ConvertToByte.ToByte(mesh.uv[i]);
                var uv1 = hadUV1 ? ConvertToByte.ToByte(mesh.uv2[i]) : ConvertToByte.ToByte(Vector2.zero);
                fs.Write(ve, 0, ve.Length);
                fs.Write(co, 0, co.Length);
                fs.Write(nr, 0, nr.Length);
                fs.Write(ta, 0, ta.Length);
                fs.Write(uv0, 0, uv0.Length);
                fs.Write(uv1, 0, uv1.Length);
            }

            //写入索引数据
            var trisCount = ConvertToByte.ToByte(mesh.triangles.Length);
            var tris      = ConvertToByte.ToByte(mesh.triangles);

            fs.Write(trisCount, 0, trisCount.Length);
            fs.Write(tris, 0, tris.Length);

            Vector3 bCenter = mesh.bounds.center;
            Vector3 bSize   = mesh.bounds.size;
            Vector3 bMin    = mesh.bounds.min;
            Vector3 bMax    = mesh.bounds.max;

            var byteCenter = ConvertToByte.ToByte(bCenter);
            var byteSize   = ConvertToByte.ToByte(bSize);
            var byteMin    = ConvertToByte.ToByte(bMin);
            var byteMax    = ConvertToByte.ToByte(bMax);

            fs.Write(byteCenter, 0, byteCenter.Length);
            fs.Write(byteSize, 0, byteSize.Length);
            fs.Write(byteMin, 0, byteMin.Length);
            fs.Write(byteMax, 0, byteMax.Length);

            Debug.Log(bCenter.ToString("F5"));
            Debug.Log(bSize.ToString("F5"));
            Debug.Log(bMin.ToString("F5"));
            Debug.Log(bMax.ToString("F5"));
        }

        Debug.Log(mesh.vertexCount);
        Debug.Log(mesh.triangles.Length);

        //添加已经保存的文件路径信息
        assetInfo.Add(mesh.GetInstanceID(), _infoPath);
    }
Example #4
0
    public void ExportMesh()
    {
        using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate))
        {
            //写入顶点总数
            var vCount = ConvertToByte.ToByte(mesh.vertexCount);
            Debug.Log(vCount.Length);
            fs.Write(vCount, 0, vCount.Length);

            bool hadColor = mesh.colors.Length == mesh.vertexCount;
            bool hadUV1   = mesh.uv2.Length == mesh.vertexCount;

            //写入顶点数据
            for (int i = 0; i < mesh.vertexCount; i++)
            {
                var ve  = ConvertToByte.ToByte(mesh.vertices[i]);
                var co  = hadColor ? ConvertToByte.ToByte(mesh.colors[i]) : ConvertToByte.ToByte(Color.white);
                var nr  = ConvertToByte.ToByte(mesh.normals[i]);
                var ta  = ConvertToByte.ToByte(mesh.tangents[i]);
                var uv0 = ConvertToByte.ToByte(mesh.uv[i]);
                var uv1 = hadUV1 ? ConvertToByte.ToByte(mesh.uv2[i]) : ConvertToByte.ToByte(Vector2.zero);
                fs.Write(ve, 0, ve.Length);
                fs.Write(co, 0, co.Length);
                fs.Write(nr, 0, nr.Length);
                fs.Write(ta, 0, ta.Length);
                fs.Write(uv0, 0, uv0.Length);
                fs.Write(uv1, 0, uv1.Length);
            }

            //写入索引数据
            var trisCount = ConvertToByte.ToByte(mesh.triangles.Length);
            var tris      = ConvertToByte.ToByte(mesh.triangles);

            fs.Write(trisCount, 0, trisCount.Length);
            fs.Write(tris, 0, tris.Length);

            Vector3 bCenter = mesh.bounds.center;
            Vector3 bSize   = mesh.bounds.size;
            Vector3 bMin    = mesh.bounds.min;
            Vector3 bMax    = mesh.bounds.max;

            var byteCenter = ConvertToByte.ToByte(bCenter);
            var byteSize   = ConvertToByte.ToByte(bSize);
            var byteMin    = ConvertToByte.ToByte(bMin);
            var byteMax    = ConvertToByte.ToByte(bMax);

            fs.Write(byteCenter, 0, byteCenter.Length);
            fs.Write(byteSize, 0, byteSize.Length);
            fs.Write(byteMin, 0, byteMin.Length);
            fs.Write(byteMax, 0, byteMax.Length);

            Debug.Log(bCenter.ToString("F5"));
            Debug.Log(bSize.ToString("F5"));
            Debug.Log(bMin.ToString("F5"));
            Debug.Log(bMax.ToString("F5"));

            /*
             * var vex = ConvertToByte.ToByte(mesh.vertices);
             * var nomrals = ConvertToByte.ToByte(mesh.normals);
             * var tangents = ConvertToByte.ToByte(mesh.tangents);
             * var uv = ConvertToByte.ToByte(mesh.uv);
             * fs.Write(vex, 0, vex.Length);
             * fs.Write(nomrals, 0, nomrals.Length);
             * fs.Write(tangents, 0, tangents.Length);
             *
             * //判断如果存在顶点色,则写入
             * bool hadColor = mesh.uv2.Length == mesh.vertexCount;
             * fs.Write(ConvertToByte.ToByte(hadColor), 0, 1);
             * if(hadColor)
             * {
             *  var colors = ConvertToByte.ToByte(mesh.colors);
             *  fs.Write(colors, 0, colors.Length);
             * }
             *
             * fs.Write(uv, 0, uv.Length);
             *
             * //判断UV2的存在并写入数据
             * //如果UV2存在则直接写入
             * bool hadUV2 = mesh.uv2.Length == mesh.vertexCount;
             * fs.Write(ConvertToByte.ToByte(hadUV2), 0, 1);
             * if(hadUV2)
             * {
             *  var uv2 = ConvertToByte.ToByte(mesh.uv2);
             *  fs.Write(uv2, 0, uv2.Length);
             * }
             *
             * var trisCount = ConvertToByte.ToByte(mesh.triangles.Length);
             * var tris = ConvertToByte.ToByte(mesh.triangles);
             *
             * fs.Write(trisCount, 0, trisCount.Length);
             * fs.Write(tris, 0, tris.Length);
             */
        }
        Debug.Log(mesh.vertexCount);
        Debug.Log(mesh.triangles.Length);

        return;

        //for(int i = 0; i < imgs.Length; i++)
        //{
        //    imgs[i] = i;
        //}

        //byte[] bytes = ObjectConvertToByte.ToBytes(imgs);

        float[] fls = new float[32];
        for (int i = 0; i < 32; i++)
        {
            fls[i] = i;
        }

        byte[] fbs = new byte[sizeof(float) * 32];
        unsafe
        {
            fixed(float *ptrf = fls)
            {
                byte *bytes = (byte *)ptrf;

                for (int i = 0; i < 32 * sizeof(float); ++i)
                {
                    fbs[i] = *bytes++;
                }
            }
        }

        using (FileStream fs = new FileStream("Assets/Save.dat", FileMode.OpenOrCreate))
        {
            fs.Write(fbs, 0, fbs.Length);
        }


        //imgs[0] = 1.0f;
        //imgs[1] = 3.0f;

        //unsafe
        //{
        //    fixed(float* ptr = &imgs[0])
        //    {
        //        byte[] bytes = null;

        //    }
        //}
    }