Exemple #1
0
        private static void createTextureFormats()
        {
            List <TextureFormatInfo> textureFormats = new List <TextureFormatInfo>();

            //DirectDraw Surface (*.dds)
            TextureFormatInfo textureFormat = null;

            textureFormat           = new TextureFormatInfo();
            textureFormat.Name      = "DirectDraw Surface";
            textureFormat.Extension = "dds";
            textureFormat.ImageType = ImageType.Dds;
            textureFormats.Add(textureFormat);

            //Portal Network Graphics (*.png)
            textureFormat           = new TextureFormatInfo();
            textureFormat.Name      = "Portable Network Graphics";
            textureFormat.Extension = "png";
            textureFormat.ImageType = ImageType.Png;
            textureFormats.Add(textureFormat);

            //Truevision TGA (*.tga)
            textureFormat           = new TextureFormatInfo();
            textureFormat.Name      = "Truevision TGA";
            textureFormat.Extension = "tga";
            textureFormat.ImageType = ImageType.Tga;
            textureFormats.Add(textureFormat);

            TextureFormats = textureFormats.ToArray();
        }
Exemple #2
0
        public void AddTextureFormat(Texture2D tx, bool isNormalMap)
        {
            //pixel values don't copy correctly from one texture to another when isNormal is set so unset it.
            TextureFormatInfo toFormat = new TextureFormatInfo(TextureImporterCompression.Uncompressed, false, MBVersionEditor.GetPlatformString(), TextureImporterFormat.RGBA32, isNormalMap);

            SetTextureFormat(tx, toFormat, true, false);
        }
Exemple #3
0
        void SetTextureFormat(Texture2D tx, TextureFormatInfo toThisFormat, bool addToList, bool setNormalMap)
        {
            AssetImporter ai = AssetImporter.GetAtPath(AssetDatabase.GetAssetOrScenePath(tx));

            if (ai != null && ai is UnityEditor.TextureImporter)
            {
                TextureImporter textureImporter = (TextureImporter)ai;
                bool            doImport        = false;

                bool is2017 = Application.unityVersion.StartsWith("2017");
                if (is2017)
                {
                    doImport = _SetTextureFormat2017(tx, toThisFormat, addToList, setNormalMap, textureImporter);
                }
                else
                {
                    doImport = _SetTextureFormatUnity5(tx, toThisFormat, addToList, setNormalMap, textureImporter);
                }
                if (textureImporter.textureType == TextureImporterType.NormalMap && !setNormalMap)
                {
                    textureImporter.textureType = TextureImporterType.Default;
                    doImport = true;
                }

                if (textureImporter.textureType != TextureImporterType.NormalMap && setNormalMap)
                {
                    textureImporter.textureType = TextureImporterType.NormalMap;
                    doImport = true;
                }
                if (doImport)
                {
                    AssetDatabase.ImportAsset(AssetDatabase.GetAssetOrScenePath(tx), ImportAssetOptions.ForceUpdate);
                }
            }
        }
Exemple #4
0
        private static void createTextureFormats()
        {
            List<TextureFormatInfo> textureFormats = new List<TextureFormatInfo>();

            //DirectDraw Surface (*.dds)
            TextureFormatInfo textureFormat = null;

            textureFormat = new TextureFormatInfo();
            textureFormat.Name = "DirectDraw Surface";
            textureFormat.Extension = "dds";
            textureFormat.ImageType = ImageType.Dds;
            textureFormats.Add(textureFormat);

            //Portal Network Graphics (*.png)
            textureFormat = new TextureFormatInfo();
            textureFormat.Name = "Portable Network Graphics";
            textureFormat.Extension = "png";
            textureFormat.ImageType = ImageType.Png;
            textureFormats.Add(textureFormat);

            //Truevision TGA (*.tga)
            textureFormat = new TextureFormatInfo();
            textureFormat.Name = "Truevision TGA";
            textureFormat.Extension = "tga";
            textureFormat.ImageType = ImageType.Tga;
            textureFormats.Add(textureFormat);

            TextureFormats = textureFormats.ToArray();
        }
