drawMesh = function (regl, obj) {
let draw = regl({
frag: `
precision mediump float;
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 float alpha;
void main () {
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, alpha);
}`,
vert: `
attribute vec3 position;
attribute vec3 normal;
varying vec3 vtxNormal;
uniform mat4 modelview;
uniform mat4 projection;
void main () {
vec4 worldpos = modelview*vec4(position, 1.0);
gl_Position = projection*worldpos;
vtxNormal = (modelview*vec4(normal,0.0)).xyz;
}`,
attributes: {
position: obj.pos,
normal: obj.normal
},
blend: {
enable: true,
func: {
src: 'src alpha',
dst: 'one minus src alpha'
}
},
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'),
alpha: regl.prop('alpha'),
},
elements: obj.faces
});
let defaults = {objColor:[1,1,1],
lightColor:[1,1,1],
lightDir:glmatrix.vec3.normalize([],[1,1,10]),
emission:0.0,
ambient:0.3,
diffuse:0.6,
specular: 0.1,
shininess: 60,
alpha: 1,
modelview: glmatrix.mat4.create(),
projection: glmatrix.mat4.perspective ([],
40 * Math.PI/180,
1.333, 0.01, 100)};
return function (uniforms = {}) {
uniforms = {... defaults, ...uniforms}
draw (uniforms)
}
}