- Home /
Gradien Skyboxes problem
Hi, I'm creating a simple game where I would like to have a gradient background with shifting colors and so I have done, and it works in the beginning. But After a while it looks really bad, the colors aren't correct, there's no blend and the colors get "stuck". Does somebody now why? I have tried both skybox, plane and different shaders.
This is my color controller: [code=CSharp]using System.Collections; using System.Collections.Generic; using UnityEngine;
public class GradientController : MonoBehaviour {
private Material gradientShader = null;
private Color currentColor0 = Color.white;
private Color currentColor1 = Color.white;
private Color targetColor0 = Color.white;
private Color targetColor1 = Color.black;
private readonly string COLOR_ONE = "_Color1";
private readonly string COLOR_TWO = "_Color2";
private readonly float LERP_SPEED = 0.5f;
private readonly float COLOR_INCREMENT = 0.01f;
private void Start()
{
gradientShader = RenderSettings.skybox;
currentColor0 = RandomColor();
currentColor1 = RandomColor();
targetColor0 = RandomColor();
targetColor1 = RandomColor();
gradientShader.SetColor(COLOR_ONE, currentColor0);
gradientShader.SetColor(COLOR_TWO, currentColor1);
}
void Update ()
{
currentColor0 = Color.Lerp(currentColor0, targetColor0, Time.deltaTime * LERP_SPEED);
currentColor1 = Color.Lerp(currentColor1, targetColor1, Time.deltaTime * LERP_SPEED);
gradientShader.SetColor(COLOR_ONE, currentColor0);
gradientShader.SetColor(COLOR_TWO, currentColor1);
targetColor0 = ShiftColor(targetColor0);
targetColor1 = ShiftColor(targetColor1);
}
private Color RandomColor()
{
return new Color(
Random.Range(0.1f, 0.9f),
Random.Range(0.1f, 0.9f),
Random.Range(0.1f, 0.9f)
);
}
private Color ShiftColor(Color start)
{
float minIncrement;
float maxIncrement;
float value;
float[] colors = new float[]{ start.r, start.g, start.b };
for (int i = 0; i < colors.Length; i++)
{
value = colors[i];
minIncrement = value > COLOR_INCREMENT ? -COLOR_INCREMENT : 0;
maxIncrement = value < 1 - COLOR_INCREMENT ? COLOR_INCREMENT : 1;
value += Random.value <= 0.5f ? minIncrement : maxIncrement;
colors[i] = value;
}
start.r = colors[0];
start.g = colors[1];
start.b = colors[2];
return start;
}
} [/code]
And in the beginning it looks like this: (Correct) https://imgur.com/fL0sSwl then like this: https://imgur.com/chaZcKP
i did not come up with the reason, but I came up with a code improvement, maybe it helps to better see the problem:
private Color ShiftColor(Color start)
{
for (int i = 0; i < start.Length - 1; i++)
{
start[i] += $$anonymous$$athf.Clamp01((Random.value <= 0.5f ? 1 : -1) * COLOR_INCRE$$anonymous$$ENT);
}
return start;
}
one reasons this might happen:
- colors at their limits might be too extreme. test the gradient for its max and $$anonymous$$ acceptable values
Your answer
![](https://koobas.hobune.stream/wayback/20220612184359im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Skybox rendered over sprites in reflection 0 Answers
Skybox reflections stopped working. 0 Answers
Oculus rendeing bug 0 Answers
Skybox blending 2 Answers
Represent Land 3D Area End 0 Answers