Hello!
For the game I’m working on I’ve created a shader that can randomize the colors of texture. This is to mimic the look of the color palette getting glitched like can happen in old videogames. It uses a float and the RGB values of each pixel as a seed for a few different Random Range nodes so that matching colors get turned into the same new color. Currently, it works exactly as expected, with one exception. For some reason, when the effect is active, certain colors will flash between two different colors, rather than staying constant. When changing the seed, the colors that flash aren’t consistent, and neither are the colors they flash to. This can be a big problem, as sometimes large areas of a specific color can rapidly flash with bright contrasting colors, and I don’t want to do that.
FLASHING IMAGES AHEAD!
Using this sample image:

If I activate the effect and randomize the seed, once it looked like this:

Colors randomize as expected, but for some reason some of them flash.
Randomizing it again, I got this:

Still flashing, and the flashing is occurring on different colors than last time. The background color (White) is flashing here as well which is way too intense to allow in my game.
Here is the Shadergraph:
I know it’s a bit of a mess, but essentially what’s happening here is it gets an input color and then combines each RGB value with the seed to get a new random value for each RGB out using the Random Range node. None of these values change after the effect is activated, so I don’t understand why certain colors flash as if certain values are changing. It seems as if the GPU isn’t consistent with the values for each frame for some reason.
Anyone have any ideas as to what might be happening here? Or how I might be able to create this effect with a better approach? Any help is appreciated! I’m using Unity 2020.3.35, and Shadergraph 10.9.0.
Bump. Anyone have any ideas?
I still have no idea what could possibly be wrong here. Today I started experimenting with a custom node to select a random color and I’m still getting inconsistent output.
First I tried this:
Out_Color.x = frac(sin(dot(float2(Seed, Color.x), float2(12.9898, 78.233)))*43758.5453);
Out_Color.y = frac(sin(dot(float2(Seed + 1, Color.y), float2(12.9898, 78.233)))*43758.5453);
Out_Color.z = frac(sin(dot(float2(Seed + 2, Color.z), float2(12.9898, 78.233)))*43758.5453);
Still get flickering colors, even though none of the values are changing. Then I tried truncating the RBG values like this:
Out_Color.x = frac(sin(dot(float2(Seed, trunc(Color.x * 256)), float2(12.9898, 78.233)))*43758.5453);
Out_Color.y = frac(sin(dot(float2(Seed + 1, trunc(Color.y * 256)), float2(12.9898, 78.233)))*43758.5453);
Out_Color.z = frac(sin(dot(float2(Seed + 2, trunc(Color.z * 256)), float2(12.9898, 78.233)))*43758.5453);
Thinking maybe there was an issue with precision or something, but nope, still flickering. I also tried truncating the seed and that didn’t help either.
Mathematically this should always output the same value so why on earth is it so inconsistent every few frames?! Anyone?
Just found more weirdness. If I replace the input values (Seed and Color) with static values (Rather than variables) the problem goes away. Even if I’m using the exact same values the variables are. For example, if I hard code a color of (1,1,1) and a seed of any value into the input of the Random Color node, results are stable. However, if I use the color found at the current UV of a solid white object (Meaning the color is exactly the same, (1,1,1)), the results become inconsistent. Same happens if I use the Seed variable in the graph vs. just hard coding a value into the seed input of Random Color. Hard coding any value into Seed, I’m good. Passing the value of the graph’s seed variable into Seed of Color Randomizer, results are inconsistent. This makes no sense.
Edit: Well, the issue seems to be resolved… Though I’m not entirely sure how I fixed it. In one case I was testing with an untextured cube, and the issue stopped when I applied a texture to it’s material (Even if it was just a solid white texture, which made didn’t change the color at all, so I don’t know what that was about). Some of the sprites I was testing with also weren’t using the correct shader the entire time I was testing, and updating their shader with the changes I’ve made made the issue go away. I’ve done so much testing and stuff today though I’ve kind of lost track of what I did that actually solved the problem. Whatever, I’m just glad this is over (For now…).