Created
June 15, 2021 03:55
-
-
Save yuriwally/c757dfd1e55fed55e96eaef74e288ea4 to your computer and use it in GitHub Desktop.
Light flicker effect based on valve reused The code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections; | |
| using UnityEngine; | |
| using System.Collections.Generic; | |
| // Written by jellyroger 2021 | |
| // License: CC0 Public Domain http://creativecommons.org/publicdomain/zero/1.0/ | |
| // See: https://www.reddit.com/r/gaming/comments/nyxzme/valve_reuses_the_source_code_for_flickering/h1n8c8h/ | |
| // See: https://80.lv/articles/valve-reused-the-code-for-flickering-lights-in-alyx-22-years-later/ | |
| /// <summary> | |
| /// Component which will flicker a linked light according to the sequence. | |
| /// A being fully dark ( intensity = 0 ) and Z being fully bright ( intensity = maxIntensity ) | |
| /// Just activate / deactivate this component as usual to pause / resume flicker | |
| /// </summary> | |
| public class LightFlickerEffect : MonoBehaviour | |
| { | |
| [Tooltip("External light to flicker; you can leave this null if you attach script to a light")] | |
| public new Light light; | |
| [Tooltip("Maximum light intensity")] | |
| [Range(0.0f, Mathf.Infinity)] | |
| public float maxIntensity; | |
| [Tooltip("Time in milliseconds of each step.")] | |
| [Range(0.0f, Mathf.Infinity)] | |
| public float stepTime = 500; | |
| [Tooltip("Use from A to Z to determine the light sequence. Default to original Quake sequence")] | |
| public string flickerSequence = "mmamammmmammamamaaamammma"; | |
| [Tooltip("Run only once, no loop?")] | |
| public bool dontLoop = false; | |
| [Tooltip("Run automatically along with the Startup method when starting the gameObject?")] | |
| public bool runAtStart = true; | |
| private List<float> cache; | |
| private int currentStep; | |
| private void Start() | |
| { | |
| if (light == null) | |
| { | |
| light = GetComponent<Light>(); | |
| } | |
| if (light == null) return; | |
| if (maxIntensity == 0) | |
| { | |
| maxIntensity = light.intensity; | |
| } | |
| ConvertAlphabetToBrightScale(); | |
| if (runAtStart) | |
| { | |
| StartFlicker(); | |
| } | |
| } | |
| private IEnumerator Flicker() | |
| { | |
| while (true) | |
| { | |
| if (light.isActiveAndEnabled) | |
| { | |
| light.intensity = cache[currentStep]; | |
| currentStep++; | |
| if (currentStep == cache.Count) | |
| { | |
| if (dontLoop != true) | |
| { | |
| currentStep = 0; | |
| } | |
| else | |
| { | |
| yield break; | |
| } | |
| } | |
| } | |
| yield return new WaitForSeconds(stepTime / 1000); | |
| } | |
| } | |
| private void ConvertAlphabetToBrightScale() | |
| { | |
| const float step = 3.84615384615f; | |
| const string alphabet = "abcdefghijklmnopqrstuvwxyz"; | |
| cache = new List<float>(); | |
| foreach (var c in flickerSequence.ToLower()) | |
| { | |
| cache.Add( step * alphabet.IndexOf(c) * maxIntensity / 100); | |
| } | |
| } | |
| public void StartFlicker() | |
| { | |
| StartCoroutine(Flicker()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment