Create a gist now

Instantly share code, notes, and snippets.

GLSL Bumped Specular
// Normalize view direction per vertex
// iPhone 3Gs: 15.0ms, but rendering is wrong!
#ifdef VERTEX
attribute vec4 a_position;
attribute vec2 a_uv;
attribute vec3 a_normal;
attribute vec4 a_tangent;
uniform mat4 u_mvp;
uniform mat4 u_world2object;
uniform vec4 u_worldlightdir;
uniform vec4 u_worldcampos;
varying vec2 v_uv;
varying vec3 v_lightdir;
varying vec3 v_viewdir;
void main()
gl_Position = u_mvp * a_position;
v_uv = a_uv;
vec3 bitan = cross (, * a_tangent.w;
mat3 tsprotation = mat3 (
a_tangent.x, bitan.x, a_normal.x,
a_tangent.y, bitan.y, a_normal.y,
a_tangent.z, bitan.z, a_normal.z);
vec3 objLightDir = (u_world2object * u_worldlightdir).xyz;
vec3 objCamPos = (u_world2object * u_worldcampos).xyz;
vec3 objViewDir = objCamPos -;
v_lightdir = tsprotation * objLightDir;
v_viewdir = normalize(tsprotation * objViewDir);
uniform lowp vec4 u_lightcolor;
uniform lowp vec4 u_matcolor;
uniform mediump float u_spec;
varying mediump vec2 v_uv;
varying lowp vec3 v_lightdir;
varying lowp vec3 v_viewdir;
uniform sampler2D u_texcolor;
uniform sampler2D u_texnormal;
void main()
lowp vec4 albedo = texture2D (u_texcolor, v_uv) * u_matcolor;
lowp vec3 normal = texture2D (u_texnormal, v_uv).rgb * 2.0 - 1.0;
lowp vec3 halfdir = normalize (v_lightdir + v_viewdir);
lowp float diff = max (0.0, dot (normal, v_lightdir));
lowp float nh = max (0.0, dot (normal, halfdir));
mediump float spec = pow (nh, u_spec);
lowp vec4 c = albedo * u_lightcolor * diff + u_lightcolor * spec;
gl_FragColor = c;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment