Ejemplo n.º 1
0
        public static Texture LoadBlpTexture(
            GraphicsDevice graphicsDevice,
            ResourceFactory resourceFactory,
            Stream stream,
            Veldrid.PixelFormat pixelFormat = Veldrid.PixelFormat.B8_G8_R8_A8_UNorm)
        {
            using var blpFile = new BlpFile(stream);

            var width  = (uint)blpFile.Width;
            var height = (uint)blpFile.Height;

            var mipMapCount = (uint)blpFile.MipMapCount;

            var sampledTexture = resourceFactory.CreateTexture(TextureDescription.Texture2D(width, height, mipMapCount, 1, pixelFormat, TextureUsage.Sampled));
            var stagingTexture = resourceFactory.CreateTexture(TextureDescription.Texture2D(width, height, mipMapCount, 1, pixelFormat, TextureUsage.Staging));

            var mipMapWidths  = new uint[mipMapCount];
            var mipMapHeights = new uint[mipMapCount];

            for (var mipMapLevel = 0; mipMapLevel < mipMapCount; mipMapLevel++)
            {
                var pixelData    = blpFile.GetPixels(mipMapLevel, out var w, out var h);
                var mipMapWidth  = (uint)w;
                var mipMapHeight = (uint)h;

                mipMapWidths[mipMapLevel]  = mipMapWidth;
                mipMapHeights[mipMapLevel] = mipMapHeight;
                graphicsDevice.UpdateTexture(stagingTexture, pixelData, 0, 0, 0, mipMapWidth, mipMapHeight, 1, (uint)mipMapLevel, 0);
            }

            CreateCommandList(graphicsDevice, resourceFactory, stagingTexture, sampledTexture);

            return(sampledTexture);
        }
Ejemplo n.º 2
0
        private static Bitmap ReadBLP(String filePath)
        {
            FileStream fs      = File.OpenRead(filePath);
            BlpFile    blpFile = new BlpFile(fs);
            int        width;
            int        height;

            // The library does not determine what's BLP1 and BLP2 properly, so we manually set bool bgra in GetPixels depending on the checkbox.
            byte[] bytes         = blpFile.GetPixels(0, out width, out height, blpFile._isBLP2); // 0 indicates first mipmap layer. width and height are assigned width and height in GetPixels().
            var    actualImage   = blpFile.GetBitmapSource(0);
            int    bytesPerPixel = (actualImage.Format.BitsPerPixel + 7) / 8;
            int    stride        = bytesPerPixel * actualImage.PixelWidth;

            // blp read and convert
            Bitmap image = new Bitmap(width, height);

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    var offset = (y * stride) + (x * bytesPerPixel);

                    byte red;
                    byte green;
                    byte blue;
                    byte alpha = 0;

                    red   = bytes[offset + 0];
                    green = bytes[offset + 1];
                    blue  = bytes[offset + 2];
                    alpha = bytes[offset + 3];

                    image.SetPixel(x, y, Color.FromArgb(alpha, blue, green, red)); // assign color to pixel
                }
            }

            blpFile.Dispose();

            return(image);
        }