Ejemplo n.º 1
0
 public void TestMethod1()
 {
     float[] actualValue   = INT_2_10_10_10_REV.FromBytes(1592787842);
     float[] expectedValue = new float[4] {
         -0.24667928063493072f, 0.0f, 0.9690971739229421f, 1.0f
     };
     Assert.AreEqual(expectedValue, actualValue, $"Assert Failed: Expected [{expectedValue[0]}, {expectedValue[1]}, {expectedValue[2]}, {expectedValue[3]}] but got [{actualValue[0]}, {actualValue[1]}, {actualValue[2]}, {actualValue[3]}]");
 }
Ejemplo n.º 2
0
        public void TestIntRevFromBytes()
        {
            float[] actualValue   = INT_2_10_10_10_REV.FromBytes(1592787842);
            float[] expectedValue = new float[4] {
                -0.24667928063493072f, 0.0f, 0.9690971739229421f, 1.0f
            };

            // accommodate floating-point inaccuracies
            bool pass = true;

            pass &= Math.Abs(expectedValue[0] - actualValue[0]) <= 1e-7f;
            pass &= Math.Abs(expectedValue[1] - actualValue[1]) <= 1e-7f;
            pass &= Math.Abs(expectedValue[2] - actualValue[2]) <= 1e-7f;
            pass &= Math.Abs(expectedValue[3] - actualValue[3]) <= 1e-7f;

            Assert.IsTrue(pass,
                          $"Assert Failed: Expected " +
                          $"[{expectedValue[0]}, {expectedValue[1]}, {expectedValue[2]}, {expectedValue[3]}]" +
                          $" but got " +
                          $"[{actualValue[0]}, {actualValue[1]}, {actualValue[2]}, {actualValue[3]}]");
        }
Ejemplo n.º 3
0
        public override object CustomDeserialize(BinaryReader reader, Type field, NMSAttribute settings, long templatePosition, FieldInfo fieldInfo)
        {
            var fieldName = fieldInfo.Name;

            Dictionary <int, int> TypeMap = new Dictionary <int, int> {
                { 5131, 8 }, { 36255, 4 }, { 5121, 4 }
            };

            switch (fieldName)
            {
            case nameof(IndexBuffer):
                reader.Align(8, 0);
                long listPosition = reader.BaseStream.Position;
                //Debug.WriteLine($"TkGeometryData.CustomDeserialize({fieldName}) start 0x{listPosition:X}");

                long listStartOffset = reader.ReadInt64();
                int  numEntries      = reader.ReadInt32() * ((Indices16Bit == 1) ? 2 : 1); // Adjust size
                uint listMagic       = reader.ReadUInt32();
                if ((listMagic & 0xFF) != 1)
                {
                    throw new Exception($"Invalid list read, magic {listMagic:X8} expected xxxxxx01");
                }

                long listEndPosition = reader.BaseStream.Position;

                reader.BaseStream.Position = listPosition + listStartOffset;
                var indices = new List <int>();
                for (int i = 0; i < numEntries; i++)
                {
                    if (Indices16Bit == 1)
                    {
                        indices.Add((int)reader.ReadUInt16());
                    }
                    else
                    {
                        indices.Add((int)reader.ReadUInt32());
                    }
                }

                reader.BaseStream.Position = listEndPosition;
                reader.Align(0x8, 0);

                return(indices);

            case nameof(VertexStream):
            case nameof(SmallVertexStream):

                List <int> LayoutTypes     = GetTypeLayouts(fieldName, reader);
                int        StreamBlockSize = 0;
                foreach (int vert_type in LayoutTypes)
                {
                    StreamBlockSize += TypeMap[vert_type];
                }
                reader.Align(8, 0);
                listPosition = reader.BaseStream.Position;
                //Debug.WriteLine($"TkGeometryData.CustomDeserialize({fieldName}) start 0x{listPosition:X}");

                listStartOffset = reader.ReadInt64();
                numEntries      = reader.ReadInt32() / StreamBlockSize; // the number of blocks of data
                listMagic       = reader.ReadUInt32();
                if ((listMagic & 0xFF) != 1)
                {
                    throw new Exception($"Invalid list read, magic {listMagic:X8} expected xxxxxx01");
                }

                listEndPosition = reader.BaseStream.Position;

                reader.BaseStream.Position = listPosition + listStartOffset;
                List <float> vertices   = new List <float>();
                int          curr_block = 0;
                while (curr_block < numEntries)
                {
                    foreach (int ltype in LayoutTypes)
                    {
                        if (ltype == 5131)
                        // Half floats
                        {
                            // read in the half-float data
                            for (int i = 0; i < 4; i++)
                            {
                                ushort vertex = reader.ReadUInt16();
                                vertices.Add((float)Half.ToHalf(vertex));
                            }
                        }
                        else if (ltype == 5121)
                        // Unsigned bytes
                        {
                            for (int i = 0; i < 4; i++)
                            {
                                byte num = reader.ReadByte();
                                vertices.Add((float)(int)num);
                            }
                        }
                        else if (ltype == 36255)
                        {
                            int vert_data = reader.ReadInt32();
                            vertices.AddRange(INT_2_10_10_10_REV.FromBytes(vert_data));
                        }
                    }
                    curr_block += 1;
                }

                /*
                 * while (reader.BaseStream.Position < (listPosition + listStartOffset + numEntries))
                 * {
                 *  ushort vertex = reader.ReadUInt16();
                 *  vertices.Add((float)Half.ToHalf(vertex));
                 * }*/

                reader.BaseStream.Position = listEndPosition;
                reader.Align(0x8, 0);

                return(vertices);
            }

            return(null);
        }