コード例 #1
0
    /// <summary>
    /// 数据写入文件
    /// </summary>
    public override void Write()
    {
        /*
         * "bufferView_4642": {
         *      "buffer": "vc.bin",
         *      "byteLength": 630080,
         *      "byteOffset": 0,
         *      "target": "ARRAY_BUFFER"
         * },
         */

        Indent();               jsonWriter.Write("{\n");
        IndentIn();

        //var binName = binary ? "binary_glTF" : Path.GetFileNameWithoutExtension(GlTF_Writer.binFileName);
        GlTF_Buffer bin = null;

        if (PiName != null)
        {
            bin = GlTF_Writer.buffers.Find(buffer => buffer.uri.Contains(PiName));
        }

        if (bin != null)
        {
            Indent();               jsonWriter.Write("\"buffer\": " + bin.BufferviewIndex + ",\n");
        }
        else
        {
            Indent();               jsonWriter.Write("\"buffer\": " + PibufferIndex + ",\n");
        }

        Indent();               jsonWriter.Write("\"byteLength\": " + byteLength + ",\n");

        /*if ((int)target != (int)-1)
         * {
         *      Indent(); jsonWriter.Write("\"target\": " + target + ",\n");
         * }*/

        if (byteStride >= 4)
        {
            Indent(); jsonWriter.Write("\"byteStride\": " + byteStride + ",\n");
        }

        Indent();               jsonWriter.Write("\"byteOffset\": " + byteOffset + "\n");

        IndentOut();
        Indent();               jsonWriter.Write("}");
    }
コード例 #2
0
    /// <summary>
    /// 填充 Mesh 数据
    /// </summary>
    /// <param name="m"></param>
    /// <param name="path"></param>
    public void Populate(Mesh m, string path)
    {
        /// 属性数据
        byte[] attrByte = primitives[0].attributes.getAttrByte(m, geometry);
        /// 图元顶点序号数组
        byte[][] indiceByte = new byte[primitives.Count][];

        /// 收集 indice 数据到 indiceByte[][]
        foreach (GlTF_Primitive p in primitives)
        {
            if (!combineMesh)
            {
                p.indices.PibufferView.initPi();
            }

            /// 获取目标 图元 顶点序号数据
            int[] indice = m.GetTriangles(p.index);
            int[] resArr = new int[indice.Length + 1];
            int[] defArr = new int[1] {
                p.materialIndex
            };

            /// 顶点序号数据
            indice.CopyTo(resArr, 0);
            /// 图元的材质序号
            defArr.CopyTo(resArr, indice.Length);

            indiceByte[p.index] = GlTF_Md5.getByte(resArr);
        }

        meshMd5          = GlTF_Md5.byteToMd5(GlTF_Md5.combine(attrByte, GlTF_Md5.combine(indiceByte)));
        geometry.GeoHash = meshMd5;
        GlTF_Geometry geo = geometryList.Find(x => x.GeoHash == meshMd5);

        /// 没有网格信息
        if (geo != null)
        {
            primitives = geo.primitives;
            return;
        }

        /// 合并 - 修改MD5
        if (combineMesh)
        {
            meshMd5 = Path.GetFileNameWithoutExtension(path) + ".mesh.bin";
        }

        if (primitives.Count > 0)
        {
            /// only populate first attributes because the data are shared between primitives仅填充第一个属性,因为数据在基元之间共享
            primitives[0].attributes.Populate(m, meshMd5);
        }

        foreach (GlTF_Primitive p in primitives)
        {
            p.Populate(m, meshMd5);
        }

        geometry.primitives = primitives;

        geometryList.Add(geometry);

        if (combineMesh)
        {
            return;
        }

        GlTF_Buffer bin = GlTF_Writer.buffers.Find(buffer => buffer.uri.Contains(meshMd5));

        if (bin == null)
        {
            GlTF_Buffer a = new GlTF_Buffer();

            PifloatBufferView.byteOffset      = 0;
            Pivec2BufferView.byteOffset       = PifloatBufferView.byteOffset + PifloatBufferView.byteLength;
            Pivec3BufferView.byteOffset       = Pivec2BufferView.byteOffset + Pivec2BufferView.byteLength;
            Pivec4BufferView.byteOffset       = Pivec3BufferView.byteOffset + Pivec3BufferView.byteLength;
            Pivec4UshortBufferView.byteOffset = Pivec4BufferView.byteOffset + Pivec4BufferView.byteLength;
            PiushortBufferView.byteOffset     = Pivec4UshortBufferView.byteOffset + Pivec4UshortBufferView.byteLength;

            filePath  = Path.Combine(Path.GetDirectoryName(path), meshMd5 + ".mesh.bin");
            PibinFile = File.Open(filePath, FileMode.Create);
            a.uri     = meshMd5 + ".mesh.bin";

            //floatBufferView.PimemoryStream.WriteTo(PibinFile);
            Pivec2BufferView.PimemoryStream.WriteTo(PibinFile);
            Pivec3BufferView.PimemoryStream.WriteTo(PibinFile);
            Pivec4BufferView.PimemoryStream.WriteTo(PibinFile);

            if (m.boneWeights.Length > 0)
            {
                Pivec4UshortBufferView.PimemoryStream.WriteTo(PibinFile);
            }

            PiushortBufferView.PimemoryStream.WriteTo(PibinFile);

            a.BLength         = PibinFile.Length;
            a.BufferviewIndex = buffers.Count;

            PibinFile.Flush();
            PibinFile.Close();

            buffers.Add(a);
        }
    }