drawObj = regl({
frag: `
precision mediump float;
uniform vec4 color;
varying vec3 vtxNormal;
uniform vec3 lightDir;
uniform vec3 lightColor;
uniform vec3 objColor;
uniform float ambient;
uniform float emission;
uniform float diffuse;
uniform float specular;
uniform float shininess;
uniform vec3 lightPos;
uniform float cutoffAngle;
uniform float spotExponent;
varying vec3 worldPos;
void main () {
vec3 lightDir = -lightDir;
vec3 lightToObject = normalize(-worldPos + lightPos);
float direction = max(dot(lightDir, lightToObject), 0.0);
if (direction < cos(radians(cutoffAngle))){
gl_FragColor = vec4(0,0,0,0.9);
return;
}
vec3 lightColor = lightColor * pow(direction,spotExponent);
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;
uniform mat4 modelview;
uniform mat4 projection;
varying vec3 worldPos;
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'),
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'),
lightPos: regl.prop('lightPos'),
cutoffAngle: regl.prop('cutoffAngle'),
spotExponent: regl.prop('spotExponent'),
},
depth: {
enable: true,
func: '<',
mask: true,
},
elements: obj.faces
})