Unity simple & fast light flicker script
This file contains 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 UnityEngine; | |
using System.Collections.Generic; | |
// Written by Steve Streeting 2017 | |
// License: CC0 Public Domain http://creativecommons.org/publicdomain/zero/1.0/ | |
/// <summary> | |
/// Component which will flicker a linked light while active by changing its | |
/// intensity between the min and max values given. The flickering can be | |
/// sharp or smoothed depending on the value of the smoothing parameter. | |
/// | |
/// 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("Minimum random light intensity")] | |
public float minIntensity = 0f; | |
[Tooltip("Maximum random light intensity")] | |
public float maxIntensity = 1f; | |
[Tooltip("How much to smooth out the randomness; lower values = sparks, higher = lantern")] | |
[Range(1, 50)] | |
public int smoothing = 5; | |
// Continuous average calculation via FIFO queue | |
// Saves us iterating every time we update, we just change by the delta | |
Queue<float> smoothQueue; | |
float lastSum = 0; | |
/// <summary> | |
/// Reset the randomness and start again. You usually don't need to call | |
/// this, deactivating/reactivating is usually fine but if you want a strict | |
/// restart you can do. | |
/// </summary> | |
public void Reset() { | |
smoothQueue.Clear(); | |
lastSum = 0; | |
} | |
void Start() { | |
smoothQueue = new Queue<float>(smoothing); | |
// External or internal light? | |
if (light == null) { | |
light = GetComponent<Light>(); | |
} | |
} | |
void Update() { | |
if (light == null) | |
return; | |
// pop off an item if too big | |
while (smoothQueue.Count >= smoothing) { | |
lastSum -= smoothQueue.Dequeue(); | |
} | |
// Generate random new item, calculate new average | |
float newVal = Random.Range(minIntensity, maxIntensity); | |
smoothQueue.Enqueue(newVal); | |
lastSum += newVal; | |
// Calculate new smoothed average | |
light.intensity = lastSum / (float)smoothQueue.Count; | |
} | |
} |
Mind if I add slight movement to the light itself to add even more realism?
Mind if I add slight movement to the light itself to add even more realism?
Feel free to fork it to do whatever you want :)
Hello
How to add a emision value with independent min max intensity but the same flicker amount?
I mean light and a emission source (for example the lamp source itself in 3d world)
Create a min max for emission then create a random math function to check every frame for a new value and apply it
…________________________________
From: Pourya <notifications@github.com>
Sent: Sunday, April 26, 2020 1:41:51 PM
To: sinbad <sinbad@noreply.github.com>
Cc: arthurmarquis <arthur.marquis@live.com>; Comment <comment@noreply.github.com>
Subject: Re: sinbad/LightFlickerEffect.cs
@pourya0111 commented on this gist.
________________________________
Hello
How to add a emision value with independent min max intensity but the same flicker amount?
I mean light and a emission source (for example the lamp source itself in 3d world)
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub<https://gist.github.com/4a9ded6b00cf6063c36a4837b15df969#gistcomment-3270785>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AJN46HJYMSUHSWWQFFQJ7K3ROSMA7ANCNFSM4HOAC7KA>.
Tbh I am not sure what you are asking. What FX are you looking for? Candle? Fire? Explosion? Flickering light in a hospital for a creepy effect?
From: Arthur Marquis<mailto:arthur.marquis@live.com>
Sent: Sunday, April 26, 2020 1:55 PM
To: sinbad<mailto:sinbad@noreply.github.com>; sinbad<mailto:reply@reply.github.com>
Cc: Comment<mailto:comment@noreply.github.com>
Subject: Re: sinbad/LightFlickerEffect.cs
Create a min max for emission then create a random math function to check every frame for a new value and apply it
From: Pourya <notifications@github.com>
Sent: Sunday, April 26, 2020 1:41:51 PM
To: sinbad <sinbad@noreply.github.com>
Cc: arthurmarquis <arthur.marquis@live.com>; Comment <comment@noreply.github.com>
Subject: Re: sinbad/LightFlickerEffect.cs
@pourya0111 commented on this gist.
Hello
How to add a emision value with independent min max intensity but the same flicker amount?
I mean light and a emission source (for example the lamp source itself in 3d world)
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub<https://gist.github.com/4a9ded6b00cf6063c36a4837b15df969#gistcomment-3270785>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AJN46HJYMSUHSWWQFFQJ7K3ROSMA7ANCNFSM4HOAC7KA>.
Just think of a parking sign that will flicker (maybe emission?) like the light
While this does work, it completely tanks performance changing the lighting on every frame, especially if several are used.
This is awesome! Thank you so much :) It's perfect to apply to other things beyond just light.luminence too
Thank you!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, Steve. Perfectly implemented!