Skip to content
Snippets Groups Projects
Select Git revision
  • 60efd25d1d598b73e69b04e0c196cd96b9450efc
  • 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 fluctuationIntensity = 2.0f;
        [Tooltip("Frequency of the periodic brightness fluctuations")]
        [Range(0.0f, 100.0f)]
        public float fluctuationSpeed = 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 fluctuations are switched off)")]
        [Range(0.0f, 1.0f)]
        public float normalitySpeed = 0.1f;
        [Tooltip("Likelihood of the normality effect to happen (where flicker and fluctuations are switched off)")]
        [Range(0.0f, 1.0f)]
        public float normalityLikelihood = 0.5f;
    
        float randomWalk = 0.0f;
        float flickerBrightness = 0.0f;
        float upperBound = 1.0f;
        float lowerBound = -1.0f;
    
        float fluctuationBrightness = 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;
    
            // fluctuation
            fluctuationBrightness =  ((Mathf.PingPong(Time.time*fluctuationSpeed, 8.0f) * Mathf.PingPong(Time.time*fluctuationSpeed*1.131517f,4.0f)) - 4.0f) * fluctuationIntensity*0.1f;
    
            // Normality
            randomWalk2 += Random.Range(-normalitySpeed * 0.2f, normalitySpeed * 0.2f);
            if (randomWalk2 >= upperBound2) { randomWalk2 = upperBound2; }
            if (randomWalk2 <= lowerBound2) { randomWalk2 = lowerBound2; }