示例#1
0
        public static void importTexture(GpkExport export, string file)
        {
            var texture2d = export.Payload as Texture2D;

            var image  = new DdsFile();
            var config = new DdsSaveConfig(texture2d.GetFormat(), 0, 0, false, false);

            image.Load(file);

            if (image.MipMaps.Count == 0 || Settings.Default.GenerateMipMaps)
            {
                image.GenerateMipMaps();
            }


            texture2d.maps = new List <MipMap>();
            foreach (DdsMipMap mipMap in image.MipMaps.OrderByDescending(mip => mip.Width))
            {
                byte[] outputData = image.WriteMipMap(mipMap, config);

                var textureMipMap = new MipMap();
                textureMipMap.compFlag                     = (int)CompressionTypes.LZO;
                textureMipMap.uncompressedData             = outputData;
                textureMipMap.uncompressedSize             = outputData.Length;
                textureMipMap.uncompressedSize_chunkheader = outputData.Length;
                textureMipMap.sizeX = mipMap.Width;
                textureMipMap.sizeY = mipMap.Height;

                textureMipMap.generateBlocks();
                texture2d.maps.Add(textureMipMap);
            }
        }
示例#2
0
        public static void importTexture(GpkExport export, string file)
        {
            try
            {
                var texture2d = export.Payload as Texture2D;

                var image  = new DdsFile();
                var config = new DdsSaveConfig(texture2d.parsedImageFormat, 0, 0, false, false);
                image.Load(file);

                if (image.MipMaps.Count == 0 || CoreSettings.Default.GenerateMipMaps)
                {
                    image.GenerateMipMaps();
                }


                texture2d.maps = new List <MipMap>();
                foreach (DdsMipMap mipMap in image.MipMaps.OrderByDescending(mip => mip.Width))
                {
                    byte[] outputData = image.WriteMipMap(mipMap, config);

                    var textureMipMap = new MipMap();
                    textureMipMap.flags = (int)CompressionTypes.LZO;
                    //textureMipMap.flags = 0;
                    textureMipMap.uncompressedData             = outputData;
                    textureMipMap.uncompressedSize             = outputData.Length;
                    textureMipMap.uncompressedSize_chunkheader = outputData.Length;
                    textureMipMap.sizeX = mipMap.Width;
                    textureMipMap.sizeY = mipMap.Height;

                    if (textureMipMap.flags != 0)
                    {
                        textureMipMap.generateBlocks();
                    }
                    texture2d.maps.Add(textureMipMap);
                }

                int mipTailBaseIdx = (int)Math.Log(image.Width > image.Height ? image.Width : image.Height, 2);
                ((GpkIntProperty)export.GetProperty("MipTailBaseIdx")).SetValue(mipTailBaseIdx.ToString());

                logger.Info("Imported image from {0}, size {1}x{2}, target format {3}, mipTailBaseIdx {4}", file, image.Width, image.Height, config.FileFormat, mipTailBaseIdx);
            }
            catch (Exception ex)
            {
                logger.Error(ex, "Failed to import texture");
                logger.Error(ex);
            }
        }
        public override async Task SetObject(string filename, List <DomainNameTableEntry> nameTable, object configuration)
        {
            DdsSaveConfig config = configuration as DdsSaveConfig ?? new DdsSaveConfig(FileFormat.Unknown, 0, 0, false, false);

            DdsFile image = await Task.Run(() => new DdsFile(filename));

            bool skipFirstMip = false;

            int width  = image.Width;
            int height = image.Height;

            if (MipMaps[0].ImageData == null || MipMaps[0].ImageData.Length == 0)
            {
                width  *= 2;
                height *= 2;

                skipFirstMip = true;
            }

            DomainPropertyIntValue sizeX = PropertyHeader.GetProperty("SizeX").FirstOrDefault()?.Value as DomainPropertyIntValue;
            DomainPropertyIntValue sizeY = PropertyHeader.GetProperty("SizeY").FirstOrDefault()?.Value as DomainPropertyIntValue;

            sizeX?.SetPropertyValue(skipFirstMip ? width * 2 : width);
            sizeY?.SetPropertyValue(skipFirstMip ? height * 2 : height);

            DomainPropertyIntValue mipTailBaseIdx = PropertyHeader.GetProperty("MipTailBaseIdx").FirstOrDefault()?.Value as DomainPropertyIntValue;

            int indexSize = width > height ? width : height;

            mipTailBaseIdx?.SetPropertyValue((int)Math.Log(skipFirstMip ? indexSize * 2 : indexSize, 2));

            DomainPropertyStringValue filePath = PropertyHeader.GetProperty("SourceFilePath").FirstOrDefault()?.Value as DomainPropertyStringValue;
            DomainPropertyStringValue fileTime = PropertyHeader.GetProperty("SourceFileTimestamp").FirstOrDefault()?.Value as DomainPropertyStringValue;

            filePath?.SetPropertyValue(filename);
            fileTime?.SetPropertyValue(File.GetLastWriteTime(filename).ToString("yyyy-MM-dd hh:mm:ss"));

            DomainPropertyByteValue pfFormat = PropertyHeader.GetProperty("Format").FirstOrDefault()?.Value as DomainPropertyByteValue;

            FileFormat imageFormat = FileFormat.Unknown;

            if (pfFormat != null)
            {
                imageFormat = DdsPixelFormat.ParseFileFormat(pfFormat.PropertyString);
            }

            if (imageFormat == FileFormat.Unknown)
            {
                throw new Exception($"Unknown DDS File Format ({pfFormat?.PropertyString ?? "Unknown"}).");
            }

            config.FileFormat = imageFormat;

            MipMaps.Clear();

            if (skipFirstMip)
            {
                MipMaps.Add(new DomainMipMap {
                    ImageData = null,
                    Width     = width,
                    Height    = height
                });
            }

            image.GenerateMipMaps(4, 4);

            foreach (DdsMipMap mipMap in image.MipMaps.OrderByDescending(mip => mip.Width))
            {
                MipMaps.Add(new DomainMipMap {
                    ImageData = image.WriteMipMap(mipMap, config),
                    Width     = mipMap.Width,
                    Height    = mipMap.Height
                });
            }
        }