home/ atoms/ glsl-sine-bias-gain

A GLSL sine oscillator needs a bias and gain to map its -1/+1 range to 0-1 for color

The GLSL sin() function returns values in the range -1 to +1. Using this directly as a color channel produces visible black for half the cycle, since negative values clamp to 0. To produce a smooth 0–1 oscillation, add a bias of 0.5 and multiply by a gain of 0.5: color = 0.5 + sin(time * frequency) * 0.5. Bias shifts the center from 0 to 0.5; gain scales the ±1 amplitude down to ±0.5, giving a full range of [0, 1]. This is a universal pattern for any oscillator driving a 0–1 parameter.

Examples

void main () {
  float frequency = 4., bias = .5, gain = .5;
  float color = bias + sin(time * frequency) * gain;
  gl_FragColor = vec4(color, color, color, 1.);
}

Assessment

Given color = sin(time * 2.0), what color will the screen be when sin() returns -0.8? Write the corrected formula using bias and gain so the color is always between 0 and 1.

“the `sin()` function returns values between -1 – 1. We can provide a bias and a scalar to get this to a range of 0–1”
corpus · workshop-notes-audiovisual-programming-wac-2019-charlie-robe · chunk 1