File: ColorGratingShader.frag.txt

package info (click to toggle)
psychtoolbox-3 3.0.19.14.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 86,796 kB
  • sloc: ansic: 176,245; cpp: 20,103; objc: 5,393; sh: 2,753; python: 1,397; php: 384; makefile: 193; java: 113
file content (59 lines) | stat: -rw-r--r-- 1,727 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
 * File: ColorGratingShader.frag.txt
 * Shader for drawing of color gratings.
 *
 * Copyright 2014, Ian Andolina <http://github.com/iandol>, licenced under the MIT Licence
 *
 */

uniform vec2    center;
uniform vec4    color1;
uniform vec4    color2;
uniform float   radius;

varying vec3    baseColor;
varying float   alpha;
varying float   phase;
varying float   frequency;
varying float   sigma;
varying float   contrast;

void main() {
    //current position
    vec2 pos = gl_TexCoord[0].xy;

    /* find our distance from center, if distance to center (aka radius of pixel) > Radius, discard this pixel: */
    if ( radius > 0.0 ) {
        float dist = distance( pos, center );
        if ( dist > radius ) discard;
    }

    //create our sinusoid in -1 to 1 range
    float sv = sin( pos.x * frequency + phase );

    //if sigma >= 0, we want a squarewave grating, step or smoothstep does this depending on sigma value
    if ( sigma == 0.0 ) {
        sv = step( sigma, sv ); //converts into 0-1 range
    }
    else if ( sigma > 0.0 ) {
        sv = smoothstep( -sigma, sigma, sv ); //converts into 0-1 range
    }
    else {
        sv = ( sv + 1.0 ) / 2.0; //simply get sv into 0 - 1 range (preserving sinusoid);
    }

    vec3 colorA = color1.rgb;
    vec3 colorB = color2.rgb;
    //blend our colours from the base colour if contrast < 1
    if ( contrast < 1.0 ) { 
        colorA = mix( baseColor, color1.rgb, contrast );
        colorB = mix( baseColor, color2.rgb, contrast );
    }

    // and then mix the two colors using sv (our position in the grating)
    vec3 colorOut = mix(colorA, colorB, sv);
    
    // off to the display, byebye little pixel!
    gl_FragColor = vec4( colorOut, alpha );
}