| | 1 | | /* |
| | 2 | | (C) 2015 AARO4130 |
| | 3 | | PARTS OF TGA LOADING CODE (C) 2013 mikezila |
| | 4 | | DO NOT USE PARTS OF, OR THE ENTIRE SCRIPT, AND CLAIM AS YOUR OWN WORK |
| | 5 | | */ |
| | 6 | |
|
| | 7 | | using System; |
| | 8 | | using System.IO; |
| | 9 | | using UnityEngine; |
| | 10 | |
|
| | 11 | | public class TextureLoader : MonoBehaviour |
| | 12 | | { |
| | 13 | | public static Texture2D LoadTGA(string fileName) |
| | 14 | | { |
| 0 | 15 | | using (var imageFile = File.OpenRead(fileName)) |
| | 16 | | { |
| 0 | 17 | | return LoadTGA(imageFile); |
| | 18 | | } |
| 0 | 19 | | } |
| | 20 | |
|
| | 21 | | public static Texture2D LoadDDSManual(string ddsPath) |
| | 22 | | { |
| | 23 | | try |
| | 24 | | { |
| | 25 | |
|
| 0 | 26 | | byte[] ddsBytes = File.ReadAllBytes(ddsPath); |
| | 27 | |
|
| 0 | 28 | | byte ddsSizeCheck = ddsBytes[4]; |
| 0 | 29 | | if (ddsSizeCheck != 124) |
| | 30 | | { |
| 0 | 31 | | throw new System.Exception("Invalid DDS DXTn texture. Unable to read"); //this header byte should be 124 |
| | 32 | | } |
| | 33 | |
|
| 0 | 34 | | int height = ddsBytes[13] * 256 + ddsBytes[12]; |
| 0 | 35 | | int width = ddsBytes[17] * 256 + ddsBytes[16]; |
| | 36 | |
|
| 0 | 37 | | byte DXTType = ddsBytes[87]; |
| 0 | 38 | | TextureFormat textureFormat = TextureFormat.DXT5; |
| 0 | 39 | | if (DXTType == 49) |
| | 40 | | { |
| 0 | 41 | | textureFormat = TextureFormat.DXT1; |
| | 42 | | // Debug.Log ("DXT1"); |
| | 43 | | } |
| | 44 | |
|
| 0 | 45 | | if (DXTType == 53) |
| | 46 | | { |
| 0 | 47 | | textureFormat = TextureFormat.DXT5; |
| | 48 | | // Debug.Log ("DXT5"); |
| | 49 | | } |
| 0 | 50 | | int DDS_HEADER_SIZE = 128; |
| 0 | 51 | | byte[] dxtBytes = new byte[ddsBytes.Length - DDS_HEADER_SIZE]; |
| 0 | 52 | | Buffer.BlockCopy(ddsBytes, DDS_HEADER_SIZE, dxtBytes, 0, ddsBytes.Length - DDS_HEADER_SIZE); |
| | 53 | |
|
| 0 | 54 | | System.IO.FileInfo finf = new System.IO.FileInfo(ddsPath); |
| 0 | 55 | | Texture2D texture = new Texture2D(width, height, textureFormat, false); |
| 0 | 56 | | texture.LoadRawTextureData(dxtBytes); |
| 0 | 57 | | texture.Apply(); |
| 0 | 58 | | texture.name = finf.Name; |
| | 59 | |
|
| 0 | 60 | | return (texture); |
| | 61 | | } |
| 0 | 62 | | catch (System.Exception ex) |
| | 63 | | { |
| 0 | 64 | | Debug.LogError("Error: Could not load DDS " + ex.ToString()); |
| 0 | 65 | | return new Texture2D(8, 8); |
| | 66 | | } |
| 0 | 67 | | } |
| | 68 | |
|
| | 69 | | public static void SetNormalMap(ref Texture2D tex) |
| | 70 | | { |
| 0 | 71 | | Color[] pixels = tex.GetPixels(); |
| 0 | 72 | | for (int i = 0; i < pixels.Length; i++) |
| | 73 | | { |
| 0 | 74 | | Color temp = pixels[i]; |
| 0 | 75 | | temp.r = pixels[i].g; |
| 0 | 76 | | temp.a = pixels[i].r; |
| 0 | 77 | | pixels[i] = temp; |
| | 78 | | } |
| 0 | 79 | | tex.SetPixels(pixels); |
| 0 | 80 | | } |
| | 81 | | public static Texture2D LoadTexture(string fn, bool normalMap = false) |
| | 82 | | { |
| 0 | 83 | | if (!File.Exists(fn)) |
| | 84 | | { |
| 0 | 85 | | return null; |
| | 86 | | } |
| | 87 | |
|
| 0 | 88 | | string ext = Path.GetExtension(fn).ToLower(); |
| 0 | 89 | | if (ext == ".png" || ext == ".jpg") |
| | 90 | | { |
| 0 | 91 | | Texture2D t2d = new Texture2D(1, 1); |
| 0 | 92 | | t2d.LoadImage(File.ReadAllBytes(fn)); |
| 0 | 93 | | if (normalMap) |
| | 94 | | { |
| 0 | 95 | | SetNormalMap(ref t2d); |
| | 96 | | } |
| | 97 | |
|
| 0 | 98 | | return t2d; |
| | 99 | | } |
| 0 | 100 | | else if (ext == ".dds") |
| | 101 | | { |
| 0 | 102 | | Texture2D returnTex = LoadDDSManual(fn); |
| 0 | 103 | | if (normalMap) |
| | 104 | | { |
| 0 | 105 | | SetNormalMap(ref returnTex); |
| | 106 | | } |
| | 107 | |
|
| 0 | 108 | | return returnTex; |
| | 109 | | } |
| 0 | 110 | | else if (ext == ".tga") |
| | 111 | | { |
| 0 | 112 | | Texture2D returnTex = LoadTGA(fn); |
| 0 | 113 | | if (normalMap) |
| | 114 | | { |
| 0 | 115 | | SetNormalMap(ref returnTex); |
| | 116 | | } |
| | 117 | |
|
| 0 | 118 | | return returnTex; |
| | 119 | | } |
| | 120 | | else |
| | 121 | | { |
| 0 | 122 | | Debug.Log("texture not supported : " + fn); |
| | 123 | | } |
| 0 | 124 | | return null; |
| | 125 | | } |
| | 126 | |
|
| | 127 | | public static Texture2D LoadTGA(Stream TGAStream) |
| | 128 | | { |
| | 129 | |
|
| 0 | 130 | | using (BinaryReader r = new BinaryReader(TGAStream)) |
| | 131 | | { |
| | 132 | | // Skip some header info we don't care about. |
| | 133 | | // Even if we did care, we have to move the stream seek point to the beginning, |
| | 134 | | // as the previous method in the workflow left it at the end. |
| 0 | 135 | | r.BaseStream.Seek(12, SeekOrigin.Begin); |
| | 136 | |
|
| 0 | 137 | | short width = r.ReadInt16(); |
| 0 | 138 | | short height = r.ReadInt16(); |
| 0 | 139 | | int bitDepth = r.ReadByte(); |
| | 140 | |
|
| | 141 | | // Skip a byte of header information we don't care about. |
| 0 | 142 | | r.BaseStream.Seek(1, SeekOrigin.Current); |
| | 143 | |
|
| 0 | 144 | | Texture2D tex = new Texture2D(width, height); |
| 0 | 145 | | Color32[] pulledColors = new Color32[width * height]; |
| | 146 | |
|
| 0 | 147 | | if (bitDepth == 32) |
| | 148 | | { |
| 0 | 149 | | for (int i = 0; i < width * height; i++) |
| | 150 | | { |
| 0 | 151 | | byte red = r.ReadByte(); |
| 0 | 152 | | byte green = r.ReadByte(); |
| 0 | 153 | | byte blue = r.ReadByte(); |
| 0 | 154 | | byte alpha = r.ReadByte(); |
| | 155 | |
|
| 0 | 156 | | pulledColors[i] = new Color32(blue, green, red, alpha); |
| | 157 | | } |
| 0 | 158 | | } |
| 0 | 159 | | else if (bitDepth == 24) |
| | 160 | | { |
| 0 | 161 | | for (int i = 0; i < width * height; i++) |
| | 162 | | { |
| 0 | 163 | | byte red = r.ReadByte(); |
| 0 | 164 | | byte green = r.ReadByte(); |
| 0 | 165 | | byte blue = r.ReadByte(); |
| | 166 | |
|
| 0 | 167 | | pulledColors[i] = new Color32(blue, green, red, 1); |
| | 168 | | } |
| 0 | 169 | | } |
| | 170 | | else |
| | 171 | | { |
| 0 | 172 | | throw new Exception("TGA texture had non 32/24 bit depth."); |
| | 173 | | } |
| | 174 | |
|
| 0 | 175 | | tex.SetPixels32(pulledColors); |
| 0 | 176 | | tex.Apply(); |
| 0 | 177 | | return tex; |
| | 178 | |
|
| | 179 | | } |
| 0 | 180 | | } |
| | 181 | | } |