예제 #1
0
        private static Color32[] LoadRawTGAData(BinaryReader r, int bitDepth, int width, int height)
        {
            Color32[] pulledColors = new Color32[width * height];

            byte[] colorData = r.ReadBytes(width * height * (bitDepth / 8));
            ImageLoaderHelper.FillPixelArray(pulledColors, colorData, (bitDepth / 8), true);

            return(pulledColors);
        }
예제 #2
0
        /// <summary>
        /// Loads a texture from a file
        /// </summary>
        /// <param name="fn"></param>
        /// <param name="normalMap"></param>
        /// <returns></returns>
        public static Texture2D LoadTexture(string fn)
        {
            if (!File.Exists(fn))
            {
                return(null);
            }

            var       textureBytes = File.ReadAllBytes(fn);
            string    ext          = Path.GetExtension(fn).ToLower();
            string    name         = Path.GetFileName(fn);
            Texture2D returnTex    = null;

            switch (ext)
            {
            case ".png":
            case ".jpg":
            case ".jpeg":
                returnTex = new Texture2D(1, 1);
                returnTex.LoadImage(textureBytes);
                returnTex = ImageLoaderHelper.VerifyFormat(returnTex);
                break;

            case ".dds":
                returnTex = DDSLoader.Load(textureBytes);
                break;

            case ".tga":
                returnTex = TGALoader.Load(textureBytes);
                break;

            case ".bmp":
                returnTex = new BMPLoader().LoadBMP(textureBytes).ToTexture2D();
                break;

            case ".crn":
                byte[] crnBytes = textureBytes;
                ushort crnWidth = System.BitConverter.ToUInt16(new byte[2] {
                    crnBytes[13], crnBytes[12]
                }, 0);
                ushort crnHeight = System.BitConverter.ToUInt16(new byte[2] {
                    crnBytes[15], crnBytes[14]
                }, 0);
                byte crnFormatByte = crnBytes[18];

                var crnTextureFormat = UnityEngine.TextureFormat.RGB24;
                if (crnFormatByte == 0)
                {
                    crnTextureFormat = UnityEngine.TextureFormat.DXT1Crunched;
                }
                else if (crnFormatByte == 2)
                {
                    crnTextureFormat = UnityEngine.TextureFormat.DXT5Crunched;
                }
                else if (crnFormatByte == 12)
                {
                    crnTextureFormat = UnityEngine.TextureFormat.ETC2_RGBA8Crunched;
                }
                else
                {
                    Debug.LogError("Could not load crunched texture " + name + " because its format is not supported (" + crnFormatByte + "): " + fn);
                    break;
                }

                returnTex = new Texture2D(crnWidth, crnHeight, crnTextureFormat, true);
                returnTex.LoadRawTextureData(crnBytes);
                returnTex.Apply(true);

                break;

            default:
                Debug.LogError("Could not load texture " + name + " because its format is not supported : " + fn);
                break;
            }

            if (returnTex != null)
            {
                returnTex.name = Path.GetFileNameWithoutExtension(fn);
            }

            return(returnTex);
        }