diff --git a/Unity Studio/Unity Classes/Texture2D.cs b/Unity Studio/Unity Classes/Texture2D.cs index 2a6ba14..ac15d8d 100644 --- a/Unity Studio/Unity Classes/Texture2D.cs +++ b/Unity Studio/Unity Classes/Texture2D.cs @@ -35,53 +35,55 @@ namespace Unity_Studio public string path; //DDS Start - public byte[] dwMagic = { 0x44, 0x44, 0x53, 0x20, 0x7c }; - public int dwFlags = 0x1 + 0x2 + 0x4 + 0x1000; + private byte[] dwMagic = { 0x44, 0x44, 0x53, 0x20, 0x7c }; + private int dwFlags = 0x1 + 0x2 + 0x4 + 0x1000; //public int dwHeight; m_Height //public int dwWidth; m_Width - public int dwPitchOrLinearSize = 0x0; - public int dwMipMapCount = 0x1; - public int dwSize = 0x20; - public int dwFlags2; - public int dwFourCC = 0x0; - public int dwRGBBitCount; - public int dwRBitMask; - public int dwGBitMask; - public int dwBBitMask; - public int dwABitMask; - public int dwCaps = 0x1000; - public int dwCaps2 = 0x0; + private int dwPitchOrLinearSize = 0x0; + private int dwMipMapCount = 0x1; + private int dwSize = 0x20; + private int dwFlags2; + private int dwFourCC = 0x0; + private int dwRGBBitCount; + private int dwRBitMask; + private int dwGBitMask; + private int dwBBitMask; + private int dwABitMask; + private int dwCaps = 0x1000; + private int dwCaps2 = 0x0; //DDS End //PVR Start - public int pvrVersion = 0x03525650; - public int pvrFlags = 0x0; - public long pvrPixelFormat; - public int pvrColourSpace = 0x0; - public int pvrChannelType = 0x0; + private int pvrVersion = 0x03525650; + private int pvrFlags = 0x0; + private long pvrPixelFormat; + private int pvrColourSpace = 0x0; + private int pvrChannelType = 0x0; //public int pvrHeight; m_Height //public int pvrWidth; m_Width - public int pvrDepth = 0x1; - public int pvrNumSurfaces = 0x1; //For texture arrays - public int pvrNumFaces = 0x1; //For cube maps + private int pvrDepth = 0x1; + private int pvrNumSurfaces = 0x1; //For texture arrays + private int pvrNumFaces = 0x1; //For cube maps //public int pvrMIPMapCount; dwMipMapCount - public int pvrMetaDataSize = 0x0; + private int pvrMetaDataSize = 0x0; //PVR End //KTX Start - public int glType = 0; - public int glTypeSize = 1; - public int glFormat = 0; - public int glInternalFormat; - public int glBaseInternalFormat; + private int glType = 0; + private int glTypeSize = 1; + private int glFormat = 0; + private int glInternalFormat; + private int glBaseInternalFormat; //public int pixelWidth; m_Width //public int pixelHeight; m_Height - public int pixelDepth = 0; - public int numberOfArrayElements = 0; - public int numberOfFaces = 1; - public int numberOfMipmapLevels = 1; - public int bytesOfKeyValueData = 0; + private int pixelDepth = 0; + private int numberOfArrayElements = 0; + private int numberOfFaces = 1; + private int numberOfMipmapLevels = 1; + private int bytesOfKeyValueData = 0; //KTX End //TextureConverter - public QFORMAT q_format; + private QFORMAT q_format; + //texgenpack + private texgenpack_texturetype texturetype; [DllImport("PVRTexLibWrapper.dll", CallingConvention = CallingConvention.Cdecl)] private static extern bool DecompressPVR(byte[] buffer, IntPtr bmp, int len); @@ -89,6 +91,12 @@ namespace Unity_Studio [DllImport("TextureConverterWrapper.dll", CallingConvention = CallingConvention.Cdecl)] private static extern bool Ponvert(byte[] buffer, IntPtr bmp, int nWidth, int nHeight, int len, int type, int bmpsize, bool fixAlpha); + [DllImport("crunch.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern bool DecompressCRN(byte[] pSrc_file_data, int src_file_size, out IntPtr dxtdata, out int dxtsize); + + [DllImport("texgenpack.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern bool texgenpackdecode(int texturetype, byte[] texturedata, int width, int height, IntPtr bmp, bool fixAlpha); + public Texture2D(AssetPreloadData preloadData, bool readSwitch) { var sourceFile = preloadData.sourceFile; @@ -492,30 +500,37 @@ namespace Unity_Studio } case TextureFormat.BC4: { + texturetype = texgenpack_texturetype.RGTC1; glInternalFormat = KTXHeader.GL_COMPRESSED_RED_RGTC1; glBaseInternalFormat = KTXHeader.GL_RED; break; } case TextureFormat.BC5: { + texturetype = texgenpack_texturetype.RGTC2; glInternalFormat = KTXHeader.GL_COMPRESSED_RG_RGTC2; glBaseInternalFormat = KTXHeader.GL_RG; break; } case TextureFormat.BC6H: { + texturetype = texgenpack_texturetype.BPTC_FLOAT; glInternalFormat = KTXHeader.GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT; glBaseInternalFormat = KTXHeader.GL_RGB; break; } case TextureFormat.BC7: { + texturetype = texgenpack_texturetype.BPTC; glInternalFormat = KTXHeader.GL_COMPRESSED_RGBA_BPTC_UNORM; glBaseInternalFormat = KTXHeader.GL_RGBA; break; } case TextureFormat.DXT1Crunched: //DXT1 Crunched + q_format = QFORMAT.Q_FORMAT_S3TC_DXT1_RGB; + break; case TextureFormat.DXT5Crunched: //DXT1 Crunched + q_format = QFORMAT.Q_FORMAT_S3TC_DXT5_RGBA; break; case TextureFormat.PVRTC_RGB2: //test pass { @@ -952,11 +967,11 @@ namespace Unity_Studio case TextureFormat.BC5: case TextureFormat.BC6H: case TextureFormat.BC7: - //texgenpack + bitmap = Texgenpack(); break; case TextureFormat.DXT1Crunched: case TextureFormat.DXT5Crunched: - //crunch + bitmap = CRNToBitmap(); break; } if (bitmap != null && flip) @@ -1014,6 +1029,33 @@ namespace Unity_Studio bitmap.UnlockBits(bmd); return bitmap; } + + private Bitmap CRNToBitmap() + { + IntPtr dxtdata; + int dxtsize; + if (DecompressCRN(image_data, image_data_size, out dxtdata, out dxtsize)) + { + var dxtbytes = new byte[dxtsize]; + Marshal.Copy(dxtdata, dxtbytes, 0, dxtsize); + Marshal.FreeHGlobal(dxtdata); + image_data = dxtbytes; + image_data_size = dxtsize; + return TextureConverter(); + } + return null; + } + + private Bitmap Texgenpack() + { + var bitmap = new Bitmap(m_Width, m_Height); + var rect = new Rectangle(0, 0, m_Width, m_Height); + var bmd = bitmap.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); + var fixAlpha = glBaseInternalFormat == KTXHeader.GL_RED || glBaseInternalFormat == KTXHeader.GL_RG; + texgenpackdecode((int)texturetype, image_data, m_Width, m_Height, bmd.Scan0, fixAlpha); + bitmap.UnlockBits(bmd); + return bitmap; + } } } @@ -1246,4 +1288,12 @@ public enum QFORMAT // ASTC Format Q_FORMAT_ASTC_8, Q_FORMAT_ASTC_16, -}; \ No newline at end of file +}; + +public enum texgenpack_texturetype +{ + RGTC1, + RGTC2, + BPTC_FLOAT, + BPTC +} \ No newline at end of file diff --git a/Unity Studio/library/crunch_license.txt b/Unity Studio/library/crunch_license.txt new file mode 100644 index 0000000..1b30d78 --- /dev/null +++ b/Unity Studio/library/crunch_license.txt @@ -0,0 +1,22 @@ +crunch/crnlib uses the ZLIB license: +http://opensource.org/licenses/Zlib + +Copyright (c) 2010-2016 Richard Geldreich, Jr. and Binomial LLC + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any damages +arising from the use of this software. + +Permission is granted to anyone to use this software for any purpose, +including commercial applications, and to alter it and redistribute it +freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must not +claim that you wrote the original software. If you use this software +in a product, an acknowledgment in the product documentation would be +appreciated but is not required. + +2. Altered source versions must be plainly marked as such, and must not be +misrepresented as being the original software. + +3. This notice may not be removed or altered from any source distribution. diff --git a/Unity Studio/library/texgenpack_license.txt b/Unity Studio/library/texgenpack_license.txt new file mode 100644 index 0000000..08d2fed --- /dev/null +++ b/Unity Studio/library/texgenpack_license.txt @@ -0,0 +1,13 @@ +Copyright (c) 2015 Harm Hanemaaijer + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. \ No newline at end of file diff --git a/Unity Studio/library/x64/crunch.dll b/Unity Studio/library/x64/crunch.dll new file mode 100644 index 0000000..22595b4 Binary files /dev/null and b/Unity Studio/library/x64/crunch.dll differ diff --git a/Unity Studio/library/x64/texgenpack.dll b/Unity Studio/library/x64/texgenpack.dll new file mode 100644 index 0000000..05db02d Binary files /dev/null and b/Unity Studio/library/x64/texgenpack.dll differ diff --git a/Unity Studio/library/x86/crunch.dll b/Unity Studio/library/x86/crunch.dll new file mode 100644 index 0000000..f89cd57 Binary files /dev/null and b/Unity Studio/library/x86/crunch.dll differ diff --git a/Unity Studio/library/x86/texgenpack.dll b/Unity Studio/library/x86/texgenpack.dll new file mode 100644 index 0000000..b258531 Binary files /dev/null and b/Unity Studio/library/x86/texgenpack.dll differ