Exemple #5
0
 private static int GetPitchOrLinearSize(InternalFormat format, int width)
 {
     if (format.ToString().Contains("Compressed"))
     {
         return(Math.Max(1, ((width + 3) / 4)) * (int)TextureFormatInfo.GetBPP(format));
     }
     return((width * (int)TextureFormatInfo.GetBPP(format) + 7) / 8);
 }
Exemple #6
0
        bool _SetTextureFormatUnity5(Texture2D tx, TextureFormatInfo toThisFormat, bool addToList, bool setNormalMap, TextureImporter textureImporter)
        {
            bool doImport = false;

            TextureFormatInfo restoreTfi = new TextureFormatInfo(textureImporter.textureCompression,
                                                                 false,
                                                                 toThisFormat.platform,
                                                                 TextureImporterFormat.RGBA32,
                                                                 textureImporter.textureType == TextureImporterType.NormalMap);

            string platform = toThisFormat.platform;

            if (platform != null)
            {
                TextureImporterPlatformSettings tips = textureImporter.GetPlatformTextureSettings(platform);
                if (tips.overridden)
                {
                    restoreTfi.platformFormat             = tips.format;
                    restoreTfi.platformCompressionQuality = tips.compressionQuality;
                    TextureImporterPlatformSettings tipsOverridden = new TextureImporterPlatformSettings();
                    tips.CopyTo(tipsOverridden);
                    tipsOverridden.compressionQuality = toThisFormat.platformCompressionQuality;
                    tipsOverridden.format             = toThisFormat.platformFormat;
                    textureImporter.SetPlatformTextureSettings(tipsOverridden);
                    doImport = true;
                }
            }

            if (textureImporter.textureCompression != toThisFormat.compression)
            {
                textureImporter.textureCompression = toThisFormat.compression;
                doImport = true;
            }

            if (doImport)
            {
                string s;
                if (addToList)
                {
                    s = "Setting texture compression for ";
                }
                else
                {
                    s = "Restoring texture compression for ";
                }
                s += String.Format("{0}  FROM: compression={1} isNormal{2} TO: compression={3} isNormal={4} ", tx, restoreTfi.compression, restoreTfi.isNormalMap, toThisFormat.compression, setNormalMap);
                if (toThisFormat.platform != null)
                {
                    s += String.Format(" setting platform override format for platform {0} to {1} compressionQuality {2}", toThisFormat.platform, toThisFormat.platformFormat, toThisFormat.platformCompressionQuality);
                }
                Debug.Log(s);
            }
            if (doImport && addToList && !_textureFormatMap.ContainsKey(tx))
            {
                _textureFormatMap.Add(tx, restoreTfi);
            }
            return(doImport);
        }
		void SetTextureFormat(Texture2D tx, TextureFormatInfo tfi, bool addToList, bool setNormalMap){	
			
			AssetImporter ai = AssetImporter.GetAtPath( AssetDatabase.GetAssetOrScenePath(tx) );
			if (ai != null && ai is UnityEditor.TextureImporter){
				string s;
				if (addToList){
					s = "Setting texture format for ";
				} else {
					s = "Restoring texture format for ";
				}
				s += tx + " to " + tfi.format;
				if (tfi.platform != null){
					s += " setting platform override format for " + tfi.platform + " to " + tfi.platformOverrideFormat;
				}
				Debug.Log(s);
				TextureImporter textureImporter = (TextureImporter) ai;
				TextureFormatInfo restoreTfi = new TextureFormatInfo(textureImporter.textureFormat, 
																	tfi.platform, 
																	TextureImporterFormat.AutomaticTruecolor, 
																	textureImporter.normalmap);
				string platform = tfi.platform;
				bool doImport = false;
				if (platform != null){
					int maxSize;
					TextureImporterFormat f;						
					textureImporter.GetPlatformTextureSettings(platform, out maxSize, out f);
					restoreTfi.platformOverrideFormat = f;
					if (f != 0){ //f == 0 means no override or platform doesn't exist
						textureImporter.SetPlatformTextureSettings(platform, maxSize, tfi.platformOverrideFormat);	
						doImport = true;
					}
				}
						
				if (textureImporter.textureFormat != tfi.format){
					textureImporter.textureFormat = tfi.format;
					doImport = true;
				}
				if (textureImporter.normalmap && !setNormalMap){
					textureImporter.normalmap = false;
					doImport = true;
				}
				if (!textureImporter.normalmap && setNormalMap){
					textureImporter.normalmap = true;
					doImport = true;					
				}
				if (addToList && !_textureFormatMap.ContainsKey(tx)) _textureFormatMap.Add(tx, restoreTfi);			
				if (doImport) AssetDatabase.ImportAsset(AssetDatabase.GetAssetOrScenePath(tx), ImportAssetOptions.ForceUpdate);
			}
		}
