drawObj = regl({
frag: `
precision mediump float;
uniform vec4 color;
varying vec3 vtxNormal;
varying vec4 worldpos;
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 float exponent;
uniform float cutoffAngle;
uniform vec3 lightPos;
void main () {
vec3 objectPos = worldpos.xyz;
vec3 halfVector = normalize(objectPos + lightPos);
vec3 normal = normalize(vtxNormal);
float diffuseComp = max(0.0,diffuse * dot(normal,-halfVector));
vec3 ref = 2.0*dot(-halfVector,normal)*normal + halfVector;
float specularComp = specular*pow(max(0.0,dot(ref,vec3(0.0,0.0,1.0))),shininess);
vec3 lightColor = lightColor*step(cos(radians(cutoffAngle)),dot(halfVector, lightDir))*pow(max(dot(halfVector,lightDir),0.0),exponent);
gl_FragColor = vec4(emission*objColor +
(ambient+diffuseComp)*lightColor*objColor +
(specularComp)*lightColor, 0.8);
}`,
vert: `
attribute vec3 position;
attribute vec3 normal;
varying vec3 vtxNormal;
varying vec4 worldpos;
uniform mat4 modelview;
uniform mat4 projection;
void main () {
worldpos = modelview*vec4(position, 1.0);
gl_Position = projection*worldpos;
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'),
exponent: regl.prop('exponent'),
cutoffAngle: regl.prop('cutoffAngle'),
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
})