drawObj = regl({
frag: `
precision mediump float;
uniform vec4 color;
varying vec3 vtxNormal;
uniform vec3 lightColor;
uniform vec3 lightPos;
uniform vec3 lightDir;
uniform float cutoffAngle;
uniform float exponent;
uniform vec3 objColor;
varying vec3 worldPos;
uniform float ambient;
uniform float emission;
uniform float diffuse;
uniform float specular;
uniform float shininess;
void main () {
vec3 lightDir = -lightDir;
vec3 lightToObject = normalize(-worldPos + lightPos);
float direction = max(dot(lightDir, lightToObject), 0.);
if (direction < cos(radians(cutoffAngle))) {
gl_FragColor = vec4(0, 0, 0, 0.9);
return;
}
vec3 lightColor = lightColor * pow(direction, exponent);
vec3 normal = normalize(vtxNormal);
float diffuseComp = max(0.0, diffuse * dot(normal, lightDir));
vec3 ref = 2.0 * dot(lightDir, normal) * normal - lightDir;
float specularComp = specular * pow(max(0.0, dot(ref, vec3(0.0, 0.0, 1.0))), shininess);
gl_FragColor = vec4(emission * objColor + (ambient + diffuseComp) * lightColor * objColor + specularComp * lightColor, 1.0);
}`,
vert: `
attribute vec3 position;
attribute vec3 normal;
varying vec3 vtxNormal;
varying vec3 worldPos;
uniform mat4 modelview;
uniform mat4 projection;
void main () {
vec4 worldpos = modelview * vec4(position, 1.0);
gl_Position = projection * worldpos;
worldPos = worldpos.xyz;
vtxNormal = (modelview * vec4(normal, 0.0)).xyz;
}`,
attributes: {
position: obj.pos,
normal: obj.normal
},
uniforms: {
objColor: regl.prop('objColor'),
lightColor: regl.prop('lightColor'),
lightDir: regl.prop('lightDir'),
lightPos: regl.prop('lightPos'),
cutoffAngle: regl.prop('cutoffAngle'),
exponent: regl.prop('exponent'),
ambient: regl.prop('ambient'),
emission: regl.prop('emission'),
diffuse: regl.prop('diffuse'),
specular: regl.prop('specular'),
shininess : regl.prop ('shininess'),
modelview : regl.prop('modelview'),
projection : regl.prop ('projection')
},
depth: {
enable: true,
func: '<',
mask: true,
},
elements: obj.faces
})