Ejemplo n.º 1
0
        static bool Generate(string sourceFileName, string sourceFileHash, int sourceFileSize, int specifiedSize, out string error)
        {
            var text         = $"Generating cubemap \"{Path.GetFileName( sourceFileName )}\"...";
            var notification = Editor.ScreenNotifications.ShowSticky(text);

            var destFileNameBase = GetDestFileNameBase(sourceFileName);

            GetDestFileNames(destFileNameBase, out var infoFileName, out var envFileName, out var irrFileName);

            string temporaryGeneratedFile = "";

            try
            {
                var sourceRealFileName = VirtualPathUtility.GetRealPathByVirtual(sourceFileName);
                {
                    //convert .image to 6 face HDR image

                    if (Path.GetExtension(sourceRealFileName) == ".image")
                    {
                        var image = ResourceManager.LoadResource <Component_Image>(sourceFileName, out error);
                        if (image == null)
                        {
                            return(false);
                        }

                        string[] loadCubeMap6Files = null;
                        string   loadFromOneFile   = null;
                        {
                            if (image.AnyCubemapSideIsSpecified())
                            {
                                if (image.AllCubemapSidesAreaSpecified())
                                {
                                    loadCubeMap6Files = new string[6];
                                    for (int n = 0; n < 6; n++)
                                    {
                                        var v = image.GetLoadCubeSide(n);
                                        if (v != null)
                                        {
                                            loadCubeMap6Files[n] = v.ResourceName;
                                        }

                                        if (string.IsNullOrEmpty(loadCubeMap6Files[n]) || !VirtualFile.Exists(loadCubeMap6Files[n]))
                                        {
                                            loadCubeMap6Files = null;
                                            break;
                                        }
                                    }
                                }
                            }
                            else
                            {
                                var v = image.LoadFile.Value;
                                loadFromOneFile = v != null ? v.ResourceName : "";
                                if (string.IsNullOrEmpty(loadFromOneFile) || !VirtualFile.Exists(loadFromOneFile))
                                {
                                    loadFromOneFile = null;
                                }
                            }
                        }

                        if (loadFromOneFile != null)
                        {
                            sourceRealFileName = VirtualPathUtility.GetRealPathByVirtual(loadFromOneFile);
                        }
                        else if (loadCubeMap6Files != null)
                        {
                            temporaryGeneratedFile = Path.GetTempPath() + Guid.NewGuid().ToString() + ".hdr";
                            sourceRealFileName     = temporaryGeneratedFile;

                            ImageUtility.Image2D image2D = null;

                            for (int face = 0; face < 6; face++)
                            {
                                if (!ImageUtility.LoadFromVirtualFile(loadCubeMap6Files[face], out var data, out var size, out _, out var format, out _, out _, out error))
                                {
                                    return(false);
                                }

                                if (image2D == null)
                                {
                                    image2D = new ImageUtility.Image2D(PixelFormat.Float32RGB, size * new Vector2I(4, 3));
                                }

                                Vector2I index = Vector2I.Zero;
                                switch (face)
                                {
                                case 0: index = new Vector2I(2, 1); break;

                                case 1: index = new Vector2I(0, 1); break;

                                case 2: index = new Vector2I(1, 0); break;

                                case 3: index = new Vector2I(1, 2); break;

                                case 4: index = new Vector2I(1, 1); break;

                                case 5: index = new Vector2I(3, 1); break;
                                }

                                var faceImage = new ImageUtility.Image2D(format, size, data);
                                image2D.Blit(index * size, faceImage);
                            }

                            if (!ImageUtility.Save(temporaryGeneratedFile, image2D.Data, image2D.Size, 1, image2D.Format, 1, 0, out error))
                            {
                                return(false);
                            }
                        }
                        else
                        {
                            error = "The image wrong configured.";
                            return(false);
                        }
                    }
                }

                int sizeEnv = 32;
                if (specifiedSize == 0)
                {
                    //detect size

                    if (!ImageUtility.LoadFromRealFile(sourceRealFileName, out _, out var sourceSize, out _, out _, out _, out _, out error))
                    {
                        return(false);
                    }

                    //!!!!если кубемапа

                    if (Math.Abs((double)sourceSize.X / (double)sourceSize.Y - 1.333333) <= 0.001)
                    {
                        sizeEnv = sourceSize.X / 4;
                    }
                    else
                    {
                        //!!!!
                        sizeEnv = MathEx.NextPowerOfTwo(sourceSize.X / 3);
                    }

                    if (sizeEnv == 0)
                    {
                        sizeEnv = 1;
                    }
                }
                else
                {
                    sizeEnv = specifiedSize;
                }

                int sizeIrr = 32;

                var destRealFileNameBase = VirtualPathUtility.GetRealPathByVirtual(destFileNameBase);
                var infoRealFileName     = VirtualPathUtility.GetRealPathByVirtual(infoFileName);
                var envRealFileName      = VirtualPathUtility.GetRealPathByVirtual(envFileName);
                var irrRealFileName      = VirtualPathUtility.GetRealPathByVirtual(irrFileName);

                //delete old files
                if (File.Exists(infoRealFileName))
                {
                    File.Delete(infoRealFileName);
                }
                if (File.Exists(envRealFileName))
                {
                    File.Delete(envRealFileName);
                }
                if (File.Exists(irrRealFileName))
                {
                    File.Delete(irrRealFileName);
                }

                var destDirectory = Path.GetDirectoryName(destRealFileNameBase);
                if (!Directory.Exists(destDirectory))
                {
                    Directory.CreateDirectory(destDirectory);
                }

                var irradianceValues = new List <Vector3>();

                if (!GenerateFile(sourceRealFileName, false, sizeEnv, envRealFileName, irradianceValues, out error))
                {
                    return(false);
                }
                if (!GenerateFile(sourceRealFileName, true, sizeIrr, irrRealFileName, null, out error))
                {
                    return(false);
                }

                //make .info file
                {
                    var block = new TextBlock();
                    block.SetAttribute("SourceFileHash", sourceFileHash);
                    block.SetAttribute("SourceFileSize", sourceFileSize.ToString());
                    //block.SetAttribute( "SourceFileFormat", sourceFileFormat.ToString() );

                    for (int n = 0; n < irradianceValues.Count; n++)
                    {
                        block.SetAttribute("Irradiance" + n.ToString(), irradianceValues[n].ToString());
                    }

                    if (!TextBlockUtility.SaveToRealFile(block, infoRealFileName, out error))
                    {
                        return(false);
                    }
                }
            }
            catch (Exception e)
            {
                error = e.Message;
                return(false);
            }
            finally
            {
                notification.Close();

                try
                {
                    if (File.Exists(temporaryGeneratedFile))
                    {
                        File.Delete(temporaryGeneratedFile);
                    }
                }
                catch { }
            }

            error = "";
            return(true);
        }
