Skip to content

Instantly share code, notes, and snippets.

@hww
Last active December 31, 2015 05:39
Show Gist options
  • Save hww/7942752 to your computer and use it in GitHub Desktop.
Save hww/7942752 to your computer and use it in GitHub Desktop.
using UnityEngine;
#pragma warning disable 414
/*
*
* By: hww
*
*/
[System.Serializable]
public class WaveModulator
{
public enum Mode
{
DISABLED,
SQUARE,
SINE
}
public Mode mode; // Mode.SINE, Mode.SQUARE or nothing DISABLED
public float speed = 1; // modulation speed
public float power = 1; // modulation power (amplitude)
public int time_on = 1; // enabled time (Mode.SQUARE)
public int time_off = 3; // disable time (Mode.SQUARE)
static float PI2 = 2.0f * Mathf.PI;
public WaveModulator() {
mode = Mode.SQUARE;
}
public float Update (float time)
{
switch (mode)
{
case Mode.DISABLED:
return 1;
case Mode.SQUARE:
int wave_number = Mathf.FloorToInt((time * speed) / PI2);
int wave_phase = wave_number % (time_on + time_off);
if (wave_phase >= time_off)
return power;
else
return 0;
case Mode.SINE:
return Mathf.Sin (time * speed) * power;
}
return 0;
}
}
[System.Serializable]
public class WaveEffect
{
public float speed = 12; // each character animation speed
public float power = 2; // each character animation power (amplitude)
public float delay = 5; // each character animation delay between characters
public WaveModulator modulator; // LFO - low frequence oscilator
public WaveEffect () {
modulator = new WaveModulator();
}
/// <summary>
/// Update the specified time and index.
/// </summary>
/// <param name="time">Usualy Time.time</param>
/// <param name="index">
/// Index per character.
/// Can be positive or negative.
/// It change dirrection of the effect
/// It can be constant that make effect same for each character
/// </param>
public float Update (float time, float index)
{
float phase = delay * index;
float ctime = time * speed - phase;
return Mathf.Sin (ctime) * power * modulator.Update(ctime);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment