Wiki

Clone wiki

Core / PosterizeShader

Back to Built-in Shader Packs


Posterize Shader

Introduction

'Posterize' is an example shader provided in Codea's 'Filters' Shaders Pack.

Vertex shader

The vertex shader simply passes the 'attribute' variables color and texCoord on to the accompanying fragment shader as 'varying' variables vColor and vTexCoord:

void main()
{
    ...
    vColor = color;
    vTexCoord = texCoord;
    ...
    gl_Position = modelViewProjection * position;
}

gl_Position is a variable that is intended for outputting the vertex position in homogenous co-ordinates (that is, as a vec4 value). All vertex shaders must write a value into that variable. (See Section 7.1 'Vertex Shader Special Variables' of the GLSL ES specification.) Here, the modelViewProjection 4x4 matrix is applied to the vertex's position. modelViewProjection is a 'uniform' mat4 variable supplied automatically by Codea when the shader is used with a mesh. It is the current model matrix * view matrix * projection matrix. position is a vec4 'attribute' variable, also supplied automatically by Codea from the mesh.

Fragment shader

[To do.]

Example of use

The code below is a simple example of the use of the shader:

function setup()
    myMesh = mesh()
    local img = readImage("Cargo Bot:Startup Screen")
    local w, h = img.width, img.height
    local s = math.min(WIDTH / w, HEIGHT / h)
    myMesh:addRect(WIDTH / 2, HEIGHT / 2, w * s, h * s)
    myMesh.texture = img
    myShader = shader("Filters:Posterize")
    parameter.boolean("isOn", true)
    parameter.integer("noOfColours", 1, 10, 5,
        function () isOn = true end)
end

function draw()
    background(0)
    if isOn then
        myMesh.shader = myShader
        myMesh.shader.numColors = noOfColours
        myMesh.shader.gamma = 1
    else
        myMesh.shader = nil
    end
    myMesh:draw()
end

Updated