Exemple #8
0
        void SetTextureFormat(Texture2D tx, TextureFormatInfo tfi, bool addToList, bool setNormalMap)
        {
            AssetImporter ai = AssetImporter.GetAtPath(AssetDatabase.GetAssetOrScenePath(tx));

            if (ai != null && ai is UnityEditor.TextureImporter)
            {
                string s;
                if (addToList)
                {
                    s = "Setting texture format for ";
                }
                else
                {
                    s = "Restoring texture format for ";
                }
                s += tx + " to " + tfi.format;
                if (tfi.platform != null)
                {
                    s += " setting platform override format for " + tfi.platform + " to " + tfi.platformOverrideFormat;
                }
                Debug.Log(s);
                TextureImporter   textureImporter = (TextureImporter)ai;
                TextureFormatInfo restoreTfi      = new TextureFormatInfo(textureImporter.textureFormat,
                                                                          tfi.platform,
                                                                          TextureImporterFormat.AutomaticTruecolor,
                                                                          textureImporter.normalmap);
                string platform = tfi.platform;
                bool   doImport = false;
                if (platform != null)
                {
                    int maxSize;
                    TextureImporterFormat f;
                    textureImporter.GetPlatformTextureSettings(platform, out maxSize, out f);
                    restoreTfi.platformOverrideFormat = f;
                    if (f != 0)
                    { //f == 0 means no override or platform doesn't exist
                        textureImporter.SetPlatformTextureSettings(platform, maxSize, tfi.platformOverrideFormat);
                        doImport = true;
                    }
                }

                if (textureImporter.textureFormat != tfi.format)
                {
                    textureImporter.textureFormat = tfi.format;
                    doImport = true;
                }
                if (textureImporter.normalmap && !setNormalMap)
                {
                    textureImporter.normalmap = false;
                    doImport = true;
                }
                if (!textureImporter.normalmap && setNormalMap)
                {
                    textureImporter.normalmap = true;
                    doImport = true;
                }
                if (addToList && !_textureFormatMap.ContainsKey(tx))
                {
                    _textureFormatMap.Add(tx, restoreTfi);
                }
                if (doImport)
                {
                    AssetDatabase.ImportAsset(AssetDatabase.GetAssetOrScenePath(tx), ImportAssetOptions.ForceUpdate);
                }
            }
        }
