Sign in to follow this  
ChaseRLewis

Unity Getting color pallette of non-shaded texture that ignores aliasing

Recommended Posts

Edit: I've gotten it from 5k to about 60 by only checking 100% opaque values and introducing floating point tolerances since Unity converts all the colors to floating point. But that's still about ~13-15x the number in the actual number in the photo ... that's with pretty generous tolerances of .1f which is is ~ +/- 25 in the 8-bit color scale.

So I'm trying to write a tool for my Unity game that will allow me to make a color edit mode. I figured if I went the extra step to make sure my colors were all completed seperated that it would be fairly easy, but I'm still getting far more colors then I actually attempted using when I colored the picture. The picture was done with vector graphics so I know the precise color of each not including the aliased lines which are interpolated with a completely alpha color.

I'm not 100% that aliasing is the problem but otherwise I am completely unsure why there would be in the realm of 4000 different colors showing in my photo. They all seem to be really similar versions of the same color so aliasing is the only thing that makes sense. The idea is have a color mask, a and a pallete to map use selected colors too at run time.

This is just a sample file of peach from paper mario I'm using to test the system [attachment=11405:PrincessColor.png]

The goal is to use this color map with the BW and a shade map to construct the final picture. I was separating the BW from the color to try and solve the aliasing issue but that isn't the solution.

Hopefully looking like this in the end.[attachment=11406:ColorPrincess.png]

I got it working in photoshop but need to find out how to export this to my game. I could do it by exporting each clip mask out as a picture then recombining them mathematically to guarantee they are exact, but exporting 6-12 pics per frame then recombining is more work then it should have to be. Any help would be appreciated.


Code:

[source lang="java"]public class ColorEditorWindow : EditorWindow
{
static ColorEditorWindow window = null;
List<Color> ColorPallette = new List<Color>();
// Use this for initialization
[MenuItem("Window/ColorEditor")]
static public void Start ()
{
if (window == null)
window = EditorWindow.GetWindow<ColorEditorWindow>();
}

public void GetPallette()
{
if (texture != null)
{

//Reset Pallette if previous one existed
if(ColorPallette.Count != 0) ColorPallette.Clear();


Color temp = new Color(); //cache color value to use as temporary

for (int i = 0; i < texture.height; i++)
{
for (int j = 0; j < texture.width; j++)
{
temp = texture.GetPixel(j, i); //Get pixel to test

if (temp.a == 0)//Ignore pure alpha colors
continue;

//See if color is unique
for (int k = 0; k < ColorPallette.Count; k++) //If color exists in pallette skip it -- goto is ugly consider something else
{ if (ColorPallette[k].r == temp.r && ColorPallette[k].g == temp.g && ColorPallette[k].b == temp.b) goto NEXT; }

ColorPallette.Add(temp); //If not in pallette add to pallette

NEXT: continue; //If temp exists go here and continue.
}
}

}
}

public Texture2D texture = null;
public void OnGUI()
{
texture = EditorGUILayout.ObjectField(texture, typeof(Texture2D)) as Texture2D;

if (GUILayout.Button("Get Pallette"))
{
GetPallette();
}

/* for (int i = 0; i < ColorPallette.Count; i++)
ColorPallette[i] = EditorGUILayout.ColorField(ColorPallette[i]);
*/

EditorGUILayout.LabelField("Colors: " + ColorPallette.Count);
}
}[/source] Edited by ChaseRLewis

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this