예제 #1
0
    private void ImportTexture(string name, TextureProcessingSettings settings)
    {
        string destinationPath = Path.Combine(destinationFolder.FullName, name + ".dds");

        if (new FileInfo(destinationPath).Exists)
        {
            return;
        }

        Console.WriteLine($"importing texture '{name}'...");

        using (var image = UnmanagedRgbaImage.Load(settings.File))  {
            using (var dilator = new TextureDilator(device, shaderCache)) {
                dilator.Dilate(settings.Mask, image.Size, settings.IsLinear, image.DataBox);
            }

            InputOptions input = new InputOptions();
            input.SetFormat(InputFormat.BGRA_8UB);
            input.SetTextureLayout(TextureType.Texture2D, image.Size.Width, image.Size.Height, 1);
            float gamma = settings.IsLinear ? 1f : 2.2f;
            input.SetGamma(gamma, gamma);
            input.SetMipmapData(image.PixelData, image.Size.Width, image.Size.Height, 1, 0, 0);
            input.SetAlphaMode(AlphaMode.None);

            input.SetMipmapGeneration(true);
            input.SetMipmapFilter(MipmapFilter.Kaiser);
            input.SetKaiserParameters(3, 4, 1);

            if (settings.Type == TextureProcessingType.Bump)
            {
                input.SetConvertToNormalMap(true);
                input.SetNormalFilter(1, 0, 0, 0);
                input.SetHeightEvaluation(1, 1, 1, 0);
            }
            else if (settings.Type == TextureProcessingType.Normal)
            {
                input.SetNormalMap(true);
            }

            CompressionOptions compression = new CompressionOptions();
            compression.SetQuality(Quality.Highest);
            compression.SetFormat(Format.RGBA);

            OutputOptions output = new OutputOptions();
            destinationFolder.CreateWithParents();
            output.SetFileName(destinationPath);
            output.SetContainer(Container.Container_DDS10);
            output.SetSrgbFlag(!settings.IsLinear);

            var  compressor = new Compressor();
            bool succeeded  = compressor.Compress(input, compression, output);
            if (!succeeded)
            {
                throw new InvalidOperationException("texture conversion failed");
            }

            //force the previous output handler to be destructed so that the file is flushed and closed
            output.SetFileName("nul");

            if (compress)
            {
                CompressTexture(new FileInfo(destinationPath), settings.Type, settings.IsLinear);
            }
        }
    }