- Home /
Texture2d color transformation made the right way
Hi everyone! i've developed a little colorpicker for my unity app. To obtain it i've used a GUI.RepeatButton with a Texture2d as background. I've used a texture2d and not a shader ( as seen in others colorpicker in asset store) because i need that colors will be in a defined range so don't want all the rgb values. By the way this works flawlessy. now i've been asked to add saturation and brightness controls, but updating the texture2d is very very very slow I've used this code
private void updateTexture(){
saturation = newSaturation;
brightness = newBrightness;
if (newSaturation == 1 && newBrightness == 1) {
Color[] colors = texture.GetPixels ();
modifiedTexture.SetPixels (colors);
modifiedTexture.Apply ();
} else {
Color[] colors = texture.GetPixels ();
float pr=0.299f;
float pg=0.587f;
float pb=0.144f;
for (int i = 0; i < colors.Length; i++) {
if(brightness < 1){
colors[i].r = colors[i].r*brightness;
colors[i].g = colors[i].g*brightness;
colors[i].b = colors[i].b*brightness;
}
if(saturation < 1){
float p = Mathf.Sqrt(Mathf.Pow(colors[i].r,2)*pr+ Mathf.Pow(colors[i].g,2)*pg+ Mathf.Pow(colors[i].b,2)*pb);
colors[i].r = p + ((colors[i].r-p) * saturation);
colors[i].g = p + ((colors[i].g-p) * saturation);
colors[i].b = p + ((colors[i].b-p) * saturation);
}
}
modifiedTexture.SetPixels (colors);
modifiedTexture.Apply ();
}
but as the image is 700x700 pixels on mobile device is quite slow. is there any smart way to improve performance of this colorpicker ? thanks in advance

Answer by Nabeel Saleem · Jun 03, 2014 at 04:24 PM
Select the texture which becomes pixelated.
-From import settings
Texture type=texture
Filter mode=Trilinear
Max size=max
Format=truecolor
and click apply
Answer by supernat · Jun 03, 2014 at 02:37 PM
Some things pop out to me. The read/set pixels are what's killing your performance.
1) you could only update the image once the sliders stop sliding (I know, not the best approach).
2) You could reduce the size of the texture so that while sliding you are adjusting a much smaller version of the texture so that brightness can be perceived even though the texture will enter a sort of blurry/degraded look during the adjustment, then return to the full size after the sliding is complete.
2a) Why not just update the small square texture's brightness and saturation while sliding and the entire image afterward.
3) You may need to use a shader pass just for the brightness if that's possible. I understand that you need to keep it as a texture lookup for the actual colors, but you can use a shader with extra brightness/saturation parameters. Although the saturation may be difficult since you are limited in your color scope, the brightness should work without a hitch.
4) Get rid of the Pow() method, just multiply the input by itself (small improvement, probably negligible, but helps code readability)
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Do our company need a Facebook business account for integrating our unity game? 1 Answer
Playing ipod audio... 0 Answers
Animation Freeze, If minimize and reopen the game, It is OK 0 Answers
In app purchase of content 0 Answers