Skip to content
Snippets Groups Projects
Select Git revision
  • f24d3015009c50b6812f316aad8ce58eb1d478fd
  • master default protected
2 results

LightFlicker.cs

Blame
  • LightFlicker.cs 2.76 KiB
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    
    [RequireComponent(typeof(Light))]
    public class LightFlicker : MonoBehaviour
    {
        [Tooltip("Base Brightness of the flickering light (used when Normality happens)")]
        [Range(0.0f, 8.0f)]
        public float baseBrightness = 1.0f;
    
        [Tooltip("Intensity of the periodic brightness fluctuations")]
        [Range(0.0f, 10.0f)]
        public float oscillationIntensity = 2.0f;
        [Tooltip("Frequency of the periodic brightness fluctuations")]
        [Range(0.0f, 100.0f)]
        public float oscillationSpeed = 44.0f;
    
        [Tooltip("Intensity of the random brightness flicker")]
        [Range(0.0f, 10.0f)]
        public float flickerIntensity = 2.0f;
        [Tooltip("Frequency of the random brightness flicker")]
        [Range(0.0f, 1.0f)]
        public float flickerSpeed = 0.1f;
        [Tooltip("Off vs On-percentage of the random brightness flicker")]
        [Range(0.0f, 1.0f)]
        public float flickerOffVsOn = 0.5f;
    
        [Tooltip("Frequency of the normality effect (where flicker and oscillations are switched off)")]
        [Range(0.0f, 1.0f)]
        public float normalitySpeed = 0.1f;
        [Tooltip("Likelyhood of the normality effect to happen (where flicker and oscillations are switched off)")]
        [Range(0.0f, 1.0f)]
        public float normalityLikelyhood = 0.5f;
    
        float randomWalk = 0.0f;
        float flickerBrightness = 0.0f;
        float upperBound = 1.0f;
        float lowerBound = -1.0f;
    
        float oscillationBrightness = 0.0f;
    
        float randomWalk2 = 0.0f;
        float upperBound2 = 1.0f;
        float lowerBound2 = 0.0f;
    
        Light thisLight;
        // Start is called before the first frame update
        void Start()
        {
            thisLight = this.gameObject.GetComponent<Light>();
        }
    
        // Update is called once per frame
        void Update()
        {
            // Flicker
            randomWalk += Random.Range(-flickerSpeed, flickerSpeed);
            if (randomWalk >= upperBound) { randomWalk = upperBound; }
            if (randomWalk <= lowerBound) { randomWalk = lowerBound; }
            flickerBrightness = (randomWalk * flickerIntensity) + (-8.0f + flickerOffVsOn * 16.0f) * flickerIntensity;
    
            // Oscillation
            oscillationBrightness =  ((Mathf.PingPong(Time.time*oscillationSpeed, 8.0f) * Mathf.PingPong(Time.time*oscillationSpeed*1.131517f,4.0f)) - 4.0f) * oscillationIntensity*0.1f;
    
            // Normality
            randomWalk2 += Random.Range(-normalitySpeed * 0.2f, normalitySpeed * 0.2f);
            if (randomWalk2 >= upperBound2) { randomWalk2 = upperBound2; }
            if (randomWalk2 <= lowerBound2) { randomWalk2 = lowerBound2; }