Step 4 GLSL

step_04.glsl
1// x, y position of the light
2uniform vec2 lightPosition;
3// Size of light in pixels
4uniform float lightSize;
5
6float terrain(vec2 samplePoint)
7{
8 float samplePointAlpha = texture(iChannel0, samplePoint).a;
9 float sampleStepped = step(0.1, samplePointAlpha);
10 float returnValue = 1.0 - sampleStepped;
11
12 return returnValue;
13}
14
15void mainImage( out vec4 fragColor, in vec2 fragCoord )
16{
17 // Distance in pixels to the light
18 float distanceToLight = length(lightPosition - fragCoord);
19
20 // Normalize the fragment coordinate from (0.0, 0.0) to (1.0, 1.0)
21 vec2 normalizedFragCoord = fragCoord/iResolution.xy;
22
23 // Start our mixing variable at 1.0
24 float lightAmount = 1.0;
25
26 float shadowAmount = terrain(normalizedFragCoord);
27 lightAmount *= shadowAmount;
28
29 // Find out how much light we have based on the distance to our light
30 lightAmount *= 1.0 - smoothstep(0.0, lightSize, distanceToLight);
31
32 // We'll alternate our display between black and whatever is in channel 1
33 vec4 blackColor = vec4(0.0, 0.0, 0.0, 1.0);
34
35 // Our fragment color will be somewhere between black and channel 1
36 // dependent on the value of b.
37 fragColor = mix(blackColor, texture(iChannel1, normalizedFragCoord), lightAmount);
38}