Skip to content

Instantly share code, notes, and snippets.

@mikezila
Created April 12, 2014 21:13
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save mikezila/10557162 to your computer and use it in GitHub Desktop.
Save mikezila/10557162 to your computer and use it in GitHub Desktop.
TGA Loader for Unity3D
// This was made by aaro4130 on the Unity forums. Thanks boss!
// It's been optimized and slimmed down for the purpose of loading Quake 3 TGA textures from memory streams.
using System;
using System.IO;
using UnityEngine;
public static class TGALoader
{
public static Texture2D LoadTGA(string fileName)
{
using (var imageFile = File.OpenRead(fileName))
{
return LoadTGA(imageFile);
}
}
public static Texture2D LoadTGA(Stream TGAStream)
{
using (BinaryReader r = new BinaryReader(TGAStream))
{
// Skip some header info we don't care about.
// Even if we did care, we have to move the stream seek point to the beginning,
// as the previous method in the workflow left it at the end.
r.BaseStream.Seek(12, SeekOrigin.Begin);
short width = r.ReadInt16();
short height = r.ReadInt16();
int bitDepth = r.ReadByte();
// Skip a byte of header information we don't care about.
r.BaseStream.Seek(1, SeekOrigin.Current);
Texture2D tex = new Texture2D(width, height);
Color32[] pulledColors = new Color32[width * height];
if (bitDepth == 32)
{
for (int i = 0; i < width * height; i++)
{
byte red = r.ReadByte();
byte green = r.ReadByte();
byte blue = r.ReadByte();
byte alpha = r.ReadByte();
pulledColors [i] = new Color32(blue, green, red, alpha);
}
} else if (bitDepth == 24)
{
for (int i = 0; i < width * height; i++)
{
byte red = r.ReadByte();
byte green = r.ReadByte();
byte blue = r.ReadByte();
pulledColors [i] = new Color32(blue, green, red, 1);
}
} else
{
throw new Exception("TGA texture had non 32/24 bit depth.");
}
tex.SetPixels32(pulledColors);
tex.Apply();
return tex;
}
}
}
@JohannesMP
Copy link

It would be awesome if you could have the reverse - Encoding a Texture2D to TGA for easy saving

@JohannesMP
Copy link

I created a Texture2D extension method for encoding Texture2D to TGA: https://gist.github.com/JohannesMP/70ef0208154f8db2fafe01aba78af0f6

@yadreny
Copy link

yadreny commented Aug 12, 2019

line 58
pulledColors [i] = new Color32(blue, green, red, 1);
1 need to be replaced by 255, in another case, texture become transparent

@24Ko8e
Copy link

24Ko8e commented Sep 24, 2020

thanks @yadreny that helped

@ThibaultPotier
Copy link

ThibaultPotier commented Aug 27, 2021

Thanks @yadreny that helped me too.

Also, I faced the case where a .tga had only one channel (grayscale for metallic PBR texture). To be able to load it I added this code :

        `else if (bitDepth == 8)
        {
            for (int i = 0; i < width * height; i++)
            {
                byte red = r.ReadByte();
                byte green = red;
                byte blue = red;

                pulledColors[i] = new Color32(blue, green, red, 255);
            }
        }`

@ZhiruiLi
Copy link

line 58 pulledColors [i] = new Color32(blue, green, red, 1); 1 need to be replaced by 255, in another case, texture become transparent

No, it should be 255 instead of 1. Elements in Color32 have type byte not float .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment