public
Last active

GLSL Bumped Specular

  • Download Gist
gistfile1.c
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
// 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_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;
v_viewdir = normalize(tsprotation * objViewDir);
}
#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_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;
}
#endif

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.