Skip to content

Instantly share code, notes, and snippets.

@khalladay
Created February 21, 2017 14:40
Show Gist options
  • Save khalladay/e017625b018531e579905369f1011c08 to your computer and use it in GitHub Desktop.
Save khalladay/e017625b018531e579905369f1011c08 to your computer and use it in GitHub Desktop.
PackRGBTool
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEditor;
public class PackToRGB : EditorWindow
{
private Texture2D[] RGB;
[MenuItem("Tools/PackRGB")]
public static void ShowWindow()
{
EditorWindow.GetWindow(typeof(PackToRGB));
}
private void OnGUI()
{
if (RGB == null)
{
RGB = new Texture2D[3];
}
GUI.enabled = true;
RGB[0] = EditorGUILayout.ObjectField("Take R From Here: ", RGB[0], typeof(Texture2D), false) as Texture2D;
RGB[1] = EditorGUILayout.ObjectField("Take G From Here: ", RGB[1], typeof(Texture2D), false) as Texture2D;
RGB[2] = EditorGUILayout.ObjectField("Take B From Here: ", RGB[2], typeof(Texture2D), false) as Texture2D;
if (GUILayout.Button("Pack"))
{
string outPath = EditorUtility.SaveFilePanel("Where to save", "", "mytexture", "png");
Texture2D outTex = new Texture2D(RGB[0].width, RGB[0].height, TextureFormat.RGB24, false);
for (int i = 0; i < 3; ++i)
{
string fileURL = AssetDatabase.GetAssetPath(RGB[i]);
byte[] imgByes = File.ReadAllBytes(fileURL);
Texture2D readableTex = new Texture2D(1, 1, TextureFormat.ARGB32, false);
readableTex.LoadImage(imgByes);
CopyChannel(readableTex, outTex, i);
outTex.Apply();
}
byte[] b = outTex.EncodeToPNG();
File.WriteAllBytes(outPath, b);
AssetDatabase.Refresh();
}
}
void CopyChannel(Texture2D src, Texture2D dst, int channel)
{
for (int i = 0; i < src.width; ++i)
{
for (int j = 0; j < src.height; ++j)
{
Color s = src.GetPixel(i, j);
Color d = dst.GetPixel(i, j);
d.r = channel == 0 ? s.r : d.r;
d.g = channel == 1 ? s.g : d.g;
d.b = channel == 2 ? s.b : d.b;
dst.SetPixel(i, j, d);
}
}
}
}
@korlinga
Copy link

If your TAM generator builds bitmaps they won't work and have to be converted into jpegs

@korlinga
Copy link

Also, to get the right shading, the 6 TAM files [0,1,2,3,4,5] have to be loaded in the RGB slots of this script (via the UI) as r2,g1,b0 & r5,g4,b3
image

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