Exemple #9
0
        bool _SetTextureFormat2017(Texture2D tx, TextureFormatInfo toThisFormat, bool addToList, bool setNormalMap, TextureImporter textureImporter)
        {
            bool is2017 = Application.unityVersion.StartsWith("20");

            if (!is2017)
            {
                Debug.LogError("Wrong texture format converter. 2017 Should not be called for Unity Version " + Application.unityVersion);
                return(false);
            }

            bool doImport = false;
            TextureFormatInfo restoreTfi = new TextureFormatInfo(textureImporter.textureCompression,
                                                                 textureImporter.crunchedCompression,
                                                                 toThisFormat.platform,
                                                                 TextureImporterFormat.RGBA32,
                                                                 textureImporter.textureType == TextureImporterType.NormalMap);
            string platform = toThisFormat.platform;

            if (platform != null)
            {
                TextureImporterPlatformSettings tips = textureImporter.GetPlatformTextureSettings(platform);
                if (tips.overridden)
                {
                    restoreTfi.platformFormat             = tips.format;
                    restoreTfi.platformCompressionQuality = tips.compressionQuality;
                    restoreTfi.doCrunchCompression        = tips.crunchedCompression;
                    restoreTfi.isNormalMap = textureImporter.textureType == TextureImporterType.NormalMap;
                    TextureImporterPlatformSettings tipsOverridden = new TextureImporterPlatformSettings();
                    tips.CopyTo(tipsOverridden);
                    tipsOverridden.compressionQuality  = toThisFormat.platformCompressionQuality;
                    tipsOverridden.crunchedCompression = toThisFormat.doCrunchCompression;
                    tipsOverridden.format = toThisFormat.platformFormat;
                    textureImporter.SetPlatformTextureSettings(tipsOverridden);
                    doImport = true;
                }
            }

            if (textureImporter.crunchedCompression != toThisFormat.doCrunchCompression)
            {
                textureImporter.crunchedCompression = toThisFormat.doCrunchCompression;
                doImport = true;
            }
            if (_ChangeNormalMapTypeIfNecessary(textureImporter, setNormalMap))
            {
                doImport = true;
            }

            if (doImport)
            {
                string s;
                if (addToList)
                {
                    s = "Setting texture compression for ";
                }
                else
                {
                    s = "Restoring texture compression for ";
                }
                s += String.Format("{0}  FROM: compression={1} isNormal{2} TO: compression={3} isNormal={4} ", tx, restoreTfi.compression, restoreTfi.isNormalMap, toThisFormat.compression, setNormalMap);
                if (toThisFormat.platform != null)
                {
                    s += String.Format(" setting platform override format for platform {0} to {1} compressionQuality {2}", toThisFormat.platform, toThisFormat.platformFormat, toThisFormat.platformCompressionQuality);
                }
                Debug.Log(s);
                if (doImport && addToList && !_textureFormatMap.ContainsKey(tx))
                {
                    _textureFormatMap.Add(tx, restoreTfi);
                }
            }
            return(doImport);
        }
Exemple #10
0
        public static SBSurface Import(string FileName)
        {
            SBSurface surface = new SBSurface();

            using (BinaryReaderExt reader = new BinaryReaderExt(new FileStream(FileName, FileMode.Open)))
            {
                DDS_Header header = new DDS_Header();
                header.Read(reader);

                surface.Name   = Path.GetFileNameWithoutExtension(FileName);
                surface.Width  = header.dwWidth;
                surface.Height = header.dwHeight;
                if (header.dwFlags.HasFlag(DDSD.DEPTH))
                {
                    surface.Depth = header.dwDepth;
                }
                else
                {
                    surface.Depth = 1;
                }

                if (header.ddspf.dwFourCC == 0x31545844)
                {
                    surface.InternalFormat = InternalFormat.CompressedRgbaS3tcDxt1Ext;
                }
                else
                if (header.ddspf.dwFourCC == 0x30315844)
                {
                    surface.InternalFormat = DXGItoInternal(header.DXT10Header.dxgiFormat);
                    if (surface.InternalFormat == 0)
                    {
                        System.Windows.Forms.MessageBox.Show("DDS format not supported " + header.DXT10Header.dxgiFormat);

                        return(null);
                    }
                }
                else
                {
                    if (((FourCC_DXGI)header.ddspf.dwFourCC) == FourCC_DXGI.D3DFMT_A32B32G32R32F)
                    {
                        surface.InternalFormat = InternalFormat.Rgba32f;
                        surface.PixelFormat    = PixelFormat.Rgba;
                        surface.PixelType      = PixelType.Float;
                    }
                    else
                    {
                        System.Windows.Forms.MessageBox.Show("DDS format not supported " + header.ddspf.dwFourCC.ToString("X"));
                        return(null);
                    }
                }


                // TODO: read other mips
                int w = surface.Width;
                int h = surface.Height;
                //SBConsole.WriteLine(header.dwCaps.ToString() + " " + header.dwCaps2.ToString() + " " + header.dwFlags.ToString());
                for (int array = 0; array < (header.dwCaps2.HasFlag(DDSCAPS2.CUBEMAP_ALLFACES) ? 6 : 1); array++)
                {
                    w = surface.Width;
                    h = surface.Height;
                    var mip = new MipArray();

                    for (int i = 0; i < (header.dwFlags.HasFlag(DDSD.MIPMAPCOUNT) ? header.dwMipMapCount : 1); i++)
                    {
                        var mipSize = Math.Max(1, ((w + 3) / 4)) * Math.Max(1, ((h + 3) / 4)) * (int)TextureFormatInfo.GetBPP(surface.InternalFormat);

                        if (mipSize % TextureFormatInfo.GetBPP(surface.InternalFormat) != 0)
                        {
                            mipSize += (int)(TextureFormatInfo.GetBPP(surface.InternalFormat) - (mipSize % TextureFormatInfo.GetBPP(surface.InternalFormat)));
                        }

                        var data = reader.ReadBytes(mipSize);

                        mip.Mipmaps.Add(data);
                        w /= 2;
                        h /= 2;
                    }

                    surface.Arrays.Add(mip);
                }
                if (reader.Position != reader.BaseStream.Length)
                {
                    SBConsole.WriteLine("Warning: error reading dds " + reader.Position.ToString("X"));
                }
            }



            return(surface);
        }
