So after skimming @Ignatz’s wonderful shader ebook and looking around the web and forums, I decided to whip up a quick lighting shader, which was easier than I imagined, but then I hit a roadblock. I wanted to make the center of the light have a bright redish tint (as if from a fire), then only a slight tint as you move away from the centre, but due to the alpha approaching zero as you approach the center, when I tried to tint it it ended up being brighter around the edges. I tried to find a workaround, but with no luck, unfortunately. If anyone knows how it can be done, I’d appreciate it greatly. This is my first shader work, and though I made it this far the rest confuses me. Below is my shader code (fully functional, except for my tinting idea)
function setup()
m = mesh()
m:addRect(WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
m:setColors(color(255))
m.shader = shader(lightshader.v,lightshader.f)
a,b,c = {},{},{}
for i=1,5 do
if i==1 then a[1],b[1] = vec3(WIDTH/2,HEIGHT/2,150),true else
a[i],b[i] = vec3(WIDTH/2,HEIGHT/2,150),false
end
c[i]=1
end
m.shader.lights = a
m.shader.on = b
m.shader.flicker = c
fps=60
lightstate(2,true)
end
function draw()
background(255)
sprite("Cargo Bot:Codea Icon",WIDTH/2,HEIGHT/2,WIDTH)
setflicker(1)
setflicker(2,10)
m:draw()
output.clear()
fps=.9*fps+.1/DeltaTime
print(fps)
end
function touched(t)
movelight(1,vec3(t.x,t.y,150))
end
function movelight(index,newval)
a[index] = newval
m.shader.lights = a
end
function lightstate(index,newval)
b[index] = newval
m.shader.on = b
end
function setflicker(index,factor,flicker)
factor = factor or 15
if flicker then c[index] = flicker else
c[index] = 1+noise(ElapsedTime*(.5+math.random()))/factor
end
m.shader.flicker = c
end
lightshader={
v=[[
uniform mat4 modelViewProjection;
attribute vec4 position;
varying highp vec4 vPosition;
void main()
{
vPosition = position;
gl_Position = modelViewProjection * position;
}]],
f=[[
uniform highp float num;
precision highp float;
uniform lowp sampler2D texture;
uniform highp vec3 lights[5];
uniform bool on[5];
uniform float flicker[5];
varying lowp vec4 vColor;
varying highp vec4 vPosition;
void main()
{
highp float d = 0.0;
for (int i=0; i<= 5; i++) {
if (on[i]==true) {
highp float f = lights[i].z*flicker[i];
d=d+clamp(1.0-distance(vPosition,vec4(lights[i].x,lights[i].y,0,1))/f+0.1,0.0,1.0);
};
};
gl_FragColor = vec4(0.,0.,0.,1.0-d);
}]]
}