Hi! I have begun to experiment with light and other effects and have been trying to import them into projects. Recently, I discovered a shadow shader made by @SpaceMonkey that had been released one year ago. I started to copy and paste a bit to make my own version but somehow it did’nt work. I would be pleased if someone could point out what’s wrong in this code:

```
function setup()
parameter.number("k",-100,50,50)
lp=vec3(0,10,50)
--simage is the shadowMap
simage=image(1000,1000)
sm=matrix()
updatemesh()
end
function draw()
lp.z=k
setContext(simage)
background(0, 0, 0, 0)
perspective(50)
camera(lp.x,lp.y,lp.z,0,0,25)
m.shader=shader(shad.shadow1vertex,shad.shadow1frag)
m.shader.lp=lp
m.shader.modmat=modelMatrix()
sm=viewMatrix()*projectionMatrix()
md:draw()
setContext()
background(127, 127, 127, 255)
perspective(50)
camera(30,50,50,30,0,0)
--camera(lp.x,lp.y,lp.z,0,0,0)
m.shader=shader(shad.shadow2vertex,shad.shadow2frag)
m.shader.lm=sm
m.shader.lp=vec4(lp.x,lp.y,lp.z,1)
m.shader.modmat=modelMatrix()
m.shader.shadowMap=simage
m:draw()
resetMatrix()
ortho()
viewMatrix(matrix())
sprite(simage,0,0,300,300)
end
function updatemesh()
m=mesh()
m.vertices={vec3(0,0,-20),vec3(0,0,20),vec3(40,0,20),vec3(40,0,20),vec3(40,0,-20),vec3(0,0,-20),
vec3(0,0,0),vec3(40,0,0),vec3(40,20,0)}
m:setColors(color(255, 255, 255, 255))
md=mesh()
md.vertices=m.vertices
end
shad={shadow1vertex=[[
uniform highp mat4 modelViewProjection;
attribute vec4 position;
varying vec4 wp;
uniform mat4 modmat;
void main()
{
//convert the vertex into world coordinates (same as the light)
wp=position*modmat;
gl_Position = modelViewProjection * position;
}
]],
shadow1frag=[[
#extension GL_EXT_shader_framebuffer_fetch : require
precision lowp float;
uniform mediump vec3 lp;
varying mediump vec4 wp;
void main()
{
//if (!gl_FrontFacing) discard;
highp vec3 tlp = wp.xyz/wp.w;
float col = max(1.0 - length(tlp.xyz - lp.xyz)/200.0,gl_LastFragData[0].x);
gl_FragColor = vec4(col, col, col, 1.0);
}
]],
shadow2vertex=[[
uniform mat4 modelViewProjection;
uniform mat4 modmat;
uniform mat4 lm;
attribute vec4 position;
attribute vec4 color;
attribute vec2 texCoord;
varying lowp vec4 vColor;
varying highp vec2 vTexCoord;
const mat4 ScaleMatrix = mat4(0.5, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.0,0.5, 0.0, 0.5, 0.5, 0.5, 1.0);
varying vec4 wpos;
varying vec4 lipos;
void main()
{
vColor = color;
vTexCoord = texCoord;
wpos=position*modmat;
lipos=ScaleMatrix*lm*wpos;
gl_Position = modelViewProjection * position;
}
]],
shadow2frag=[[
precision highp float;
uniform lowp sampler2D texture;
uniform lowp sampler2D shadowMap;
uniform vec4 lp;
varying lowp vec4 vColor;
varying highp vec2 vTexCoord;
varying vec4 lipos;
varying vec4 wpos;
void main()
{
//if (!gl_FrontFacing) discard;
highp vec3 tlp = lipos.xyz/lipos.w; //varying
float shadowMapDepth = texture2D(shadowMap, tlp.xy).r;
//calculate distance to compare to our shadow map
//1.004 <- avoid shadow acne compared to 1.0
float lightDistance = 1.004 - length((wpos.xyz/wpos.w)-lp.xyz)/30.0; //uniform
//if the depth from the shadow map is higher (closer) t
//han the current depth just use ambience otherwise normal ADS
//additionally we'll check whether we are out of
//bounds on the texture as the shadow map only covers 60 degrees
//Note that these comments are made by
if (shadowMapDepth > lightDistance && tlp.x < 1.0 && tlp.x > 0.0) {
gl_FragColor =vec4(0,0,0,1);
}
else {
gl_FragColor =vec4(1.,1.,1.,1.);
}
}
]]}
```