Пример #1
0
        /// <summary>
        /// Matches a given DDS file on the disk to a D3DTX object.
        /// </summary>
        /// <param name="ddsPath"></param>
        /// <param name="d3dtx"></param>
        /// <param name="options"></param>
        public void Match_DDS_With_D3DTX(string ddsPath, D3DTX_Master d3dtx, DDS_Matching_Options options)
        {
            //load in the DDS image using DirectXTexNet
            ScratchImage scratchImage = TexHelper.Instance.LoadFromDDSFile(ddsPath, DDS_FLAGS.NONE);

            //create our main variables that will be used when doing conversion operations
            int             d3dtx_width     = 0;
            int             d3dtx_height    = 0;
            int             d3dtx_mipAmount = 0;
            T3SurfaceFormat d3dtx_format    = T3SurfaceFormat.eSurface_DXT1;
            T3SurfaceGamma  d3dtx_gamma     = T3SurfaceGamma.eSurfaceGamma_sRGB;

            //assign the values depending on which version is active
            if (d3dtx.d3dtx9 != null)
            {
                d3dtx_width     = (int)d3dtx.d3dtx9.mWidth;
                d3dtx_height    = (int)d3dtx.d3dtx9.mHeight;
                d3dtx_mipAmount = (int)d3dtx.d3dtx9.mNumMipLevels;
                d3dtx_format    = d3dtx.d3dtx9.mSurfaceFormat;
                d3dtx_gamma     = d3dtx.d3dtx9.mSurfaceGamma;
            }
            else if (d3dtx.d3dtx8 != null)
            {
                d3dtx_width     = (int)d3dtx.d3dtx8.mWidth;
                d3dtx_height    = (int)d3dtx.d3dtx8.mHeight;
                d3dtx_mipAmount = (int)d3dtx.d3dtx8.mNumMipLevels;
                d3dtx_format    = d3dtx.d3dtx8.mSurfaceFormat;
                d3dtx_gamma     = d3dtx.d3dtx8.mSurfaceGamma;
            }
            else if (d3dtx.d3dtx7 != null)
            {
                d3dtx_width     = (int)d3dtx.d3dtx7.mWidth;
                d3dtx_height    = (int)d3dtx.d3dtx7.mHeight;
                d3dtx_mipAmount = (int)d3dtx.d3dtx7.mNumMipLevels;
                d3dtx_format    = d3dtx.d3dtx7.mSurfaceFormat;
                d3dtx_gamma     = d3dtx.d3dtx7.mSurfaceGamma;
            }
            else if (d3dtx.d3dtx6 != null)
            {
                d3dtx_width     = (int)d3dtx.d3dtx6.mWidth;
                d3dtx_height    = (int)d3dtx.d3dtx6.mHeight;
                d3dtx_mipAmount = (int)d3dtx.d3dtx6.mNumMipLevels;
                d3dtx_format    = d3dtx.d3dtx6.mSurfaceFormat;
                d3dtx_gamma     = T3SurfaceGamma.eSurfaceGamma_sRGB; //this version doesn't have a surface gamma field, so give it an SRGB by default
            }
            else if (d3dtx.d3dtx5 != null)
            {
                d3dtx_width     = (int)d3dtx.d3dtx5.mWidth;
                d3dtx_height    = (int)d3dtx.d3dtx5.mHeight;
                d3dtx_mipAmount = (int)d3dtx.d3dtx5.mNumMipLevels;
                d3dtx_format    = d3dtx.d3dtx5.mSurfaceFormat;
                d3dtx_gamma     = T3SurfaceGamma.eSurfaceGamma_sRGB; //this version doesn't have a surface gamma field, so give it an SRGB by default
            }
            else if (d3dtx.d3dtx4 != null)
            {
                d3dtx_width     = (int)d3dtx.d3dtx4.mWidth;
                d3dtx_height    = (int)d3dtx.d3dtx4.mHeight;
                d3dtx_mipAmount = (int)d3dtx.d3dtx4.mNumMipLevels;
                d3dtx_format    = d3dtx.d3dtx4.mSurfaceFormat;
                d3dtx_gamma     = T3SurfaceGamma.eSurfaceGamma_sRGB; //this version doesn't have a surface gamma field, so give it an SRGB by default
            }

            //-------------------------------------- CONVERSION START --------------------------------------
            //change the compression if needed
            if (options.MatchCompression)
            {
                DXGI_FORMAT dxgi_format = DDS_Functions.GetSurfaceFormatAsDXGI(d3dtx_format, d3dtx_gamma);
                scratchImage.Convert(dxgi_format, TEX_FILTER_FLAGS.DITHER, 0.0f);
            }

            //rescale the image to match if needed
            if (options.MatchResolution)
            {
                scratchImage.Resize(d3dtx_width, d3dtx_height, TEX_FILTER_FLAGS.CUBIC);
            }

            //generate mip maps if needed
            if (options.GenerateMipMaps)
            {
                if (options.MatchMipMapCount)
                {
                    scratchImage.GenerateMipMaps(0, TEX_FILTER_FLAGS.CUBIC, d3dtx_mipAmount, false);
                }
                else
                {
                    scratchImage.GenerateMipMaps(0, TEX_FILTER_FLAGS.CUBIC, 0, false);
                }
            }

            //resave the newly modified DDS
            scratchImage.SaveToDDSFile(DDS_FLAGS.NONE, ddsPath);
        }
        public static DXGI_FORMAT GetSurfaceFormatAsDXGI(T3SurfaceFormat format, T3SurfaceGamma gamma = T3SurfaceGamma.eSurfaceGamma_sRGB)
        {
            switch (format)
            {
            default:
                return(DXGI_FORMAT.BC1_UNORM);    //just choose classic DXT1 if the format isn't known

            //--------------------DXT1--------------------
            case T3SurfaceFormat.eSurface_BC1:
                if (gamma == T3SurfaceGamma.eSurfaceGamma_sRGB)
                {
                    return(DXGI_FORMAT.BC1_UNORM_SRGB);
                }
                else
                {
                    return(DXGI_FORMAT.BC1_UNORM);
                }

            case T3SurfaceFormat.eSurface_DXT1:
                if (gamma == T3SurfaceGamma.eSurfaceGamma_sRGB)
                {
                    return(DXGI_FORMAT.BC1_UNORM_SRGB);
                }
                else
                {
                    return(DXGI_FORMAT.BC1_UNORM);
                }

            //--------------------DXT2 and DXT3--------------------
            case T3SurfaceFormat.eSurface_BC2:
                if (gamma == T3SurfaceGamma.eSurfaceGamma_sRGB)
                {
                    return(DXGI_FORMAT.BC2_UNORM_SRGB);
                }
                else
                {
                    return(DXGI_FORMAT.BC2_UNORM);
                }

            case T3SurfaceFormat.eSurface_DXT3:
                if (gamma == T3SurfaceGamma.eSurfaceGamma_sRGB)
                {
                    return(DXGI_FORMAT.BC2_UNORM_SRGB);
                }
                else
                {
                    return(DXGI_FORMAT.BC2_UNORM);
                }

            //--------------------DXT4 and DXT5--------------------
            case T3SurfaceFormat.eSurface_BC3:
                if (gamma == T3SurfaceGamma.eSurfaceGamma_sRGB)
                {
                    return(DXGI_FORMAT.BC3_UNORM_SRGB);
                }
                else
                {
                    return(DXGI_FORMAT.BC3_UNORM);
                }

            case T3SurfaceFormat.eSurface_DXT5:
                if (gamma == T3SurfaceGamma.eSurfaceGamma_sRGB)
                {
                    return(DXGI_FORMAT.BC3_UNORM_SRGB);
                }
                else
                {
                    return(DXGI_FORMAT.BC3_UNORM);
                }

            //--------------------ATI1--------------------
            case T3SurfaceFormat.eSurface_BC4:
                return(DXGI_FORMAT.BC4_UNORM);

            case T3SurfaceFormat.eSurface_DXT5A:
                return(DXGI_FORMAT.BC4_UNORM);

            //--------------------ATI2--------------------
            case T3SurfaceFormat.eSurface_BC5:
                return(DXGI_FORMAT.BC5_UNORM);

            case T3SurfaceFormat.eSurface_DXN:
                return(DXGI_FORMAT.BC5_UNORM);


            case T3SurfaceFormat.eSurface_BC6:
                return(DXGI_FORMAT.BC6H_UF16);

            case T3SurfaceFormat.eSurface_BC7:
                if (gamma == T3SurfaceGamma.eSurfaceGamma_sRGB)
                {
                    return(DXGI_FORMAT.BC7_UNORM_SRGB);
                }
                else
                {
                    return(DXGI_FORMAT.BC7_UNORM);
                }
            }
        }