public byte[] getAttrByte(Mesh m, GlTF_Geometry geometry) { byte[] byteArr = GlTF_Md5.getByte(m.vertices); if (colorAccessor != null) { byteArr = GlTF_Md5.combine(byteArr, GlTF_Md5.getByte(m.colors)); } if (normalAccessor != null) { if (!combineMesh) { normalAccessor.PibufferView.initPi(); } byteArr = GlTF_Md5.combine(byteArr, GlTF_Md5.getByte(m.normals)); } if (texCoord0Accessor != null) { if (!combineMesh) { texCoord0Accessor.PibufferView.initPi(); } byteArr = GlTF_Md5.combine(byteArr, GlTF_Md5.getByte(m.uv)); } if (texCoord1Accessor != null) { if (!combineMesh) { texCoord1Accessor.PibufferView.initPi(); } byteArr = GlTF_Md5.combine(byteArr, GlTF_Md5.getByte(m.uv2)); } if (texCoord2Accessor != null) { if (!combineMesh) { texCoord2Accessor.PibufferView.initPi(); } byteArr = GlTF_Md5.combine(byteArr, GlTF_Md5.getByte(m.uv3)); } if (texCoord3Accessor != null) { if (!combineMesh) { texCoord3Accessor.PibufferView.initPi(); } byteArr = GlTF_Md5.combine(byteArr, GlTF_Md5.getByte(m.uv4)); } if (lightmapTexCoordAccessor != null) { if (!combineMesh) { lightmapTexCoordAccessor.PibufferView.initPi(); } lightmapTexCoordAccessor.PopulateWithOffsetScale(m.uv2, false); } if (jointAccessor != null) { if (!combineMesh) { jointAccessor.PibufferView.initPi(); } //byteArr = GlTF_Md5.combine (byteArr,GlTF_Md5.getByte (m.boneWeights)); } if (tangentAccessor != null) { if (!combineMesh) { tangentAccessor.PibufferView.initPi(); } byteArr = GlTF_Md5.combine(byteArr, GlTF_Md5.getByte(m.tangents)); } return(byteArr); }
/// <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); } }