Skip to content

Instantly share code, notes, and snippets.

@pyalot
Created March 27, 2015 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pyalot/a6f3f3bf1921339ac842 to your computer and use it in GitHub Desktop.
Save pyalot/a6f3f3bf1921339ac842 to your computer and use it in GitHub Desktop.
simple physically inspired shading function
vec3 shade(vec3 color, float roughness, float metallness){
float fresnelFactor = 0.93;
float specularFactor = 0.65;
vec3 normal = normalize(vNormal);
vec3 eyeDir = getEyeDir();
vec3 N = normal;
vec3 V = -eyeDir;
vec3 reflected = reflect(eyeDir, normal);
vec3 diffuse = textureCube3DLogLuv(envTex, normal, 1.0);
vec3 specular = textureCube3DLogLuv(envTex, reflected, roughness);
vec3 specularColor = specular*mix(vec3(1), color, metallness);
vec3 diffuseColor = diffuse*color;
float nonMetallness = 1.0-metallness;
float glossiness = 1.0-roughness;
float fresnel = pow(1.0-max(0.0, dot(N,V)), 3.5);
fresnel = mix(1.0, fresnel, mix(fresnelFactor, fresnelFactor*0.2, metallness));
float specularity = max(metallness, glossiness)*specularFactor*fresnel;
vec3 excident = diffuseColor*nonMetallness + specularColor*specularity;
return excident;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment