Shaders a written in Metal

Shader Reference

A shader is code run for every pixel in a graphic.

shader-dark.png

Shader

fragment float4 shader() {
    float red = 1.0;
    float green = 1.0;
    float blue = 1.0;
    float alpha = 1.0;
    return float4(red, green, blue, alpha);
}

This shader returns 1.0 for every channel in the pixel, displaying a fully white graphic. Colors are represented but not limited to values between 0.0 and 1.0.

Coordinates

fragment float4 shader() {
    return float4(u, v, 0.0, 1.0);
}

uv.png

To determine the location of the current pixel, use the normalized uv coordinates. This variable is pre defined and available in the main shader function.

Functions

float smooth(float value) {
    return cos(value * M_PI_F + M_PI_F) / 2.0 + 0.5;
}

fragment float4 shader() {
    return float4(smooth(u), smooth(v), 0.0, 1.0);
}

uv_smooth.png

Define a function by first defining what type to return, in this case a float. Then the name and the arguments. This function smoothes out values between 0.0 and 1.0.

<aside> <img src="/icons/light-bulb_gray.svg" alt="/icons/light-bulb_gray.svg" width="40px" />

Note that the custom function needs to be above the main shader function for the shader to compile.

</aside>

Colors

color-dark.png