- Home /
Question by
Patxiku · Jun 15, 2016 at 03:21 PM ·
shaderstimeshader programmingshaderlab
How to fluctuate between values in a shader
I need to change _GlowPower
value within the range of 1 to 6 in runtime. I've tried this:
float _GlowPower;
float _UpDown = 0;
float _TimeElapsed = 0;
void surf(Input IN, inout SurfaceOutput o) {
if (_UpDown == 0) {
_GlowPower += _Time.z - _TimeElapsed;
}
if (_UpDown == 1) {
_GlowPower -= _Time.z - _TimeElapsed;
}
if (_GlowPower <= 1) {
_UpDown = 0;
}
if (_GlowPower >= 6) {
_UpDown = 1;
}
_TimeElapsed = _Time.z;
IN.color = _ColorTint;
o.Albedo = tex2D(_MainTex, IN.uv_MainTex).rgb * IN.color;
half rim = 1.0 - saturate(dot(normalize(IN.viewDir), o.Normal));
o.Emission = _GlowColor.rgb * pow(rim, _GlowPower);
}
_GlowPower
always goes up, never down. How could achieve this inside the shader? I know I can do this by script, but I need to keep this clean. Thanks in advice.
Comment
Best Answer
Answer by Glurth · Jun 15, 2016 at 05:04 PM
Get the SIN function of the time. This will make it into a number that fluctuates between -1 and +1. You can then scale this to 1 though 6.
The sin of the time is available as a builtin shader variable: _SinTime