public static byte[] UnpackNormals(byte[] data, VertexAttributeType attributeType, ref int stride) { switch (attributeType) { case VertexAttributeType.TangentSpaceUnitVectorsCompressed: var packedElementSize = attributeType.GetSize(); stride = Vector3.SizeInBytes * 3; var count = (data.Length / packedElementSize); var bufferLength = count * stride; var buffer = new byte[bufferLength]; using (var binaryReader = new BinaryReader(new MemoryStream(data))) using (var binaryWriter = new BinaryWriter(new MemoryStream(buffer))) { while (binaryReader.BaseStream.Position < data.Length) { var normal = VertexFunctions.UnpackVectorInt(binaryReader.ReadInt32()); var tangent = VertexFunctions.UnpackVectorInt(binaryReader.ReadInt32()); var bitangent = VertexFunctions.UnpackVectorInt(binaryReader.ReadInt32()); binaryWriter.Write(normal); binaryWriter.Write(tangent); binaryWriter.Write(bitangent); } } return(buffer); default: return(data); } }
private static VertexBuffer UnpackTextureCoordinateData(ref VertexBuffer vertexBuffer, GlobalGeometryCompressionInfoBlock compressionInfo) { var packedElementSize = vertexBuffer.Type.GetSize( ); const int stride = 2 * sizeof(float); var count = vertexBuffer.Data.Length / packedElementSize; var bufferLength = count * stride; var buffer = new byte[bufferLength]; using (var binaryReader = new BinaryReader(new MemoryStream(vertexBuffer.Data))) using (var binaryWriter = new BinaryWriter(new MemoryStream(buffer))) { while (binaryReader.BaseStream.Position < vertexBuffer.Data.Length) { var s = VertexFunctions.Unpack(binaryReader.ReadInt16( ), compressionInfo.TexcoordBoundsX.Min, compressionInfo.TexcoordBoundsX.Max); var t = VertexFunctions.Unpack(binaryReader.ReadInt16( ), compressionInfo.TexcoordBoundsY.Min, compressionInfo.TexcoordBoundsY.Max); binaryWriter.Write(s); binaryWriter.Write(t); } } return(new VertexBuffer { Data = buffer, Type = VertexAttributeType.UnpackedTextureCoordinateData }); }
private static VertexBuffer UnpackLightingData(ref VertexBuffer vertexBuffer) { var packedElementSize = vertexBuffer.Type.GetSize( ); var stride = Vector3.SizeInBytes * 3; var count = vertexBuffer.Data.Length / packedElementSize; var bufferLength = count * stride; var buffer = new byte[bufferLength]; using (var binaryReader = new BinaryReader(new MemoryStream(vertexBuffer.Data))) using (var binaryWriter = new BinaryWriter(new MemoryStream(buffer))) { while (binaryReader.BaseStream.Position < vertexBuffer.Data.Length) { var normal = VertexFunctions.UnpackVectorInt(binaryReader.ReadInt32( )); var tangent = VertexFunctions.UnpackVectorInt(binaryReader.ReadInt32( )); var bitangent = VertexFunctions.UnpackVectorInt(binaryReader.ReadInt32( )); binaryWriter.Write(normal); binaryWriter.Write(tangent); binaryWriter.Write(bitangent); } } return(new VertexBuffer { Data = buffer, Type = VertexAttributeType.UnpackedLightingData }); }
private static byte[] UnpackCoordinates(byte[] data, VertexAttributeType attributeType, GlobalGeometryCompressionInfoBlock compressionInfo, ref int stride) { switch (attributeType) { case VertexAttributeType.CoordinateCompressed: case VertexAttributeType.CoordinateWithTripleNode: case VertexAttributeType.CoordinateWithDoubleNode: case VertexAttributeType.CoordinateWithSingleNode: var packedElementSize = attributeType.GetSize(); stride = (3 * sizeof(float)) + (4 * sizeof(float)) + 4; var count = (data.Length / packedElementSize); var bufferLength = count * stride; var buffer = new byte[bufferLength]; using (var binaryReader = new BinaryReader(new MemoryStream(data))) using (var binaryWriter = new BinaryWriter(new MemoryStream(buffer))) { while (binaryReader.BaseStream.Position < data.Length) { var x = VertexFunctions.Unpack(binaryReader.ReadInt16(), compressionInfo.PositionBoundsX.Min, compressionInfo.PositionBoundsX.Max); var y = VertexFunctions.Unpack(binaryReader.ReadInt16(), compressionInfo.PositionBoundsY.Min, compressionInfo.PositionBoundsY.Max); var z = VertexFunctions.Unpack(binaryReader.ReadInt16(), compressionInfo.PositionBoundsZ.Min, compressionInfo.PositionBoundsZ.Max); binaryWriter.Write(x); binaryWriter.Write(y); binaryWriter.Write(z); switch (attributeType) { case VertexAttributeType.CoordinateCompressed: WriteVertexNodeInformation(binaryWriter, 0, 0, 0, 1.0f, 0, 0); break; case VertexAttributeType.CoordinateWithSingleNode: WriteVertexNodeInformation(binaryWriter, binaryReader.ReadByte(), binaryReader.ReadByte(), 0, 1.0f, 0, 0); break; case VertexAttributeType.CoordinateWithDoubleNode: binaryReader.ReadBytes(2); WriteVertexNodeInformation(binaryWriter, binaryReader.ReadByte(), binaryReader.ReadByte(), 0, binaryReader.ReadByte() / 255.0f, binaryReader.ReadByte() / 255.0f, 0); break; case VertexAttributeType.CoordinateWithTripleNode: WriteVertexNodeInformation(binaryWriter, binaryReader.ReadByte(), binaryReader.ReadByte(), binaryReader.ReadByte(), binaryReader.ReadByte() / 255.0f, binaryReader.ReadByte() / 255.0f, binaryReader.ReadByte() / 255.0f); break; } } } return(buffer); default: return(data); } }
private static VertexBuffer UnpackWorldCoordinateData(ref VertexBuffer vertexBuffer, GlobalGeometryCompressionInfoBlock compressionInfo) { var packedElementSize = vertexBuffer.Type.GetSize( ); const int stride = 3 * sizeof(float) + 4 * sizeof(float) + 4; var count = vertexBuffer.Data.Length / packedElementSize; var bufferLength = count * stride; var buffer = new byte[bufferLength]; using (var binaryReader = new BinaryReader(new MemoryStream(vertexBuffer.Data))) using (var binaryWriter = new BinaryWriter(new MemoryStream(buffer))) { while (binaryReader.BaseStream.Position < vertexBuffer.Data.Length) { switch (vertexBuffer.Type) { case VertexAttributeType.CoordinateFloat: { var x = binaryReader.ReadSingle( ); var y = binaryReader.ReadSingle( ); var z = binaryReader.ReadSingle( ); binaryWriter.Write(x); binaryWriter.Write(y); binaryWriter.Write(z); WriteVertexNodeInformation(binaryWriter, 0, 0, 0, 1.0f, 0, 0); } break; default: { var x = VertexFunctions.Unpack(binaryReader.ReadInt16( ), compressionInfo.PositionBoundsX.Min, compressionInfo.PositionBoundsX.Max); var y = VertexFunctions.Unpack(binaryReader.ReadInt16( ), compressionInfo.PositionBoundsY.Min, compressionInfo.PositionBoundsY.Max); var z = VertexFunctions.Unpack(binaryReader.ReadInt16( ), compressionInfo.PositionBoundsZ.Min, compressionInfo.PositionBoundsZ.Max); binaryWriter.Write(x); binaryWriter.Write(y); binaryWriter.Write(z); switch (vertexBuffer.Type) { case VertexAttributeType.CoordinateFloat: case VertexAttributeType.CoordinateCompressed: WriteVertexNodeInformation(binaryWriter, 0, 0, 0, 1.0f, 0, 0); break; case VertexAttributeType.CoordinateWithSingleNode: WriteVertexNodeInformation(binaryWriter, binaryReader.ReadByte( ), binaryReader.ReadByte( ), 0, 1.0f, 0, 0); break; case VertexAttributeType.CoordinateWithDoubleNode: binaryReader.ReadBytes(2); WriteVertexNodeInformation(binaryWriter, binaryReader.ReadByte( ), binaryReader.ReadByte( ), 0, binaryReader.ReadByte( ) / 255.0f, binaryReader.ReadByte( ) / 255.0f, 0); break; case VertexAttributeType.CoordinateWithTripleNode: WriteVertexNodeInformation(binaryWriter, binaryReader.ReadByte( ), binaryReader.ReadByte( ), binaryReader.ReadByte( ), binaryReader.ReadByte( ) / 255.0f, binaryReader.ReadByte( ) / 255.0f, binaryReader.ReadByte( ) / 255.0f); break; } } break; } } return(new VertexBuffer { Data = buffer, Type = VertexAttributeType.UnpackedWorldCoordinateData }); } }