Skip to main content
Version: 🚧 Nightly

Final shaders

Specify a final full screen shader with the option --final-shader. F3D executes it as the last shader in the rendering pipeline.

Pass the shader code directly as a single code line. On Linux with bash, you can pass the content of a shader file using the following command:

f3d model.obj --final-shader "$(cat final.glsl)"

For other operating systems and shells, adapt the command.

Requirements​

Implement the function vec4 pixel(vec2 uv). The value uv ranges from 0 to 1 in both directions. (0,0) is the bottom left corner, and (1,1) is the upper right corner. The first component is the horizontal direction.

Uniforms​

Access these uniforms:

  • source (type: sampler2d): texture of the image generated by rendering pipeline.
  • resolution (type: ivec2): resolution of the texture source.
  • time (type: float): elapsed time passed to the rendering pipeline.

Examples​

These three shader examples illustrate how an implementation looks.

Negative​

vec4 pixel(vec2 uv)
{
vec3 value = texture(source, uv).rgb;
return vec4(vec3(1.0) - value, 1.0);
}

Vignette​

vec4 pixel(vec2 uv)
{
float l = clamp(1.0 - 2.0 * length(uv - 0.5), 0.0, 1.0);
return vec4(l*texture(source, uv).rgb, 1.0);
}

Box blur​

vec4 pixel(vec2 uv)
{
const float radius = 0.03;
const int samples = 20;
const float step = radius / float(samples);

vec3 sum = vec3(0);

for (int i = -samples; i <= samples; i++)
{
for (int j = -samples; j <= samples; j++)
{
sum += texture(source, uv + vec2(i, j) * step).rgb;
}
}

float d = 1.0 + 2.0 * float(samples);
sum /= d * d;

return vec4(sum, 1.0);
}

Oscillating colors​

vec4 pixel(vec2 uv)
{
return vec4(texture(source, uv).rgb * (0.5 + 0.5*cos(time+uv.xyx+vec3(0,2,4))), 1.0)
}