Clone wiki

Core / ArcShader

Back to Built-in Shader Packs

Arc Shader


'Arc' is an example shader provided in Codea's 'Patterns' Shaders Pack. Applied to a square mesh with appropriate texture co-ordinates, it renders a circular arc of a specified width from one specified angle to another in a specified colour.

Vertex shader

The vertex shader simply passes the 'attribute' variable texCoord on to the accompanying fragment shader as 'varying' variable vTexCoord:

void main()
    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()
    cx, cy = WIDTH / 2, HEIGHT / 2
    parameter.integer("width", 1, 100, 40)
    parameter.integer("radius", 100, WIDTH / 2, 200)
    parameter.integer("startAngle", -180, 180, -130)
    parameter.integer("endAngle", -180, 180, 130)
    stroke(118, 223, 25)

function draw()
    arc(cx, cy, radius, startAngle, endAngle)

function arc(x, y, radius, a1, a2)
    local m = mesh()
    m:addRect(x, y, radius * 2, radius * 2)
    m.shader = shader("Patterns:Arc")
    m.shader.size = (1 - strokeWidth()/radius) * 0.5
    m.shader.color = color(stroke())
    m.shader.a1 = math.rad(a1)
    m.shader.a2 = math.rad(a2)