Skip to content

Instantly share code, notes, and snippets.

@aras-p
Created January 17, 2011 23:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aras-p/783703 to your computer and use it in GitHub Desktop.
Save aras-p/783703 to your computer and use it in GitHub Desktop.
GLSL Bumped Specular
// Compute normalized half direction per vertex
// iPhone 3Gs: 16.3ms
#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_halfdir;
void main()
{
gl_Position = u_mvp * a_position;
v_uv = a_uv;
vec3 bitan = cross (a_normal.xyz, a_tangent.xyz) * 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 - a_position.xyz;
v_lightdir = tsprotation * objLightDir;
vec3 viewdir = normalize(tsprotation * objViewDir);
v_halfdir = normalize (v_lightdir + viewdir);
}
#endif
#ifdef FRAGMENT
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_halfdir;
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 float diff = max (0.0, dot (normal, v_lightdir));
lowp float nh = max (0.0, dot (normal, v_halfdir));
mediump float spec = pow (nh, u_spec);
lowp vec4 c = albedo * u_lightcolor * diff + u_lightcolor * spec;
gl_FragColor = c;
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment