public void TestIntRevFromVerts() { int actualValue = INT_2_10_10_10_REV.FromVerts(new float[4] { -0.24667928063493072f, 0.0f, 0.9690971739229421f, 1.0f }); int expectedValue = 1592787842; Assert.AreEqual(expectedValue, actualValue, $"Assert Failed: Expected [{expectedValue}] but got [{actualValue}]"); }
public override bool CustomSerialize(BinaryWriter writer, Type field, object fieldData, NMSAttribute settings, FieldInfo fieldInfo, ref List <Tuple <long, object> > additionalData, ref int addtDataIndex) { if (field == null || fieldInfo == null) { return(false); } Dictionary <int, int> TypeMap = new Dictionary <int, int> { { 5131, 8 }, { 36255, 4 }, { 5121, 4 } }; var fieldName = fieldInfo.Name; switch (fieldName) { case nameof(IndexBuffer): writer.Align(8, 0); // write empty list header long listPos = writer.BaseStream.Position; writer.Write((Int64)0); // listPosition writer.Write((Int32)0); // listCount writer.Write((UInt32)0xAAAAAA01); IList data = (IList)fieldData; if (Indices16Bit != 1) // if 32bit indices, we can just pass it directly { additionalData.Insert(addtDataIndex, new Tuple <long, object>(listPos, data)); } else { // otherwise we have to create 32bit indices from the 16bit ones var list32Bit = new List <uint>(); int effective_count = (data.Count / 2) * 2; for (int i = 0; i < effective_count; i += 2) { uint val32Bit = (uint)((int)data[i + 1] << 16 | (int)data[i]); list32Bit.Add(val32Bit); } //Handle odd cases if (data.Count % 2 == 1) { //uint val32Bit = (uint)((int)data[data.Count - 1] << 16); uint val32Bit = (uint)((int)data[data.Count - 1]); list32Bit.Add(val32Bit); } additionalData.Insert(addtDataIndex, new Tuple <long, object>(listPos, list32Bit)); } addtDataIndex++; return(true); case nameof(VertexStream): case nameof(SmallVertexStream): writer.Align(8, 0); List <int> LayoutTypes = new List <int>(); TkVertexLayout layout; if (fieldName == nameof(VertexStream)) { layout = VertexLayout; } else { layout = SmallVertexLayout; } foreach (TkVertexElement ve in layout.VertexElements) { int type = ve.Type; LayoutTypes.Add(type); } int stride = 4 * LayoutTypes.Count; // write empty list header long listPos2 = writer.BaseStream.Position; writer.Write((Int64)0); // listPosition writer.Write((Int32)0); // listCount writer.Write((UInt32)0xAAAAAA01); IList vertexData = (IList)fieldData; // list size field for VertexStream/SmallVertexSteam is the number of bytes, so we'll have to use a List<byte> in the additionalData byte[] streamData = null; using (var ms = new MemoryStream()) using (var writer2 = new BinaryWriter(ms)) { if (vertexData != null) { for (int v = 0; v < VertexCount; v++) { for (int i = 0; i < LayoutTypes.Count; i++) { if (LayoutTypes[i] == 5131) { // half-float for (int j = 0; j < 4; j++) { var floatVertex = (float)vertexData[v * stride + 4 * i + j]; var halfVertex = (Half)floatVertex; writer2.Write(halfVertex.value); } } else if (LayoutTypes[i] == 5121) { // Unsigned bytes for (int j = 0; j < 4; j++) { byte ubyteVertex = (byte)vertexData[v * stride + 4 * i + j]; writer2.Write(ubyteVertex); } } else if (LayoutTypes[i] == 36255) { // INT_2_10_10_10_REV float[] vertexes = new float[4] { (float)vertexData[v * stride + 4 * i], (float)vertexData[v * stride + 4 * i + 1], (float)vertexData[v * stride + 4 * i + 2], (float)vertexData[v * stride + 4 * i + 3] }; int val = INT_2_10_10_10_REV.FromVerts(vertexes); writer2.Write((UInt32)val); } } } } streamData = ms.ToArray(); } /* * using (var ms = new MemoryStream()) * using (var writer2 = new BinaryWriter(ms)) * { * if(vertexData != null) * foreach (var vertex in vertexData) * { * var floatVertex = (float)vertex; * var halfVertex = (Half)floatVertex; * writer2.Write(halfVertex.value); * } * streamData = ms.ToArray(); * }*/ var listBytes = new List <byte>(streamData); additionalData.Insert(addtDataIndex, new Tuple <long, object>(listPos2, listBytes)); addtDataIndex++; return(true); } return(false); }