Ejemplo n.º 2
0
        public static bool Convert2DToDDS(string virtualFileName, string outputRealFileName, DDSImage.FormatEnum outputFormat, bool normalMap, bool generateMipmaps, out Vector2I sourceFileSize, out PixelFormat sourceFileFormat, out string error)
        {
            sourceFileSize   = Vector2I.Zero;
            sourceFileFormat = PixelFormat.Unknown;

            if (!ImageUtility.LoadFromVirtualFile(virtualFileName, out var data, out var size, out var depth, out var format, out var numFaces, out var numMipmaps, out error))
            {
                return(false);
            }

            sourceFileSize   = size;
            sourceFileFormat = format;

            byte[] rgba = new byte[size.X * size.Y * 4];

            if (format != PixelFormat.R8G8B8A8)
            {
                for (int y = 0; y < size.Y; y++)
                {
                    for (int x = 0; x < size.X; x++)
                    {
                        byte r;
                        byte g;
                        byte b;
                        byte a = 255;
                        int  offset;

                        switch (format)
                        {
                        case PixelFormat.R8G8B8:
                            offset = (y * size.X + x) * 3;
                            r      = data[offset + 2];
                            g      = data[offset + 1];
                            b      = data[offset + 0];
                            break;

                        case PixelFormat.X8R8G8B8:
                            offset = (y * size.X + x) * 4;
                            r      = data[offset + 2];
                            g      = data[offset + 1];
                            b      = data[offset + 0];
                            break;

                        case PixelFormat.A8R8G8B8:
                            offset = (y * size.X + x) * 4;
                            a      = data[offset + 3];
                            r      = data[offset + 2];
                            g      = data[offset + 1];
                            b      = data[offset + 0];
                            break;

                        case PixelFormat.L8:
                            offset = (y * size.X + x);
                            r      = g = b = data[offset];
                            break;

                        case PixelFormat.ShortRGB:
                            unsafe
                            {
                                fixed(byte *pData = data)
                                {
                                    ushort *pData2 = (ushort *)pData;

                                    offset = (y * size.X + x) * 3;
                                    r      = (byte)((float)pData2[offset + 0] / ushort.MaxValue * 255.0f);
                                    g      = (byte)((float)pData2[offset + 1] / ushort.MaxValue * 255.0f);
                                    b      = (byte)((float)pData2[offset + 2] / ushort.MaxValue * 255.0f);
                                }
                            }
                            break;

                        case PixelFormat.ShortRGBA:
                            unsafe
                            {
                                fixed(byte *pData = data)
                                {
                                    ushort *pData2 = (ushort *)pData;

                                    offset = (y * size.X + x) * 4;
                                    r      = (byte)((float)pData2[offset + 0] / ushort.MaxValue * 255.0f);
                                    g      = (byte)((float)pData2[offset + 1] / ushort.MaxValue * 255.0f);
                                    b      = (byte)((float)pData2[offset + 2] / ushort.MaxValue * 255.0f);
                                    a      = (byte)((float)pData2[offset + 3] / ushort.MaxValue * 255.0f);
                                }
                            }
                            break;

                        case PixelFormat.Float32RGB:
                            unsafe
                            {
                                fixed(byte *pData = data)
                                {
                                    float *pData2 = (float *)pData;

                                    offset = (y * size.X + x) * 3;
                                    r      = (byte)MathEx.Clamp((float)pData2[offset + 0] * 255.0f, 0.0f, 255.0f);
                                    g      = (byte)MathEx.Clamp((float)pData2[offset + 1] * 255.0f, 0.0f, 255.0f);
                                    b      = (byte)MathEx.Clamp((float)pData2[offset + 2] * 255.0f, 0.0f, 255.0f);
                                }
                            }
                            break;

                        case PixelFormat.Float32RGBA:
                            unsafe
                            {
                                fixed(byte *pData = data)
                                {
                                    float *pData2 = (float *)pData;

                                    offset = (y * size.X + x) * 4;
                                    r      = (byte)MathEx.Clamp((float)pData2[offset + 0] * 255.0f, 0.0f, 255.0f);
                                    g      = (byte)MathEx.Clamp((float)pData2[offset + 1] * 255.0f, 0.0f, 255.0f);
                                    b      = (byte)MathEx.Clamp((float)pData2[offset + 2] * 255.0f, 0.0f, 255.0f);
                                    a      = (byte)MathEx.Clamp((float)pData2[offset + 3] * 255.0f, 0.0f, 255.0f);
                                }
                            }
                            break;

                        default:
                            error = $"Conversion from \'{format}\' format is not supported.";
                            return(false);
                        }

                        //copy to rgba array
                        offset           = (y * size.X + x) * 4;
                        rgba[offset + 0] = r;
                        rgba[offset + 1] = g;
                        rgba[offset + 2] = b;
                        rgba[offset + 3] = a;
                    }
                }
            }

            var image = GenerateDDS(rgba, size, outputFormat, normalMap, generateMipmaps, out error);

            if (image == null)
            {
                return(false);
            }

            if (!WriteToFile(outputRealFileName, image, out error))
            {
                return(false);
            }

            error = "";
            return(true);

            //MapFormats mapFormat = textureData.normalMap ? NormalMapFormat : BaseMapFormat;

            //DDSImage.FormatTypes ddsFormat = DDSImage.FormatTypes.DXT1;
            //switch( mapFormat )
            //{
            //case MapFormats.DDS_DXT1:
            //	ddsFormat = DDSImage.FormatTypes.DXT1;
            //	break;
            //case MapFormats.DDS_DXT5:
            //	ddsFormat = DDSImage.FormatTypes.DXT5;
            //	break;
            //case MapFormats.DDS_3DC:
            //	ddsFormat = DDSImage.FormatTypes._3DC;
            //	break;
            //case MapFormats.DDS_ARGB:
            //	ddsFormat = DDSImage.FormatTypes.A8R8G8B8;
            //	break;
            //}

            //DDSImage ddsImage = DDSImageManager.Instance.Generate( rgba, size, ddsFormat, true );

            //bool oldExists = File.Exists( outputRealFullPath );
            //using( FileStream stream = new FileStream( outputRealFullPath, FileMode.Create ) )
            //{
            //	DDSImageManager.WriteFile( stream, ddsImage );
            //}
            //createdFiles.Add( new CreatedFileItem( outputRealFullPath, oldExists ) );

            //error = null;
            //return true;
        }