159 lines
4.0 KiB
C#
159 lines
4.0 KiB
C#
using UnityEngine;
|
|
|
|
public class LightFlickerer : MonoBehaviour
|
|
{
|
|
[Header("Base Color")]
|
|
[SerializeField] public Color initialColor = new Color(1f, 0.55f, 0.2f);
|
|
|
|
[Header("Color Variations")]
|
|
[SerializeField, Range(0f, 1f)]
|
|
private float colorVariation = 0.12f;
|
|
|
|
[SerializeField, Min(2)]
|
|
private int colorPermutations = 8;
|
|
|
|
[Header("Flicker")]
|
|
[SerializeField] private float minIntensity = 1.0f;
|
|
[SerializeField] private float maxIntensity = 2.0f;
|
|
|
|
[SerializeField] private float flickerSpeed = 4f;
|
|
|
|
[Header("Torch Movement")]
|
|
[SerializeField] private float movementAmount = 0.03f;
|
|
[SerializeField] private float movementSpeed = 2f;
|
|
|
|
private Light torchLight;
|
|
|
|
private Color[] colors;
|
|
private int currentColor;
|
|
private int nextColor;
|
|
|
|
private float colorTimer;
|
|
private float colorTransitionTime;
|
|
|
|
private Vector3 initialPosition;
|
|
private float noiseOffset;
|
|
|
|
private void Start()
|
|
{
|
|
torchLight = GetComponent<Light>();
|
|
|
|
initialPosition = transform.localPosition;
|
|
noiseOffset = Random.Range(0f, 1000f);
|
|
|
|
GenerateColorPermutations();
|
|
|
|
currentColor = Random.Range(0, colors.Length);
|
|
nextColor = GetDifferentColor(currentColor);
|
|
|
|
colorTransitionTime = Random.Range(0.3f, 0.8f);
|
|
colorTimer = 0f;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
FlickerColor();
|
|
FlickerIntensity();
|
|
FlickerMovement();
|
|
}
|
|
|
|
private void GenerateColorPermutations()
|
|
{
|
|
colors = new Color[colorPermutations];
|
|
|
|
for (int i = 0; i < colorPermutations; i++)
|
|
{
|
|
float variation = Random.Range(-colorVariation, colorVariation);
|
|
|
|
// Slightly vary each RGB channel independently.
|
|
float r = Mathf.Clamp01(initialColor.r + variation * Random.Range(0.7f, 1.2f));
|
|
float g = Mathf.Clamp01(initialColor.g + variation * Random.Range(0.7f, 1.2f));
|
|
float b = Mathf.Clamp01(initialColor.b + variation * Random.Range(0.7f, 1.2f));
|
|
|
|
colors[i] = new Color(r, g, b, initialColor.a);
|
|
}
|
|
}
|
|
|
|
private void FlickerColor()
|
|
{
|
|
colorTimer += Time.deltaTime;
|
|
|
|
float t = colorTimer / colorTransitionTime;
|
|
|
|
// Smoothly transition between two colors.
|
|
float smoothT = Mathf.SmoothStep(0f, 1f, t);
|
|
|
|
torchLight.color = Color.Lerp(
|
|
colors[currentColor],
|
|
colors[nextColor],
|
|
smoothT
|
|
);
|
|
|
|
if (colorTimer >= colorTransitionTime)
|
|
{
|
|
currentColor = nextColor;
|
|
nextColor = GetDifferentColor(currentColor);
|
|
|
|
colorTimer = 0f;
|
|
|
|
// Randomize how long the next transition takes.
|
|
colorTransitionTime = Random.Range(
|
|
0.25f / flickerSpeed,
|
|
0.8f / flickerSpeed
|
|
);
|
|
}
|
|
}
|
|
|
|
private void FlickerIntensity()
|
|
{
|
|
// Perlin noise produces a much more natural flame than Random.Range().
|
|
float noise = Mathf.PerlinNoise(
|
|
noiseOffset,
|
|
Time.time * flickerSpeed
|
|
);
|
|
|
|
torchLight.intensity = Mathf.Lerp(
|
|
minIntensity,
|
|
maxIntensity,
|
|
noise
|
|
);
|
|
}
|
|
|
|
private void FlickerMovement()
|
|
{
|
|
float time = Time.time * movementSpeed;
|
|
|
|
float x = Mathf.PerlinNoise(noiseOffset, time) - 0.5f;
|
|
float y = Mathf.PerlinNoise(noiseOffset + 100f, time) - 0.5f;
|
|
float z = Mathf.PerlinNoise(noiseOffset + 200f, time) - 0.5f;
|
|
|
|
transform.localPosition = initialPosition + new Vector3(
|
|
x,
|
|
y,
|
|
z
|
|
) * movementAmount;
|
|
}
|
|
|
|
private int GetDifferentColor(int current)
|
|
{
|
|
if (colors.Length <= 1)
|
|
return current;
|
|
|
|
int result;
|
|
|
|
do
|
|
{
|
|
result = Random.Range(0, colors.Length);
|
|
}
|
|
while (result == current);
|
|
|
|
return result;
|
|
}
|
|
|
|
// Regenerate the palette if the initial color is changed at runtime.
|
|
public void RegenerateColors()
|
|
{
|
|
GenerateColorPermutations();
|
|
}
|
|
}
|