Exemple #11
0
        void SetTextureFormat(Texture2D tx, TextureFormatInfo toThisFormat, bool addToList, bool setNormalMap)
        {
            AssetImporter ai = AssetImporter.GetAtPath(AssetDatabase.GetAssetOrScenePath(tx));

            if (ai != null && ai is UnityEditor.TextureImporter)
            {
                TextureImporter textureImporter = (TextureImporter)ai;
                bool            doImport        = false;

                TextureFormatInfo restoreTfi = new TextureFormatInfo(textureImporter.textureCompression,
                                                                     toThisFormat.platform,
                                                                     TextureImporterFormat.RGBA32,
                                                                     textureImporter.textureType == TextureImporterType.NormalMap);

                bool is2017 = Application.unityVersion.StartsWith("2017");
                if (is2017)
                {
                    //don't do any of this for 2017
                }
                else
                {
                    string s;
                    if (addToList)
                    {
                        s = "Setting texture compression for ";
                    }
                    else
                    {
                        s = "Restoring texture compression for ";
                    }
                    s += String.Format("{0}  to compression={1} isNormal={2} ", tx, toThisFormat.compression, setNormalMap);
                    if (toThisFormat.platform != null)
                    {
                        s += String.Format(" setting platform override format for platform {0} to {1} compressionQuality {2}", toThisFormat.platform, toThisFormat.platformFormat, toThisFormat.platformCompressionQuality);
                    }
                    Debug.Log(s);
                    string platform = toThisFormat.platform;
                    if (platform != null)
                    {
                        TextureImporterPlatformSettings tips = textureImporter.GetPlatformTextureSettings(platform);
                        if (tips.overridden)
                        {
                            restoreTfi.platformFormat             = tips.format;
                            restoreTfi.platformCompressionQuality = tips.compressionQuality;
                            TextureImporterPlatformSettings tipsOverridden = new TextureImporterPlatformSettings();
                            tips.CopyTo(tipsOverridden);
                            tipsOverridden.compressionQuality = toThisFormat.platformCompressionQuality;
                            tipsOverridden.format             = toThisFormat.platformFormat;
                            textureImporter.SetPlatformTextureSettings(tipsOverridden);
                            doImport = true;
                        }
                    }
                    if (textureImporter.textureCompression != toThisFormat.compression)
                    {
                        textureImporter.textureCompression = toThisFormat.compression;
                        doImport = true;
                    }
                }
                if (textureImporter.textureType == TextureImporterType.NormalMap && !setNormalMap)
                {
                    textureImporter.textureType = TextureImporterType.Default;
                    doImport = true;
                }

                if (textureImporter.textureType != TextureImporterType.NormalMap && setNormalMap)
                {
                    textureImporter.textureType = TextureImporterType.NormalMap;
                    doImport = true;
                }
                if (addToList && !_textureFormatMap.ContainsKey(tx))
                {
                    _textureFormatMap.Add(tx, restoreTfi);
                }
                if (doImport)
                {
                    AssetDatabase.ImportAsset(AssetDatabase.GetAssetOrScenePath(tx), ImportAssetOptions.ForceUpdate);
                }
            }
        }