This example shows how the diffuse colour of the surface can be defined by a mathematical function. It also shows how a multilayer material can be created by stochastically selecting either diffuse or mirror reflection. This is done by calling random which is a function provided by Beam. function RainbowCube(shadingContext)
p = shadingContext.op
ecol = { 0, 0, 0 }
 Select either diffuse or mirror reflection based on a random value.
if random() < 0.6 then
col = { math.sin(p.x/10) * .5 + .5, math.sin(p.y/10) * .5 + .5, math.sin(p.z/10) * .5 + .5 }
return { albedo = col, brdf = lambert, emission = ecol }
else
col = { 1, 1, 1 }
return { albedo = col, brdf = mirrorBRDF, emission = ecol }
end
end
function mirrorBRDF(normal, outgoing, u, v)
d = (normal.x * outgoing.x + normal.y * outgoing.y + normal.z * outgoing.z) * 2
return { x = outgoing.x + normal.x * d, y = outgoing.y + normal.y * d, z = outgoing.z + normal.z * d }
end  
A BRDF can be completely defined from within the shader code. In this case a glossy BRDF is defined, and the glossiness value is stored in a lambda function which is returned by the shader. function GlossyCube(shadingContext)
 Modulate glossiness based on the localspace position of the shading point
glossiness = mix(0.1, 0.5, step(fract(shadingContext.op.y / 30), 0.5))
result = { albedo = { 1, 1, 1 }, emission = { 0, 0, 0 } }
 Use a lambda function to store the glossiness along with the BRDF function reference
result.brdf = function(normal, outgoing, u, v) return glossyBRDF(normal, outgoing, u, v, glossiness) end
return result
end
function glossyBRDF(normal, outgoing, u, v, glossiness)
v0 = mirrorBRDF(normal, outgoing, u, v)
v1 = lambert(normal, outgoing, u, v)
return { x = mix(v0.x, v1.x, glossiness), y = mix(v0.y, v1.y, glossiness), z = mix(v0.z, v1.z, glossiness) }
end
function mirrorBRDF(normal, outgoing, u, v)
d = (normal.x * outgoing.x + normal.y * outgoing.y + normal.z * outgoing.z) * 2
return { x = outgoing.x + normal.x * d, y = outgoing.y + normal.y * d, z = outgoing.z + normal.z * d }
end  
The builtin shadow function can be used to implement an ambient occlusion effect completely in Lua. function AmbientOcclusionCube(shadingContext)
point = shadingContext.wp
normal = shadingContext.wn
a = 0
sampleCount = 4
radius = 2
for i = 0, sampleCount do
d = lambert(normal, normal, random(), random())
d.x = point.x + d.x * radius
d.y = point.y + d.y * radius
d.z = point.z + d.z * radius
if not shadow(point, d) then a = a + 1 end
end
lightAmount = a / sampleCount
col = { 0, 0, 0 }
ecol = { lightAmount, lightAmount, lightAmount }
return { albedo = col, brdf = lambert, emission = ecol }
end
 
Markers are points in the scene which can be defined in the scene description and labelled with a name. The marker function can be used to retrieve a point. Here a point is used to cast a shadow, as if from a point light source. function CustomPointLightCube(shadingContext)
col = { .2, .2, .2 }
ecol = { 0, 0, 0 }
if not shadow(shadingContext.wp, marker("mypointlight")) then
ecol = { 1, 1, 1 }
end
return { albedo = col, brdf = lambert, emission = ecol }
ends  
A scene description can also be generated by a Lua script. Here a set of nested forloops is used to create instances of the mesh object. The rest of the scene is described using the custom format, through which the script can be executed using the execute command. push()
scale({ 0.25, 0.25, 0.25 })
for x = 0, 3 do
for y = 0, 3 do
for z = 0, 3 do
push()
translate({ x * 200  200, y * 200  200, z * 200  200 })
scale({ 0.9, 0.9, 0.9 })
instance("roundedcube", "white")
pop()
end
end
end
pop()
