Wiki

Clone wiki

Core / EdgeShader

Back to Built-in Shader Packs


Edge Shader

Introduction

'Edge' 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:Edge")
    myShader.conPixel = vec2(1 / w, 1 / h)
    myShader.conWeight = 1 / 9
    parameter.boolean("isEdged", false, onChange)
end

function draw()
    background(0)
    myMesh:draw()
end

function onChange(value)
    if value then
        myMesh.shader = myShader
    else
        myMesh.shader = nil
    end
end

Updated