Skip to content

Instantly share code, notes, and snippets.

@xoppa
Last active December 29, 2015 20:39
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 xoppa/7724943 to your computer and use it in GitHub Desktop.
Save xoppa/7724943 to your computer and use it in GitHub Desktop.
default precompiled
attribute vec3 a_position;
uniform mat4 u_projViewTrans;
attribute vec3 a_normal;
uniform mat3 u_normalMatrix;
varying vec3 v_normal;
uniform mat4 u_worldTrans;
const float u_shininess = 20.0;
varying vec3 v_lightDiffuse;
uniform vec3 u_ambientCubemap[6];
varying vec3 v_lightSpecular;
uniform vec4 u_cameraPosition;
struct DirectionalLight
{
vec3 color;
vec3 direction;
};
uniform DirectionalLight u_dirLights[ 2 ];
struct PointLight
{
vec3 color;
vec3 position;
float intensity;
};
uniform PointLight u_pointLights[ 5 ];
void main() {
vec4 pos = u_worldTrans * vec4(a_position, 1.0);
gl_Position = u_projViewTrans * pos;
vec3 normal = normalize(u_normalMatrix * a_normal);
v_normal = normal;
vec3 ambientLight = vec3(0.0);
vec3 squaredNormal = normal * normal;
vec3 isPositive = step(0.0, normal);
ambientLight += squaredNormal.x * mix(u_ambientCubemap[0], u_ambientCubemap[1], isPositive.x) +
squaredNormal.y * mix(u_ambientCubemap[2], u_ambientCubemap[3], isPositive.y) +
squaredNormal.z * mix(u_ambientCubemap[4], u_ambientCubemap[5], isPositive.z);
v_lightDiffuse = ambientLight;
v_lightSpecular = vec3(0.0);
vec3 viewVec = normalize(u_cameraPosition.xyz - pos.xyz);
for (int i = 0; i < 2 ; i++) {
vec3 lightDir = -u_dirLights[i].direction;
float NdotL = clamp(dot(normal, lightDir), 0.0, 1.0);
v_lightDiffuse += u_dirLights[i].color * NdotL;
float halfDotView = clamp(dot(normal, normalize(lightDir + viewVec)), 0.0, 2.0);
v_lightSpecular += u_dirLights[i].color * clamp(NdotL * pow(halfDotView, u_shininess), 0.0, 1.0);
}
for (int i = 0; i < 5 ; i++) {
vec3 lightDir = u_pointLights[i].position - pos.xyz;
float dist2 = dot(lightDir, lightDir);
lightDir *= inversesqrt(dist2);
float NdotL = clamp(dot(normal, lightDir), 0.0, 2.0);
float falloff = clamp(u_pointLights[i].intensity / (1.0 + dist2), 0.0, 2.0);
v_lightDiffuse += u_pointLights[i].color * (NdotL * falloff);
float halfDotView = clamp(dot(normal, normalize(lightDir + viewVec)), 0.0, 2.0);
v_lightSpecular += u_pointLights[i].color * clamp(NdotL * pow(halfDotView, u_shininess) * falloff, 0.0, 2.0);
}
}
varying vec3 v_normal;
uniform vec4 u_diffuseColor;
uniform vec4 u_specularColor;
varying vec3 v_lightDiffuse;
varying vec3 v_lightSpecular;
void main() {
vec3 normal = v_normal;
vec4 diffuse = u_diffuseColor;
vec3 specular = u_specularColor.rgb * v_lightSpecular;
gl_FragColor.rgb = (diffuse.rgb * v_lightDiffuse) + specular;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment