Pages

sâmbătă, 5 septembrie 2020

Shadertoy: The sin math function - 002.

This is another shader example to draw a sine math function using a sine wave, see Wikipedia. I used shadertoy website and the mat sin function by time. The source code is commented for a better understanding of it.
// this size of line to paint each pixel from screen
const float size = 0.01;

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
    // Normalized pixel coordinates (from 0 to 1)
    vec2 uv = fragCoord/iResolution.xy;
    // resize uv 
    uv = uv  * 2.0;
    // translate normalized pixel coordinates uv from [0,1] to [-1, 1] with 
    uv = (uv - 1.0);

    // get uv.x aspect ratio
    uv.x *= iResolution.x / iResolution.y;
  
    // get simple sine 
    //float t = sin(uv.x);

    // get sine * 3 with same size uv will zoom the sine graphic
    // see also https://en.wikipedia.org/wiki/Sine_wave
    float t = sin(uv.x * 3.0);
    
 // select domain area of sine and drwa yellow color 
    // else put an blue color on rest
    if (uv.y >= t - size && uv.y <= t + size) {
        // draw sine
     fragColor = vec4(1.0,1.0,0.0,1.0);
    } else {
        // draw background
        fragColor = vec4(0.0,0.0,1.0,1.0);
    }
}
The result of this shader is this: