shdr
precision highp float;
uniform float time;
uniform vec2 resolution;
varying vec3 fPosition;
varying vec3 fNormal;
Ward
float Ward(vec3 lightDir, vec3 viewDir, vec3 normal, float exponent)
{
vec3 H = normalize(lightDir + viewDir);
float delta = acos(dot(H,normal));
float alpha2 = exponent * exponent;
float temp = exp(-pow(tan(delta), 2.) / (alpha2)) / (4. * 3.1415 * alpha2);
float temp2 = sqrt(dot(viewDir,normal) * dot(lightDir,normal));
float K = temp2 * temp;
return K;
}
Ashikhmin
float fresnel(float costheta, float fresnelCoef)
{
return fresnelCoef + (1. - fresnelCoef) * pow(1. - costheta, 5.);
}
float Ashikhmin(vec3 lightDir, vec3 viewDir, vec3 normal, float exponent, float fresnelCoef)
{
vec3 H = normalize(lightDir+viewDir);
float numerateur_s = ( exponent + 1.)/(8.*3.14159) * pow(dot(normal,H), exponent );
float denominateur_s = dot(lightDir,H)*(dot(normal,lightDir) + dot(normal, viewDir) -
dot(normal, lightDir) * dot(normal, viewDir));
float K = fresnel(dot(normal,lightDir), fresnelCoef) * ( numerateur_s / denominateur_s ) ;
return K;
}
OrenNayarDir
float OrenNayarDir(vec3 lightDir, vec3 viewDir, vec3 normal, float exponent)
{
float LdotN = dot(lightDir,normal);
float VdotN = dot(viewDir,normal);
float result = clamp( LdotN, 0. , 1.);
float soft_rim = clamp( 1. - VdotN/2., 0. , 1.);
float fakey = pow(1. - result * soft_rim , 2.);
float fakey_magic = 0.62;
fakey = fakey_magic - fakey*fakey_magic;
float K = mix(result, fakey, exponent) ;
return K;
}
void main()
{
vec3 pos = fPosition;
vec3 c = vec3(1,1,0);
vec3 lightDir = normalize(vec3(10,100,50) - pos);
vec3 viewDir = normalize(-pos);
float f = 10. * Ashikhmin(lightDir, viewDir, fNormal, 2., .9);
gl_FragColor = vec4(f * c, 1.0);
}