- Home /
Screen Gradient Transition - Using Pixels, Poor Performance
Pixel Transition
I'm currently using a system where I'm finding all of the pixels in a gradient texture and scaling through the image comparing all of the pixels to a changing gradient color (white to black). I then set that color in the image to be alpha 0.0f instead of 1.0f.
The thing is, GetPixels(), SetPixels() and Apply() are super slow and I can't get a clean transition, even when I have an image that's only 100x100 in size.
Can someone give me some sort of advice? The effect I want to achieve is below. I'll post a code example.
IEnumerator FadeOut(float waitDelay)
{
InTransition = true;
Color origColor = fadeScreenHUD.color;
origColor.a = 1.0f;
fadeScreenHUD.color = origColor;
// SET IMAGE
Texture2D texReset = Instantiate<Texture2D> (shownTexture as Texture2D);
Texture2D tex = Instantiate<Texture2D> (gradient as Texture2D);
TextureScale.Bilinear (texReset, tex.width, tex.height);
//TextureScale.Bilinear (tex, texReset.width, texReset.height);
Color[] pixelsGradient = tex.GetPixels ();
Color[] pixels = texReset.GetPixels ();
// Get invisible, please
for (int i = 0; i < pixels.Length; i++)
pixels [i] = new Color (pixels [i].r, pixels [i].g, pixels [i].b, 0.0f);
// PERFORM FADE
int counter = 255;
float shade = 1.0f / 255;
float colorVal = 0.0f;
while (counter > 0)
{
for(int j = 0; j < speedValue; j++)
{
for (int i = 0; i < pixelsGradient.Length; i++)
{
if (pixelsGradient [i] == new Color(colorVal, colorVal, colorVal))
pixels [i] = new Color (pixels [i].r, pixels [i].g, pixels [i].b, 1.0f);
}
colorVal += shade;
texReset.SetPixels (pixels);
fadeScreenHUD.sprite = Sprite.Create (texReset, fadeScreenHUD.sprite.rect, new Vector2 (0.5f, 0.5f));
counter -= 1;
}
texReset.Apply ();
yield return new WaitForEndOfFrame();
}
yield return new WaitForSeconds (waitDelay);
StartCoroutine (FadeIn ());
}
That'd be awesome, but I lack the experience to write a shader that does this. Would you know where to look? I've searched for one but couldn't find something that fit the idea.
You could simply use a screenspace canvas with a single image component of type filled, horizontal, origin left. then you can change how much of it is visible by changing the fill amount
The thing is, with what I'm doing now I can add any gradient to the script and it'll perform the wipe from White to Black. This allows for unique patterns.
I totally get your point. The only way to get proper performances is via shader writing. This is part of my upco$$anonymous$$g training chapter on the topic. I'll post a link when it's ready.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Sort pixels according to their color in Texture2D in Unity 0 Answers
how to get pixel colors in the GUI brush area 0 Answers
Reference Variables and Drag-And-Drop 0 Answers